nar: Implement restoration from Nar.
[jackhill/guix/guix.git] / guix / nar.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
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)
31 #:export (nar-error?
32 nar-read-error?
33 nar-read-error-file
34 nar-read-error-port
35 nar-read-error-token
36
37 write-file
38 restore-file))
39
40 ;;; Comment:
41 ;;;
42 ;;; Read and write Nix archives, aka. ‘nar’.
43 ;;;
44 ;;; Code:
45
46 (define-condition-type &nar-error &error ; XXX: inherit from &nix-error ?
47 nar-error?)
48
49 (define-condition-type &nar-read-error &nar-error
50 nar-read-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
54
55
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))
60
61 (let loop ((left size))
62 (if (<= left 0)
63 0
64 (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
65 (if (eof-object? read)
66 left
67 (begin
68 (put-bytevector out buf 0 read)
69 (loop (- left read))))))))
70
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)
80 (lambda args
81 (close-port port)
82 (apply throw args))))))
83
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))
92
93 (define (read-contents in out)
94 "Read the contents of a file from the Nar at IN, write it to OUT, and return
95 the size in bytes."
96 (define executable?
97 (match (read-string in)
98 ("contents"
99 #f)
100 ("executable"
101 (match (list (read-string in) (read-string in))
102 (("" "contents") #t)
103 (x (raise
104 (condition (&message
105 (message "unexpected executable file marker"))
106 (&nar-read-error (port in)
107 (file #f)
108 (token x))))))
109 #t)
110 (x
111 (raise
112 (condition (&message (message "unsupported nar file type"))
113 (&nar-read-error (port in) (file #f) (token x)))))))
114
115 (let ((size (read-long-long in)))
116 ;; Note: `sendfile' cannot be used here because of port buffering on IN.
117 (dump in out size)
118
119 (when executable?
120 (chmod out #o755))
121 (let ((m (modulo size 8)))
122 (unless (zero? m)
123 (get-bytevector-n in (- 8 m))))
124 size))
125
126 (define %archive-version-1
127 ;; Magic cookie for Nix archives.
128 "nix-archive-1")
129
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."
133 (define p port)
134
135 (write-string %archive-version-1 p)
136
137 (let dump ((f file))
138 (let ((s (lstat f)))
139 (write-string "(" p)
140 (case (stat:type s)
141 ((regular)
142 (write-string "type" p)
143 (write-string "regular" p)
144 (if (not (zero? (logand (stat:mode s) #o100)))
145 (begin
146 (write-string "executable" p)
147 (write-string "" p)))
148 (write-contents f p (stat:size s)))
149 ((directory)
150 (write-string "type" p)
151 (write-string "directory" p)
152 (let ((entries (remove (cut member <> '("." ".."))
153 (scandir f))))
154 (for-each (lambda (e)
155 (let ((f (string-append f "/" e)))
156 (write-string "entry" p)
157 (write-string "(" p)
158 (write-string "name" p)
159 (write-string e p)
160 (write-string "node" p)
161 (dump f)
162 (write-string ")" p)))
163 entries)))
164 (else
165 (raise (condition (&message (message "ENOSYS"))
166 (&nar-error)))))
167 (write-string ")" p))))
168
169 (define (restore-file port file)
170 "Read a file (possibly a directory structure) in Nar format from PORT.
171 Restore it as FILE."
172 (let ((signature (read-string port)))
173 (unless (equal? signature %archive-version-1)
174 (raise
175 (condition (&message (message "invalid nar signature"))
176 (&nar-read-error (port port)
177 (token signature)
178 (file #f))))))
179
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)
185 (")" #t)
186 (x (raise
187 (condition
188 (&message (message "invalid nar end-of-file marker"))
189 (&nar-read-error (port port) (file file) (token x)))))))
190 (("(" "type" "directory")
191 (let ((dir file))
192 (mkdir dir)
193 (let loop ((prefix (read-string port)))
194 (match prefix
195 ("entry"
196 (match (list (read-string port)
197 (read-string port) (read-string port)
198 (read-string port))
199 (("(" "name" file "node")
200 (restore (string-append dir "/" file))
201 (match (read-string port)
202 (")" #t)
203 (x
204 (raise
205 (condition
206 (&message
207 (message "unexpected directory entry termination"))
208 (&nar-read-error (port port)
209 (file file)
210 (token x))))))
211 (loop (read-string port)))))
212 (")" #t) ; done with DIR
213 (x
214 (raise
215 (condition
216 (&message (message "unexpected directory inter-entry marker"))
217 (&nar-read-error (port port) (file file) (token x)))))))))
218 (x
219 (raise
220 (condition
221 (&message (message "unsupported nar entry type"))
222 (&nar-read-error (port port) (file file) (token x))))))))
223
224 ;;; nar.scm ends here