aboutsummaryrefslogtreecommitdiff
path: root/nix/libutil/archive.cc
blob: 54bcd21f936118ced3317663a8642557383d68b3 (about) (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
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
#define _XOPEN_SOURCE 600

#include "config.h"

#include <cerrno>
#include <algorithm>
#include <vector>
#include <map>

#include <strings.h> // for strcasecmp

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

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


namespace nix {

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

static string caseHackSuffix = "~nix~case~hack~";

PathFilter defaultPathFilter;


static void dumpContents(const Path & path, size_t size,
    Sink & sink)
{
    writeString("contents", sink);
    writeLongLong(size, sink);

    AutoCloseFD fd = open(path.c_str(), O_RDONLY);
    if (fd == -1) throw SysError(format("opening file `%1%'") % path);

    unsigned char buf[65536];
    size_t left = size;

    while (left > 0) {
        size_t n = left > sizeof(buf) ? sizeof(buf) : left;
        readFull(fd, buf, n);
        left -= n;
        sink(buf, n);
    }

    writePadding(size, sink);
}


static void dump(const Path & path, Sink & sink, PathFilter & filter)
{
    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, (size_t) st.st_size, sink);
    }

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

        /* If we're on a case-insensitive system like Mac OS X, undo
           the case hack applied by restorePath(). */
        std::map<string, string> unhacked;
        for (auto & i : readDirectory(path))
	    unhacked[i.name] = i.name;

        for (auto & i : unhacked)
            if (filter(path + "/" + i.first)) {
                writeString("entry", sink);
                writeString("(", sink);
                writeString("name", sink);
                writeString(i.first, sink);
                writeString("node", sink);
                dump(path + "/" + i.second, sink, filter);
                writeString(")", sink);
            }
    }

    else if (S_ISLNK(st.st_mode)) {
        writeString("type", sink);
        writeString("symlink", sink);
        writeString("target", sink);
        writeString(readLink(path), sink);
    }

    else throw Error(format("file `%1%' has an unsupported type") % path);

    writeString(")", sink);
}


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


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


#if 0
static void skipGeneric(Source & source)
{
    if (readString(source) == "(") {
        while (readString(source) != ")")
            skipGeneric(source);
    }
}
#endif


static void parseContents(ParseSink & sink, Source & source, const Path & path)
{
    unsigned long long size = readLongLong(source);

    sink.preallocateContents(size);

    unsigned long long left = size;
    unsigned char buf[65536];

    while (left) {
        checkInterrupt();
        unsigned int n = sizeof(buf);
        if ((unsigned long long) n > left) n = left;
        source(buf, n);
        sink.receiveContents(buf, n);
        left -= n;
    }

    readPadding(size, source);
}


struct CaseInsensitiveCompare
{
    bool operator() (const string & a, const string & b) const
    {
        return strcasecmp(a.c_str(), b.c_str()) < 0;
    }
};


static void parse(ParseSink & sink, Source & source, const Path & path)
{
    string s;

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

    enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;

    std::map<Path, int, CaseInsensitiveCompare> names;

    while (1) {
        checkInterrupt();

        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;
                sink.createRegularFile(path);
            }

            else if (t == "directory") {
                sink.createDirectory(path);
                type = tpDirectory;
            }

            else if (t == "symlink") {
                type = tpSymlink;
            }

            else throw badArchive("unknown file type " + t);

        }

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

        else if (s == "executable" && type == tpRegular) {
            readString(source);
            sink.isExecutable();
        }

        else if (s == "entry" && type == tpDirectory) {
            string name, prevName;

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

            while (1) {
                checkInterrupt();

                s = readString(source);

                if (s == ")") {
                    break;
                } else if (s == "name") {
                    name = readString(source);
                    if (name.empty() || name == "." || name == ".." || name.find('/') != string::npos || name.find((char) 0) != string::npos)
                        throw Error(format("NAR contains invalid file name `%1%'") % name);
                    if (name <= prevName)
                        throw Error("NAR directory is not sorted");
                    prevName = name;
                } else if (s == "node") {
                    if (s.empty()) throw badArchive("entry name missing");
                    parse(sink, source, path + "/" + name);
                } else
                    throw badArchive("unknown field " + s);
            }
        }

        else if (s == "target" && type == tpSymlink) {
            string target = readString(source);
            sink.createSymlink(path, target);
        }

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


void parseDump(ParseSink & sink, Source & source)
{
    string version;
    try {
        version = readString(source);
    } catch (SerialisationError & e) {
        /* This generally means the integer at the start couldn't be
           decoded.  Ignore and throw the exception below. */
    }
    if (version != archiveVersion1)
        throw badArchive("input doesn't look like a normalized archive");
    parse(sink, source, "");
}


struct RestoreSink : ParseSink
{
    Path dstPath;
    AutoCloseFD fd;

    void createDirectory(const Path & path)
    {
        Path p = dstPath + path;
        if (mkdir(p.c_str(), 0777) == -1)
            throw SysError(format("creating directory `%1%'") % p);
    };

    void createRegularFile(const Path & path)
    {
        Path p = dstPath + path;
        fd.close();
        fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
        if (fd == -1) throw SysError(format("creating file `%1%'") % p);
    }

    void isExecutable()
    {
        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");
    }

    void preallocateContents(unsigned long long len)
    {
#if HAVE_POSIX_FALLOCATE
        if (len) {
            errno = posix_fallocate(fd, 0, len);
            /* Note that EINVAL may indicate that the underlying
               filesystem doesn't support preallocation (e.g. on
               OpenSolaris).  Since preallocation is just an
               optimisation, ignore it. */
            if (errno && errno != EINVAL)
                throw SysError(format("preallocating file of %1% bytes") % len);
        }
#endif
    }

    void receiveContents(unsigned char * data, unsigned int len)
    {
        writeFull(fd, data, len);
    }

    void createSymlink(const Path & path, const string & target)
    {
        Path p = dstPath + path;
        nix::createSymlink(target, p);
    }
};


void restorePath(const Path & path, Source & source)
{
    RestoreSink sink;
    sink.dstPath = path;
    parseDump(sink, source);
}


}
the non-monadic procedures and turn into a regular procedure. (skeleton-directory): Likewise. * gnu/system.scm (etc-directory): Adjust accordingly. 2015-10-10system: pam: Use 'computed-file' instead of 'gexp->derivation'.Ludovic Courtès * gnu/system/linux.scm (pam-service->configuration): Use 'computed-file' instead of 'gexp->derivation'. (pam-services->directory): Likewise. * gnu/system.scm (etc-directory): Adjust accordingly. 2015-10-10system: Make service procedures non-monadic.Ludovic Courtès * gnu/services/avahi.scm (configuration-file): Use 'plain-file' instead of 'text-file'. (avahi-service): Turn into a regular procedure that returns a <service>. * gnu/services/base.scm (root-file-system-service, file-system-service, user-unmount-service, user-processes-service, host-name-service, console-keymap-service, console-font-service, mingetty-service, nscd.conf-file, nscd-service): Likewise. (%default-syslog.conf): New variable. (syslog-service): Use it. Turn into a regular procedure. (guix-service, udev-rules-union, kvm-udev-rule, udev-service, device-mapping-service, swap-service): Likewise. * gnu/services/databases.scm (%default-postgres-hba, %default-postgres-ident): Use 'plain-file' instead of 'text-file'. (%default-postgres-config): Use 'mixed-text-file' instead of 'text-file*'. (postgresql-service): Use 'program-file' instead of 'gexp->script'. Turn into a regular procedure. * gnu/services/desktop.scm (dbus-configuration-directory): Use 'computed-file' instead of 'gexp->derivation'. (upower-configuration-file, geoclue-configuration-file, elogind-configuration-file): Use 'plain-file' instead of 'text-file'. (dbus-service, upower-service, colord-service, geoclue-service, polkit-service, elogind-service): Turn into regular procedures. (%desktop-services): Remove use of 'mlet' when iterating on %BASE-SERVICES. * gnu/services/lirc.scm (lirc-service): Turn into a regular procedure. * gnu/services/networking.scm (static-networking-service, dhcp-client-service, ntp-service, tor-service, bitlbee-service, wicd-service): Likewise. * gnu/services/ssh.scm (lsh-service): Likewise. * gnu/services/web.scm (nginx-service): Likewise. * gnu/services/xorg.scm (xorg-configuration-file): Use 'mixed-text-file' instead of 'text-file*'. (xorg-start-command, slim-service): Turn into regular procedures. (xinitrc): Use 'program-file' instead of 'gexp->script'. * gnu/system/install.scm (cow-store-service, configuration-template-service): Turn into regular procedures. * gnu/system.scm (other-file-system-services, device-mapping-services, swap-services, essential-services, operating-system-services, user-shells, operating-system-accounts): Remove now unnecessary 'mlet' and turn into regular procedures. (operating-system-etc-directory, operating-system-activation-script, operating-system-boot-script): Adjust accordingly. * doc/guix.texi (Base Services, Networking Services, X Window, Desktop Services, Database Services, Web Services, Various Services, Name Service Switch): Adjust accordingly. 2015-09-14gnu: system: Add Linux container module.David Thompson * gnu/system/linux-container.scm: New file. * gnu-system.am (GNU_SYSTEM_MODULES): Add it. * gnu/system.scm: Export 'operating-system-etc-directory', 'operating-system-boot-script', 'operating-system-locale-directory', and 'file-union'. (operating-system-boot-script): Add #:container? keyword argument. (operating-system-activation-script): Add #:container? keyword argument. Don't call 'activate-firmware' or 'activate-ptrace-attach' when activating a container. 2015-08-18Revert "PRELIMINARY: Add three programs to %setuid-programs."Mark H Weaver This reverts commit fb1e06fc5f7648ab3078876f009fa7a983b17c41. 2015-08-18PRELIMINARY: Add three programs to %setuid-programs.Mark H Weaver 2015-08-04system: Default to newest linux-libre.Mark H Weaver * gnu/system.scm (<operating-system>)[kernel]: Change default to LINUX-LIBRE. 2015-07-26system: Add 'ping6' to %SETUID-PROGRAMS.Ludovic Courtès * gnu/system.scm (%setuid-programs): Add 'ping6'. 2015-07-20system: Default to Linux-libre 4.0.Ludovic Courtès * gnu/system.scm (<operating-system>)[kernel]: Change default to LINUX-LIBRE-4.0. 2015-07-17file-systems: Add a 'dependencies' field to <file-system>.Ludovic Courtès * gnu/system/file-systems.scm (<file-system>)[dependencies]: New field. * gnu/system.scm (other-file-system-services)[requirements]: Honor 'file-system-dependencies'. * doc/guix.texi (File Systems): Document it. 2015-07-17system: Add 'kernel-arguments' field.Ludovic Courtès * gnu/system.scm (<operating-system>)[kernel-arguments]: New field. (operating-system-grub.cfg): Honor it. (operating-system-parameters-file): Add 'kernel-arguments' to the parameters file. * guix/scripts/system.scm (previous-grub-entries)[system->grub-entry]: Read the 'kernel-arguments' field of the parameters file, when available. * gnu/system/vm.scm (system-qemu-image/shared-store-script): Use (operating-system-kernel-arguments os) in '-append'. * doc/guix.texi (operating-system Reference): Document it. 2015-07-09build: file-systems: Import (guix build syscalls) for non-static Guiles.David Thompson * gnu/build/file-systems.scm: Import (guix build syscalls) when 'mount' is not defined. * gnu/system.scm (operating-system-activation-script): Include (guix build syscalls) module in derivation. 2015-07-07system: Fix typo.Alex Kost * gnu/system.scm (etc-directory): Fix typo in a comment. 2015-06-23system: emacs-site-file: Use 'geiser-install to load geiser.宋文武 Suggested by Alex Kost <alezost@gmail.com>. * gnu/system.scm (emacs-site-file): Use 'geiser-install to load geiser instead of setting 'geiser-guile-load-path' manually. 2015-06-16system: Rename 'sudoers' into 'sudoers-file'.Alex Kost * gnu/system.scm (<operating-system>): Rename record field. (etc-directory): Rename argument. (operating-system-etc-directory): Adjust accordingly. * doc/guix.texi (operating-system Reference): Likewise. 2015-06-05system: 'hosts-file' is now a file-like object.Ludovic Courtès Partly fixes <http://bugs.gnu.org/20720>. Reported by Alex Kost <alezost@gmail.com>. * gnu/system.scm (default-/etc/hosts): Change 'text-file' to 'plain-file'. (maybe-file->monadic): New procedure. (operating-system-etc-directory): Use it. * doc/guix.texi (operating-system Reference, Networking Services): Adjust accordingly. 2015-06-05system: 'sudoers' is now a file-like object.Ludovic Courtès Partly fixes <http://bugs.gnu.org/20720> Reported by Alex Kost <alezost@gmail.com>. * gnu/system.scm (etc-directory): Change default #:sudoers value to a 'plain-file'. Don't bind it. Remove #~#$. (maybe-string->file): New procedure. (operating-system-etc-directory): Use it. (%sudoers-specification): Use 'plain-file'. * doc/guix.texi (operating-system Reference): Adjust accordingly. 2015-05-25system: Define '%base-user-accounts'.Ludovic Courtès * gnu/system/shadow.scm (%base-user-accounts): New variable. * gnu/system.scm (<operating-system>)[users]: Use it as the default value. * gnu/system/examples/bare-bones.tmpl (users): Use it. * gnu/system/examples/desktop.tmpl (users): Likewise. * doc/guix.texi (operating-system Reference, User Accounts): Adjust accordingly. 2015-05-24system: Make sure user accounts refer to existing groups.Ludovic Courtès Fixes <http://bugs.gnu.org/20646>. Reported by David Thompson <davet@gnu.org>. * gnu/system/shadow.scm (assert-valid-users/groups): New procedure * gnu/system.scm (operating-system-activation-script): Use it. * tests/guix-system.sh (make_user_config): New function. Add 3 tests using it. * po/guix/POTFILES.in: Add gnu/system/shadow.scm. 2015-05-07system: Use "." instead of "source" in /etc/profile.Ludovic Courtès * gnu/system.scm (etc-directory)[profile]: Use "." instead of "source", the latter being Bash-specific. 2015-05-07system: Check whether ~/.guix-profile/etc/profile exists.Ludovic Courtès * gnu/system.scm (etc-directory)[profile]: Check for ~/.guix-profile/etc/profile rather than just ~/.guix-profile. 2015-05-06system: /etc/profile sources each profile's /etc/profile.Ludovic Courtès Partly fixes <http://bugs.gnu.org/20255>. Reported by 宋文武 <iyzsong@gmail.com>. * gnu/system.scm (etc-directory)[profile]: Source /run/current-system/profile/etc/profile and $HOME/.guix-profile/etc/profile when available. Move definitions of SSL_CERT_DIR, SSL_CERT_FILE, and GIT_SSL_CAINFO before that. 2015-04-17system: Populate /etc/shells with the list of all the shells in use.Ludovic Courtès Reported by Andy Wingo <wingo@pobox.com>. * gnu/system.scm (user-shells, shells-file): New procedures. (etc-directory): Add #:shell parameter. Use 'shells-file' instead of 'text-file'. (operating-system-etc-directory): Call 'user-shells' and pass #:shells to 'etc-directory'. 2015-04-12system: Allow users to PTRACE_ATTACH to their own processes.Ludovic Courtès * gnu/build/activation.scm (activate-ptrace-attach): New procedure. * gnu/system.scm (operating-system-activation-script): Use it. 2015-04-10system: Clean /tmp and /var/run during early boot.Mark H Weaver * gnu/system.scm (operating-system-boot-script): Clean out /tmp and /var/run before activating the system. 2015-04-05system: Take kernel modules from the user-specified kernel.Andy Wingo * gnu/system/linux-initrd.scm (base-initrd): Add #:linux option to specify the linux kernel to use. * gnu/system/vm.scm (expression->derivation-in-linux-vm): Propagate #:linux to base-initrd. * gnu/system.scm (operating-system-initrd-file): Pass #:linux to 'make-initrd'. Co-authored-by: Ludovic Courtès <ludo@gnu.org>