1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 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 nar)
20 #:use-module (guix utils)
21 #:use-module (guix serialization)
22 #:use-module ((guix build utils) #:select (with-directory-excursion))
23 #:use-module (rnrs bytevectors)
24 #:use-module (rnrs io ports)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-26)
27 #:use-module (srfi srfi-34)
28 #:use-module (srfi srfi-35)
29 #:use-module (ice-9 ftw)
30 #:use-module (ice-9 match)
42 ;;; Read and write Nix archives, aka. ‘nar’.
46 (define-condition-type &nar-error &error ; XXX: inherit from &nix-error ?
49 (define-condition-type &nar-read-error &nar-error
51 (port nar-read-error-port) ; port from which we read
52 (file nar-read-error-file) ; file we were restoring, or #f
53 (token nar-read-error-token)) ; faulty token, or #f
56 (define (dump in out size)
57 "Copy SIZE bytes from IN to OUT."
58 (define buf-size 65536)
59 (define buf (make-bytevector buf-size))
61 (let loop ((left size))
64 (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
65 (if (eof-object? read)
68 (put-bytevector out buf 0 read)
69 (loop (- left read))))))))
71 (define (write-contents file p size)
72 "Write SIZE bytes from FILE to output port P."
73 (define (call-with-binary-input-file file proc)
74 ;; Open FILE as a binary file. This avoids scan-for-encoding, and thus
75 ;; avoids any initial buffering. Disable file name canonicalization to
76 ;; avoid stat'ing like crazy.
77 (with-fluids ((%file-port-name-canonicalization #f))
78 (let ((port (open-file file "rb")))
79 (catch #t (cut proc port)
82 (apply throw args))))))
84 (write-string "contents" p)
85 (write-long-long size p)
86 (call-with-binary-input-file file
87 ;; Use `sendfile' when available (Guile 2.0.8+).
88 (if (compile-time-value (defined? 'sendfile))
89 (cut sendfile p <> size 0)
90 (cut dump <> p size)))
91 (write-padding size p))
93 (define (read-contents in out)
94 "Read the contents of a file from the Nar at IN, write it to OUT, and return
97 (match (read-string in)
101 (match (list (read-string in) (read-string in))
105 (message "unexpected executable file marker"))
106 (&nar-read-error (port in)
112 (condition (&message (message "unsupported nar file type"))
113 (&nar-read-error (port in) (file #f) (token x)))))))
115 (let ((size (read-long-long in)))
116 ;; Note: `sendfile' cannot be used here because of port buffering on IN.
121 (let ((m (modulo size 8)))
123 (get-bytevector-n in (- 8 m))))
126 (define %archive-version-1
127 ;; Magic cookie for Nix archives.
130 (define (write-file file port)
131 "Write the contents of FILE to PORT in Nar format, recursing into
132 sub-directories of FILE as needed."
135 (write-string %archive-version-1 p)
142 (write-string "type" p)
143 (write-string "regular" p)
144 (if (not (zero? (logand (stat:mode s) #o100)))
146 (write-string "executable" p)
147 (write-string "" p)))
148 (write-contents f p (stat:size s)))
150 (write-string "type" p)
151 (write-string "directory" p)
152 (let ((entries (remove (cut member <> '("." ".."))
154 (for-each (lambda (e)
155 (let ((f (string-append f "/" e)))
156 (write-string "entry" p)
158 (write-string "name" p)
160 (write-string "node" p)
162 (write-string ")" p)))
165 (raise (condition (&message (message "ENOSYS"))
167 (write-string ")" p))))
169 (define (restore-file port file)
170 "Read a file (possibly a directory structure) in Nar format from PORT.
172 (let ((signature (read-string port)))
173 (unless (equal? signature %archive-version-1)
175 (condition (&message (message "invalid nar signature"))
176 (&nar-read-error (port port)
180 (let restore ((file file))
181 (match (list (read-string port) (read-string port) (read-string port))
182 (("(" "type" "regular")
183 (call-with-output-file file (cut read-contents port <>))
184 (match (read-string port)
188 (&message (message "invalid nar end-of-file marker"))
189 (&nar-read-error (port port) (file file) (token x)))))))
190 (("(" "type" "directory")
193 (let loop ((prefix (read-string port)))
196 (match (list (read-string port)
197 (read-string port) (read-string port)
199 (("(" "name" file "node")
200 (restore (string-append dir "/" file))
201 (match (read-string port)
207 (message "unexpected directory entry termination"))
208 (&nar-read-error (port port)
211 (loop (read-string port)))))
212 (")" #t) ; done with DIR
216 (&message (message "unexpected directory inter-entry marker"))
217 (&nar-read-error (port port) (file file) (token x)))))))))
221 (&message (message "unsupported nar entry type"))
222 (&nar-read-error (port port) (file file) (token x))))))))
224 ;;; nar.scm ends here