gnu: emacs-doom-themes: Only disable breaking compilations.
[jackhill/guix/guix.git] / guix / serialization.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
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)
20 #:use-module (guix combinators)
21 #:use-module (rnrs bytevectors)
22 #:use-module (srfi srfi-1)
23 #:use-module (srfi srfi-26)
24 #:use-module (srfi srfi-34)
25 #:use-module (srfi srfi-35)
26 #:use-module (ice-9 binary-ports)
27 #:use-module ((ice-9 rdelim) #:prefix rdelim:)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 ftw)
30 #:export (write-int read-int
31 write-long-long read-long-long
32 write-padding
33 write-bytevector write-string
34 read-string read-latin1-string read-maybe-utf8-string
35 write-string-list read-string-list
36 write-string-pairs
37 write-store-path read-store-path
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
50 write-file-tree
51 fold-archive
52 restore-file))
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
63 (define-condition-type &nar-error &error ; XXX: inherit from &store-error ?
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
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)
88 (let ((b (get-bytevector-n* p 8)))
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)
97 (let ((b (get-bytevector-n* p 8)))
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
107 (define* (write-bytevector s p
108 #:optional (l (bytevector-length s)))
109 (let* ((m (modulo l 8))
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
115 (define (write-string s p)
116 (write-bytevector (string->utf8 s) p))
117
118 (define (read-byte-string p)
119 (let* ((len (read-int p))
120 (m (modulo len 8))
121 (bv (get-bytevector-n* p len)))
122 (or (zero? m)
123 (get-bytevector-n* p (- 8 m)))
124 bv))
125
126 (define (read-string p)
127 (utf8->string (read-byte-string p)))
128
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)))
135 ;; XXX: Rewrite using (ice-9 iconv).
136 (list->string (map integer->char (bytevector->u8-list bv)))))
137
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))
147 (port (open-bytevector-input-port bv)))
148 (set-port-encoding! port "UTF-8")
149 (set-port-conversion-strategy! port 'substitute)
150 (rdelim:read-string port)))
151
152 (define (write-string-list l p)
153 (write-int (length l) p)
154 (for-each (cut write-string <> p) l))
155
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
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
181 \f
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
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))
211
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)))))))
231
232 (define %archive-version-1
233 ;; Magic cookie for Nix archives.
234 "nix-archive-1")
235
236 (define* (write-file file port
237 #:key (select? (const #t)))
238 "Write the contents of FILE to PORT in Nar format, recursing into
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."
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)))
274 (and (select? file (lstat file))
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'."
306 (define p port)
307
308 (write-string %archive-version-1 p)
309
310 (let dump ((f file))
311 (define-values (type size)
312 (file-type+size f))
313
314 (write-string "(" p)
315 (case type
316 ((regular executable)
317 (write-string "type" p)
318 (write-string "regular" p)
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)))))
329 ((directory)
330 (write-string "type" p)
331 (write-string "directory" p)
332 (let ((entries (postprocess-entries (directory-entries f))))
333 (for-each (lambda (e)
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)))
342 entries)))
343 ((symlink)
344 (write-string "type" p)
345 (write-string "symlink" p)
346 (write-string "target" p)
347 (write-string (symlink-target f) p))
348 (else
349 (raise (condition (&message (message "unsupported file type"))
350 (&nar-error (file f) (port port))))))
351 (write-string ")" p)))
352
353 (define port-conversion-strategy
354 (fluid->parameter %default-port-conversion-strategy))
355
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."
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))
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
379 (let read ((file file)
380 (result seed))
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")
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))
403 (("(" "type" "symlink")
404 (match (list (read-string port) (read-string port))
405 (("target" target)
406 (let ((result (proc file 'symlink target result)))
407 (read-eof-marker)
408 result))
409 (x (raise
410 (condition
411 (&message (message "invalid symlink tokens"))
412 (&nar-read-error (port port) (file file) (token x)))))))
413 (("(" "type" "directory")
414 (let ((dir file))
415 (let loop ((prefix (read-string port))
416 (result (proc file 'directory #f result)))
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")
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
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)))))))))
446
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
468 ;;; Local Variables:
469 ;;; eval: (put 'call-with-binary-input-file 'scheme-indent-function 1)
470 ;;; End:
471
472 ;;; serialization.scm ends here