1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 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
33 write-bytevector write-string
34 read-string read-latin1-string read-maybe-utf8-string
35 write-string-list read-string-list
37 write-store-path read-store-path
38 write-store-path-list read-store-path-list
54 ;;; Serialization procedures used by the RPCs and the Nar format. This module
55 ;;; is for internal consumption.
59 ;; Similar to serialize.cc in Nix.
61 (define-condition-type &nar-error &error ; XXX: inherit from &nix-error ?
63 (file nar-error-file) ; file we were restoring, or #f
64 (port nar-error-port)) ; port from which we read
66 (define currently-restored-file
67 ;; Name of the file being restored. Used internally for error reporting.
71 (define (get-bytevector-n* port count)
72 (let ((bv (get-bytevector-n port count)))
73 (when (or (eof-object? bv)
74 (< (bytevector-length bv) count))
75 (raise (condition (&nar-error
76 (file (currently-restored-file))
80 (define (write-int n p)
81 (let ((b (make-bytevector 8 0)))
82 (bytevector-u32-set! b 0 n (endianness little))
83 (put-bytevector p b)))
86 (let ((b (get-bytevector-n* p 8)))
87 (bytevector-u32-ref b 0 (endianness little))))
89 (define (write-long-long n p)
90 (let ((b (make-bytevector 8 0)))
91 (bytevector-u64-set! b 0 n (endianness little))
92 (put-bytevector p b)))
94 (define (read-long-long p)
95 (let ((b (get-bytevector-n* p 8)))
96 (bytevector-u64-ref b 0 (endianness little))))
99 (let ((zero (make-bytevector 8 0)))
101 (let ((m (modulo n 8)))
103 (put-bytevector p zero 0 (- 8 m)))))))
105 (define* (write-bytevector s p
106 #:optional (l (bytevector-length s)))
107 (let* ((m (modulo l 8))
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 (write-string s p)
114 (write-bytevector (string->utf8 s) p))
116 (define (read-byte-string p)
117 (let* ((len (read-int p))
119 (bv (get-bytevector-n* p len)))
121 (get-bytevector-n* p (- 8 m)))
124 (define (read-string p)
125 (utf8->string (read-byte-string p)))
127 (define (read-latin1-string p)
128 "Read an ISO-8859-1 string from P."
129 ;; Note: do not use 'get-string-n' to work around Guile bug
130 ;; <http://bugs.gnu.org/19621>. See <http://bugs.gnu.org/19610> for
132 (let ((bv (read-byte-string p)))
133 ;; XXX: Rewrite using (ice-9 iconv).
134 (list->string (map integer->char (bytevector->u8-list bv)))))
136 (define (read-maybe-utf8-string p)
137 "Read a serialized string from port P. Attempt to decode it as UTF-8 and
138 substitute invalid byte sequences with question marks. This is a
139 \"permissive\" UTF-8 decoder."
140 ;; XXX: We rely on the port's decoding mechanism to do permissive decoding
141 ;; and substitute invalid byte sequences with question marks, but this is
142 ;; not very efficient. Eventually Guile may provide a lightweight
143 ;; permissive UTF-8 decoder.
144 (let* ((bv (read-byte-string p))
145 (port (open-bytevector-input-port bv)))
146 (set-port-encoding! port "UTF-8")
147 (set-port-conversion-strategy! port 'substitute)
148 (rdelim:read-string port)))
150 (define (write-string-list l p)
151 (write-int (length l) p)
152 (for-each (cut write-string <> p) l))
154 (define (write-string-pairs l p)
155 (write-int (length l) p)
156 (for-each (match-lambda
158 (write-string first p)
159 (write-string second p)))
162 (define (read-string-list p)
163 (let ((len (read-int p)))
164 (unfold (cut >= <> len)
170 (define (write-store-path f p)
171 (write-string f p)) ; TODO: assert path
173 (define (read-store-path p)
174 (read-string p)) ; TODO: assert path
176 (define write-store-path-list write-string-list)
177 (define read-store-path-list read-string-list)
180 (define-condition-type &nar-read-error &nar-error
182 (token nar-read-error-token)) ; faulty token, or #f
185 (define (dump in out size)
186 "Copy SIZE bytes from IN to OUT."
187 (define buf-size 65536)
188 (define buf (make-bytevector buf-size))
190 (let loop ((left size))
193 (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
194 (if (eof-object? read)
197 (put-bytevector out buf 0 read)
198 (loop (- left read))))))))
200 (define (write-contents file p size)
201 "Write SIZE bytes from FILE to output port P."
202 (define (call-with-binary-input-file file proc)
203 ;; Open FILE as a binary file. This avoids scan-for-encoding, and thus
204 ;; avoids any initial buffering. Disable file name canonicalization to
205 ;; avoid stat'ing like crazy.
206 (with-fluids ((%file-port-name-canonicalization #f))
207 (let ((port (open-file file "rb")))
212 (close-port port))))))
214 (write-string "contents" p)
215 (write-long-long size p)
216 (call-with-binary-input-file file
217 ;; Use 'sendfile' when P is a file port.
219 (cut sendfile p <> size 0)
220 (cut dump <> p size)))
221 (write-padding size p))
223 (define (read-contents in out)
224 "Read the contents of a file from the Nar at IN, write it to OUT, and return
227 (match (read-string in)
231 (match (list (read-string in) (read-string in))
235 (message "unexpected executable file marker"))
236 (&nar-read-error (port in)
242 (condition (&message (message "unsupported nar file type"))
243 (&nar-read-error (port in) (file #f) (token x)))))))
245 (let ((size (read-long-long in)))
246 ;; Note: `sendfile' cannot be used here because of port buffering on IN.
251 (let ((m (modulo size 8)))
253 (get-bytevector-n* in (- 8 m))))
256 (define %archive-version-1
257 ;; Magic cookie for Nix archives.
260 (define* (write-file file port
261 #:key (select? (const #t)))
262 "Write the contents of FILE to PORT in Nar format, recursing into
263 sub-directories of FILE as needed. For each directory entry, call (SELECT?
264 FILE STAT), where FILE is the entry's absolute file name and STAT is the
265 result of 'lstat'; exclude entries for which SELECT? does not return true."
268 (write-string %archive-version-1 p)
270 (let dump ((f file) (s (lstat file)))
274 (write-string "type" p)
275 (write-string "regular" p)
276 (if (not (zero? (logand (stat:mode s) #o100)))
278 (write-string "executable" p)
279 (write-string "" p)))
280 (write-contents f p (stat:size s)))
282 (write-string "type" p)
283 (write-string "directory" p)
285 ;; 'scandir' defaults to 'string-locale<?' to sort files, but
286 ;; this happens to be case-insensitive (at least in 'en_US'
287 ;; locale on libc 2.18.) Conversely, we want files to be
288 ;; sorted in a case-sensitive fashion.
289 (scandir f (negate (cut member <> '("." ".."))) string<?)))
290 (for-each (lambda (e)
291 (let* ((f (string-append f "/" e))
294 (write-string "entry" p)
296 (write-string "name" p)
298 (write-string "node" p)
300 (write-string ")" p))))
303 (write-string "type" p)
304 (write-string "symlink" p)
305 (write-string "target" p)
306 (write-string (readlink f) p))
308 (raise (condition (&message (message "unsupported file type"))
309 (&nar-error (file f) (port port))))))
310 (write-string ")" p)))
312 (define (restore-file port file)
313 "Read a file (possibly a directory structure) in Nar format from PORT.
315 (parameterize ((currently-restored-file file))
316 (let ((signature (read-string port)))
317 (unless (equal? signature %archive-version-1)
319 (condition (&message (message "invalid nar signature"))
320 (&nar-read-error (port port)
324 (let restore ((file file))
325 (define (read-eof-marker)
326 (match (read-string port)
330 (&message (message "invalid nar end-of-file marker"))
331 (&nar-read-error (port port) (file file) (token x)))))))
333 (currently-restored-file file)
335 (match (list (read-string port) (read-string port) (read-string port))
336 (("(" "type" "regular")
337 (call-with-output-file file (cut read-contents port <>))
339 (("(" "type" "symlink")
340 (match (list (read-string port) (read-string port))
342 (symlink target file)
346 (&message (message "invalid symlink tokens"))
347 (&nar-read-error (port port) (file file) (token x)))))))
348 (("(" "type" "directory")
351 (let loop ((prefix (read-string port)))
354 (match (list (read-string port)
355 (read-string port) (read-string port)
357 (("(" "name" file "node")
358 (restore (string-append dir "/" file))
359 (match (read-string port)
365 (message "unexpected directory entry termination"))
366 (&nar-read-error (port port)
369 (loop (read-string port)))))
370 (")" #t) ; done with DIR
374 (&message (message "unexpected directory inter-entry marker"))
375 (&nar-read-error (port port) (file file) (token x)))))))))
379 (&message (message "unsupported nar entry type"))
380 (&nar-read-error (port port) (file file) (token x)))))))))
382 ;;; serialization.scm ends here