WIP:afs-service-commit
[jackhill/guix/guix.git] / nix / libutil / archive.cc
CommitLineData
2bb04905
LC
1#define _XOPEN_SOURCE 600
2
36457566
LC
3#include "config.h"
4
5#include <cerrno>
6#include <algorithm>
7#include <vector>
2bb04905
LC
8#include <map>
9
10#include <strings.h> // for strcasecmp
36457566 11
36457566
LC
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <unistd.h>
15#include <dirent.h>
16#include <fcntl.h>
17
18#include "archive.hh"
19#include "util.hh"
20
21
22namespace nix {
23
36457566
LC
24static string archiveVersion1 = "nix-archive-1";
25
2bb04905 26static string caseHackSuffix = "~nix~case~hack~";
36457566
LC
27
28PathFilter defaultPathFilter;
29
30
2bb04905 31static void dumpContents(const Path & path, size_t size,
36457566
LC
32 Sink & sink)
33{
34 writeString("contents", sink);
35 writeLongLong(size, sink);
36
37 AutoCloseFD fd = open(path.c_str(), O_RDONLY);
38 if (fd == -1) throw SysError(format("opening file `%1%'") % path);
2bb04905 39
36457566
LC
40 unsigned char buf[65536];
41 size_t left = size;
42
43 while (left > 0) {
44 size_t n = left > sizeof(buf) ? sizeof(buf) : left;
45 readFull(fd, buf, n);
46 left -= n;
47 sink(buf, n);
48 }
49
50 writePadding(size, sink);
51}
52
53
54static void dump(const Path & path, Sink & sink, PathFilter & filter)
55{
56 struct stat st;
57 if (lstat(path.c_str(), &st))
58 throw SysError(format("getting attributes of path `%1%'") % path);
59
60 writeString("(", sink);
61
62 if (S_ISREG(st.st_mode)) {
63 writeString("type", sink);
64 writeString("regular", sink);
65 if (st.st_mode & S_IXUSR) {
66 writeString("executable", sink);
67 writeString("", sink);
68 }
69 dumpContents(path, (size_t) st.st_size, sink);
2bb04905 70 }
36457566
LC
71
72 else if (S_ISDIR(st.st_mode)) {
73 writeString("type", sink);
74 writeString("directory", sink);
2bb04905
LC
75
76 /* If we're on a case-insensitive system like Mac OS X, undo
77 the case hack applied by restorePath(). */
78 std::map<string, string> unhacked;
79 for (auto & i : readDirectory(path))
7eca7892 80 unhacked[i.name] = i.name;
2bb04905
LC
81
82 for (auto & i : unhacked)
83 if (filter(path + "/" + i.first)) {
84 writeString("entry", sink);
85 writeString("(", sink);
86 writeString("name", sink);
87 writeString(i.first, sink);
88 writeString("node", sink);
89 dump(path + "/" + i.second, sink, filter);
90 writeString(")", sink);
91 }
36457566
LC
92 }
93
94 else if (S_ISLNK(st.st_mode)) {
95 writeString("type", sink);
96 writeString("symlink", sink);
97 writeString("target", sink);
98 writeString(readLink(path), sink);
99 }
100
15ddeff5 101 else throw Error(format("file `%1%' has an unsupported type") % path);
36457566
LC
102
103 writeString(")", sink);
104}
105
106
107void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
108{
109 writeString(archiveVersion1, sink);
110 dump(path, sink, filter);
111}
112
113
114static SerialisationError badArchive(string s)
115{
116 return SerialisationError("bad archive: " + s);
117}
118
119
2bb04905 120#if 0
36457566
LC
121static void skipGeneric(Source & source)
122{
123 if (readString(source) == "(") {
124 while (readString(source) != ")")
125 skipGeneric(source);
126 }
127}
2bb04905 128#endif
36457566
LC
129
130
131static void parseContents(ParseSink & sink, Source & source, const Path & path)
132{
133 unsigned long long size = readLongLong(source);
2bb04905 134
36457566
LC
135 sink.preallocateContents(size);
136
137 unsigned long long left = size;
138 unsigned char buf[65536];
139
140 while (left) {
141 checkInterrupt();
142 unsigned int n = sizeof(buf);
143 if ((unsigned long long) n > left) n = left;
144 source(buf, n);
145 sink.receiveContents(buf, n);
146 left -= n;
147 }
148
149 readPadding(size, source);
150}
151
152
2bb04905
LC
153struct CaseInsensitiveCompare
154{
155 bool operator() (const string & a, const string & b) const
156 {
157 return strcasecmp(a.c_str(), b.c_str()) < 0;
158 }
159};
160
161
36457566
LC
162static void parse(ParseSink & sink, Source & source, const Path & path)
163{
164 string s;
165
166 s = readString(source);
167 if (s != "(") throw badArchive("expected open tag");
168
169 enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;
170
2bb04905
LC
171 std::map<Path, int, CaseInsensitiveCompare> names;
172
36457566
LC
173 while (1) {
174 checkInterrupt();
175
176 s = readString(source);
177
178 if (s == ")") {
179 break;
180 }
181
182 else if (s == "type") {
183 if (type != tpUnknown)
184 throw badArchive("multiple type fields");
185 string t = readString(source);
186
187 if (t == "regular") {
188 type = tpRegular;
189 sink.createRegularFile(path);
190 }
191
192 else if (t == "directory") {
193 sink.createDirectory(path);
194 type = tpDirectory;
195 }
196
197 else if (t == "symlink") {
198 type = tpSymlink;
199 }
2bb04905 200
36457566 201 else throw badArchive("unknown file type " + t);
2bb04905 202
36457566
LC
203 }
204
205 else if (s == "contents" && type == tpRegular) {
206 parseContents(sink, source, path);
207 }
208
209 else if (s == "executable" && type == tpRegular) {
210 readString(source);
211 sink.isExecutable();
212 }
213
214 else if (s == "entry" && type == tpDirectory) {
2bb04905
LC
215 string name, prevName;
216
217 s = readString(source);
218 if (s != "(") throw badArchive("expected open tag");
219
220 while (1) {
221 checkInterrupt();
222
223 s = readString(source);
224
225 if (s == ")") {
226 break;
227 } else if (s == "name") {
228 name = readString(source);
229 if (name.empty() || name == "." || name == ".." || name.find('/') != string::npos || name.find((char) 0) != string::npos)
230 throw Error(format("NAR contains invalid file name `%1%'") % name);
231 if (name <= prevName)
232 throw Error("NAR directory is not sorted");
233 prevName = name;
2bb04905
LC
234 } else if (s == "node") {
235 if (s.empty()) throw badArchive("entry name missing");
236 parse(sink, source, path + "/" + name);
237 } else
238 throw badArchive("unknown field " + s);
239 }
36457566
LC
240 }
241
242 else if (s == "target" && type == tpSymlink) {
243 string target = readString(source);
244 sink.createSymlink(path, target);
245 }
246
2bb04905 247 else
36457566 248 throw badArchive("unknown field " + s);
36457566
LC
249 }
250}
251
252
253void parseDump(ParseSink & sink, Source & source)
254{
2bb04905 255 string version;
36457566
LC
256 try {
257 version = readString(source);
258 } catch (SerialisationError & e) {
259 /* This generally means the integer at the start couldn't be
260 decoded. Ignore and throw the exception below. */
261 }
262 if (version != archiveVersion1)
8327e733 263 throw badArchive("input doesn't look like a normalized archive");
36457566
LC
264 parse(sink, source, "");
265}
266
267
268struct RestoreSink : ParseSink
269{
270 Path dstPath;
271 AutoCloseFD fd;
272
273 void createDirectory(const Path & path)
274 {
275 Path p = dstPath + path;
276 if (mkdir(p.c_str(), 0777) == -1)
277 throw SysError(format("creating directory `%1%'") % p);
278 };
279
280 void createRegularFile(const Path & path)
281 {
282 Path p = dstPath + path;
283 fd.close();
284 fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
285 if (fd == -1) throw SysError(format("creating file `%1%'") % p);
286 }
287
288 void isExecutable()
289 {
290 struct stat st;
291 if (fstat(fd, &st) == -1)
292 throw SysError("fstat");
293 if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
294 throw SysError("fchmod");
295 }
296
297 void preallocateContents(unsigned long long len)
298 {
299#if HAVE_POSIX_FALLOCATE
300 if (len) {
301 errno = posix_fallocate(fd, 0, len);
302 /* Note that EINVAL may indicate that the underlying
303 filesystem doesn't support preallocation (e.g. on
304 OpenSolaris). Since preallocation is just an
305 optimisation, ignore it. */
306 if (errno && errno != EINVAL)
307 throw SysError(format("preallocating file of %1% bytes") % len);
308 }
309#endif
310 }
311
312 void receiveContents(unsigned char * data, unsigned int len)
313 {
314 writeFull(fd, data, len);
315 }
316
317 void createSymlink(const Path & path, const string & target)
318 {
319 Path p = dstPath + path;
320 nix::createSymlink(target, p);
321 }
322};
323
2bb04905 324
36457566
LC
325void restorePath(const Path & path, Source & source)
326{
327 RestoreSink sink;
328 sink.dstPath = path;
329 parseDump(sink, source);
330}
331
2bb04905 332
36457566 333}