From 6aada88dc77a95a7b2726c39e008d4ddcd38d58c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 19 Aug 2016 18:10:12 +0200 Subject: Add dir_empty() test --- butl/filesystem | 12 +++++++++--- butl/filesystem.cxx | 8 ++++---- butl/filesystem.ixx | 6 ++++++ 3 files changed, 19 insertions(+), 7 deletions(-) (limited to 'butl') diff --git a/butl/filesystem b/butl/filesystem index 118fa01..41b9266 100644 --- a/butl/filesystem +++ b/butl/filesystem @@ -32,17 +32,23 @@ namespace butl { + // Return true if the path is to an existing regular file. Note that + // this function resolves symlinks. + // + LIBBUTL_EXPORT bool + file_exists (const path&); + // Return true if the path is to an existing directory. Note that // this function resolves symlinks. // LIBBUTL_EXPORT bool dir_exists (const path&); - // Return true if the path is to an existing regular file. Note that - // this function resolves symlinks. + // Return true if the directory is empty. Note that the path must exist + // and be a directory. // LIBBUTL_EXPORT bool - file_exists (const path&); + dir_empty (const dir_path&); // Try to create a directory unless it already exists. If you expect // the directory to exist and performance is important, then you diff --git a/butl/filesystem.cxx b/butl/filesystem.cxx index e5819e2..a807f4c 100644 --- a/butl/filesystem.cxx +++ b/butl/filesystem.cxx @@ -35,7 +35,7 @@ using namespace std; namespace butl { bool - dir_exists (const path& p) + file_exists (const path& p) { #ifndef _WIN32 struct stat s; @@ -51,11 +51,11 @@ namespace butl throw system_error (errno, system_category ()); } - return S_ISDIR (s.st_mode); + return S_ISREG (s.st_mode); } bool - file_exists (const path& p) + dir_exists (const path& p) { #ifndef _WIN32 struct stat s; @@ -71,7 +71,7 @@ namespace butl throw system_error (errno, system_category ()); } - return S_ISREG (s.st_mode); + return S_ISDIR (s.st_mode); } mkdir_status diff --git a/butl/filesystem.ixx b/butl/filesystem.ixx index af1125e..6062c38 100644 --- a/butl/filesystem.ixx +++ b/butl/filesystem.ixx @@ -4,6 +4,12 @@ namespace butl { + inline bool + dir_empty (const dir_path& d) + { + return dir_iterator (d) == dir_iterator (); + } + inline rmdir_status try_rmdir_r (const dir_path& p, bool ignore_error) { -- cgit v1.1