From 50bdae2c0547051228361e439a72f8be62c3e936 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 4 Oct 2017 15:19:47 +0300 Subject: Add ignore_error parameter for path_entry() and *_exists() functions --- libbutl/filesystem.cxx | 20 ++++++++++---------- libbutl/filesystem.mxx | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/libbutl/filesystem.cxx b/libbutl/filesystem.cxx index 8c3abc4..da2f12c 100644 --- a/libbutl/filesystem.cxx +++ b/libbutl/filesystem.cxx @@ -75,34 +75,34 @@ using namespace std; namespace butl { bool - file_exists (const char* p, bool fl) + file_exists (const char* p, bool fl, bool ie) { - auto pe (path_entry (p, fl)); + auto pe (path_entry (p, fl, ie)); return pe.first && (pe.second.type == entry_type::regular || (!fl && pe.second.type == entry_type::symlink)); } bool - entry_exists (const char* p, bool fl) + entry_exists (const char* p, bool fl, bool ie) { - return path_entry (p, fl).first; + return path_entry (p, fl, ie).first; } bool - dir_exists (const char* p) + dir_exists (const char* p, bool ie) { - auto pe (path_entry (p, true)); + auto pe (path_entry (p, true, ie)); return pe.first && pe.second.type == entry_type::directory; } #ifndef _WIN32 pair - path_entry (const char* p, bool fl) + path_entry (const char* p, bool fl, bool ie) { struct stat s; if ((fl ? stat (p, &s) : lstat (p, &s)) != 0) { - if (errno == ENOENT || errno == ENOTDIR) + if (errno == ENOENT || errno == ENOTDIR || ie) return make_pair (false, entry_stat {entry_type::unknown, 0}); else throw_generic_error (errno); @@ -124,7 +124,7 @@ namespace butl } #else pair - path_entry (const char* p, bool) + path_entry (const char* p, bool, bool ie) { // A path like 'C:', while being a root path in our terminology, is not as // such for Windows, that maintains current directory for each drive, and @@ -157,7 +157,7 @@ namespace butl if (_stat64 (p, &s) != 0) { - if (errno == ENOENT || errno == ENOTDIR) + if (errno == ENOENT || errno == ENOTDIR || ie) return make_pair (false, entry_stat {entry_type::unknown, 0}); else throw_generic_error (errno); diff --git a/libbutl/filesystem.mxx b/libbutl/filesystem.mxx index b4caffe..bb78a4f 100644 --- a/libbutl/filesystem.mxx +++ b/libbutl/filesystem.mxx @@ -57,33 +57,41 @@ import butl.utility; LIBBUTL_MODEXPORT namespace butl { // Return true if the path is to an existing regular file. Note that by - // default this function follows symlinks. + // default this function follows symlinks. Underlying OS errors are reported + // by throwing std::system_error, unless ignore_error is true. // LIBBUTL_SYMEXPORT bool - file_exists (const char*, bool follow_symlinks = true); + file_exists (const char*, + bool follow_symlinks = true, + bool ignore_error = false); inline bool - file_exists (const path& p, bool fs = true) { - return file_exists (p.string ().c_str (), fs);} + file_exists (const path& p, bool fs = true, bool ie = false) { + return file_exists (p.string ().c_str (), fs, ie);} // Return true if the path is to an existing directory. Note that this - // function follows symlinks. + // function follows symlinks. Underlying OS errors are reported by throwing + // std::system_error, unless ignore_error is true. // LIBBUTL_SYMEXPORT bool - dir_exists (const char*); + dir_exists (const char*, bool ignore_error = false); inline bool - dir_exists (const path& p) {return dir_exists (p.string ().c_str ());} + dir_exists (const path& p, bool ie = false) { + return dir_exists (p.string ().c_str (), ie);} // Return true if the path is to an existing file system entry. Note that by - // default this function doesn't follow symlinks. + // default this function doesn't follow symlinks. Underlying OS errors are + // reported by throwing std::system_error, unless ignore_error is true. // LIBBUTL_SYMEXPORT bool - entry_exists (const char*, bool follow_symlinks = false); + entry_exists (const char*, + bool follow_symlinks = false, + bool ignore_error = false); inline bool - entry_exists (const path& p, bool fs = false) { - return entry_exists (p.string ().c_str (), fs);} + entry_exists (const path& p, bool fs = false, bool ie = false) { + return entry_exists (p.string ().c_str (), fs, ie);} // Filesystem entry type. // @@ -106,14 +114,17 @@ LIBBUTL_MODEXPORT namespace butl // Return a flag indicating if the path is to an existing file system entry // and its type if so. Note that by default this function doesn't follow - // symlinks. + // symlinks. Underlying OS errors are reported by throwing std::system_error, + // unless ignore_error is true. // LIBBUTL_SYMEXPORT std::pair - path_entry (const char*, bool follow_symlinks = false); + path_entry (const char*, + bool follow_symlinks = false, + bool ignore_error = false); inline std::pair - path_entry (const path& p, bool fs = false) { - return path_entry (p.string ().c_str (), fs);} + path_entry (const path& p, bool fs = false, bool ie = false) { + return path_entry (p.string ().c_str (), fs, ie);} // Return true if the directory is empty. Note that the path must exist // and be a directory. This function follows symlinks. -- cgit v1.1