Commit | Line | Data |
---|---|---|
0f41c26f | 1 | ;;; GNU Guix --- Functional package management for GNU |
9fe3f113 | 2 | ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 serialization) | |
958dd3ce | 20 | #:use-module (guix combinators) |
0f41c26f | 21 | #:use-module (rnrs bytevectors) |
0f41c26f LC |
22 | #:use-module (srfi srfi-1) |
23 | #:use-module (srfi srfi-26) | |
0363991a LC |
24 | #:use-module (srfi srfi-34) |
25 | #:use-module (srfi srfi-35) | |
2535635f LC |
26 | #:use-module (ice-9 binary-ports) |
27 | #:use-module ((ice-9 rdelim) #:prefix rdelim:) | |
6c20d1d0 | 28 | #:use-module (ice-9 match) |
0363991a | 29 | #:use-module (ice-9 ftw) |
0f41c26f LC |
30 | #:export (write-int read-int |
31 | write-long-long read-long-long | |
32 | write-padding | |
0d268c5d | 33 | write-bytevector write-string |
ce72c780 | 34 | read-string read-latin1-string read-maybe-utf8-string |
0f41c26f | 35 | write-string-list read-string-list |
6c20d1d0 | 36 | write-string-pairs |
0f41c26f | 37 | write-store-path read-store-path |
0363991a LC |
38 | write-store-path-list read-store-path-list |
39 | ||
40 | &nar-error | |
41 | nar-error? | |
42 | nar-error-port | |
43 | nar-error-file | |
44 | ||
45 | &nar-read-error | |
46 | nar-read-error? | |
47 | nar-read-error-token | |
48 | ||
49 | write-file | |
b94b698d | 50 | write-file-tree |
12c1afcd | 51 | fold-archive |
0363991a | 52 | restore-file)) |
0f41c26f LC |
53 | |
54 | ;;; Comment: | |
55 | ;;; | |
56 | ;;; Serialization procedures used by the RPCs and the Nar format. This module | |
57 | ;;; is for internal consumption. | |
58 | ;;; | |
59 | ;;; Code: | |
60 | ||
61 | ;; Similar to serialize.cc in Nix. | |
62 | ||
f9e8a123 | 63 | (define-condition-type &nar-error &error ; XXX: inherit from &store-error ? |
46b8aadb 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 | |
67 | ||
68 | (define currently-restored-file | |
69 | ;; Name of the file being restored. Used internally for error reporting. | |
70 | (make-parameter #f)) | |
71 | ||
72 | ||
73 | (define (get-bytevector-n* port count) | |
74 | (let ((bv (get-bytevector-n port count))) | |
75 | (when (or (eof-object? bv) | |
76 | (< (bytevector-length bv) count)) | |
77 | (raise (condition (&nar-error | |
78 | (file (currently-restored-file)) | |
79 | (port port))))) | |
80 | bv)) | |
81 | ||
0f41c26f LC |
82 | (define (write-int n p) |
83 | (let ((b (make-bytevector 8 0))) | |
84 | (bytevector-u32-set! b 0 n (endianness little)) | |
85 | (put-bytevector p b))) | |
86 | ||
87 | (define (read-int p) | |
46b8aadb | 88 | (let ((b (get-bytevector-n* p 8))) |
0f41c26f LC |
89 | (bytevector-u32-ref b 0 (endianness little)))) |
90 | ||
91 | (define (write-long-long n p) | |
92 | (let ((b (make-bytevector 8 0))) | |
93 | (bytevector-u64-set! b 0 n (endianness little)) | |
94 | (put-bytevector p b))) | |
95 | ||
96 | (define (read-long-long p) | |
46b8aadb | 97 | (let ((b (get-bytevector-n* p 8))) |
0f41c26f LC |
98 | (bytevector-u64-ref b 0 (endianness little)))) |
99 | ||
100 | (define write-padding | |
101 | (let ((zero (make-bytevector 8 0))) | |
102 | (lambda (n p) | |
103 | (let ((m (modulo n 8))) | |
104 | (or (zero? m) | |
105 | (put-bytevector p zero 0 (- 8 m))))))) | |
106 | ||
39d1e965 LC |
107 | (define* (write-bytevector s p |
108 | #:optional (l (bytevector-length s))) | |
109 | (let* ((m (modulo l 8)) | |
0f41c26f LC |
110 | (b (make-bytevector (+ 8 l (if (zero? m) 0 (- 8 m)))))) |
111 | (bytevector-u32-set! b 0 l (endianness little)) | |
112 | (bytevector-copy! s 0 b 8 l) | |
113 | (put-bytevector p b))) | |
114 | ||
0d268c5d LC |
115 | (define (write-string s p) |
116 | (write-bytevector (string->utf8 s) p)) | |
117 | ||
472e4c43 | 118 | (define (read-byte-string p) |
0f41c26f LC |
119 | (let* ((len (read-int p)) |
120 | (m (modulo len 8)) | |
472e4c43 | 121 | (bv (get-bytevector-n* p len))) |
0f41c26f | 122 | (or (zero? m) |
46b8aadb | 123 | (get-bytevector-n* p (- 8 m))) |
472e4c43 | 124 | bv)) |
0f41c26f | 125 | |
472e4c43 LC |
126 | (define (read-string p) |
127 | (utf8->string (read-byte-string p))) | |
aa27b560 | 128 | |
472e4c43 LC |
129 | (define (read-latin1-string p) |
130 | "Read an ISO-8859-1 string from P." | |
131 | ;; Note: do not use 'get-string-n' to work around Guile bug | |
132 | ;; <http://bugs.gnu.org/19621>. See <http://bugs.gnu.org/19610> for | |
133 | ;; a discussion. | |
134 | (let ((bv (read-byte-string p))) | |
36626c55 | 135 | ;; XXX: Rewrite using (ice-9 iconv). |
472e4c43 | 136 | (list->string (map integer->char (bytevector->u8-list bv))))) |
0f41c26f | 137 | |
ce72c780 LC |
138 | (define (read-maybe-utf8-string p) |
139 | "Read a serialized string from port P. Attempt to decode it as UTF-8 and | |
140 | substitute invalid byte sequences with question marks. This is a | |
141 | \"permissive\" UTF-8 decoder." | |
142 | ;; XXX: We rely on the port's decoding mechanism to do permissive decoding | |
143 | ;; and substitute invalid byte sequences with question marks, but this is | |
144 | ;; not very efficient. Eventually Guile may provide a lightweight | |
145 | ;; permissive UTF-8 decoder. | |
146 | (let* ((bv (read-byte-string p)) | |
bc7d089a LC |
147 | (port (open-bytevector-input-port bv))) |
148 | (set-port-encoding! port "UTF-8") | |
149 | (set-port-conversion-strategy! port 'substitute) | |
2535635f | 150 | (rdelim:read-string port))) |
ce72c780 | 151 | |
0f41c26f LC |
152 | (define (write-string-list l p) |
153 | (write-int (length l) p) | |
154 | (for-each (cut write-string <> p) l)) | |
155 | ||
6c20d1d0 LC |
156 | (define (write-string-pairs l p) |
157 | (write-int (length l) p) | |
158 | (for-each (match-lambda | |
159 | ((first . second) | |
160 | (write-string first p) | |
161 | (write-string second p))) | |
162 | l)) | |
163 | ||
0f41c26f LC |
164 | (define (read-string-list p) |
165 | (let ((len (read-int p))) | |
166 | (unfold (cut >= <> len) | |
167 | (lambda (i) | |
168 | (read-string p)) | |
169 | 1+ | |
170 | 0))) | |
171 | ||
172 | (define (write-store-path f p) | |
173 | (write-string f p)) ; TODO: assert path | |
174 | ||
175 | (define (read-store-path p) | |
176 | (read-string p)) ; TODO: assert path | |
177 | ||
178 | (define write-store-path-list write-string-list) | |
179 | (define read-store-path-list read-string-list) | |
180 | ||
0363991a | 181 | \f |
0363991a LC |
182 | (define-condition-type &nar-read-error &nar-error |
183 | nar-read-error? | |
184 | (token nar-read-error-token)) ; faulty token, or #f | |
185 | ||
186 | ||
187 | (define (dump in out size) | |
188 | "Copy SIZE bytes from IN to OUT." | |
189 | (define buf-size 65536) | |
190 | (define buf (make-bytevector buf-size)) | |
191 | ||
192 | (let loop ((left size)) | |
193 | (if (<= left 0) | |
194 | 0 | |
195 | (let ((read (get-bytevector-n! in buf 0 (min left buf-size)))) | |
196 | (if (eof-object? read) | |
197 | left | |
198 | (begin | |
199 | (put-bytevector out buf 0 read) | |
200 | (loop (- left read)))))))) | |
201 | ||
b94b698d LC |
202 | (define (write-contents-from-port input output size) |
203 | "Write SIZE bytes from port INPUT to port OUTPUT." | |
204 | (write-string "contents" output) | |
205 | (write-long-long size output) | |
206 | ;; Use 'sendfile' when both OUTPUT and INPUT are file ports. | |
207 | (if (and (file-port? output) (file-port? input)) | |
208 | (sendfile output input size 0) | |
209 | (dump input output size)) | |
210 | (write-padding size output)) | |
0363991a | 211 | |
12c1afcd LC |
212 | (define (read-file-type port) |
213 | "Read the file type tag from PORT, and return either 'regular or | |
214 | 'executable." | |
215 | (match (read-string port) | |
216 | ("contents" | |
217 | 'regular) | |
218 | ("executable" | |
219 | (match (list (read-string port) (read-string port)) | |
220 | (("" "contents") 'executable) | |
221 | (x (raise | |
222 | (condition (&message | |
223 | (message "unexpected executable file marker")) | |
224 | (&nar-read-error (port port) | |
225 | (file #f) | |
226 | (token x))))))) | |
227 | (x | |
228 | (raise | |
229 | (condition (&message (message "unsupported nar file type")) | |
230 | (&nar-read-error (port port) (file #f) (token x))))))) | |
0363991a LC |
231 | |
232 | (define %archive-version-1 | |
233 | ;; Magic cookie for Nix archives. | |
234 | "nix-archive-1") | |
235 | ||
fe585be9 LC |
236 | (define* (write-file file port |
237 | #:key (select? (const #t))) | |
0363991a | 238 | "Write the contents of FILE to PORT in Nar format, recursing into |
fe585be9 LC |
239 | sub-directories of FILE as needed. For each directory entry, call (SELECT? |
240 | FILE STAT), where FILE is the entry's absolute file name and STAT is the | |
241 | result of 'lstat'; exclude entries for which SELECT? does not return true." | |
b94b698d LC |
242 | (write-file-tree file port |
243 | #:file-type+size | |
244 | (lambda (file) | |
245 | (let* ((stat (lstat file)) | |
246 | (size (stat:size stat))) | |
247 | (case (stat:type stat) | |
248 | ((directory) | |
249 | (values 'directory size)) | |
250 | ((regular) | |
251 | (values (if (zero? (logand (stat:mode stat) | |
252 | #o100)) | |
253 | 'regular | |
254 | 'executable) | |
255 | size)) | |
256 | (else | |
257 | (values (stat:type stat) size))))) ;bah! | |
258 | #:file-port (cut open-file <> "r0b") | |
259 | #:symlink-target readlink | |
260 | ||
261 | #:directory-entries | |
262 | (lambda (directory) | |
263 | ;; 'scandir' defaults to 'string-locale<?' to sort files, | |
264 | ;; but this happens to be case-insensitive (at least in | |
265 | ;; 'en_US' locale on libc 2.18.) Conversely, we want | |
266 | ;; files to be sorted in a case-sensitive fashion. | |
267 | (define basenames | |
268 | (scandir directory (negate (cut member <> '("." ".."))) | |
269 | string<?)) | |
270 | ||
271 | (filter-map (lambda (base) | |
272 | (let ((file (string-append directory | |
273 | "/" base))) | |
c122a2e5 | 274 | (and (select? file (lstat file)) |
b94b698d LC |
275 | base))) |
276 | basenames)) | |
277 | ||
278 | ;; The 'scandir' call above gives us filtered and sorted | |
279 | ;; entries, so no post-processing is needed. | |
280 | #:postprocess-entries identity)) | |
281 | ||
282 | (define (filter/sort-directory-entries lst) | |
283 | "Remove dot and dot-dot entries from LST, and sort it in lexicographical | |
284 | order." | |
285 | (delete-duplicates | |
286 | (sort (remove (cute member <> '("." "..")) lst) | |
287 | string<?) | |
288 | string=?)) | |
289 | ||
290 | (define* (write-file-tree file port | |
291 | #:key | |
292 | file-type+size | |
293 | file-port | |
294 | symlink-target | |
295 | directory-entries | |
296 | (postprocess-entries filter/sort-directory-entries)) | |
297 | "Write the contents of FILE to PORT in Nar format, recursing into | |
298 | sub-directories of FILE as needed. | |
299 | ||
300 | This procedure does not make any file-system I/O calls. Instead, it calls the | |
301 | user-provided FILE-TYPE+SIZE, FILE-PORT, SYMLINK-TARGET, and DIRECTORY-ENTRIES | |
302 | procedures, which roughly correspond to 'lstat', 'readlink', and 'scandir'. | |
303 | POSTPROCESS-ENTRIES ensures that directory entries are valid; leave it as-is | |
304 | unless you know that DIRECTORY-ENTRIES provide filtered and sorted entries, in | |
305 | which case you can use 'identity'." | |
0363991a LC |
306 | (define p port) |
307 | ||
308 | (write-string %archive-version-1 p) | |
309 | ||
b94b698d LC |
310 | (let dump ((f file)) |
311 | (define-values (type size) | |
312 | (file-type+size f)) | |
313 | ||
fe585be9 | 314 | (write-string "(" p) |
b94b698d LC |
315 | (case type |
316 | ((regular executable) | |
fe585be9 LC |
317 | (write-string "type" p) |
318 | (write-string "regular" p) | |
b94b698d LC |
319 | (when (eq? 'executable type) |
320 | (write-string "executable" p) | |
321 | (write-string "" p)) | |
322 | (let ((input (file-port f))) | |
323 | (dynamic-wind | |
324 | (const #t) | |
325 | (lambda () | |
326 | (write-contents-from-port input p size)) | |
327 | (lambda () | |
328 | (close-port input))))) | |
fe585be9 LC |
329 | ((directory) |
330 | (write-string "type" p) | |
331 | (write-string "directory" p) | |
b94b698d | 332 | (let ((entries (postprocess-entries (directory-entries f)))) |
fe585be9 | 333 | (for-each (lambda (e) |
b94b698d LC |
334 | (let* ((f (string-append f "/" e))) |
335 | (write-string "entry" p) | |
336 | (write-string "(" p) | |
337 | (write-string "name" p) | |
338 | (write-string e p) | |
339 | (write-string "node" p) | |
340 | (dump f) | |
341 | (write-string ")" p))) | |
fe585be9 LC |
342 | entries))) |
343 | ((symlink) | |
344 | (write-string "type" p) | |
345 | (write-string "symlink" p) | |
346 | (write-string "target" p) | |
b94b698d | 347 | (write-string (symlink-target f) p)) |
fe585be9 LC |
348 | (else |
349 | (raise (condition (&message (message "unsupported file type")) | |
350 | (&nar-error (file f) (port port)))))) | |
351 | (write-string ")" p))) | |
0363991a | 352 | |
9fe3f113 LC |
353 | (define port-conversion-strategy |
354 | (fluid->parameter %default-port-conversion-strategy)) | |
355 | ||
12c1afcd LC |
356 | (define (fold-archive proc seed port file) |
357 | "Read a file (possibly a directory structure) in Nar format from PORT. Call | |
358 | PROC on each file or directory read from PORT using: | |
359 | ||
360 | (PROC FILE TYPE CONTENTS RESULT) | |
361 | ||
362 | using SEED as the first RESULT. TYPE is a symbol like 'regular, and CONTENTS | |
363 | depends on TYPE." | |
9fe3f113 LC |
364 | (parameterize ((currently-restored-file file) |
365 | ||
366 | ;; Error out if we can convert file names to the current | |
367 | ;; locale. (XXX: We'd prefer UTF-8 encoding for file names | |
368 | ;; regardless of the locale, but that's what Guile gives us | |
369 | ;; so far.) | |
370 | (port-conversion-strategy 'error)) | |
46b8aadb LC |
371 | (let ((signature (read-string port))) |
372 | (unless (equal? signature %archive-version-1) | |
373 | (raise | |
374 | (condition (&message (message "invalid nar signature")) | |
375 | (&nar-read-error (port port) | |
376 | (token signature) | |
377 | (file #f)))))) | |
378 | ||
12c1afcd LC |
379 | (let read ((file file) |
380 | (result seed)) | |
46b8aadb LC |
381 | (define (read-eof-marker) |
382 | (match (read-string port) | |
383 | (")" #t) | |
384 | (x (raise | |
385 | (condition | |
386 | (&message (message "invalid nar end-of-file marker")) | |
387 | (&nar-read-error (port port) (file file) (token x))))))) | |
388 | ||
389 | (currently-restored-file file) | |
390 | ||
391 | (match (list (read-string port) (read-string port) (read-string port)) | |
392 | (("(" "type" "regular") | |
12c1afcd LC |
393 | (let* ((type (read-file-type port)) |
394 | (size (read-long-long port)) | |
395 | ||
396 | ;; The caller must read exactly SIZE bytes from PORT. | |
397 | (result (proc file type `(,port . ,size) result))) | |
398 | (let ((m (modulo size 8))) | |
399 | (unless (zero? m) | |
400 | (get-bytevector-n* port (- 8 m)))) | |
401 | (read-eof-marker) | |
402 | result)) | |
46b8aadb LC |
403 | (("(" "type" "symlink") |
404 | (match (list (read-string port) (read-string port)) | |
405 | (("target" target) | |
12c1afcd LC |
406 | (let ((result (proc file 'symlink target result))) |
407 | (read-eof-marker) | |
408 | result)) | |
46b8aadb | 409 | (x (raise |
0363991a | 410 | (condition |
46b8aadb LC |
411 | (&message (message "invalid symlink tokens")) |
412 | (&nar-read-error (port port) (file file) (token x))))))) | |
413 | (("(" "type" "directory") | |
414 | (let ((dir file)) | |
12c1afcd LC |
415 | (let loop ((prefix (read-string port)) |
416 | (result (proc file 'directory #f result))) | |
46b8aadb LC |
417 | (match prefix |
418 | ("entry" | |
419 | (match (list (read-string port) | |
420 | (read-string port) (read-string port) | |
421 | (read-string port)) | |
422 | (("(" "name" file "node") | |
12c1afcd LC |
423 | (let ((result (read (string-append dir "/" file) result))) |
424 | (match (read-string port) | |
425 | (")" #f) | |
426 | (x | |
427 | (raise | |
428 | (condition | |
429 | (&message | |
430 | (message "unexpected directory entry termination")) | |
431 | (&nar-read-error (port port) | |
432 | (file file) | |
433 | (token x)))))) | |
434 | (loop (read-string port) result))))) | |
435 | (")" result) ;done with DIR | |
46b8aadb LC |
436 | (x |
437 | (raise | |
438 | (condition | |
439 | (&message (message "unexpected directory inter-entry marker")) | |
440 | (&nar-read-error (port port) (file file) (token x))))))))) | |
441 | (x | |
442 | (raise | |
443 | (condition | |
444 | (&message (message "unsupported nar entry type")) | |
445 | (&nar-read-error (port port) (file file) (token x))))))))) | |
0363991a | 446 | |
12c1afcd LC |
447 | (define (restore-file port file) |
448 | "Read a file (possibly a directory structure) in Nar format from PORT. | |
449 | Restore it as FILE." | |
450 | (fold-archive (lambda (file type content result) | |
451 | (match type | |
452 | ('directory | |
453 | (mkdir file)) | |
454 | ('symlink | |
455 | (symlink content file)) | |
456 | ((or 'regular 'executable) | |
457 | (match content | |
458 | ((input . size) | |
459 | (call-with-output-file file | |
460 | (lambda (output) | |
461 | (dump input output size) | |
462 | (when (eq? type 'executable) | |
463 | (chmod output #o755))))))))) | |
464 | #t | |
465 | port | |
466 | file)) | |
467 | ||
b94b698d LC |
468 | ;;; Local Variables: |
469 | ;;; eval: (put 'call-with-binary-input-file 'scheme-indent-function 1) | |
470 | ;;; End: | |
471 | ||
0f41c26f | 472 | ;;; serialization.scm ends here |