Skip to content

Commit

Permalink
use path for from arg in nix::copyFile
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantk232 committed May 13, 2024
1 parent ccf9454 commit 62e1ea2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/libstore/unix/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ static void doBind(const Path & source, const Path & target, bool optional = fal
// Symlinks can (apparently) not be bind-mounted, so just copy it
createDirs(dirOf(target));
copyFile(
std::filesystem::directory_entry(std::filesystem::path(source)),
std::filesystem::path(source),
std::filesystem::path(target), false);
} else {
createDirs(dirOf(target));
Expand Down Expand Up @@ -2571,7 +2571,7 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs()
// that there's no stale file descriptor pointing to it
Path tmpOutput = actualPath + ".tmp";
copyFile(
std::filesystem::directory_entry(std::filesystem::path(actualPath)),
std::filesystem::path(actualPath),
std::filesystem::path(tmpOutput), true);

std::filesystem::rename(tmpOutput, actualPath);
Expand Down
20 changes: 10 additions & 10 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -605,38 +605,38 @@ static void setWriteTime(const fs::path & p, const struct stat & st)
}
#endif

void copyFile(const fs::directory_entry & from, const fs::path & to, bool andDelete)
void copyFile(const fs::path & from, const fs::path & to, bool andDelete)
{
#ifndef _WIN32
// TODO: Rewrite the `is_*` to use `symlink_status()`
auto statOfFrom = lstat(from.path().c_str());
auto statOfFrom = lstat(from.c_str());
#endif
auto fromStatus = from.symlink_status();
auto fromStatus = fs::symlink_status(from);

// Mark the directory as writable so that we can delete its children
if (andDelete && fs::is_directory(fromStatus)) {
fs::permissions(from.path(), fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
fs::permissions(from, fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
}


if (fs::is_symlink(fromStatus) || fs::is_regular_file(fromStatus)) {
fs::copy(from.path(), to, fs::copy_options::copy_symlinks | fs::copy_options::overwrite_existing);
fs::copy(from, to, fs::copy_options::copy_symlinks | fs::copy_options::overwrite_existing);
} else if (fs::is_directory(fromStatus)) {
fs::create_directory(to);
for (auto & entry : fs::directory_iterator(from.path())) {
for (auto & entry : fs::directory_iterator(from)) {
copyFile(entry, to / entry.path().filename(), andDelete);
}
} else {
throw Error("file '%s' has an unsupported type", from.path());
throw Error("file '%s' has an unsupported type", from);
}

#ifndef _WIN32
setWriteTime(to, statOfFrom);
#endif
if (andDelete) {
if (!fs::is_symlink(fromStatus))
fs::permissions(from.path(), fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
fs::remove(from.path());
fs::permissions(from, fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
fs::remove(from);
}
}

Expand All @@ -657,7 +657,7 @@ void moveFile(const Path & oldName, const Path & newName)
if (e.code().value() == EXDEV) {
fs::remove(newPath);
warn("Can’t rename %s as %s, copying instead", oldName, newName);
copyFile(fs::directory_entry(oldPath), tempCopyTarget, true);
copyFile(oldPath, tempCopyTarget, true);
std::filesystem::rename(
os_string_to_string(PathViewNG { tempCopyTarget }),
os_string_to_string(PathViewNG { newPath }));
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void moveFile(const Path & src, const Path & dst);
* with the guaranty that the destination will be “fresh”, with no stale inode
* or file descriptor pointing to it).
*/
void copyFile(const std::filesystem::directory_entry & from, const std::filesystem::path & to, bool andDelete);
void copyFile(const std::filesystem::path & from, const std::filesystem::path & to, bool andDelete);

/**
* Automatic cleanup of resources.
Expand Down

0 comments on commit 62e1ea2

Please sign in to comment.