diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2016-11-01 22:58:25 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2016-11-01 22:58:25 +0300 |
commit | cc8a2a1517cc3c55bdeb066a038868fb8c7f04d6 (patch) | |
tree | 54d826346d84b957d1e3cb595f3b89da73d3cb03 | |
parent | 4010a0ee23adea7938b005f72576c5e716888abf (diff) |
Add entry_exists(path)
-rw-r--r-- | butl/filesystem | 10 | ||||
-rw-r--r-- | butl/filesystem.cxx | 21 |
2 files changed, 31 insertions, 0 deletions
diff --git a/butl/filesystem b/butl/filesystem index e6aaa20..3de1fcb 100644 --- a/butl/filesystem +++ b/butl/filesystem @@ -51,6 +51,16 @@ namespace butl inline bool dir_exists (const path& p) {return dir_exists (p.string ().c_str ());} + // Return true if the path is to an existing file system entry. Note that by + // default this function doesn't follow symlinks. + // + LIBBUTL_EXPORT bool + entry_exists (const char*, bool follow_symlinks = false); + + inline bool + entry_exists (const path& p, bool fs = false) { + return entry_exists (p.string ().c_str (), fs);} + // Return true if the directory is empty. Note that the path must exist // and be a directory. // diff --git a/butl/filesystem.cxx b/butl/filesystem.cxx index a47140d..424e361 100644 --- a/butl/filesystem.cxx +++ b/butl/filesystem.cxx @@ -66,6 +66,27 @@ namespace butl } #endif +#ifndef _WIN32 + bool + entry_exists (const char* p, bool fl) + { + struct stat s; + if ((fl ? stat (p, &s) : lstat (p, &s)) == 0) +#else + bool + entry_exists (const char* p, bool) + { + struct _stat s; + if (_stat (p, &s) == 0) +#endif + return true; + + if (errno == ENOENT || errno == ENOTDIR) + return false; + + throw system_error (errno, system_category ()); + } + bool dir_exists (const char* p) { |