aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/tarfile.cc
blob: 2cc7793fde8ce22d1b32410947e87192daf4ef46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "rust-ffi.hh"
#include "compression.hh"

extern "C" {
    rust::Result<std::tuple<>> *
    unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
}

namespace nix {

void unpackTarfile(Source & source, const Path & destDir)
{
    rust::Source source2(source);
    rust::CBox(unpack_tarfile(source2, destDir))->unwrap();
}

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);
}

}