aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/tarfile.cc
blob: 1be0ba24c0f89d8bed2a60154ad4b799804dcefd (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
37
38
#include "rust-ffi.hh"
#include "compression.hh"

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

namespace nix {

void unpackTarfile(Source & source, const Path & destDir)
{
    rust::Source source2(source);
    rust::Result<std::tuple<>> res;
    unpack_tarfile(source2, destDir, res);
    res.unwrap();
}

void unpackTarfile(const Path & tarFile, const Path & destDir,
    std::optional<std::string> baseName)
{
    if (!baseName) baseName = std::string(baseNameOf(tarFile));

    auto source = sinkToSource([&](Sink & sink) {
        // FIXME: look at first few bytes to determine compression type.
        auto decompressor =
            hasSuffix(*baseName, ".bz2") ? makeDecompressionSink("bzip2", sink) :
            hasSuffix(*baseName, ".gz") ? makeDecompressionSink("gzip", sink) :
            hasSuffix(*baseName, ".xz") ? makeDecompressionSink("xz", sink) :
            makeDecompressionSink("none", sink);
        readFile(tarFile, *decompressor);
        decompressor->finish();
    });

    unpackTarfile(*source, destDir);
}

}