1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00

builtins.unpackChannel: wrap filesystem errors and sanitize channelName

Otherwise these errors are not caught correctly
This commit is contained in:
Jörg Thalheim 2024-09-05 12:59:54 +02:00
parent 05a1ffe236
commit 70c52d72f4

View file

@ -13,21 +13,37 @@ void builtinUnpackChannel(
return i->second; return i->second;
}; };
auto out = outputs.at("out"); std::filesystem::path out(outputs.at("out"));
auto channelName = getAttr("channelName"); std::filesystem::path channelName(getAttr("channelName"));
auto src = getAttr("src"); auto src = getAttr("src");
if (channelName.filename() != channelName) {
throw Error("channelName is not allowed to contain filesystem seperators, got %1%", channelName);
}
createDirs(out); createDirs(out);
unpackTarfile(src, out); unpackTarfile(src, out);
auto entries = std::filesystem::directory_iterator{out}; size_t fileCount;
auto fileName = entries->path().string(); std::string fileName;
auto fileCount = std::distance(std::filesystem::begin(entries), std::filesystem::end(entries)); try {
auto entries = std::filesystem::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);
}
if (fileCount != 1) if (fileCount != 1)
throw Error("channel tarball '%s' contains more than one file", src); throw Error("channel tarball '%s' contains more than one file", src);
std::filesystem::rename(fileName, (out + "/" + channelName)); std::filesystem::path 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);
}
} }
} }