aboutsummaryrefslogtreecommitdiff
path: root/src/nix/test.cc
blob: 7f1f0d47aab9da26f0c11f1482893cbf34d756e1 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "command.hh"
#include "store-api.hh"
#include "common-args.hh"
#include "compression.hh"

using namespace nix;

namespace rust {

// Depending on the internal representation of Rust slices is slightly
// evil...
template<typename T> struct Slice
{
    T * ptr;
    size_t size;

    Slice(T * ptr, size_t size) : ptr(ptr), size(size)
    {
        assert(ptr);
    }
};

struct StringSlice : Slice<char>
{
    StringSlice(const std::string & s): Slice((char *) s.data(), s.size()) {}
};

struct Source
{
    size_t (*fun)(void * source_this, rust::Slice<uint8_t> data);
    nix::Source * _this;

    Source(nix::Source & _this)
        : fun(sourceWrapper), _this(&_this)
    {}

    // FIXME: how to propagate exceptions?
    static size_t sourceWrapper(void * _this, rust::Slice<uint8_t> data)
    {
        auto n = ((nix::Source *) _this)->read(data.ptr, data.size);
        return n;
    }
};

}

extern "C" {
    bool unpack_tarfile(rust::Source source, rust::StringSlice dest_dir);
}

struct CmdTest : StoreCommand
{
    CmdTest()
    {
    }

    std::string name() override
    {
        return "test";
    }

    std::string description() override
    {
        return "bla bla";
    }

    void run(ref<Store> store) override
    {
        auto source = sinkToSource([&](Sink & sink) {
            auto decompressor = makeDecompressionSink("bzip2", sink);
            readFile("./nix-2.2.tar.bz2", *decompressor);
            decompressor->finish();
        });

        std::string destDir = "./dest";

        deletePath(destDir);

        unpack_tarfile(*source, destDir);
    }
};

static RegisterCommand r(make_ref<CmdTest>());