cpio.scm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix cpio)
  20. #:use-module ((guix build syscalls) #:select (device-number
  21. device-number->major+minor))
  22. #:use-module ((guix build utils) #:select (dump-port))
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (rnrs bytevectors)
  26. #:use-module (rnrs io ports)
  27. #:use-module (ice-9 match)
  28. #:export (cpio-header?
  29. make-cpio-header
  30. file->cpio-header
  31. file->cpio-header*
  32. special-file->cpio-header*
  33. write-cpio-header
  34. read-cpio-header
  35. write-cpio-archive))
  36. ;;; Commentary:
  37. ;;;
  38. ;;; This module implements the cpio "new ASCII" format, bit-for-bit identical
  39. ;;; to GNU cpio with the '-H newc' option.
  40. ;;;
  41. ;;; Code:
  42. ;; Values for 'mode', OR'd together.
  43. (define C_IRUSR #o000400)
  44. (define C_IWUSR #o000200)
  45. (define C_IXUSR #o000100)
  46. (define C_IRGRP #o000040)
  47. (define C_IWGRP #o000020)
  48. (define C_IXGRP #o000010)
  49. (define C_IROTH #o000004)
  50. (define C_IWOTH #o000002)
  51. (define C_IXOTH #o000001)
  52. (define C_ISUID #o004000)
  53. (define C_ISGID #o002000)
  54. (define C_ISVTX #o001000)
  55. (define C_FMT #o170000) ;bit mask
  56. (define C_ISBLK #o060000)
  57. (define C_ISCHR #o020000)
  58. (define C_ISDIR #o040000)
  59. (define C_ISFIFO #o010000)
  60. (define C_ISSOCK #o0140000)
  61. (define C_ISLNK #o0120000)
  62. (define C_ISCTG #o0110000)
  63. (define C_ISREG #o0100000)
  64. (define MAGIC
  65. ;; The "new" portable format with ASCII header, as produced by GNU cpio with
  66. ;; '-H newc'.
  67. (string->number "070701" 16))
  68. (define (read-header-field size port)
  69. (string->number (get-string-n port size) 16))
  70. (define (write-header-field value size port)
  71. (put-bytevector port
  72. (string->utf8
  73. (string-pad (string-upcase (number->string value 16))
  74. size #\0))))
  75. (define-syntax define-pack
  76. (syntax-rules ()
  77. ((_ type ctor pred write read (field-names field-sizes field-getters) ...)
  78. (begin
  79. (define-record-type type
  80. (ctor field-names ...)
  81. pred
  82. (field-names field-getters) ...)
  83. (define (read port)
  84. (set-port-encoding! port "ISO-8859-1")
  85. (ctor (read-header-field field-sizes port)
  86. ...))
  87. (define (write obj port)
  88. (let* ((size (+ field-sizes ...)))
  89. (match obj
  90. (($ type field-names ...)
  91. (write-header-field field-names field-sizes port)
  92. ...))))))))
  93. ;; cpio header in "new ASCII" format, without checksum.
  94. (define-pack <cpio-header>
  95. %make-cpio-header cpio-header?
  96. write-cpio-header read-cpio-header
  97. (magic 6 cpio-header-magic)
  98. (ino 8 cpio-header-inode)
  99. (mode 8 cpio-header-mode)
  100. (uid 8 cpio-header-uid)
  101. (gid 8 cpio-header-gid)
  102. (nlink 8 cpio-header-nlink)
  103. (mtime 8 cpio-header-mtime)
  104. (file-size 8 cpio-header-file-size)
  105. (dev-maj 8 cpio-header-device-major)
  106. (dev-min 8 cpio-header-device-minor)
  107. (rdev-maj 8 cpio-header-rdevice-major)
  108. (rdev-min 8 cpio-header-rdevice-minor)
  109. (name-size 8 cpio-header-name-size)
  110. (checksum 8 cpio-header-checksum)) ;0 for "newc" format
  111. (define* (make-cpio-header #:key
  112. (inode 0)
  113. (mode (logior C_ISREG C_IRUSR))
  114. (uid 0) (gid 0)
  115. (nlink 1) (mtime 0) (size 0)
  116. (dev 0) (rdev 0) (name-size 0))
  117. "Return a new cpio file header."
  118. (let-values (((major minor) (device-number->major+minor dev))
  119. ((rmajor rminor) (device-number->major+minor rdev)))
  120. (%make-cpio-header MAGIC
  121. inode mode uid gid
  122. nlink mtime
  123. (if (or (= C_ISLNK (logand mode C_FMT))
  124. (= C_ISREG (logand mode C_FMT)))
  125. size
  126. 0)
  127. major minor rmajor rminor
  128. (+ name-size 1) ;include trailing zero
  129. 0))) ;checksum
  130. (define (mode->type mode)
  131. "Given the number MODE, return a symbol representing the kind of file MODE
  132. denotes, similar to 'stat:type'."
  133. (let ((fmt (logand mode C_FMT)))
  134. (cond ((= C_ISREG fmt) 'regular)
  135. ((= C_ISDIR fmt) 'directory)
  136. ((= C_ISLNK fmt) 'symlink)
  137. ((= C_ISBLK fmt) 'block-special)
  138. ((= C_ISCHR fmt) 'char-special)
  139. (else
  140. (error "unsupported file type" mode)))))
  141. (define* (file->cpio-header file #:optional (file-name file)
  142. #:key (stat lstat))
  143. "Return a cpio header corresponding to the info returned by STAT for FILE,
  144. using FILE-NAME as its file name."
  145. (let ((st (stat file)))
  146. (make-cpio-header #:inode (stat:ino st)
  147. #:mode (stat:mode st)
  148. #:uid (stat:uid st)
  149. #:gid (stat:gid st)
  150. #:nlink (stat:nlink st)
  151. #:mtime (stat:mtime st)
  152. #:size (stat:size st)
  153. #:dev (stat:dev st)
  154. #:rdev (stat:rdev st)
  155. #:name-size (string-utf8-length file-name))))
  156. (define* (file->cpio-header* file
  157. #:optional (file-name file)
  158. #:key (stat lstat))
  159. "Similar to 'file->cpio-header', but return a header with a zeroed
  160. modification time, inode number, UID/GID, etc. This allows archives to be
  161. produced in a deterministic fashion."
  162. (let ((st (stat file)))
  163. (make-cpio-header #:mode (stat:mode st)
  164. #:nlink (stat:nlink st)
  165. #:size (stat:size st)
  166. #:name-size (string-utf8-length file-name))))
  167. (define* (special-file->cpio-header* file
  168. device-type
  169. device-major
  170. device-minor
  171. permission-bits
  172. #:optional (file-name file))
  173. "Create a character or block device header.
  174. DEVICE-TYPE is either 'char-special or 'block-special.
  175. The number of hard links is assumed to be 1."
  176. (make-cpio-header #:mode (logior (match device-type
  177. ('block-special C_ISBLK)
  178. ('char-special C_ISCHR))
  179. permission-bits)
  180. #:nlink 1
  181. #:rdev (device-number device-major device-minor)
  182. #:name-size (string-utf8-length file-name)))
  183. (define %trailer
  184. "TRAILER!!!")
  185. (define %last-header
  186. ;; The header that marks the end of the archive.
  187. (make-cpio-header #:mode 0
  188. #:name-size (string-length %trailer)))
  189. (define* (write-cpio-archive files port
  190. #:key (file->header file->cpio-header))
  191. "Write to PORT a cpio archive in \"new ASCII\" format containing all of FILES.
  192. The archive written to PORT is intended to be bit-identical to what GNU cpio
  193. produces with the '-H newc' option."
  194. (define (write-padding offset port)
  195. (let ((padding (modulo (- 4 (modulo offset 4)) 4)))
  196. (put-bytevector port (make-bytevector padding))))
  197. (define (pad-block port)
  198. ;; Write padding to PORT such that we finish with a 512-byte block.
  199. ;; XXX: We rely on PORT's internal state, assuming it's a file port.
  200. (let* ((offset (seek port 0 SEEK_CUR))
  201. (padding (modulo (- 512 (modulo offset 512)) 512)))
  202. (put-bytevector port (make-bytevector padding))))
  203. (define (dump-file file)
  204. (let* ((header (file->header file))
  205. (size (cpio-header-file-size header)))
  206. (write-cpio-header header port)
  207. (put-bytevector port (string->utf8 file))
  208. (put-u8 port 0)
  209. ;; We're padding the header + following file name + trailing zero, and
  210. ;; the header is 110 byte long.
  211. (write-padding (+ 110 (string-utf8-length file) 1) port)
  212. (case (mode->type (cpio-header-mode header))
  213. ((regular)
  214. (call-with-input-file file
  215. (lambda (input)
  216. (dump-port input port))))
  217. ((symlink)
  218. (let ((target (readlink file)))
  219. (put-bytevector port (string->utf8 target))))
  220. ((directory)
  221. #t)
  222. ((block-special)
  223. #t)
  224. ((char-special)
  225. #t)
  226. (else
  227. (error "file type not supported")))
  228. ;; Pad the file content.
  229. (write-padding size port)))
  230. (set-port-encoding! port "ISO-8859-1")
  231. (for-each dump-file files)
  232. (write-cpio-header %last-header port)
  233. (put-bytevector port (string->utf8 %trailer))
  234. (write-padding (string-length %trailer) port)
  235. ;; Pad so the last block is 512-byte long.
  236. (pad-block port))
  237. ;;; cpio.scm ends here