summaryrefslogtreecommitdiffstats
path: root/src/libutil/tarfile.cc
blob: 68e918891d92d8c7549a12ca9dfa4fa8734d5f81 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <archive.h>
#include <archive_entry.h>

#include "serialise.hh"

namespace nix {

struct TarArchive {
    struct archive *archive;
    Source *source;
    std::vector<unsigned char> buffer;

    void check(int err, const char *reason = "Failed to extract archive (%s)") {
        if (err == ARCHIVE_EOF)
            throw EndOfFile("reached end of archive");
        else if (err != ARCHIVE_OK)
            throw Error(reason, archive_error_string(this->archive));
    }

    TarArchive(Source& source) : buffer(4096) {
        this->archive = archive_read_new();
        this->source = &source;

        archive_read_support_filter_all(archive);
        archive_read_support_format_all(archive);
        check(archive_read_open(archive, (void *)this, TarArchive::callback_open, TarArchive::callback_read, TarArchive::callback_close), "Failed to open archive (%s)");
    }

    TarArchive(const Path &path) {
        this->archive = archive_read_new();

        archive_read_support_filter_all(archive);
        archive_read_support_format_all(archive);
        check(archive_read_open_filename(archive, path.c_str(), 16384), "Failed to open archive (%s)");
    }

    // disable copy constructor
    TarArchive(const TarArchive&) = delete;

    void close() {
        check(archive_read_close(archive), "Failed to close archive (%s)");
    }

    ~TarArchive() {
        if (this->archive) archive_read_free(this->archive);
    }

private:
    static int callback_open(struct archive *, void *self) {
        return ARCHIVE_OK;
    }

    static ssize_t callback_read(struct archive *archive, void *_self, const void **buffer) {
        TarArchive *self = (TarArchive *)_self;
        *buffer = self->buffer.data();

        try {
            return self->source->read(self->buffer.data(), 4096);
        } catch (EndOfFile &) {
            return 0;
        } catch (std::exception &err) {
            archive_set_error(archive, EIO, "Source threw exception: %s", err.what());

            return -1;
        }
    }

    static int callback_close(struct archive *, void *self) {
        return ARCHIVE_OK;
    }
};

struct PushD {
    char * oldDir;

    PushD(const std::string &newDir)  {
        oldDir = getcwd(0, 0);
        if (!oldDir) throw SysError("getcwd");
        int r = chdir(newDir.c_str());
        if (r != 0) throw SysError("changing directory to tar output path");
    }

    ~PushD() {
        int r = chdir(oldDir);
        free(oldDir);
        if (r != 0)
            std::cerr << "warning: failed to change directory back after tar extraction";
        /* can't throw out of a destructor */
    }
};

static void extract_archive(TarArchive &archive, const Path & destDir) {
    // need to chdir back *after* archive closing
    PushD newDir(destDir);
    struct archive_entry *entry;
    int flags = ARCHIVE_EXTRACT_FFLAGS
        | ARCHIVE_EXTRACT_PERM
        | ARCHIVE_EXTRACT_SECURE_SYMLINKS
        | ARCHIVE_EXTRACT_SECURE_NODOTDOT
        | ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;

    for(;;) {
        int r = archive_read_next_header(archive.archive, &entry);
        if (r == ARCHIVE_EOF) break;
        else if (r == ARCHIVE_WARN)
            std::cerr << "warning: " << archive_error_string(archive.archive) << std::endl;
        else
            archive.check(r);

        archive.check(archive_read_extract(archive.archive, entry, flags));
    }

    archive.close();
}

void unpackTarfile(Source & source, const Path & destDir)
{
    auto archive = TarArchive(source);

    createDirs(destDir);
    extract_archive(archive, destDir);
}

void unpackTarfile(const Path & tarFile, const Path & destDir)
{
    auto archive = TarArchive(tarFile);

    createDirs(destDir);
    extract_archive(archive, destDir);
}

}