mirror of
https://github.com/NixOS/nix
synced 2025-07-06 05:01:48 +02:00
Merge pull request #10362 from obsidiansystems/maybeLstat
Factor out `nix::maybeLstat`
This commit is contained in:
commit
9b88e52846
5 changed files with 40 additions and 37 deletions
|
@ -174,15 +174,23 @@ struct stat lstat(const Path & path)
|
|||
}
|
||||
|
||||
|
||||
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)
|
||||
|
|
|
@ -84,6 +84,11 @@ bool isDirOrInDir(std::string_view path, std::string_view dir);
|
|||
*/
|
||||
struct stat stat(const Path & path);
|
||||
struct stat lstat(const Path & path);
|
||||
/**
|
||||
* `lstat` the given path if it exists.
|
||||
* @return std::nullopt if the path doesn't exist, or an optional containing the result of `lstat` otherwise
|
||||
*/
|
||||
std::optional<struct stat> maybeLstat(const Path & path);
|
||||
|
||||
/**
|
||||
* @return true iff the given path exists.
|
||||
|
|
|
@ -97,13 +97,7 @@ std::optional<struct stat> PosixSourceAccessor::cachedLstat(const CanonPath & pa
|
|||
if (i != cache->end()) return i->second;
|
||||
}
|
||||
|
||||
std::optional<struct stat> st{std::in_place};
|
||||
if (::lstat(absPath.c_str(), &*st)) {
|
||||
if (errno == ENOENT || errno == ENOTDIR)
|
||||
st.reset();
|
||||
else
|
||||
throw SysError("getting status of '%s'", showPath(path));
|
||||
}
|
||||
auto st = nix::maybeLstat(absPath.c_str());
|
||||
|
||||
auto cache(_cache.lock());
|
||||
if (cache->size() >= 16384) cache->clear();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue