Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace call to stat, lstat, and maybeLstat with appropriate functions from std::filesystem #10937

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Path -> fs::path in functions of file-system.cc
  • Loading branch information
siddhantk232 committed Jun 26, 2024
commit 131008397bc9f37d6f7bb4d879dd8e30dc422858
4 changes: 2 additions & 2 deletions src/libstore/builtins/buildenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir,
/* not matched by glob */
continue;
auto srcFile = (fs::path{srcDir} / name).string();
auto dstFile = (fs::path{dstDir} / name).string();
auto dstFile = fs::path{dstDir} / name;

fs::file_status srcSt;

Expand Down Expand Up @@ -79,7 +79,7 @@ static void createLinks(State & state, const Path & srcDir, const Path & dstDir,
createLinks(state, srcFile, dstFile, priority);
continue;
} else if (fs::is_symlink(dstSt)) {
auto target = canonPath(dstFile, true);
auto target = canonPath(dstFile.string(), true);
if (!fs::is_directory(fs::symlink_status(target)))
throw Error("collision between '%1%' and non-directory '%2%'", srcFile, target);
if (unlink(dstFile.c_str()) == -1)
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/unix/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ void LocalDerivationGoal::runChild()
if (pathExists(path))
ss.push_back(path);

if (settings.caFile != "" && pathExists(settings.caFile)) {
if (settings.caFile != "" && pathExists(settings.caFile.to_string())) {
Path caFile = settings.caFile;
pathsInChroot.try_emplace("/etc/ssl/certs/ca-certificates.crt", canonPath(caFile, true), true);
}
Expand Down
8 changes: 4 additions & 4 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ std::optional<struct stat> unix::maybeLstat(const Path & path)
}
#endif

std::optional<fs::file_status> maybeSymlinkStat(const Path & path)
std::optional<fs::file_status> maybeSymlinkStat(const fs::path & path)
{
try {
auto st = fs::symlink_status(path);
Expand All @@ -199,12 +199,12 @@ std::optional<fs::file_status> maybeSymlinkStat(const Path & path)
}


bool pathExists(const Path & path)
bool pathExists(const fs::path & path)
{
return maybeSymlinkStat(path).has_value();
}

bool pathAccessible(const Path & path)
bool pathAccessible(const fs::path & path)
{
try {
return pathExists(path);
Expand Down Expand Up @@ -640,7 +640,7 @@ void copyFile(const fs::path & from, const fs::path & to, bool andDelete)
throw Error("file '%s' has an unsupported type", from);
}

setWriteTime(to, lstat(from.string().c_str()));
setWriteTime(to, unix::lstat(from.string().c_str()));
if (andDelete) {
if (!fs::is_symlink(fromStatus))
fs::permissions(from, fs::perms::owner_write, fs::perm_options::add | fs::perm_options::);
Expand Down
6 changes: 3 additions & 3 deletions src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ struct stat lstat(const Path & path);
* call `std::filesystem::symlink_status` on the given path if it exists.
* @return std::nullopt if the path doesn't exist, or an optional containing the result of `std::filesystem::symlink_status` otherwise
*/
std::optional<std::filesystem::file_status> maybeSymlinkStat(const Path & path);
std::optional<std::filesystem::file_status> maybeSymlinkStat(const std::filesystem::path & path);

/**
* @return true iff the given path exists.
*/
bool pathExists(const Path & path);
bool pathExists(const std::filesystem::path & path);

/**
* A version of pathExists that returns false on a permission error.
* Useful for inferring default paths across directories that might not
* be readable.
* @return true iff the given path can be accessed and exists
*/
bool pathAccessible(const Path & path);
bool pathAccessible(const std::filesystem::path & path);

/**
* Read the contents (target) of a symbolic link. The result is not
Expand Down
Loading