1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-18 00:12:43 +01:00

tweak unpack channel built-in, std::filesystem::path for tarball

This commit is contained in:
John Ericson 2024-09-11 11:59:11 -04:00
parent 04ce0e648a
commit 193dc49097
3 changed files with 37 additions and 27 deletions

View file

@ -3,46 +3,52 @@
namespace nix {
namespace fs { using namespace std::filesystem; }
void builtinUnpackChannel(
const BasicDerivation & drv,
const std::map<std::string, Path> & outputs)
{
auto getAttr = [&](const std::string & name) {
auto getAttr = [&](const std::string & name) -> const std::string & {
auto i = drv.env.find(name);
if (i == drv.env.end()) throw Error("attribute '%s' missing", name);
return i->second;
};
std::filesystem::path out(outputs.at("out"));
std::filesystem::path channelName(getAttr("channelName"));
auto src = getAttr("src");
fs::path out{outputs.at("out")};
auto & channelName = getAttr("channelName");
auto & src = getAttr("src");
if (channelName.filename() != channelName) {
if (fs::path{channelName}.filename().string() != channelName) {
throw Error("channelName is not allowed to contain filesystem seperators, got %1%", channelName);
}
createDirs(out);
try {
fs::create_directories(out);
} catch (fs::filesystem_error &) {
throw SysError("creating directory '%1%'", out.string());
}
unpackTarfile(src, out);
size_t fileCount;
std::string fileName;
try {
auto entries = std::filesystem::directory_iterator{out};
auto entries = fs::directory_iterator{out};
fileName = entries->path().string();
fileCount = std::distance(std::filesystem::begin(entries), std::filesystem::end(entries));
} catch (std::filesystem::filesystem_error &e) {
throw SysError("failed to read directory %1%", out);
fileCount = std::distance(fs::begin(entries), fs::end(entries));
} catch (fs::filesystem_error &) {
throw SysError("failed to read directory %1%", out.string());
}
if (fileCount != 1)
throw Error("channel tarball '%s' contains more than one file", src);
std::filesystem::path target(out / channelName);
auto target = out / channelName;
try {
std::filesystem::rename(fileName, target);
} catch (std::filesystem::filesystem_error &e) {
throw SysError("failed to rename %1% to %2%", fileName, target);
fs::rename(fileName, target);
} catch (fs::filesystem_error &) {
throw SysError("failed to rename %1% to %2%", fileName, target.string());
}
}