diff options
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r-- | src/libutil/util.cc | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 5cd4df8e6..dc724db3e 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -256,16 +256,22 @@ struct stat lstat(const Path & path) return st; } +std::optional<struct stat> maybeLstat(const Path & path) +{ + std::optional<struct stat> st{std::in_place}; + if (lstat(path.c_str(), &*st)) + { + if (errno == ENOENT || errno == ENOTDIR) + st.reset(); + else + throw SysError("getting status of '%s'", path); + } + return st; +} bool pathExists(const Path & path) { - int res; - struct stat st; - res = lstat(path.c_str(), &st); - if (!res) return true; - if (errno != ENOENT && errno != ENOTDIR) - throw SysError("getting status of %1%", path); - return false; + return maybeLstat(path).has_value(); } bool pathAccessible(const Path & path) |