1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (guix serialization)
20 #:use-module (guix utils)
21 #:use-module (rnrs bytevectors)
22 #:use-module (rnrs io ports)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-26)
25 #:use-module (srfi srfi-34)
26 #:use-module (srfi srfi-35)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 ftw)
29 #:export (write-int read-int
30 write-long-long read-long-long
33 read-string read-latin1-string read-maybe-utf8-string
34 write-string-list read-string-list
36 write-store-path read-store-path
37 write-store-path-list read-store-path-list
53 ;;; Serialization procedures used by the RPCs and the Nar format. This module
54 ;;; is for internal consumption.
58 ;; Similar to serialize.cc in Nix.
60 (define-condition-type &nar-error &error ; XXX: inherit from &nix-error ?
62 (file nar-error-file) ; file we were restoring, or #f
63 (port nar-error-port)) ; port from which we read
65 (define currently-restored-file
66 ;; Name of the file being restored. Used internally for error reporting.
70 (define (get-bytevector-n* port count)
71 (let ((bv (get-bytevector-n port count)))
72 (when (or (eof-object? bv)
73 (< (bytevector-length bv) count))
74 (raise (condition (&nar-error
75 (file (currently-restored-file))
79 (define (write-int n p)
80 (let ((b (make-bytevector 8 0)))
81 (bytevector-u32-set! b 0 n (endianness little))
82 (put-bytevector p b)))
85 (let ((b (get-bytevector-n* p 8)))
86 (bytevector-u32-ref b 0 (endianness little))))
88 (define (write-long-long n p)
89 (let ((b (make-bytevector 8 0)))
90 (bytevector-u64-set! b 0 n (endianness little))
91 (put-bytevector p b)))
93 (define (read-long-long p)
94 (let ((b (get-bytevector-n* p 8)))
95 (bytevector-u64-ref b 0 (endianness little))))
98 (let ((zero (make-bytevector 8 0)))
100 (let ((m (modulo n 8)))
102 (put-bytevector p zero 0 (- 8 m)))))))
104 (define (write-string s p)
105 (let* ((s (string->utf8 s))
106 (l (bytevector-length s))
108 (b (make-bytevector (+ 8 l (if (zero? m) 0 (- 8 m))))))
109 (bytevector-u32-set! b 0 l (endianness little))
110 (bytevector-copy! s 0 b 8 l)
111 (put-bytevector p b)))
113 (define (read-byte-string p)
114 (let* ((len (read-int p))
116 (bv (get-bytevector-n* p len)))
118 (get-bytevector-n* p (- 8 m)))
121 (define (read-string p)
122 (utf8->string (read-byte-string p)))
124 (define (read-latin1-string p)
125 "Read an ISO-8859-1 string from P."
126 ;; Note: do not use 'get-string-n' to work around Guile bug
127 ;; <http://bugs.gnu.org/19621>. See <http://bugs.gnu.org/19610> for
129 (let ((bv (read-byte-string p)))
130 ;; XXX: Rewrite using (ice-9 iconv) when the minimum requirement is
131 ;; upgraded to Guile >= 2.0.9.
132 (list->string (map integer->char (bytevector->u8-list bv)))))
134 (define (read-maybe-utf8-string p)
135 "Read a serialized string from port P. Attempt to decode it as UTF-8 and
136 substitute invalid byte sequences with question marks. This is a
137 \"permissive\" UTF-8 decoder."
138 ;; XXX: We rely on the port's decoding mechanism to do permissive decoding
139 ;; and substitute invalid byte sequences with question marks, but this is
140 ;; not very efficient. Eventually Guile may provide a lightweight
141 ;; permissive UTF-8 decoder.
142 (let* ((bv (read-byte-string p))
143 (port (open-bytevector-input-port bv)))
144 (set-port-encoding! port "UTF-8")
145 (set-port-conversion-strategy! port 'substitute)
146 (get-string-all port)))
148 (define (write-string-list l p)
149 (write-int (length l) p)
150 (for-each (cut write-string <> p) l))
152 (define (write-string-pairs l p)
153 (write-int (length l) p)
154 (for-each (match-lambda
156 (write-string first p)
157 (write-string second p)))
160 (define (read-string-list p)
161 (let ((len (read-int p)))
162 (unfold (cut >= <> len)
168 (define (write-store-path f p)
169 (write-string f p)) ; TODO: assert path
171 (define (read-store-path p)
172 (read-string p)) ; TODO: assert path
174 (define write-store-path-list write-string-list)
175 (define read-store-path-list read-string-list)
178 (define-condition-type &nar-read-error &nar-error
180 (token nar-read-error-token)) ; faulty token, or #f
183 (define (dump in out size)
184 "Copy SIZE bytes from IN to OUT."
185 (define buf-size 65536)
186 (define buf (make-bytevector buf-size))
188 (let loop ((left size))
191 (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
192 (if (eof-object? read)
195 (put-bytevector out buf 0 read)
196 (loop (- left read))))))))
198 (define (write-contents file p size)
199 "Write SIZE bytes from FILE to output port P."
200 (define (call-with-binary-input-file file proc)
201 ;; Open FILE as a binary file. This avoids scan-for-encoding, and thus
202 ;; avoids any initial buffering. Disable file name canonicalization to
203 ;; avoid stat'ing like crazy.
204 (with-fluids ((%file-port-name-canonicalization #f))
205 (let ((port (open-file file "rb")))
210 (close-port port))))))
212 (write-string "contents" p)
213 (write-long-long size p)
214 (call-with-binary-input-file file
215 ;; Use `sendfile' when available (Guile 2.0.8+).
216 (if (and (compile-time-value (defined? 'sendfile))
218 (cut sendfile p <> size 0)
219 (cut dump <> p size)))
220 (write-padding size p))
222 (define (read-contents in out)
223 "Read the contents of a file from the Nar at IN, write it to OUT, and return
226 (match (read-string in)
230 (match (list (read-string in) (read-string in))
234 (message "unexpected executable file marker"))
235 (&nar-read-error (port in)
241 (condition (&message (message "unsupported nar file type"))
242 (&nar-read-error (port in) (file #f) (token x)))))))
244 (let ((size (read-long-long in)))
245 ;; Note: `sendfile' cannot be used here because of port buffering on IN.
250 (let ((m (modulo size 8)))
252 (get-bytevector-n* in (- 8 m))))
255 (define %archive-version-1
256 ;; Magic cookie for Nix archives.
259 (define (write-file file port)
260 "Write the contents of FILE to PORT in Nar format, recursing into
261 sub-directories of FILE as needed."
264 (write-string %archive-version-1 p)
271 (write-string "type" p)
272 (write-string "regular" p)
273 (if (not (zero? (logand (stat:mode s) #o100)))
275 (write-string "executable" p)
276 (write-string "" p)))
277 (write-contents f p (stat:size s)))
279 (write-string "type" p)
280 (write-string "directory" p)
282 ;; NOTE: Guile 2.0.5's 'scandir' returns all subdirectories
283 ;; unconditionally, including "." and "..", regardless of the
284 ;; 'select?' predicate passed to it, so we have to filter
285 ;; those out externally.
286 (filter (negate (cut member <> '("." "..")))
287 ;; 'scandir' defaults to 'string-locale<?' to sort
288 ;; files, but this happens to be case-insensitive (at
289 ;; least in 'en_US' locale on libc 2.18.) Conversely,
290 ;; we want files to be sorted in a case-sensitive
292 (scandir f (const #t) string<?))))
293 (for-each (lambda (e)
294 (let ((f (string-append f "/" e)))
295 (write-string "entry" p)
297 (write-string "name" p)
299 (write-string "node" p)
301 (write-string ")" p)))
304 (write-string "type" p)
305 (write-string "symlink" p)
306 (write-string "target" p)
307 (write-string (readlink f) p))
309 (raise (condition (&message (message "unsupported file type"))
310 (&nar-error (file f) (port port))))))
311 (write-string ")" p))))
313 (define (restore-file port file)
314 "Read a file (possibly a directory structure) in Nar format from PORT.
316 (parameterize ((currently-restored-file file))
317 (let ((signature (read-string port)))
318 (unless (equal? signature %archive-version-1)
320 (condition (&message (message "invalid nar signature"))
321 (&nar-read-error (port port)
325 (let restore ((file file))
326 (define (read-eof-marker)
327 (match (read-string port)
331 (&message (message "invalid nar end-of-file marker"))
332 (&nar-read-error (port port) (file file) (token x)))))))
334 (currently-restored-file file)
336 (match (list (read-string port) (read-string port) (read-string port))
337 (("(" "type" "regular")
338 (call-with-output-file file (cut read-contents port <>))
340 (("(" "type" "symlink")
341 (match (list (read-string port) (read-string port))
343 (symlink target file)
347 (&message (message "invalid symlink tokens"))
348 (&nar-read-error (port port) (file file) (token x)))))))
349 (("(" "type" "directory")
352 (let loop ((prefix (read-string port)))
355 (match (list (read-string port)
356 (read-string port) (read-string port)
358 (("(" "name" file "node")
359 (restore (string-append dir "/" file))
360 (match (read-string port)
366 (message "unexpected directory entry termination"))
367 (&nar-read-error (port port)
370 (loop (read-string port)))))
371 (")" #t) ; done with DIR
375 (&message (message "unexpected directory inter-entry marker"))
376 (&nar-read-error (port port) (file file) (token x)))))))))
380 (&message (message "unsupported nar entry type"))
381 (&nar-read-error (port port) (file file) (token x)))))))))
383 ;;; serialization.scm ends here