WIP:afs-service-commit
[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 static string archiveVersion1 = "nix-archive-1";
25
26 static string caseHackSuffix = "~nix~case~hack~";
27
28 PathFilter defaultPathFilter;
29
30
31 static void dumpContents(const Path & path, size_t size,
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);
39
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
54 static 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);
70 }
71
72 else if (S_ISDIR(st.st_mode)) {
73 writeString("type", sink);
74 writeString("directory", sink);
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))
80 unhacked[i.name] = i.name;
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 }
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
101 else throw Error(format("file `%1%' has an unsupported type") % path);
102
103 writeString(")", sink);
104 }
105
106
107 void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
108 {
109 writeString(archiveVersion1, sink);
110 dump(path, sink, filter);
111 }
112
113
114 static SerialisationError badArchive(string s)
115 {
116 return SerialisationError("bad archive: " + s);
117 }
118
119
120 #if 0
121 static void skipGeneric(Source & source)
122 {
123 if (readString(source) == "(") {
124 while (readString(source) != ")")
125 skipGeneric(source);
126 }
127 }
128 #endif
129
130
131 static void parseContents(ParseSink & sink, Source & source, const Path & path)
132 {
133 unsigned long long size = readLongLong(source);
134
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
153 struct 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
162 static 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
171 std::map<Path, int, CaseInsensitiveCompare> names;
172
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 }
200
201 else throw badArchive("unknown file type " + t);
202
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) {
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;
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 }
240 }
241
242 else if (s == "target" && type == tpSymlink) {
243 string target = readString(source);
244 sink.createSymlink(path, target);
245 }
246
247 else
248 throw badArchive("unknown field " + s);
249 }
250 }
251
252
253 void parseDump(ParseSink & sink, Source & source)
254 {
255 string version;
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)
263 throw badArchive("input doesn't look like a normalized archive");
264 parse(sink, source, "");
265 }
266
267
268 struct 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
324
325 void restorePath(const Path & path, Source & source)
326 {
327 RestoreSink sink;
328 sink.dstPath = path;
329 parseDump(sink, source);
330 }
331
332
333 }