serialization.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 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. (define-module (guix serialization)
  19. #:use-module (guix combinators)
  20. #:use-module (rnrs bytevectors)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-26)
  23. #:use-module (srfi srfi-34)
  24. #:use-module (srfi srfi-35)
  25. #:use-module (ice-9 binary-ports)
  26. #:use-module ((ice-9 rdelim) #:prefix rdelim:)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 ftw)
  29. #:export (write-int read-int
  30. write-long-long read-long-long
  31. write-padding
  32. write-bytevector write-string
  33. read-string read-latin1-string read-maybe-utf8-string
  34. write-string-list read-string-list
  35. write-string-pairs
  36. write-store-path read-store-path
  37. write-store-path-list read-store-path-list
  38. &nar-error
  39. nar-error?
  40. nar-error-port
  41. nar-error-file
  42. &nar-read-error
  43. nar-read-error?
  44. nar-read-error-token
  45. write-file
  46. write-file-tree
  47. fold-archive
  48. restore-file))
  49. ;;; Comment:
  50. ;;;
  51. ;;; Serialization procedures used by the RPCs and the Nar format. This module
  52. ;;; is for internal consumption.
  53. ;;;
  54. ;;; Code:
  55. ;; Similar to serialize.cc in Nix.
  56. (define-condition-type &nar-error &error ; XXX: inherit from &store-error ?
  57. nar-error?
  58. (file nar-error-file) ; file we were restoring, or #f
  59. (port nar-error-port)) ; port from which we read
  60. (define currently-restored-file
  61. ;; Name of the file being restored. Used internally for error reporting.
  62. (make-parameter #f))
  63. (define (get-bytevector-n* port count)
  64. (let ((bv (get-bytevector-n port count)))
  65. (when (or (eof-object? bv)
  66. (< (bytevector-length bv) count))
  67. (raise (condition (&nar-error
  68. (file (currently-restored-file))
  69. (port port)))))
  70. bv))
  71. (define (write-int n p)
  72. (let ((b (make-bytevector 8 0)))
  73. (bytevector-u32-set! b 0 n (endianness little))
  74. (put-bytevector p b)))
  75. (define (read-int p)
  76. (let ((b (get-bytevector-n* p 8)))
  77. (bytevector-u32-ref b 0 (endianness little))))
  78. (define (write-long-long n p)
  79. (let ((b (make-bytevector 8 0)))
  80. (bytevector-u64-set! b 0 n (endianness little))
  81. (put-bytevector p b)))
  82. (define (read-long-long p)
  83. (let ((b (get-bytevector-n* p 8)))
  84. (bytevector-u64-ref b 0 (endianness little))))
  85. (define write-padding
  86. (let ((zero (make-bytevector 8 0)))
  87. (lambda (n p)
  88. (let ((m (modulo n 8)))
  89. (or (zero? m)
  90. (put-bytevector p zero 0 (- 8 m)))))))
  91. (define* (write-bytevector s p
  92. #:optional (l (bytevector-length s)))
  93. (let* ((m (modulo l 8))
  94. (b (make-bytevector (+ 8 l (if (zero? m) 0 (- 8 m))))))
  95. (bytevector-u32-set! b 0 l (endianness little))
  96. (bytevector-copy! s 0 b 8 l)
  97. (put-bytevector p b)))
  98. (define (write-string s p)
  99. (write-bytevector (string->utf8 s) p))
  100. (define (read-byte-string p)
  101. (let* ((len (read-int p))
  102. (m (modulo len 8))
  103. (bv (get-bytevector-n* p len)))
  104. (or (zero? m)
  105. (get-bytevector-n* p (- 8 m)))
  106. bv))
  107. (define (read-string p)
  108. (utf8->string (read-byte-string p)))
  109. (define (read-latin1-string p)
  110. "Read an ISO-8859-1 string from P."
  111. ;; Note: do not use 'get-string-n' to work around Guile bug
  112. ;; <http://bugs.gnu.org/19621>. See <http://bugs.gnu.org/19610> for
  113. ;; a discussion.
  114. (let ((bv (read-byte-string p)))
  115. ;; XXX: Rewrite using (ice-9 iconv).
  116. (list->string (map integer->char (bytevector->u8-list bv)))))
  117. (define (read-maybe-utf8-string p)
  118. "Read a serialized string from port P. Attempt to decode it as UTF-8 and
  119. substitute invalid byte sequences with question marks. This is a
  120. \"permissive\" UTF-8 decoder."
  121. ;; XXX: We rely on the port's decoding mechanism to do permissive decoding
  122. ;; and substitute invalid byte sequences with question marks, but this is
  123. ;; not very efficient. Eventually Guile may provide a lightweight
  124. ;; permissive UTF-8 decoder.
  125. (let* ((bv (read-byte-string p))
  126. (port (open-bytevector-input-port bv)))
  127. (set-port-encoding! port "UTF-8")
  128. (set-port-conversion-strategy! port 'substitute)
  129. (rdelim:read-string port)))
  130. (define (write-string-list l p)
  131. (write-int (length l) p)
  132. (for-each (cut write-string <> p) l))
  133. (define (write-string-pairs l p)
  134. (write-int (length l) p)
  135. (for-each (match-lambda
  136. ((first . second)
  137. (write-string first p)
  138. (write-string second p)))
  139. l))
  140. (define (read-string-list p)
  141. (let ((len (read-int p)))
  142. (unfold (cut >= <> len)
  143. (lambda (i)
  144. (read-string p))
  145. 1+
  146. 0)))
  147. (define (write-store-path f p)
  148. (write-string f p)) ; TODO: assert path
  149. (define (read-store-path p)
  150. (read-string p)) ; TODO: assert path
  151. (define write-store-path-list write-string-list)
  152. (define read-store-path-list read-string-list)
  153. (define-condition-type &nar-read-error &nar-error
  154. nar-read-error?
  155. (token nar-read-error-token)) ; faulty token, or #f
  156. (define (dump in out size)
  157. "Copy SIZE bytes from IN to OUT."
  158. (define buf-size 65536)
  159. (define buf (make-bytevector buf-size))
  160. (let loop ((left size))
  161. (if (<= left 0)
  162. 0
  163. (let ((read (get-bytevector-n! in buf 0 (min left buf-size))))
  164. (if (eof-object? read)
  165. left
  166. (begin
  167. (put-bytevector out buf 0 read)
  168. (loop (- left read))))))))
  169. (define (write-contents-from-port input output size)
  170. "Write SIZE bytes from port INPUT to port OUTPUT."
  171. (write-string "contents" output)
  172. (write-long-long size output)
  173. ;; Use 'sendfile' when both OUTPUT and INPUT are file ports.
  174. (if (and (file-port? output) (file-port? input))
  175. (sendfile output input size 0)
  176. (dump input output size))
  177. (write-padding size output))
  178. (define (read-file-type port)
  179. "Read the file type tag from PORT, and return either 'regular or
  180. 'executable."
  181. (match (read-string port)
  182. ("contents"
  183. 'regular)
  184. ("executable"
  185. (match (list (read-string port) (read-string port))
  186. (("" "contents") 'executable)
  187. (x (raise
  188. (condition (&message
  189. (message "unexpected executable file marker"))
  190. (&nar-read-error (port port)
  191. (file #f)
  192. (token x)))))))
  193. (x
  194. (raise
  195. (condition (&message (message "unsupported nar file type"))
  196. (&nar-read-error (port port) (file #f) (token x)))))))
  197. (define %archive-version-1
  198. ;; Magic cookie for Nix archives.
  199. "nix-archive-1")
  200. (define* (write-file file port
  201. #:key (select? (const #t)))
  202. "Write the contents of FILE to PORT in Nar format, recursing into
  203. sub-directories of FILE as needed. For each directory entry, call (SELECT?
  204. FILE STAT), where FILE is the entry's absolute file name and STAT is the
  205. result of 'lstat'; exclude entries for which SELECT? does not return true."
  206. (write-file-tree file port
  207. #:file-type+size
  208. (lambda (file)
  209. (let* ((stat (lstat file))
  210. (size (stat:size stat)))
  211. (case (stat:type stat)
  212. ((directory)
  213. (values 'directory size))
  214. ((regular)
  215. (values (if (zero? (logand (stat:mode stat)
  216. #o100))
  217. 'regular
  218. 'executable)
  219. size))
  220. (else
  221. (values (stat:type stat) size))))) ;bah!
  222. #:file-port (cut open-file <> "r0b")
  223. #:symlink-target readlink
  224. #:directory-entries
  225. (lambda (directory)
  226. ;; 'scandir' defaults to 'string-locale<?' to sort files,
  227. ;; but this happens to be case-insensitive (at least in
  228. ;; 'en_US' locale on libc 2.18.) Conversely, we want
  229. ;; files to be sorted in a case-sensitive fashion.
  230. (define basenames
  231. (scandir directory (negate (cut member <> '("." "..")))
  232. string<?))
  233. (filter-map (lambda (base)
  234. (let ((file (string-append directory
  235. "/" base)))
  236. (and (select? file (lstat file))
  237. base)))
  238. basenames))
  239. ;; The 'scandir' call above gives us filtered and sorted
  240. ;; entries, so no post-processing is needed.
  241. #:postprocess-entries identity))
  242. (define (filter/sort-directory-entries lst)
  243. "Remove dot and dot-dot entries from LST, and sort it in lexicographical
  244. order."
  245. (delete-duplicates
  246. (sort (remove (cute member <> '("." "..")) lst)
  247. string<?)
  248. string=?))
  249. (define* (write-file-tree file port
  250. #:key
  251. file-type+size
  252. file-port
  253. symlink-target
  254. directory-entries
  255. (postprocess-entries filter/sort-directory-entries))
  256. "Write the contents of FILE to PORT in Nar format, recursing into
  257. sub-directories of FILE as needed.
  258. This procedure does not make any file-system I/O calls. Instead, it calls the
  259. user-provided FILE-TYPE+SIZE, FILE-PORT, SYMLINK-TARGET, and DIRECTORY-ENTRIES
  260. procedures, which roughly correspond to 'lstat', 'readlink', and 'scandir'.
  261. POSTPROCESS-ENTRIES ensures that directory entries are valid; leave it as-is
  262. unless you know that DIRECTORY-ENTRIES provide filtered and sorted entries, in
  263. which case you can use 'identity'."
  264. (define p port)
  265. (write-string %archive-version-1 p)
  266. (let dump ((f file))
  267. (define-values (type size)
  268. (file-type+size f))
  269. (write-string "(" p)
  270. (case type
  271. ((regular executable)
  272. (write-string "type" p)
  273. (write-string "regular" p)
  274. (when (eq? 'executable type)
  275. (write-string "executable" p)
  276. (write-string "" p))
  277. (let ((input (file-port f)))
  278. (dynamic-wind
  279. (const #t)
  280. (lambda ()
  281. (write-contents-from-port input p size))
  282. (lambda ()
  283. (close-port input)))))
  284. ((directory)
  285. (write-string "type" p)
  286. (write-string "directory" p)
  287. (let ((entries (postprocess-entries (directory-entries f))))
  288. (for-each (lambda (e)
  289. (let* ((f (string-append f "/" e)))
  290. (write-string "entry" p)
  291. (write-string "(" p)
  292. (write-string "name" p)
  293. (write-string e p)
  294. (write-string "node" p)
  295. (dump f)
  296. (write-string ")" p)))
  297. entries)))
  298. ((symlink)
  299. (write-string "type" p)
  300. (write-string "symlink" p)
  301. (write-string "target" p)
  302. (write-string (symlink-target f) p))
  303. (else
  304. (raise (condition (&message (message "unsupported file type"))
  305. (&nar-error (file f) (port port))))))
  306. (write-string ")" p)))
  307. (define port-conversion-strategy
  308. (fluid->parameter %default-port-conversion-strategy))
  309. (define (fold-archive proc seed port file)
  310. "Read a file (possibly a directory structure) in Nar format from PORT. Call
  311. PROC on each file or directory read from PORT using:
  312. (PROC FILE TYPE CONTENTS RESULT)
  313. using SEED as the first RESULT. TYPE is a symbol like 'regular, and CONTENTS
  314. depends on TYPE."
  315. (parameterize ((currently-restored-file file)
  316. ;; Error out if we can convert file names to the current
  317. ;; locale. (XXX: We'd prefer UTF-8 encoding for file names
  318. ;; regardless of the locale, but that's what Guile gives us
  319. ;; so far.)
  320. (port-conversion-strategy 'error))
  321. (let ((signature (read-string port)))
  322. (unless (equal? signature %archive-version-1)
  323. (raise
  324. (condition (&message (message "invalid nar signature"))
  325. (&nar-read-error (port port)
  326. (token signature)
  327. (file #f))))))
  328. (let read ((file file)
  329. (result seed))
  330. (define (read-eof-marker)
  331. (match (read-string port)
  332. (")" #t)
  333. (x (raise
  334. (condition
  335. (&message (message "invalid nar end-of-file marker"))
  336. (&nar-read-error (port port) (file file) (token x)))))))
  337. (currently-restored-file file)
  338. (match (list (read-string port) (read-string port) (read-string port))
  339. (("(" "type" "regular")
  340. (let* ((type (read-file-type port))
  341. (size (read-long-long port))
  342. ;; The caller must read exactly SIZE bytes from PORT.
  343. (result (proc file type `(,port . ,size) result)))
  344. (let ((m (modulo size 8)))
  345. (unless (zero? m)
  346. (get-bytevector-n* port (- 8 m))))
  347. (read-eof-marker)
  348. result))
  349. (("(" "type" "symlink")
  350. (match (list (read-string port) (read-string port))
  351. (("target" target)
  352. (let ((result (proc file 'symlink target result)))
  353. (read-eof-marker)
  354. result))
  355. (x (raise
  356. (condition
  357. (&message (message "invalid symlink tokens"))
  358. (&nar-read-error (port port) (file file) (token x)))))))
  359. (("(" "type" "directory")
  360. (let ((dir file))
  361. (let loop ((prefix (read-string port))
  362. (result (proc file 'directory #f result)))
  363. (match prefix
  364. ("entry"
  365. (match (list (read-string port)
  366. (read-string port) (read-string port)
  367. (read-string port))
  368. (("(" "name" file "node")
  369. (let ((result (read (string-append dir "/" file) result)))
  370. (match (read-string port)
  371. (")" #f)
  372. (x
  373. (raise
  374. (condition
  375. (&message
  376. (message "unexpected directory entry termination"))
  377. (&nar-read-error (port port)
  378. (file file)
  379. (token x))))))
  380. (loop (read-string port) result)))))
  381. (")" result) ;done with DIR
  382. (x
  383. (raise
  384. (condition
  385. (&message (message "unexpected directory inter-entry marker"))
  386. (&nar-read-error (port port) (file file) (token x)))))))))
  387. (x
  388. (raise
  389. (condition
  390. (&message (message "unsupported nar entry type"))
  391. (&nar-read-error (port port) (file file) (token x)))))))))
  392. (define (restore-file port file)
  393. "Read a file (possibly a directory structure) in Nar format from PORT.
  394. Restore it as FILE."
  395. (fold-archive (lambda (file type content result)
  396. (match type
  397. ('directory
  398. (mkdir file))
  399. ('symlink
  400. (symlink content file))
  401. ((or 'regular 'executable)
  402. (match content
  403. ((input . size)
  404. (call-with-output-file file
  405. (lambda (output)
  406. (dump input output size)
  407. (when (eq? type 'executable)
  408. (chmod output #o755)))))))))
  409. #t
  410. port
  411. file))
  412. ;;; Local Variables:
  413. ;;; eval: (put 'call-with-binary-input-file 'scheme-indent-function 1)
  414. ;;; End:
  415. ;;; serialization.scm ends here