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
32 write-string read-string read-latin1-string
33 write-string-list read-string-list
35 write-store-path read-store-path
36 write-store-path-list read-store-path-list
52 ;;; Serialization procedures used by the RPCs and the Nar format. This module
53 ;;; is for internal consumption.
57 ;; Similar to serialize.cc in Nix.
59 (define (write-int n p)
60 (let ((b (make-bytevector 8 0)))
61 (bytevector-u32-set! b 0 n (endianness little))
62 (put-bytevector p b)))
65 (let ((b (get-bytevector-n p 8)))
66 (bytevector-u32-ref b 0 (endianness little))))
68 (define (write-long-long n p)
69 (let ((b (make-bytevector 8 0)))
70 (bytevector-u64-set! b 0 n (endianness little))
71 (put-bytevector p b)))
73 (define (read-long-long p)
74 (let ((b (get-bytevector-n p 8)))
75 (bytevector-u64-ref b 0 (endianness little))))
78 (let ((zero (make-bytevector 8 0)))
80 (let ((m (modulo n 8)))
82 (put-bytevector p zero 0 (- 8 m)))))))
84 (define (write-string s p)
85 (let* ((s (string->utf8 s))
86 (l (bytevector-length s))
88 (b (make-bytevector (+ 8 l (if (zero? m) 0 (- 8 m))))))
89 (bytevector-u32-set! b 0 l (endianness little))
90 (bytevector-copy! s 0 b 8 l)
91 (put-bytevector p b)))
93 (define (read-string p)
94 (let* ((len (read-int p))
96 (bv (get-bytevector-n p len))
97 (str (utf8->string bv)))
99 (get-bytevector-n p (- 8 m)))
102 (define (read-latin1-string p)
103 (let* ((len (read-int p))
105 ;; Note: do not use 'get-string-n' to work around Guile bug
106 ;; <http://bugs.gnu.org/19621>. See <http://bugs.gnu.org/19610> for
108 (str (get-bytevector-n p len)))
110 (get-bytevector-n p (- 8 m)))
112 ;; XXX: Rewrite using (ice-9 iconv) when the minimum requirement is
113 ;; upgraded to Guile >= 2.0.9.
114 (list->string (map integer->char (bytevector->u8-list str)))))
116 (define (write-string-list l p)
117 (write-int (length l) p)
118 (for-each (cut write-string <> p) l))
120 (define (write-string-pairs l p)
121 (write-int (length l) p)
122 (for-each (match-lambda
124 (write-string first p)
125 (write-string second p)))
128 (define (read-string-list p)
129 (let ((len (read-int p)))
130 (unfold (cut >= <> len)
136 (define (write-store-path f p)
137 (write-string f p)) ; TODO: assert path
139 (define (read-store-path p)
140 (read-string p)) ; TODO: assert path
142 (define write-store-path-list write-string-list)
143 (define read-store-path-list read-string-list)
146 (define-condition-type &nar-error &error ; XXX: inherit from &nix-error ?
148 (file nar-error-file) ; file we were restoring, or #f
149 (port nar-error-port)) ; port from which we read
151 (define-condition-type &nar-read-error &nar-error
153 (token nar-read-error-token)) ; faulty token, or #f
156 (define (dump in out size)
157 "Copy SIZE bytes from IN to OUT."
158 (define buf-size 65536)
159 (define buf (make-bytevector buf-size))
161 (let loop ((left size))
164 (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
165 (if (eof-object? read)
168 (put-bytevector out buf 0 read)
169 (loop (- left read))))))))
171 (define (write-contents file p size)
172 "Write SIZE bytes from FILE to output port P."
173 (define (call-with-binary-input-file file proc)
174 ;; Open FILE as a binary file. This avoids scan-for-encoding, and thus
175 ;; avoids any initial buffering. Disable file name canonicalization to
176 ;; avoid stat'ing like crazy.
177 (with-fluids ((%file-port-name-canonicalization #f))
178 (let ((port (open-file file "rb")))
183 (close-port port))))))
185 (write-string "contents" p)
186 (write-long-long size p)
187 (call-with-binary-input-file file
188 ;; Use `sendfile' when available (Guile 2.0.8+).
189 (if (and (compile-time-value (defined? 'sendfile))
191 (cut sendfile p <> size 0)
192 (cut dump <> p size)))
193 (write-padding size p))
195 (define (read-contents in out)
196 "Read the contents of a file from the Nar at IN, write it to OUT, and return
199 (match (read-string in)
203 (match (list (read-string in) (read-string in))
207 (message "unexpected executable file marker"))
208 (&nar-read-error (port in)
214 (condition (&message (message "unsupported nar file type"))
215 (&nar-read-error (port in) (file #f) (token x)))))))
217 (let ((size (read-long-long in)))
218 ;; Note: `sendfile' cannot be used here because of port buffering on IN.
223 (let ((m (modulo size 8)))
225 (get-bytevector-n in (- 8 m))))
228 (define %archive-version-1
229 ;; Magic cookie for Nix archives.
232 (define (write-file file port)
233 "Write the contents of FILE to PORT in Nar format, recursing into
234 sub-directories of FILE as needed."
237 (write-string %archive-version-1 p)
244 (write-string "type" p)
245 (write-string "regular" p)
246 (if (not (zero? (logand (stat:mode s) #o100)))
248 (write-string "executable" p)
249 (write-string "" p)))
250 (write-contents f p (stat:size s)))
252 (write-string "type" p)
253 (write-string "directory" p)
255 ;; NOTE: Guile 2.0.5's 'scandir' returns all subdirectories
256 ;; unconditionally, including "." and "..", regardless of the
257 ;; 'select?' predicate passed to it, so we have to filter
258 ;; those out externally.
259 (filter (negate (cut member <> '("." "..")))
260 ;; 'scandir' defaults to 'string-locale<?' to sort
261 ;; files, but this happens to be case-insensitive (at
262 ;; least in 'en_US' locale on libc 2.18.) Conversely,
263 ;; we want files to be sorted in a case-sensitive
265 (scandir f (const #t) string<?))))
266 (for-each (lambda (e)
267 (let ((f (string-append f "/" e)))
268 (write-string "entry" p)
270 (write-string "name" p)
272 (write-string "node" p)
274 (write-string ")" p)))
277 (write-string "type" p)
278 (write-string "symlink" p)
279 (write-string "target" p)
280 (write-string (readlink f) p))
282 (raise (condition (&message (message "unsupported file type"))
283 (&nar-error (file f) (port port))))))
284 (write-string ")" p))))
286 (define (restore-file port file)
287 "Read a file (possibly a directory structure) in Nar format from PORT.
289 (let ((signature (read-string port)))
290 (unless (equal? signature %archive-version-1)
292 (condition (&message (message "invalid nar signature"))
293 (&nar-read-error (port port)
297 (let restore ((file file))
298 (define (read-eof-marker)
299 (match (read-string port)
303 (&message (message "invalid nar end-of-file marker"))
304 (&nar-read-error (port port) (file file) (token x)))))))
306 (match (list (read-string port) (read-string port) (read-string port))
307 (("(" "type" "regular")
308 (call-with-output-file file (cut read-contents port <>))
310 (("(" "type" "symlink")
311 (match (list (read-string port) (read-string port))
313 (symlink target file)
317 (&message (message "invalid symlink tokens"))
318 (&nar-read-error (port port) (file file) (token x)))))))
319 (("(" "type" "directory")
322 (let loop ((prefix (read-string port)))
325 (match (list (read-string port)
326 (read-string port) (read-string port)
328 (("(" "name" file "node")
329 (restore (string-append dir "/" file))
330 (match (read-string port)
336 (message "unexpected directory entry termination"))
337 (&nar-read-error (port port)
340 (loop (read-string port)))))
341 (")" #t) ; done with DIR
345 (&message (message "unexpected directory inter-entry marker"))
346 (&nar-read-error (port port) (file file) (token x)))))))))
350 (&message (message "unsupported nar entry type"))
351 (&nar-read-error (port port) (file file) (token x))))))))
353 ;;; serialization.scm ends here