diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2013-04-08 22:54:08 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2013-04-08 23:21:38 +0200 |
| commit | 53c63ee93790e4e4054bf6547199d3490b78bf47 (patch) | |
| tree | 7066d23fbf0d7e474269bae078c9d4f8196a136e | |
| parent | 52e5910cdc0275cbc668682346172be2673d150d (diff) | |
nar: Implement restoration from Nar.
* guix/nar.scm (&nar-error, &nar-read-error): New condition types.
(dump): New procedure.
(write-contents)[dump]: Remove. Use the one above instead.
(read-contents, write-file, restore-file): New procedures.
(%archive-version-1): New variable.
| -rw-r--r-- | Makefile.am | 1 | ||||
| -rw-r--r-- | guix/nar.scm | 150 | ||||
| -rw-r--r-- | tests/nar.scm | 95 |
3 files changed, 228 insertions, 18 deletions
diff --git a/Makefile.am b/Makefile.am index a8bd2f8daf7..930ea6ce722 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -296,6 +296,7 @@ TESTS = \ | |||
| 296 | tests/packages.scm \ | 296 | tests/packages.scm \ |
| 297 | tests/snix.scm \ | 297 | tests/snix.scm \ |
| 298 | tests/store.scm \ | 298 | tests/store.scm \ |
| 299 | tests/nar.scm \ | ||
| 299 | tests/union.scm \ | 300 | tests/union.scm \ |
| 300 | tests/guix-build.sh \ | 301 | tests/guix-build.sh \ |
| 301 | tests/guix-download.sh \ | 302 | tests/guix-download.sh \ |
diff --git a/guix/nar.scm b/guix/nar.scm index b42f03c514e..9ae76ff2a97 100644 --- a/guix/nar.scm +++ b/guix/nar.scm | |||
| @@ -19,12 +19,23 @@ | |||
| 19 | (define-module (guix nar) | 19 | (define-module (guix nar) |
| 20 | #:use-module (guix utils) | 20 | #:use-module (guix utils) |
| 21 | #:use-module (guix serialization) | 21 | #:use-module (guix serialization) |
| 22 | #:use-module ((guix build utils) #:select (with-directory-excursion)) | ||
| 22 | #:use-module (rnrs bytevectors) | 23 | #:use-module (rnrs bytevectors) |
| 23 | #:use-module (rnrs io ports) | 24 | #:use-module (rnrs io ports) |
| 24 | #:use-module (srfi srfi-1) | 25 | #:use-module (srfi srfi-1) |
| 25 | #:use-module (srfi srfi-26) | 26 | #:use-module (srfi srfi-26) |
| 27 | #:use-module (srfi srfi-34) | ||
| 28 | #:use-module (srfi srfi-35) | ||
| 26 | #:use-module (ice-9 ftw) | 29 | #:use-module (ice-9 ftw) |
| 27 | #:export (write-file)) | 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)) | ||
| 28 | 39 | ||
| 29 | ;;; Comment: | 40 | ;;; Comment: |
| 30 | ;;; | 41 | ;;; |
| @@ -32,6 +43,31 @@ | |||
| 32 | ;;; | 43 | ;;; |
| 33 | ;;; Code: | 44 | ;;; Code: |
| 34 | 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 | |||
| 35 | (define (write-contents file p size) | 71 | (define (write-contents file p size) |
| 36 | "Write SIZE bytes from FILE to output port P." | 72 | "Write SIZE bytes from FILE to output port P." |
| 37 | (define (call-with-binary-input-file file proc) | 73 | (define (call-with-binary-input-file file proc) |
| @@ -45,33 +81,55 @@ | |||
| 45 | (close-port port) | 81 | (close-port port) |
| 46 | (apply throw args)))))) | 82 | (apply throw args)))))) |
| 47 | 83 | ||
| 48 | (define (dump in size) | ||
| 49 | (define buf-size 65536) | ||
| 50 | (define buf (make-bytevector buf-size)) | ||
| 51 | |||
| 52 | (let loop ((left size)) | ||
| 53 | (if (<= left 0) | ||
| 54 | 0 | ||
| 55 | (let ((read (get-bytevector-n! in buf 0 buf-size))) | ||
| 56 | (if (eof-object? read) | ||
| 57 | left | ||
| 58 | (begin | ||
| 59 | (put-bytevector p buf 0 read) | ||
| 60 | (loop (- left read)))))))) | ||
| 61 | |||
| 62 | (write-string "contents" p) | 84 | (write-string "contents" p) |
| 63 | (write-long-long size p) | 85 | (write-long-long size p) |
| 64 | (call-with-binary-input-file file | 86 | (call-with-binary-input-file file |
| 65 | ;; Use `sendfile' when available (Guile 2.0.8+). | 87 | ;; Use `sendfile' when available (Guile 2.0.8+). |
| 66 | (if (compile-time-value (defined? 'sendfile)) | 88 | (if (compile-time-value (defined? 'sendfile)) |
| 67 | (cut sendfile p <> size 0) | 89 | (cut sendfile p <> size 0) |
| 68 | (cut dump <> size))) | 90 | (cut dump <> p size))) |
| 69 | (write-padding size p)) | 91 | (write-padding size p)) |
| 70 | 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 | |||
| 71 | (define (write-file file port) | 130 | (define (write-file file port) |
| 72 | "Write the contents of FILE to PORT in Nar format, recursing into | 131 | "Write the contents of FILE to PORT in Nar format, recursing into |
| 73 | sub-directories of FILE as needed." | 132 | sub-directories of FILE as needed." |
| 74 | (define %archive-version-1 "nix-archive-1") | ||
| 75 | (define p port) | 133 | (define p port) |
| 76 | 134 | ||
| 77 | (write-string %archive-version-1 p) | 135 | (write-string %archive-version-1 p) |
| @@ -104,7 +162,63 @@ sub-directories of FILE as needed." | |||
| 104 | (write-string ")" p))) | 162 | (write-string ")" p))) |
| 105 | entries))) | 163 | entries))) |
| 106 | (else | 164 | (else |
| 107 | (error "ENOSYS"))) | 165 | (raise (condition (&message (message "ENOSYS")) |
| 166 | (&nar-error))))) | ||
| 108 | (write-string ")" p)))) | 167 | (write-string ")" p)))) |
| 109 | 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 | |||
| 110 | ;;; nar.scm ends here | 224 | ;;; nar.scm ends here |
diff --git a/tests/nar.scm b/tests/nar.scm new file mode 100644 index 00000000000..2d9bffd487f --- /dev/null +++ b/tests/nar.scm | |||
| @@ -0,0 +1,95 @@ | |||
| 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 (test-nar) | ||
| 20 | #:use-module (guix nar) | ||
| 21 | #:use-module (rnrs bytevectors) | ||
| 22 | #:use-module (rnrs io ports) | ||
| 23 | #:use-module (srfi srfi-26) | ||
| 24 | #:use-module (srfi srfi-64) | ||
| 25 | #:use-module (ice-9 ftw)) | ||
| 26 | |||
| 27 | ;; Test the (guix nar) module. | ||
| 28 | |||
| 29 | (define (rm-rf dir) | ||
| 30 | (file-system-fold (const #t) ; enter? | ||
| 31 | (lambda (file stat result) ; leaf | ||
| 32 | (delete-file file)) | ||
| 33 | (const #t) ; down | ||
| 34 | (lambda (dir stat result) ; up | ||
| 35 | (rmdir dir)) | ||
| 36 | (const #t) ; skip | ||
| 37 | (const #t) ; error | ||
| 38 | #t | ||
| 39 | dir | ||
| 40 | lstat)) | ||
| 41 | |||
| 42 | |||
| 43 | (test-begin "nar") | ||
| 44 | |||
| 45 | (test-assert "write-file + restore-file" | ||
| 46 | (let* ((input (string-append (dirname (search-path %load-path "guix.scm")) | ||
| 47 | "/guix")) | ||
| 48 | (output (string-append (dirname input) | ||
| 49 | "/test-nar-" | ||
| 50 | (number->string (getpid)))) | ||
| 51 | (nar (string-append output ".nar"))) | ||
| 52 | (dynamic-wind | ||
| 53 | (lambda () #t) | ||
| 54 | (lambda () | ||
| 55 | (call-with-output-file nar | ||
| 56 | (cut write-file input <>)) | ||
| 57 | (call-with-input-file nar | ||
| 58 | (cut restore-file <> output)) | ||
| 59 | (let* ((strip (cute string-drop <> (string-length input))) | ||
| 60 | (sibling (compose (cut string-append output <>) strip)) | ||
| 61 | (file=? (lambda (a b) | ||
| 62 | (and (eq? (stat:type (lstat a)) (stat:type (lstat b))) | ||
| 63 | (case (stat:type (lstat a)) | ||
| 64 | ((regular) | ||
| 65 | (equal? | ||
| 66 | (call-with-input-file a get-bytevector-all) | ||
| 67 | (call-with-input-file b get-bytevector-all))) | ||
| 68 | ((symlink) | ||
| 69 | (string=? (readlink a) (readlink b))) | ||
| 70 | (else | ||
| 71 | (error "what?" (lstat a)))))))) | ||
| 72 | (file-system-fold (const #t) | ||
| 73 | (lambda (name stat result) ; leaf | ||
| 74 | (and result | ||
| 75 | (file=? name (sibling name)))) | ||
| 76 | (lambda (name stat result) ; down | ||
| 77 | result) | ||
| 78 | (lambda (name stat result) ; up | ||
| 79 | result) | ||
| 80 | (const #f) ; skip | ||
| 81 | (lambda (name stat errno result) | ||
| 82 | (pk 'error name stat errno) | ||
| 83 | #f) | ||
| 84 | (> (stat:nlink (stat output)) 2) | ||
| 85 | input | ||
| 86 | lstat))) | ||
| 87 | (lambda () | ||
| 88 | (false-if-exception (delete-file nar)) | ||
| 89 | (false-if-exception (rm-rf output)) | ||
| 90 | )))) | ||
| 91 | |||
| 92 | (test-end "nar") | ||
| 93 | |||
| 94 | |||
| 95 | (exit (= (test-runner-fail-count (test-runner-current)) 0)) | ||
