diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2022-07-01 12:06:39 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2022-07-01 12:10:29 +0200 |
| commit | 0dab106a6af36eaebfd533d3e38c1275deb85935 (patch) | |
| tree | ffe91000c88f59849b8f2890173c12bf0a7f7b93 | |
| parent | 05a759ab36c66e6336cdcf9f04ecc9e6e8e29dc2 (diff) | |
image: Add sanitizers for 'format' and 'partition-table-type'.
This allows common mistakes to be diagnosed early.
* gnu/image.scm (define-set-sanitizer): New macro.
(validate-image-format, validate-partition-table-type): New sanitizers.
(<image>)[format, partition-table-type]: Add 'sanitize' property.
| -rw-r--r-- | gnu/image.scm | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/gnu/image.scm b/gnu/image.scm index e347089b342..486c02aadc1 100644 --- a/gnu/image.scm +++ b/gnu/image.scm | |||
| @@ -19,6 +19,10 @@ | |||
| 19 | (define-module (gnu image) | 19 | (define-module (gnu image) |
| 20 | #:use-module (guix platform) | 20 | #:use-module (guix platform) |
| 21 | #:use-module (guix records) | 21 | #:use-module (guix records) |
| 22 | #:use-module (guix diagnostics) | ||
| 23 | #:use-module (guix i18n) | ||
| 24 | #:use-module (srfi srfi-34) | ||
| 25 | #:use-module (srfi srfi-35) | ||
| 22 | #:export (partition | 26 | #:export (partition |
| 23 | partition? | 27 | partition? |
| 24 | partition-device | 28 | partition-device |
| @@ -77,12 +81,31 @@ | |||
| 77 | ;;; Image record. | 81 | ;;; Image record. |
| 78 | ;;; | 82 | ;;; |
| 79 | 83 | ||
| 84 | (define-syntax-rule (define-set-sanitizer name field set) | ||
| 85 | "Define NAME as a procedure or macro that raises an error if passed a value | ||
| 86 | that is not in SET, mentioning FIELD in the error message." | ||
| 87 | (define-with-syntax-properties (name (value properties)) | ||
| 88 | (unless (memq value 'set) | ||
| 89 | (raise | ||
| 90 | (make-compound-condition | ||
| 91 | (condition | ||
| 92 | (&error-location | ||
| 93 | (location (source-properties->location properties)))) | ||
| 94 | (formatted-message (G_ "~s: invalid '~a' value") value 'field)))) | ||
| 95 | value)) | ||
| 96 | |||
| 97 | (define-set-sanitizer validate-image-format format | ||
| 98 | (disk-image compressed-qcow2 docker iso9660)) | ||
| 99 | (define-set-sanitizer validate-partition-table-type partition-table-type | ||
| 100 | (mbr gpt)) | ||
| 101 | |||
| 80 | (define-record-type* <image> | 102 | (define-record-type* <image> |
| 81 | image make-image | 103 | image make-image |
| 82 | image? | 104 | image? |
| 83 | (name image-name ;symbol | 105 | (name image-name ;symbol |
| 84 | (default #f)) | 106 | (default #f)) |
| 85 | (format image-format) ;symbol | 107 | (format image-format ;symbol |
| 108 | (sanitize validate-image-format)) | ||
| 86 | (platform image-platform ;<platform> | 109 | (platform image-platform ;<platform> |
| 87 | (default #f)) | 110 | (default #f)) |
| 88 | (size image-size ;size in bytes as integer | 111 | (size image-size ;size in bytes as integer |
| @@ -90,7 +113,8 @@ | |||
| 90 | (operating-system image-operating-system ;<operating-system> | 113 | (operating-system image-operating-system ;<operating-system> |
| 91 | (default #f)) | 114 | (default #f)) |
| 92 | (partition-table-type image-partition-table-type ; 'mbr or 'gpt | 115 | (partition-table-type image-partition-table-type ; 'mbr or 'gpt |
| 93 | (default 'mbr)) | 116 | (default 'mbr) |
| 117 | (sanitize validate-partition-table-type)) | ||
| 94 | (partitions image-partitions ;list of <partition> | 118 | (partitions image-partitions ;list of <partition> |
| 95 | (default '())) | 119 | (default '())) |
| 96 | (compression? image-compression? ;boolean | 120 | (compression? image-compression? ;boolean |
