cpio.scm 10 KB

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