uuid.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Danny Milosavljevic <dannym@scratchpost.org>
  4. ;;; Copyright © 2019, 2020 Tobias Geerinckx-Rice <me@tobias.gr>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu system uuid)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-9)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 vlist)
  26. #:use-module (ice-9 regex)
  27. #:use-module (ice-9 format)
  28. #:export (uuid
  29. uuid?
  30. uuid-type
  31. uuid-bytevector
  32. uuid=?
  33. bytevector->uuid
  34. uuid->string
  35. dce-uuid->string
  36. string->uuid
  37. string->dce-uuid
  38. string->iso9660-uuid
  39. string->ext2-uuid
  40. string->ext3-uuid
  41. string->ext4-uuid
  42. string->bcachefs-uuid
  43. string->btrfs-uuid
  44. string->fat-uuid
  45. string->jfs-uuid
  46. string->ntfs-uuid
  47. iso9660-uuid->string
  48. ;; XXX: For lack of a better place.
  49. sub-bytevector
  50. latin1->string))
  51. ;;;
  52. ;;; Tools that lack a better place.
  53. ;;;
  54. (define (sub-bytevector bv start size)
  55. "Return a copy of the SIZE bytes of BV starting from offset START."
  56. (let ((result (make-bytevector size)))
  57. (bytevector-copy! bv start result 0 size)
  58. result))
  59. (define (latin1->string bv terminator)
  60. "Return a string of BV, a latin1 bytevector, or #f. TERMINATOR is a predicate
  61. that takes a number and returns #t when a termination character is found."
  62. (let ((bytes (take-while (negate terminator) (bytevector->u8-list bv))))
  63. (if (null? bytes)
  64. #f
  65. (list->string (map integer->char bytes)))))
  66. ;;;
  67. ;;; DCE UUIDs.
  68. ;;;
  69. (define-syntax %network-byte-order
  70. (identifier-syntax (endianness big)))
  71. (define (dce-uuid->string uuid)
  72. "Convert UUID, a 16-byte bytevector, to its string representation, something
  73. like \"6b700d61-5550-48a1-874c-a3d86998990e\"."
  74. ;; See <https://tools.ietf.org/html/rfc4122>.
  75. (let ((time-low (bytevector-uint-ref uuid 0 %network-byte-order 4))
  76. (time-mid (bytevector-uint-ref uuid 4 %network-byte-order 2))
  77. (time-hi (bytevector-uint-ref uuid 6 %network-byte-order 2))
  78. (clock-seq (bytevector-uint-ref uuid 8 %network-byte-order 2))
  79. (node (bytevector-uint-ref uuid 10 %network-byte-order 6)))
  80. (format #f "~8,'0x-~4,'0x-~4,'0x-~4,'0x-~12,'0x"
  81. time-low time-mid time-hi clock-seq node)))
  82. (define %uuid-rx
  83. ;; The regexp of a UUID.
  84. (make-regexp "^([[:xdigit:]]{8})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{4})-([[:xdigit:]]{12})$"))
  85. (define (string->dce-uuid str)
  86. "Parse STR as a DCE UUID (see <https://tools.ietf.org/html/rfc4122>) and
  87. return its contents as a 16-byte bytevector. Return #f if STR is not a valid
  88. UUID representation."
  89. (and=> (regexp-exec %uuid-rx str)
  90. (lambda (match)
  91. (letrec-syntax ((hex->number
  92. (syntax-rules ()
  93. ((_ index)
  94. (string->number (match:substring match index)
  95. 16))))
  96. (put!
  97. (syntax-rules ()
  98. ((_ bv index (number len) rest ...)
  99. (begin
  100. (bytevector-uint-set! bv index number
  101. (endianness big) len)
  102. (put! bv (+ index len) rest ...)))
  103. ((_ bv index)
  104. bv))))
  105. (let ((time-low (hex->number 1))
  106. (time-mid (hex->number 2))
  107. (time-hi (hex->number 3))
  108. (clock-seq (hex->number 4))
  109. (node (hex->number 5))
  110. (uuid (make-bytevector 16)))
  111. (put! uuid 0
  112. (time-low 4) (time-mid 2) (time-hi 2)
  113. (clock-seq 2) (node 6)))))))
  114. ;;;
  115. ;;; ISO-9660.
  116. ;;;
  117. ;; <http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf>.
  118. (define %iso9660-uuid-rx
  119. ;; Y m d H M S ss
  120. (make-regexp "^([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})$"))
  121. (define (string->iso9660-uuid str)
  122. "Parse STR as a ISO9660 UUID (which is really a timestamp - see /dev/disk/by-uuid).
  123. Return its contents as a 16-byte bytevector. Return #f if STR is not a valid
  124. ISO9660 UUID representation."
  125. (and=> (regexp-exec %iso9660-uuid-rx str)
  126. (lambda (match)
  127. (letrec-syntax ((match-numerals
  128. (syntax-rules ()
  129. ((_ index (name rest ...) body)
  130. (let ((name (match:substring match index)))
  131. (match-numerals (+ 1 index) (rest ...) body)))
  132. ((_ index () body)
  133. body))))
  134. (match-numerals 1 (year month day hour minute second hundredths)
  135. (string->utf8 (string-append year month day
  136. hour minute second hundredths)))))))
  137. (define (iso9660-uuid->string uuid)
  138. "Given an UUID bytevector, return its timestamp string."
  139. (define (digits->string bytes)
  140. (latin1->string bytes (lambda (c) #f)))
  141. (let* ((year (sub-bytevector uuid 0 4))
  142. (month (sub-bytevector uuid 4 2))
  143. (day (sub-bytevector uuid 6 2))
  144. (hour (sub-bytevector uuid 8 2))
  145. (minute (sub-bytevector uuid 10 2))
  146. (second (sub-bytevector uuid 12 2))
  147. (hundredths (sub-bytevector uuid 14 2))
  148. (parts (list year month day hour minute second hundredths)))
  149. (string-append (string-join (map digits->string parts) "-"))))
  150. ;;;
  151. ;;; FAT32/FAT16.
  152. ;;;
  153. (define-syntax %fat-endianness
  154. ;; Endianness of FAT32/FAT16 file systems.
  155. (identifier-syntax (endianness little)))
  156. (define (fat-uuid->string uuid)
  157. "Convert FAT32/FAT16 UUID, a 4-byte bytevector, to its string representation."
  158. (let ((high (bytevector-uint-ref uuid 0 %fat-endianness 2))
  159. (low (bytevector-uint-ref uuid 2 %fat-endianness 2)))
  160. (format #f "~:@(~4,'0x-~4,'0x~)" low high)))
  161. (define %fat-uuid-rx
  162. (make-regexp "^([[:xdigit:]]{4})-([[:xdigit:]]{4})$"))
  163. (define (string->fat-uuid str)
  164. "Parse STR, which is in FAT32/FAT16 format, and return a bytevector or #f."
  165. (match (regexp-exec %fat-uuid-rx str)
  166. (#f
  167. #f)
  168. (rx-match
  169. (uint-list->bytevector (list (string->number
  170. (match:substring rx-match 2) 16)
  171. (string->number
  172. (match:substring rx-match 1) 16))
  173. %fat-endianness
  174. 2))))
  175. ;;;
  176. ;;; NTFS.
  177. ;;;
  178. (define-syntax %ntfs-endianness
  179. ;; Endianness of NTFS file system.
  180. (identifier-syntax (endianness little)))
  181. (define (ntfs-uuid->string uuid)
  182. "Convert NTFS UUID, a 8-byte bytevector, to its string representation."
  183. (format #f "~{~:@(~x~)~}" (reverse (bytevector->u8-list uuid))))
  184. (define %ntfs-uuid-rx
  185. (make-regexp "^([[:xdigit:]]{16})$"))
  186. (define (string->ntfs-uuid str)
  187. "Parse STR, which is in NTFS format, and return a bytevector or #f."
  188. (match (regexp-exec %ntfs-uuid-rx str)
  189. (#f
  190. #f)
  191. (rx-match
  192. (u8-list->bytevector
  193. (let loop ((str str)
  194. (res '()))
  195. (if (string=? str "")
  196. res
  197. (loop (string-drop str 2)
  198. (cons
  199. (string->number (string-take str 2) 16)
  200. res))))))))
  201. ;;;
  202. ;;; Generic interface.
  203. ;;;
  204. (define string->ext2-uuid string->dce-uuid)
  205. (define string->ext3-uuid string->dce-uuid)
  206. (define string->ext4-uuid string->dce-uuid)
  207. (define string->bcachefs-uuid string->dce-uuid)
  208. (define string->btrfs-uuid string->dce-uuid)
  209. (define string->jfs-uuid string->dce-uuid)
  210. (define-syntax vhashq
  211. (syntax-rules (=>)
  212. ((_)
  213. vlist-null)
  214. ((_ (key others ... => value) rest ...)
  215. (vhash-consq key value
  216. (vhashq (others ... => value) rest ...)))
  217. ((_ (=> value) rest ...)
  218. (vhashq rest ...))))
  219. (define %uuid-parsers
  220. (vhashq
  221. ('dce 'ext2 'ext3 'ext4 'bcachefs 'btrfs 'jfs 'luks => string->dce-uuid)
  222. ('fat32 'fat16 'fat => string->fat-uuid)
  223. ('ntfs => string->ntfs-uuid)
  224. ('iso9660 => string->iso9660-uuid)))
  225. (define %uuid-printers
  226. (vhashq
  227. ('dce 'ext2 'ext3 'ext4 'bcachefs 'btrfs 'jfs 'luks => dce-uuid->string)
  228. ('iso9660 => iso9660-uuid->string)
  229. ('fat32 'fat16 'fat => fat-uuid->string)
  230. ('ntfs => ntfs-uuid->string)))
  231. (define* (string->uuid str #:optional (type 'dce))
  232. "Parse STR as a UUID of the given TYPE. On success, return the
  233. corresponding bytevector; otherwise return #f."
  234. (match (vhash-assq type %uuid-parsers)
  235. (#f #f)
  236. ((_ . (? procedure? parse)) (parse str))))
  237. ;; High-level UUID representation that carries its type with it.
  238. ;;
  239. ;; This is necessary to serialize bytevectors with the right printer in some
  240. ;; circumstances. For instance, GRUB "search --fs-uuid" command compares the
  241. ;; string representation of UUIDs, not the raw bytes; thus, when emitting a
  242. ;; GRUB 'search' command, we need to produce the right string representation
  243. ;; (see <https://debbugs.gnu.org/cgi/bugreport.cgi?msg=52;att=0;bug=27735>).
  244. (define-record-type <uuid>
  245. (make-uuid type bv)
  246. uuid?
  247. (type uuid-type) ;'dce | 'iso9660 | ...
  248. (bv uuid-bytevector))
  249. (define* (bytevector->uuid bv #:optional (type 'dce))
  250. "Return a UUID object make of BV and TYPE."
  251. (make-uuid type bv))
  252. (define-syntax uuid
  253. (lambda (s)
  254. "Return the UUID object corresponding to the given UUID representation or
  255. #f if the string could not be parsed."
  256. (syntax-case s (quote)
  257. ((_ str (quote type))
  258. (and (string? (syntax->datum #'str))
  259. (identifier? #'type))
  260. ;; A literal string: do the conversion at expansion time.
  261. (let ((bv (string->uuid (syntax->datum #'str)
  262. (syntax->datum #'type))))
  263. (unless bv
  264. (syntax-violation 'uuid "invalid UUID" s))
  265. #`(make-uuid 'type #,(datum->syntax s bv))))
  266. ((_ str)
  267. (string? (syntax->datum #'str))
  268. #'(uuid str 'dce))
  269. ((_ str)
  270. #'(let ((bv (string->uuid str 'dce)))
  271. (and bv (make-uuid 'dce bv))))
  272. ((_ str type)
  273. #'(let ((bv (string->uuid str type)))
  274. (and bv (make-uuid type bv)))))))
  275. (define uuid->string
  276. ;; Convert the given bytevector or UUID object, to the corresponding UUID
  277. ;; string representation.
  278. (match-lambda*
  279. (((? bytevector? bv))
  280. (uuid->string bv 'dce))
  281. (((? bytevector? bv) type)
  282. (match (vhash-assq type %uuid-printers)
  283. (#f #f)
  284. ((_ . (? procedure? unparse)) (unparse bv))))
  285. (((? uuid? uuid))
  286. (uuid->string (uuid-bytevector uuid) (uuid-type uuid)))))
  287. (define uuid=?
  288. ;; Return true if A is equal to B, comparing only the actual bits.
  289. (match-lambda*
  290. (((? bytevector? a) (? bytevector? b))
  291. (bytevector=? a b))
  292. (((? uuid? a) (? bytevector? b))
  293. (bytevector=? (uuid-bytevector a) b))
  294. (((? uuid? a) (? uuid? b))
  295. (bytevector=? (uuid-bytevector a) (uuid-bytevector b)))
  296. (((or (? uuid? a) (? bytevector? a)) (or (? uuid? b) (? bytevector? b)))
  297. (uuid=? b a))))