about summary refs log tree commit diff
path: root/src/libnix/archive.cc
blob: 9039ad7db43e238f7bf493f1342fce48a185ba38 (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <cerrno>
#include <algorithm>
#include <vector>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>

#include "archive.hh"
#include "util.hh"


static string archiveVersion1 = "nix-archive-1";


static void writePadding(unsigned int len, DumpSink & sink)
{
    if (len % 8) {
        unsigned char zero[8];
        memset(zero, 0, sizeof(zero));
        sink(zero, 8 - (len % 8));
    }
}


static void writeInt(unsigned int n, DumpSink & sink)
{
    unsigned char buf[8];
    memset(buf, 0, sizeof(buf));
    buf[0] = n & 0xff;
    buf[1] = (n >> 8) & 0xff;
    buf[2] = (n >> 16) & 0xff;
    buf[3] = (n >> 24) & 0xff;
    sink(buf, sizeof(buf));
}


static void writeString(const string & s, DumpSink & sink)
{
    unsigned int len = s.length();
    writeInt(len, sink);
    sink((const unsigned char *) s.c_str(), len);
    writePadding(len, sink);
}


static void dump(const string & path, DumpSink & sink);


static void dumpEntries(const Path & path, DumpSink & sink)
{
    DIR * dir = opendir(path.c_str());
    if (!dir) throw SysError("opening directory " + path);

    vector<string> names;

    struct dirent * dirent;
    while (errno = 0, dirent = readdir(dir)) {
        string name = dirent->d_name;
        if (name == "." || name == "..") continue;
        names.push_back(name);
    }
    if (errno) throw SysError("reading directory " + path);

    sort(names.begin(), names.end());

    for (vector<string>::iterator it = names.begin();
         it != names.end(); it++)
    {
        writeString("entry", sink);
        writeString("(", sink);
        writeString("name", sink);
        writeString(*it, sink);
        writeString("node", sink);
        dump(path + "/" + *it, sink);
        writeString(")", sink);
    }
    
    closedir(dir); /* !!! close on exception */
}


static void dumpContents(const Path & path, unsigned int size, 
    DumpSink & sink)
{
    writeString("contents", sink);
    writeInt(size, sink);

    int fd = open(path.c_str(), O_RDONLY);
    if (fd == -1) throw SysError(format("opening file `%1%'") % path);
    
    unsigned char buf[65536];

    unsigned int total = 0;
    ssize_t n;
    while ((n = read(fd, buf, sizeof(buf)))) {
        if (n == -1) throw SysError("reading file " + path);
        total += n;
        sink(buf, n);
    }

    if (total != size)
        throw SysError("file changed while reading it: " + path);

    writePadding(size, sink);

    close(fd); /* !!! close on exception */
}


static void dump(const Path & path, DumpSink & sink)
{
    struct stat st;
    if (lstat(path.c_str(), &st))
        throw SysError(format("getting attributes of path `%1%'") % path);

    writeString("(", sink);

    if (S_ISREG(st.st_mode)) {
        writeString("type", sink);
        writeString("regular", sink);
        if (st.st_mode & S_IXUSR) {
            writeString("executable", sink);
            writeString("", sink);
        }
        dumpContents(path, st.st_size, sink);
    } 

    else if (S_ISDIR(st.st_mode)) {
        writeString("type", sink);
        writeString("directory", sink);
        dumpEntries(path, sink);
    }

    else if (S_ISLNK(st.st_mode)) {
        writeString("type", sink);
        writeString("symlink", sink);
        char buf[st.st_size];
        if (readlink(path.c_str(), buf, st.st_size) != st.st_size)
            throw SysError("reading symbolic link " + path);
        writeString("target", sink);
        writeString(string(buf, st.st_size), sink);
    }

    else throw Error("unknown file type: " + path);

    writeString(")", sink);
}


void dumpPath(const Path & path, DumpSink & sink)
{
    writeString(archiveVersion1, sink);
    dump(path, sink);
}


static Error badArchive(string s)
{
    return Error("bad archive: " + s);
}


static void readPadding(unsigned int len, RestoreSource & source)
{
    if (len % 8) {
        unsigned char zero[8];
        unsigned int n = 8 - (len % 8);
        source(zero, n);
        for (unsigned int i = 0; i < n; i++)
            if (zero[i]) throw badArchive("non-zero padding");
    }
}

static unsigned int readInt(RestoreSource & source)
{
    unsigned char buf[8];
    source(buf, sizeof(buf));
    if (buf[4] || buf[5] || buf[6] || buf[7])
        throw Error("implementation cannot deal with > 32-bit integers");
    return
        buf[0] |
        (buf[1] << 8) |
        (buf[2] << 16) |
        (buf[3] << 24);
}


static string readString(RestoreSource & source)
{
    unsigned int len = readInt(source);
    char buf[len];
    source((unsigned char *) buf, len);
    readPadding(len, source);
    return string(buf, len);
}


static void skipGeneric(RestoreSource & source)
{
    if (readString(source) == "(") {
        while (readString(source) != ")")
            skipGeneric(source);
    }
}


static void restore(const Path & path, RestoreSource & source);


static void restoreEntry(const Path & path, RestoreSource & source)
{
    string s, name;

    s = readString(source);
    if (s != "(") throw badArchive("expected open tag");

    while (1) {
        s = readString(source);

        if (s == ")") {
            break;
        } else if (s == "name") {
            name = readString(source);
        } else if (s == "node") {
            if (s == "") throw badArchive("entry name missing");
            restore(path + "/" + name, source);
        } else {
            throw badArchive("unknown field " + s);
            skipGeneric(source);
        }
    }
}


static void restoreContents(int fd, const Path & path, RestoreSource & source)
{
    unsigned int size = readInt(source);
    unsigned int left = size;
    unsigned char buf[65536];

    while (left) {
        unsigned int n = sizeof(buf);
        if (n > left) n = left;
        source(buf, n);
        if (write(fd, buf, n) != (ssize_t) n)
            throw SysError("writing file " + path);
        left -= n;
    }

    readPadding(size, source);
}


static void restore(const Path & path, RestoreSource & source)
{
    string s;

    s = readString(source);
    if (s != "(") throw badArchive("expected open tag");

    enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;
    int fd = -1; /* !!! close on exception */

    while (1) {
        s = readString(source);

        if (s == ")") {
            break;
        }

        else if (s == "type") {
            if (type != tpUnknown)
                throw badArchive("multiple type fields");
            string t = readString(source);

            if (t == "regular") {
                type = tpRegular;
                fd = open(path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
                if (fd == -1)
                    throw SysError("creating file " + path);
            }

            else if (t == "directory") {
                type = tpDirectory;
                if (mkdir(path.c_str(), 0777) == -1)
                    throw SysError("creating directory " + path);
            }

            else if (t == "symlink") {
                type = tpSymlink;
            }
            
            else throw badArchive("unknown file type " + t);
            
        }

        else if (s == "contents" && type == tpRegular) {
            restoreContents(fd, path, source);
        }

        else if (s == "executable" && type == tpRegular) {
            readString(source);
            struct stat st;
            if (fstat(fd, &st) == -1)
                throw SysError("fstat");
            if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
                throw SysError("fchmod");
        }

        else if (s == "entry" && type == tpDirectory) {
            restoreEntry(path, source);
        }

        else if (s == "target" && type == tpSymlink) {
            string target = readString(source);
            if (symlink(target.c_str(), path.c_str()) == -1)
                throw SysError("creating symlink " + path);
        }

        else {
            throw badArchive("unknown field " + s);
            skipGeneric(source);
        }
        
    }

    if (fd != -1) close(fd);
}


void restorePath(const Path & path, RestoreSource & source)
{
    if (readString(source) != archiveVersion1)
        throw badArchive("expected Nix archive");
    restore(path, source);
}