aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/builtins/unpack-channel.cc
blob: 5fc68cd666cec60aa38f5fb1aec6364d2d8d92eb (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
39
#include "builtins.hh"
#include "compression.hh"
#include "tarfile.hh"

namespace nix {

void builtinUnpackChannel(const BasicDerivation & drv)
{
    auto getAttr = [&](const string & name) {
        auto i = drv.env.find(name);
        if (i == drv.env.end()) throw Error("attribute '%s' missing", name);
        return i->second;
    };

    Path out = getAttr("out");
    auto channelName = getAttr("channelName");
    auto src = getAttr("src");

    createDirs(out);

    auto source = sinkToSource([&](Sink & sink) {
        auto decompressor =
            hasSuffix(src, ".bz2") ? makeDecompressionSink("bzip2", sink) :
            hasSuffix(src, ".xz") ? makeDecompressionSink("xz", sink) :
            makeDecompressionSink("none", sink);
        readFile(src, *decompressor);
        decompressor->finish();
    });

    unpackTarfile(*source, out);

    auto entries = readDirectory(out);
    if (entries.size() != 1)
        throw Error("channel tarball '%s' contains more than one file", src);
    if (rename((out + "/" + entries[0].name).c_str(), (out + "/" + channelName).c_str()) == -1)
        throw SysError("renaming channel directory");
}

}