uuid.scm 12 KB

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