uuid.scm 11 KB

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