substitute-binary: Ignore $GUIX_BINARY_SUBSTITUTE_URL.
[jackhill/guix/guix.git] / guix / serialization.scm
CommitLineData
0f41c26f 1;;; GNU Guix --- Functional package management for GNU
6c20d1d0 2;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
0f41c26f
LC
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 serialization)
20 #:use-module (guix utils)
21 #:use-module (rnrs bytevectors)
22 #:use-module (rnrs io ports)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-26)
6c20d1d0 25 #:use-module (ice-9 match)
0f41c26f
LC
26 #:export (write-int read-int
27 write-long-long read-long-long
28 write-padding
29 write-string read-string read-latin1-string
30 write-string-list read-string-list
6c20d1d0 31 write-string-pairs
0f41c26f
LC
32 write-store-path read-store-path
33 write-store-path-list read-store-path-list))
34
35;;; Comment:
36;;;
37;;; Serialization procedures used by the RPCs and the Nar format. This module
38;;; is for internal consumption.
39;;;
40;;; Code:
41
42;; Similar to serialize.cc in Nix.
43
44(define (write-int n p)
45 (let ((b (make-bytevector 8 0)))
46 (bytevector-u32-set! b 0 n (endianness little))
47 (put-bytevector p b)))
48
49(define (read-int p)
50 (let ((b (get-bytevector-n p 8)))
51 (bytevector-u32-ref b 0 (endianness little))))
52
53(define (write-long-long n p)
54 (let ((b (make-bytevector 8 0)))
55 (bytevector-u64-set! b 0 n (endianness little))
56 (put-bytevector p b)))
57
58(define (read-long-long p)
59 (let ((b (get-bytevector-n p 8)))
60 (bytevector-u64-ref b 0 (endianness little))))
61
62(define write-padding
63 (let ((zero (make-bytevector 8 0)))
64 (lambda (n p)
65 (let ((m (modulo n 8)))
66 (or (zero? m)
67 (put-bytevector p zero 0 (- 8 m)))))))
68
69(define (write-string s p)
70 (let* ((s (string->utf8 s))
71 (l (bytevector-length s))
72 (m (modulo l 8))
73 (b (make-bytevector (+ 8 l (if (zero? m) 0 (- 8 m))))))
74 (bytevector-u32-set! b 0 l (endianness little))
75 (bytevector-copy! s 0 b 8 l)
76 (put-bytevector p b)))
77
78(define (read-string p)
79 (let* ((len (read-int p))
80 (m (modulo len 8))
81 (bv (get-bytevector-n p len))
82 (str (utf8->string bv)))
83 (or (zero? m)
84 (get-bytevector-n p (- 8 m)))
85 str))
86
87(define (read-latin1-string p)
88 (let* ((len (read-int p))
89 (m (modulo len 8))
90 (str (get-string-n p len)))
91 (or (zero? m)
92 (get-bytevector-n p (- 8 m)))
93 str))
94
95(define (write-string-list l p)
96 (write-int (length l) p)
97 (for-each (cut write-string <> p) l))
98
6c20d1d0
LC
99(define (write-string-pairs l p)
100 (write-int (length l) p)
101 (for-each (match-lambda
102 ((first . second)
103 (write-string first p)
104 (write-string second p)))
105 l))
106
0f41c26f
LC
107(define (read-string-list p)
108 (let ((len (read-int p)))
109 (unfold (cut >= <> len)
110 (lambda (i)
111 (read-string p))
112 1+
113 0)))
114
115(define (write-store-path f p)
116 (write-string f p)) ; TODO: assert path
117
118(define (read-store-path p)
119 (read-string p)) ; TODO: assert path
120
121(define write-store-path-list write-string-list)
122(define read-store-path-list read-string-list)
123
124;;; serialization.scm ends here