1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-21 17:59:36 +01:00
nix/src/libstore/builtins/unpack-channel.cc
Graham Christensen e4f62e4608 Apply clang-format universally.
* It is tough to contribute to a project that doesn't use a formatter,
* It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
* Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
2025-07-18 12:47:27 -04:00

46 lines
1.4 KiB
C++

#include "nix/store/builtins.hh"
#include "nix/util/tarfile.hh"
namespace nix {
static void builtinUnpackChannel(const BuiltinBuilderContext & ctx)
{
auto getAttr = [&](const std::string & name) -> const std::string & {
auto i = ctx.drv.env.find(name);
if (i == ctx.drv.env.end())
throw Error("attribute '%s' missing", name);
return i->second;
};
std::filesystem::path out{ctx.outputs.at("out")};
auto & channelName = getAttr("channelName");
auto & src = getAttr("src");
if (std::filesystem::path{channelName}.filename().string() != channelName) {
throw Error("channelName is not allowed to contain filesystem separators, got %1%", channelName);
}
createDirs(out);
unpackTarfile(src, out);
size_t fileCount;
std::string fileName;
auto entries = DirectoryIterator{out};
fileName = entries->path().string();
fileCount = std::distance(entries.begin(), entries.end());
if (fileCount != 1)
throw Error("channel tarball '%s' contains more than one file", src);
auto target = out / channelName;
try {
std::filesystem::rename(fileName, target);
} catch (std::filesystem::filesystem_error &) {
throw SysError("failed to rename %1% to %2%", fileName, target.string());
}
}
static RegisterBuiltinBuilder registerUnpackChannel("unpack-channel", builtinUnpackChannel);
} // namespace nix