mirror of
https://git.in.rschanz.org/ryan77627/guix.git
synced 2025-01-12 14:16:55 -05:00
file-systems: Refactor file-system predicates.
* gnu/build/file-systems.scm (partition-field-reader, read-partition-field, %partition-label-readers, %partition-uuid-readers, read-partition-label, read-partition-uuid): New variables. (partition-predicate, partition-label-predicate, partition-uuid-predicate, luks-partition-uuid-predicate): Use partition field readers. (find-partition): New variable. (find-partition-by-label, find-partition-by-uuid, find-partition-by-luks-uuid): Use find-partition-by.
This commit is contained in:
parent
26905ec8a6
commit
ab4e939c50
1 changed files with 56 additions and 39 deletions
|
@ -1,6 +1,6 @@
|
||||||
;;; GNU Guix --- Functional package management for GNU
|
;;; GNU Guix --- Functional package management for GNU
|
||||||
;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
|
;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
|
||||||
;;; Copyright © 2016 David Craven <david@craven.ch>
|
;;; Copyright © 2016, 2017 David Craven <david@craven.ch>
|
||||||
;;;
|
;;;
|
||||||
;;; This file is part of GNU Guix.
|
;;; This file is part of GNU Guix.
|
||||||
;;;
|
;;;
|
||||||
|
@ -238,56 +238,73 @@ (define (ENOENT-safe proc)
|
||||||
(else
|
(else
|
||||||
(apply throw args))))))))
|
(apply throw args))))))))
|
||||||
|
|
||||||
(define (partition-predicate read field =)
|
(define (partition-field-reader read field)
|
||||||
|
"Return a procedure that takes a device and returns the value of a FIELD in
|
||||||
|
the partition superblock or #f."
|
||||||
|
(let ((read (ENOENT-safe read)))
|
||||||
|
(lambda (device)
|
||||||
|
(let ((sblock (read device)))
|
||||||
|
(and sblock
|
||||||
|
(field sblock))))))
|
||||||
|
|
||||||
|
(define (read-partition-field device partition-field-readers)
|
||||||
|
"Returns the value of a FIELD in the partition superblock of DEVICE or #f. It
|
||||||
|
takes a list of PARTITION-FIELD-READERS and returns the result of the first
|
||||||
|
partition field reader that returned a value."
|
||||||
|
(match (filter-map (cut apply <> (list device)) partition-field-readers)
|
||||||
|
((field . _) field)
|
||||||
|
(_ #f)))
|
||||||
|
|
||||||
|
(define %partition-label-readers
|
||||||
|
(list (partition-field-reader read-ext2-superblock
|
||||||
|
ext2-superblock-volume-name)))
|
||||||
|
|
||||||
|
(define %partition-uuid-readers
|
||||||
|
(list (partition-field-reader read-ext2-superblock
|
||||||
|
ext2-superblock-uuid)))
|
||||||
|
|
||||||
|
(define read-partition-label
|
||||||
|
(cut read-partition-field <> %partition-label-readers))
|
||||||
|
|
||||||
|
(define read-partition-uuid
|
||||||
|
(cut read-partition-field <> %partition-uuid-readers))
|
||||||
|
|
||||||
|
(define (partition-predicate reader =)
|
||||||
"Return a predicate that returns true if the FIELD of partition header that
|
"Return a predicate that returns true if the FIELD of partition header that
|
||||||
was READ is = to the given value."
|
was READ is = to the given value."
|
||||||
(let ((read (ENOENT-safe read)))
|
|
||||||
(lambda (expected)
|
(lambda (expected)
|
||||||
"Return a procedure that, when applied to a partition name such as \"sda1\",
|
(lambda (device)
|
||||||
returns #t if that partition's volume name is LABEL."
|
(let ((actual (reader device)))
|
||||||
(lambda (part)
|
|
||||||
(let* ((device (string-append "/dev/" part))
|
|
||||||
(sblock (read device)))
|
|
||||||
(and sblock
|
|
||||||
(let ((actual (field sblock)))
|
|
||||||
(and actual
|
(and actual
|
||||||
(= actual expected)))))))))
|
(= actual expected))))))
|
||||||
|
|
||||||
(define partition-label-predicate
|
(define partition-label-predicate
|
||||||
(partition-predicate read-ext2-superblock
|
(partition-predicate read-partition-label string=?))
|
||||||
ext2-superblock-volume-name
|
|
||||||
string=?))
|
|
||||||
|
|
||||||
(define partition-uuid-predicate
|
(define partition-uuid-predicate
|
||||||
(partition-predicate read-ext2-superblock
|
(partition-predicate read-partition-uuid bytevector=?))
|
||||||
ext2-superblock-uuid
|
|
||||||
bytevector=?))
|
|
||||||
|
|
||||||
(define luks-partition-uuid-predicate
|
(define luks-partition-uuid-predicate
|
||||||
(partition-predicate read-luks-header
|
(partition-predicate
|
||||||
luks-header-uuid
|
(partition-field-reader read-luks-header luks-header-uuid)
|
||||||
bytevector=?))
|
bytevector=?))
|
||||||
|
|
||||||
(define (find-partition-by-label label)
|
(define (find-partition predicate)
|
||||||
"Return the first partition found whose volume name is LABEL, or #f if none
|
"Return the first partition found that matches PREDICATE, or #f if none
|
||||||
were found."
|
were found."
|
||||||
(and=> (find (partition-label-predicate label)
|
(lambda (expected)
|
||||||
(disk-partitions))
|
(find (predicate expected)
|
||||||
(cut string-append "/dev/" <>)))
|
(map (cut string-append "/dev/" <>)
|
||||||
|
(disk-partitions)))))
|
||||||
|
|
||||||
(define (find-partition-by-uuid uuid)
|
(define find-partition-by-label
|
||||||
"Return the first partition whose unique identifier is UUID (a bytevector),
|
(find-partition partition-label-predicate))
|
||||||
or #f if none was found."
|
|
||||||
(and=> (find (partition-uuid-predicate uuid)
|
|
||||||
(disk-partitions))
|
|
||||||
(cut string-append "/dev/" <>)))
|
|
||||||
|
|
||||||
(define (find-partition-by-luks-uuid uuid)
|
(define find-partition-by-uuid
|
||||||
"Return the first LUKS partition whose unique identifier is UUID (a bytevector),
|
(find-partition partition-uuid-predicate))
|
||||||
or #f if none was found."
|
|
||||||
(and=> (find (luks-partition-uuid-predicate uuid)
|
(define find-partition-by-luks-uuid
|
||||||
(disk-partitions))
|
(find-partition luks-partition-uuid-predicate))
|
||||||
(cut string-append "/dev/" <>)))
|
|
||||||
|
|
||||||
|
|
||||||
;;;
|
;;;
|
||||||
|
|
Loading…
Reference in a new issue