Commit | Line | Data |
---|---|---|
0f41c26f | 1 | ;;; GNU Guix --- Functional package management for GNU |
cd4027fa | 2 | ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org> |
0f41c26f LC |
3 | ;;; |
4 | ;;; This file is part of GNU Guix. | |
5 | ;;; | |
6 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
7 | ;;; under the terms of the GNU General Public License as published by | |
8 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
9 | ;;; your option) any later version. | |
10 | ;;; | |
11 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
12 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;;; GNU General Public License for more details. | |
15 | ;;; | |
16 | ;;; You should have received a copy of the GNU General Public License | |
17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
18 | ||
19 | (define-module (guix nar) | |
20 | #:use-module (guix utils) | |
21 | #:use-module (guix serialization) | |
cd4027fa LC |
22 | #:use-module ((guix build utils) |
23 | #:select (delete-file-recursively with-directory-excursion)) | |
24 | #:use-module (guix store) | |
25 | #:use-module (guix ui) ; for '_' | |
26 | #:use-module (guix hash) | |
27 | #:use-module (guix pki) | |
28 | #:use-module (guix pk-crypto) | |
0f41c26f LC |
29 | #:use-module (rnrs bytevectors) |
30 | #:use-module (rnrs io ports) | |
31 | #:use-module (srfi srfi-1) | |
cd4027fa | 32 | #:use-module (srfi srfi-11) |
0f41c26f | 33 | #:use-module (srfi srfi-26) |
53c63ee9 LC |
34 | #:use-module (srfi srfi-34) |
35 | #:use-module (srfi srfi-35) | |
0f41c26f | 36 | #:use-module (ice-9 ftw) |
53c63ee9 LC |
37 | #:use-module (ice-9 match) |
38 | #:export (nar-error? | |
cd4027fa LC |
39 | nar-error-port |
40 | nar-error-file | |
41 | ||
53c63ee9 | 42 | nar-read-error? |
53c63ee9 LC |
43 | nar-read-error-token |
44 | ||
cd4027fa LC |
45 | nar-invalid-hash-error? |
46 | nar-invalid-hash-error-expected | |
47 | nar-invalid-hash-error-actual | |
48 | ||
49 | nar-signature-error? | |
50 | nar-signature-error-signature | |
51 | ||
53c63ee9 | 52 | write-file |
cd4027fa LC |
53 | restore-file |
54 | ||
55 | restore-file-set)) | |
0f41c26f LC |
56 | |
57 | ;;; Comment: | |
58 | ;;; | |
59 | ;;; Read and write Nix archives, aka. ‘nar’. | |
60 | ;;; | |
61 | ;;; Code: | |
62 | ||
53c63ee9 | 63 | (define-condition-type &nar-error &error ; XXX: inherit from &nix-error ? |
cd4027fa LC |
64 | nar-error? |
65 | (file nar-error-file) ; file we were restoring, or #f | |
66 | (port nar-error-port)) ; port from which we read | |
53c63ee9 LC |
67 | |
68 | (define-condition-type &nar-read-error &nar-error | |
69 | nar-read-error? | |
53c63ee9 LC |
70 | (token nar-read-error-token)) ; faulty token, or #f |
71 | ||
cd4027fa LC |
72 | (define-condition-type &nar-signature-error &nar-error |
73 | nar-signature-error? | |
74 | (signature nar-signature-error-signature)) ; faulty signature or #f | |
53c63ee9 | 75 | |
cd4027fa LC |
76 | (define-condition-type &nar-invalid-hash-error &nar-signature-error |
77 | nar-invalid-hash-error? | |
78 | (expected nar-invalid-hash-error-expected) ; expected hash (a bytevector) | |
79 | (actual nar-invalid-hash-error-actual)) ; actual hash | |
80 | ||
81 | \f | |
53c63ee9 LC |
82 | (define (dump in out size) |
83 | "Copy SIZE bytes from IN to OUT." | |
84 | (define buf-size 65536) | |
85 | (define buf (make-bytevector buf-size)) | |
86 | ||
87 | (let loop ((left size)) | |
88 | (if (<= left 0) | |
89 | 0 | |
90 | (let ((read (get-bytevector-n! in buf 0 (min left buf-size)))) | |
91 | (if (eof-object? read) | |
92 | left | |
93 | (begin | |
94 | (put-bytevector out buf 0 read) | |
95 | (loop (- left read)))))))) | |
96 | ||
0f41c26f LC |
97 | (define (write-contents file p size) |
98 | "Write SIZE bytes from FILE to output port P." | |
99 | (define (call-with-binary-input-file file proc) | |
100 | ;; Open FILE as a binary file. This avoids scan-for-encoding, and thus | |
101 | ;; avoids any initial buffering. Disable file name canonicalization to | |
102 | ;; avoid stat'ing like crazy. | |
103 | (with-fluids ((%file-port-name-canonicalization #f)) | |
104 | (let ((port (open-file file "rb"))) | |
48e488eb LC |
105 | (dynamic-wind |
106 | (const #t) | |
107 | (cut proc port) | |
108 | (lambda () | |
109 | (close-port port)))))) | |
0f41c26f | 110 | |
0f41c26f LC |
111 | (write-string "contents" p) |
112 | (write-long-long size p) | |
113 | (call-with-binary-input-file file | |
114 | ;; Use `sendfile' when available (Guile 2.0.8+). | |
a93e91ff LC |
115 | (if (and (compile-time-value (defined? 'sendfile)) |
116 | (file-port? p)) | |
0f41c26f | 117 | (cut sendfile p <> size 0) |
53c63ee9 | 118 | (cut dump <> p size))) |
0f41c26f LC |
119 | (write-padding size p)) |
120 | ||
53c63ee9 LC |
121 | (define (read-contents in out) |
122 | "Read the contents of a file from the Nar at IN, write it to OUT, and return | |
123 | the size in bytes." | |
124 | (define executable? | |
125 | (match (read-string in) | |
126 | ("contents" | |
127 | #f) | |
128 | ("executable" | |
129 | (match (list (read-string in) (read-string in)) | |
130 | (("" "contents") #t) | |
131 | (x (raise | |
132 | (condition (&message | |
133 | (message "unexpected executable file marker")) | |
134 | (&nar-read-error (port in) | |
135 | (file #f) | |
136 | (token x)))))) | |
137 | #t) | |
138 | (x | |
139 | (raise | |
140 | (condition (&message (message "unsupported nar file type")) | |
141 | (&nar-read-error (port in) (file #f) (token x))))))) | |
142 | ||
143 | (let ((size (read-long-long in))) | |
144 | ;; Note: `sendfile' cannot be used here because of port buffering on IN. | |
145 | (dump in out size) | |
146 | ||
147 | (when executable? | |
148 | (chmod out #o755)) | |
149 | (let ((m (modulo size 8))) | |
150 | (unless (zero? m) | |
151 | (get-bytevector-n in (- 8 m)))) | |
152 | size)) | |
153 | ||
154 | (define %archive-version-1 | |
155 | ;; Magic cookie for Nix archives. | |
156 | "nix-archive-1") | |
157 | ||
0f41c26f LC |
158 | (define (write-file file port) |
159 | "Write the contents of FILE to PORT in Nar format, recursing into | |
160 | sub-directories of FILE as needed." | |
0f41c26f LC |
161 | (define p port) |
162 | ||
163 | (write-string %archive-version-1 p) | |
164 | ||
165 | (let dump ((f file)) | |
166 | (let ((s (lstat f))) | |
167 | (write-string "(" p) | |
168 | (case (stat:type s) | |
169 | ((regular) | |
170 | (write-string "type" p) | |
171 | (write-string "regular" p) | |
172 | (if (not (zero? (logand (stat:mode s) #o100))) | |
173 | (begin | |
174 | (write-string "executable" p) | |
175 | (write-string "" p))) | |
176 | (write-contents f p (stat:size s))) | |
177 | ((directory) | |
178 | (write-string "type" p) | |
179 | (write-string "directory" p) | |
96c7448f LC |
180 | (let* ((select? (negate (cut member <> '("." "..")))) |
181 | ||
182 | ;; 'scandir' defaults to 'string-locale<?' to sort files, but | |
183 | ;; this happens to be case-insensitive (at least in 'en_US' | |
184 | ;; locale on libc 2.18.) Conversely, we want files to be | |
185 | ;; sorted in a case-sensitive fashion. | |
186 | (entries (scandir f select? string<?))) | |
0f41c26f LC |
187 | (for-each (lambda (e) |
188 | (let ((f (string-append f "/" e))) | |
189 | (write-string "entry" p) | |
190 | (write-string "(" p) | |
191 | (write-string "name" p) | |
192 | (write-string e p) | |
193 | (write-string "node" p) | |
194 | (dump f) | |
195 | (write-string ")" p))) | |
196 | entries))) | |
8f3114b7 LC |
197 | ((symlink) |
198 | (write-string "type" p) | |
199 | (write-string "symlink" p) | |
200 | (write-string "target" p) | |
201 | (write-string (readlink f) p)) | |
0f41c26f | 202 | (else |
3140f2df LC |
203 | (raise (condition (&message (message "unsupported file type")) |
204 | (&nar-error (file f) (port port)))))) | |
0f41c26f LC |
205 | (write-string ")" p)))) |
206 | ||
53c63ee9 LC |
207 | (define (restore-file port file) |
208 | "Read a file (possibly a directory structure) in Nar format from PORT. | |
209 | Restore it as FILE." | |
210 | (let ((signature (read-string port))) | |
211 | (unless (equal? signature %archive-version-1) | |
212 | (raise | |
213 | (condition (&message (message "invalid nar signature")) | |
214 | (&nar-read-error (port port) | |
215 | (token signature) | |
216 | (file #f)))))) | |
217 | ||
218 | (let restore ((file file)) | |
8f3114b7 LC |
219 | (define (read-eof-marker) |
220 | (match (read-string port) | |
221 | (")" #t) | |
222 | (x (raise | |
223 | (condition | |
224 | (&message (message "invalid nar end-of-file marker")) | |
225 | (&nar-read-error (port port) (file file) (token x))))))) | |
226 | ||
53c63ee9 LC |
227 | (match (list (read-string port) (read-string port) (read-string port)) |
228 | (("(" "type" "regular") | |
229 | (call-with-output-file file (cut read-contents port <>)) | |
8f3114b7 LC |
230 | (read-eof-marker)) |
231 | (("(" "type" "symlink") | |
232 | (match (list (read-string port) (read-string port)) | |
233 | (("target" target) | |
234 | (symlink target file) | |
235 | (read-eof-marker)) | |
53c63ee9 LC |
236 | (x (raise |
237 | (condition | |
8f3114b7 | 238 | (&message (message "invalid symlink tokens")) |
53c63ee9 LC |
239 | (&nar-read-error (port port) (file file) (token x))))))) |
240 | (("(" "type" "directory") | |
241 | (let ((dir file)) | |
242 | (mkdir dir) | |
243 | (let loop ((prefix (read-string port))) | |
244 | (match prefix | |
245 | ("entry" | |
246 | (match (list (read-string port) | |
247 | (read-string port) (read-string port) | |
248 | (read-string port)) | |
249 | (("(" "name" file "node") | |
250 | (restore (string-append dir "/" file)) | |
251 | (match (read-string port) | |
252 | (")" #t) | |
253 | (x | |
254 | (raise | |
255 | (condition | |
256 | (&message | |
257 | (message "unexpected directory entry termination")) | |
258 | (&nar-read-error (port port) | |
259 | (file file) | |
260 | (token x)))))) | |
261 | (loop (read-string port))))) | |
262 | (")" #t) ; done with DIR | |
263 | (x | |
264 | (raise | |
265 | (condition | |
266 | (&message (message "unexpected directory inter-entry marker")) | |
267 | (&nar-read-error (port port) (file file) (token x))))))))) | |
268 | (x | |
269 | (raise | |
270 | (condition | |
271 | (&message (message "unsupported nar entry type")) | |
272 | (&nar-read-error (port port) (file file) (token x)))))))) | |
273 | ||
cd4027fa LC |
274 | \f |
275 | ;;; | |
276 | ;;; Restoring a file set into the store. | |
277 | ;;; | |
278 | ||
279 | ;; The code below accesses the store directly and is meant to be run from | |
280 | ;; "build hooks", which cannot invoke the daemon's 'import-paths' RPC since | |
281 | ;; (1) the locks on the files to be restored as already held, and (2) the | |
282 | ;; $NIX_HELD_LOCKS hackish environment variable cannot be set. | |
283 | ;; | |
284 | ;; So we're really duplicating that functionality of the daemon (well, until | |
285 | ;; most of the daemon is in Scheme :-)). But note that we do use a couple of | |
286 | ;; RPCs for functionality not available otherwise, like 'valid-path?'. | |
287 | ||
288 | (define (lock-store-file file) | |
289 | "Acquire exclusive access to FILE, a store file." | |
290 | (call-with-output-file (string-append file ".lock") | |
291 | (cut fcntl-flock <> 'write-lock))) | |
292 | ||
293 | (define (unlock-store-file file) | |
294 | "Release access to FILE." | |
295 | (call-with-input-file (string-append file ".lock") | |
296 | (cut fcntl-flock <> 'unlock))) | |
297 | ||
298 | (define* (finalize-store-file source target | |
299 | #:key (references '()) deriver (lock? #t)) | |
300 | "Rename SOURCE to TARGET and register TARGET as a valid store item, with | |
301 | REFERENCES and DERIVER. When LOCK? is true, acquire exclusive locks on TARGET | |
302 | before attempting to register it; otherwise, assume TARGET's locks are already | |
303 | held." | |
304 | ||
305 | ;; XXX: Currently we have to call out to the daemon to check whether TARGET | |
306 | ;; is valid. | |
307 | (with-store store | |
308 | (unless (valid-path? store target) | |
309 | (when lock? | |
310 | (lock-store-file target)) | |
311 | ||
312 | (unless (valid-path? store target) | |
313 | ;; If FILE already exists, delete it (it's invalid anyway.) | |
314 | (when (file-exists? target) | |
315 | (delete-file-recursively target)) | |
316 | ||
317 | ;; Install the new TARGET. | |
318 | (rename-file source target) | |
319 | ||
320 | ;; Register TARGET. As a side effect, it resets the timestamps of all | |
321 | ;; its files, recursively. However, it doesn't attempt to deduplicate | |
322 | ;; its files like 'importPaths' does (FIXME). | |
323 | (register-path target | |
324 | #:references references | |
325 | #:deriver deriver)) | |
326 | ||
327 | (when lock? | |
328 | (unlock-store-file target))))) | |
329 | ||
330 | (define (temporary-store-directory) | |
331 | "Return the file name of a temporary directory created in the store that is | |
332 | protected from garbage collection." | |
333 | (let* ((template (string-append (%store-prefix) "/guix-XXXXXX")) | |
334 | (port (mkstemp! template))) | |
335 | (close-port port) | |
336 | (with-store store | |
337 | (add-temp-root store template)) | |
338 | ||
339 | ;; There's a small window during which the GC could delete the file. Try | |
340 | ;; again if that happens. | |
341 | (if (file-exists? template) | |
342 | (begin | |
343 | ;; It's up to the caller to create that file or directory. | |
344 | (delete-file template) | |
345 | template) | |
346 | (temporary-store-directory)))) | |
347 | ||
348 | (define* (restore-file-set port | |
349 | #:key (verify-signature? #t) (lock? #t) | |
350 | (log-port (current-error-port))) | |
351 | "Restore the file set read from PORT to the store. The format of the data | |
352 | on PORT must be as created by 'export-paths'---i.e., a series of Nar-formatted | |
353 | archives with interspersed meta-data joining them together, possibly with a | |
354 | digital signature at the end. Log progress to LOG-PORT. Return the list of | |
355 | files restored. | |
356 | ||
357 | When LOCK? is #f, assume locks for the files to be restored are already held. | |
358 | This is the case when the daemon calls a build hook. | |
359 | ||
360 | Note that this procedure accesses the store directly, so it's only meant to be | |
361 | used by the daemon's build hooks since they cannot call back to the daemon | |
362 | while the locks are held." | |
363 | (define %export-magic | |
364 | ;; Number used to identify genuine file set archives. | |
365 | #x4558494e) | |
366 | ||
367 | (define port* | |
368 | ;; Keep that one around, for error conditions. | |
369 | port) | |
370 | ||
371 | (define (assert-valid-signature signature hash file) | |
24194b6b NK |
372 | ;; Bail out if SIGNATURE, which must be a string as produced by |
373 | ;; 'canonical-sexp->string', doesn't match HASH, a bytevector containing | |
374 | ;; the expected hash for FILE. | |
e4687a5e LC |
375 | (let ((signature (catch 'gcry-error |
376 | (lambda () | |
377 | (string->canonical-sexp signature)) | |
378 | (lambda (err . _) | |
379 | (raise (condition | |
380 | (&message | |
381 | (message "signature is not a valid \ | |
cd4027fa | 382 | s-expression")) |
e4687a5e LC |
383 | (&nar-signature-error |
384 | (file file) | |
385 | (signature signature) (port port)))))))) | |
386 | (signature-case (signature hash (current-acl)) | |
387 | (valid-signature #t) | |
388 | (invalid-signature | |
389 | (raise (condition | |
390 | (&message (message "invalid signature")) | |
391 | (&nar-signature-error | |
392 | (file file) (signature signature) (port port))))) | |
393 | (hash-mismatch | |
394 | (raise (condition (&message (message "invalid hash")) | |
395 | (&nar-invalid-hash-error | |
396 | (port port) (file file) | |
397 | (signature signature) | |
398 | (expected (hash-data->bytevector | |
399 | (signature-signed-data signature))) | |
400 | (actual hash))))) | |
401 | (unauthorized-key | |
402 | (raise (condition (&message (message "unauthorized public key")) | |
403 | (&nar-signature-error | |
404 | (signature signature) (file file) (port port))))) | |
405 | (corrupt-signature | |
406 | (raise (condition | |
407 | (&message (message "corrupt signature data")) | |
408 | (&nar-signature-error | |
409 | (signature signature) (file file) (port port)))))))) | |
cd4027fa LC |
410 | |
411 | (let loop ((n (read-long-long port)) | |
412 | (files '())) | |
413 | (case n | |
414 | ((0) | |
415 | (reverse files)) | |
416 | ((1) | |
417 | (let-values (((port get-hash) | |
418 | (open-sha256-input-port port))) | |
419 | (let ((temp (temporary-store-directory))) | |
420 | (restore-file port temp) | |
421 | (let ((magic (read-int port))) | |
422 | (unless (= magic %export-magic) | |
423 | (raise (condition | |
424 | (&message (message "corrupt file set archive")) | |
425 | (&nar-read-error | |
426 | (port port*) (file #f) (token #f)))))) | |
427 | ||
428 | (let ((file (read-store-path port)) | |
429 | (refs (read-store-path-list port)) | |
430 | (deriver (read-string port)) | |
431 | (hash (get-hash)) | |
432 | (has-sig? (= 1 (read-int port)))) | |
433 | (format log-port | |
434 | (_ "importing file or directory '~a'...~%") | |
435 | file) | |
436 | ||
437 | (let ((sig (and has-sig? (read-string port)))) | |
438 | (when verify-signature? | |
439 | (if sig | |
440 | (begin | |
441 | (assert-valid-signature sig hash file) | |
442 | (format log-port | |
443 | (_ "found valid signature for '~a'~%") | |
444 | file) | |
445 | (finalize-store-file temp file | |
446 | #:references refs | |
447 | #:deriver deriver | |
448 | #:lock? lock?) | |
449 | (loop (read-long-long port) | |
450 | (cons file files))) | |
451 | (raise (condition | |
452 | (&message (message "imported file lacks \ | |
453 | a signature")) | |
454 | (&nar-signature-error | |
455 | (port port*) (file file) (signature #f))))))))))) | |
456 | (else | |
457 | ;; Neither 0 nor 1. | |
458 | (raise (condition | |
459 | (&message (message "invalid inter-file archive mark")) | |
460 | (&nar-read-error | |
461 | (port port) (file #f) (token #f)))))))) | |
462 | ||
0f41c26f | 463 | ;;; nar.scm ends here |