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")))
83 (close-port port))))))
85 (write-string "contents" p)
86 (write-long-long size p)
87 (call-with-binary-input-file file
88 ;; Use `sendfile' when available (Guile 2.0.8+).
89 (if (compile-time-value (defined? 'sendfile))
90 (cut sendfile p <> size 0)
91 (cut dump <> p size)))
92 (write-padding size p))
94 (define (read-contents in out)
95 "Read the contents of a file from the Nar at IN, write it to OUT, and return
98 (match (read-string in)
102 (match (list (read-string in) (read-string in))
106 (message "unexpected executable file marker"))
107 (&nar-read-error (port in)
113 (condition (&message (message "unsupported nar file type"))
114 (&nar-read-error (port in) (file #f) (token x)))))))
116 (let ((size (read-long-long in)))
117 ;; Note: `sendfile' cannot be used here because of port buffering on IN.
122 (let ((m (modulo size 8)))
124 (get-bytevector-n in (- 8 m))))
127 (define %archive-version-1
128 ;; Magic cookie for Nix archives.
131 (define (write-file file port)
132 "Write the contents of FILE to PORT in Nar format, recursing into
133 sub-directories of FILE as needed."
136 (write-string %archive-version-1 p)
143 (write-string "type" p)
144 (write-string "regular" p)
145 (if (not (zero? (logand (stat:mode s) #o100)))
147 (write-string "executable" p)
148 (write-string "" p)))
149 (write-contents f p (stat:size s)))
151 (write-string "type" p)
152 (write-string "directory" p)
153 (let ((entries (remove (cut member <> '("." ".."))
155 (for-each (lambda (e)
156 (let ((f (string-append f "/" e)))
157 (write-string "entry" p)
159 (write-string "name" p)
161 (write-string "node" p)
163 (write-string ")" p)))
166 (write-string "type" p)
167 (write-string "symlink" p)
168 (write-string "target" p)
169 (write-string (readlink f) p))
171 (raise (condition (&message (message "ENOSYS"))
173 (write-string ")" p))))
175 (define (restore-file port file)
176 "Read a file (possibly a directory structure) in Nar format from PORT.
178 (let ((signature (read-string port)))
179 (unless (equal? signature %archive-version-1)
181 (condition (&message (message "invalid nar signature"))
182 (&nar-read-error (port port)
186 (let restore ((file file))
187 (define (read-eof-marker)
188 (match (read-string port)
192 (&message (message "invalid nar end-of-file marker"))
193 (&nar-read-error (port port) (file file) (token x)))))))
195 (match (list (read-string port) (read-string port) (read-string port))
196 (("(" "type" "regular")
197 (call-with-output-file file (cut read-contents port <>))
199 (("(" "type" "symlink")
200 (match (list (read-string port) (read-string port))
202 (symlink target file)
206 (&message (message "invalid symlink tokens"))
207 (&nar-read-error (port port) (file file) (token x)))))))
208 (("(" "type" "directory")
211 (let loop ((prefix (read-string port)))
214 (match (list (read-string port)
215 (read-string port) (read-string port)
217 (("(" "name" file "node")
218 (restore (string-append dir "/" file))
219 (match (read-string port)
225 (message "unexpected directory entry termination"))
226 (&nar-read-error (port port)
229 (loop (read-string port)))))
230 (")" #t) ; done with DIR
234 (&message (message "unexpected directory inter-entry marker"))
235 (&nar-read-error (port port) (file file) (token x)))))))))
239 (&message (message "unsupported nar entry type"))
240 (&nar-read-error (port port) (file file) (token x))))))))
242 ;;; nar.scm ends here