Fix tests when test working directory is on another device

* Fallback to copying if hard link creation fails due to cross device link
This commit is contained in:
Martchus 2021-09-30 22:22:22 +02:00
parent a93924bb3a
commit ace478be22
1 changed files with 15 additions and 3 deletions

View File

@ -580,6 +580,19 @@ void BuildActionsTests::testConductingBuild()
std::filesystem::is_regular_file("repos/boost-staging/os/x86_64/boost-libs-1.73.0-1-x86_64.pkg.tar.zst.sig"));
}
static void hardlinkOrCopy(
const std::filesystem::path &from, const std::filesystem::path &to, std::filesystem::copy_options options = std::filesystem::copy_options::none)
{
try {
std::filesystem::copy(from, to, options | std::filesystem::copy_options::create_hard_links);
} catch (const std::filesystem::filesystem_error &e) {
if (e.code() != std::errc::cross_device_link) {
throw;
}
std::filesystem::copy(from, to, options);
}
}
static void copyRepo(const std::filesystem::path &origRepoDir, const std::filesystem::path &destRepoDir)
{
const auto origFiles = std::filesystem::directory_iterator(origRepoDir);
@ -592,11 +605,10 @@ static void copyRepo(const std::filesystem::path &origRepoDir, const std::filesy
if (const auto parentPath = target.parent_path(); parentPath.empty() || parentPath == "../any") {
std::filesystem::copy(origFile.path(), destRepoDir / origFile.path().filename(), std::filesystem::copy_options::copy_symlinks);
} else {
std::filesystem::copy(std::filesystem::absolute(origRepoDir / target), destRepoDir / origFile.path().filename(),
std::filesystem::copy_options::create_hard_links);
hardlinkOrCopy(std::filesystem::absolute(origRepoDir / target), destRepoDir / origFile.path().filename());
}
} else {
std::filesystem::copy(origFile.path(), destRepoDir / origFile.path().filename(), std::filesystem::copy_options::create_hard_links);
hardlinkOrCopy(origFile.path(), destRepoDir / origFile.path().filename());
}
}
}