lix/src/libutil/tarfile.cc

37 lines
1,023 B
C++
Raw Normal View History

2019-09-11 12:43:07 +02:00
#include "rust-ffi.hh"
#include "compression.hh"
2019-03-27 14:12:20 +01:00
extern "C" {
rust::Result<std::tuple<>> *
unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
2019-03-27 14:12:20 +01:00
}
2019-09-11 13:10:46 +02:00
namespace nix {
void unpackTarfile(Source & source, const Path & destDir)
2019-09-11 13:10:46 +02:00
{
rust::Source source2(source);
rust::CBox(unpack_tarfile(source2, destDir))->unwrap();
2019-09-11 13:10:46 +02:00
}
void unpackTarfile(const Path & tarFile, const Path & destDir,
std::optional<std::string> baseName)
{
if (!baseName) baseName = baseNameOf(tarFile);
auto source = sinkToSource([&](Sink & sink) {
// FIXME: look at first few bytes to determine compression type.
auto decompressor =
// FIXME: add .gz support
hasSuffix(*baseName, ".bz2") ? makeDecompressionSink("bzip2", sink) :
hasSuffix(*baseName, ".xz") ? makeDecompressionSink("xz", sink) :
makeDecompressionSink("none", sink);
readFile(tarFile, *decompressor);
decompressor->finish();
});
unpackTarfile(*source, destDir);
}
2019-09-11 13:10:46 +02:00
}