;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2016, 2017, 2018 Ludovic Courtès ;;; Copyright © 2017 Danny Milosavljevic ;;; ;;; This file is part of GNU Guix. ;;; ;;; GNU Guix is free software; you can redistribute it and/or modify it ;;; under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 3 of the License, or (at ;;; your option) any later version. ;;; ;;; GNU Guix is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with GNU Guix. If not, see . (define-module (gnu system uuid) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) #:use-module (rnrs bytevectors) #:use-module (ice-9 match) #:use-module (ice-9 vlist) #:use-module (ice-9 regex) #:use-module (ice-9 format) #:export (
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'po/packages')
0 files changed, 0 insertions, 0 deletions
Y m d H M S ss (make-regexp "^([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})-([[:digit:]]{2})$")) (define (string->iso9660-uuid str) "Parse STR as a ISO9660 UUID (which is really a timestamp - see /dev/disk/by-uuid). Return its contents as a 16-byte bytevector. Return #f if STR is not a valid ISO9660 UUID representation." (and=> (regexp-exec %iso9660-uuid-rx str) (lambda (match) (letrec-syntax ((match-numerals (syntax-rules () ((_ index (name rest ...) body) (let ((name (match:substring match index))) (match-numerals (+ 1 index) (rest ...) body))) ((_ index () body) body)))) (match-numerals 1 (year month day hour minute second hundredths) (string->utf8 (string-append year month day hour minute second hundredths))))))) (define (iso9660-uuid->string uuid) "Given an UUID bytevector, return its timestamp string." (define (digits->string bytes) (latin1->string bytes (lambda (c) #f))) (let* ((year (sub-bytevector uuid 0 4)) (month (sub-bytevector uuid 4 2)) (day (sub-bytevector uuid 6 2)) (hour (sub-bytevector uuid 8 2)) (minute (sub-bytevector uuid 10 2)) (second (sub-bytevector uuid 12 2)) (hundredths (sub-bytevector uuid 14 2)) (parts (list year month day hour minute second hundredths))) (string-append (string-join (map digits->string parts) "-")))) ;;; ;;; FAT32/FAT16. ;;; (define-syntax %fat-endianness ;; Endianness of FAT32/FAT16 file systems. (identifier-syntax (endianness little))) (define (fat-uuid->string uuid) "Convert FAT32/FAT16 UUID, a 4-byte bytevector, to its string representation." (let ((high (bytevector-uint-ref uuid 0 %fat-endianness 2)) (low (bytevector-uint-ref uuid 2 %fat-endianness 2))) (format #f "~:@(~x-~x~)" low high))) (define %fat-uuid-rx (make-regexp "^([[:xdigit:]]{4})-([[:xdigit:]]{4})$")) (define (string->fat-uuid str) "Parse STR, which is in FAT32/FAT16 format, and return a bytevector or #f." (match (regexp-exec %fat-uuid-rx str) (#f #f) (rx-match (uint-list->bytevector (list (string->number (match:substring rx-match 2) 16) (string->number (match:substring rx-match 1) 16)) %fat-endianness 2)))) ;;; ;;; Generic interface. ;;; (define string->ext2-uuid string->dce-uuid) (define string->ext3-uuid string->dce-uuid) (define string->ext4-uuid string->dce-uuid) (define string->btrfs-uuid string->dce-uuid) (define-syntax vhashq (syntax-rules (=>) ((_) vlist-null) ((_ (key others ... => value) rest ...) (vhash-consq key value (vhashq (others ... => value) rest ...))) ((_ (=> value) rest ...) (vhashq rest ...)))) (define %uuid-parsers (vhashq ('dce 'ext2 'ext3 'ext4 'btrfs 'luks => string->dce-uuid) ('fat32 'fat16 'fat => string->fat-uuid) ('iso9660 => string->iso9660-uuid))) (define %uuid-printers (vhashq ('dce 'ext2 'ext3 'ext4 'btrfs 'luks => dce-uuid->string) ('iso9660 => iso9660-uuid->string) ('fat32 'fat16 'fat => fat-uuid->string))) (define* (string->uuid str #:optional (type 'dce)) "Parse STR as a UUID of the given TYPE. On success, return the corresponding bytevector; otherwise return #f." (match (vhash-assq type %uuid-parsers) (#f #f) ((_ . (? procedure? parse)) (parse str)))) ;; High-level UUID representation that carries its type with it. ;; ;; This is necessary to serialize bytevectors with the right printer in some ;; circumstances. For instance, GRUB "search --fs-uuid" command compares the ;; string representation of UUIDs, not the raw bytes; thus, when emitting a ;; GRUB 'search' command, we need to produce the right string representation ;; (see ). (define-record-type (make-uuid type bv) uuid? (type uuid-type) ;'dce | 'iso9660 | ... (bv uuid-bytevector)) (define* (bytevector->uuid bv #:optional (type 'dce)) "Return a UUID object make of BV and TYPE." (make-uuid type bv)) (define-syntax uuid (lambda (s) "Return the UUID object corresponding to the given UUID representation or #f if the string could not be parsed." (syntax-case s (quote) ((_ str (quote type)) (and (string? (syntax->datum #'str)) (identifier? #'type)) ;; A literal string: do the conversion at expansion time. (let ((bv (string->uuid (syntax->datum #'str) (syntax->datum #'type)))) (unless bv (syntax-violation 'uuid "invalid UUID" s)) #`(make-uuid 'type #,(datum->syntax s bv)))) ((_ str) (string? (syntax->datum #'str)) #'(uuid str 'dce)) ((_ str) #'(let ((bv (string->uuid str 'dce))) (and bv (make-uuid 'dce bv)))) ((_ str type) #'(let ((bv (string->uuid str type))) (and bv (make-uuid type bv))))))) (define uuid->string ;; Convert the given bytevector or UUID object, to the corresponding UUID ;; string representation. (match-lambda* (((? bytevector? bv)) (uuid->string bv 'dce)) (((? bytevector? bv) type) (match (vhash-assq type %uuid-printers) (#f #f) ((_ . (? procedure? unparse)) (unparse bv)))) (((? uuid? uuid)) (uuid->string (uuid-bytevector uuid) (uuid-type uuid))))) (define uuid=? ;; Return true if A is equal to B, comparing only the actual bits. (match-lambda* (((? bytevector? a) (? bytevector? b)) (bytevector=? a b)) (((? uuid? a) (? bytevector? b)) (bytevector=? (uuid-bytevector a) b)) (((? uuid? a) (? uuid? b)) (bytevector=? (uuid-bytevector a) (uuid-bytevector b))) ((a b) (uuid=? b a))))