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