Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1 | From 00e53f17c8462cb34ece08cc10db60a7da29a305 Mon Sep 17 00:00:00 2001 |
| 2 | From: Karel Zak <kzak@redhat.com> |
| 3 | Date: Tue, 4 Feb 2020 15:11:19 +0100 |
| 4 | Subject: [PATCH] libfdisk: (script) accept sector-size, ignore unknown headers |
| 5 | |
| 6 | - add sector-size between supported headers (already in --dump output) |
| 7 | |
| 8 | - report unknown headers by -ENOTSUP |
| 9 | |
| 10 | - ignore ENOTSUP in sfdisk (but print warning) and in fdisk_script_read_file() |
| 11 | |
| 12 | Upstream-Status: Backport [https://github.com/karelzak/util-linux/commit/00e53f17c8462cb34ece08cc10db60a7da29a305] |
| 13 | |
| 14 | Addresses: https://github.com/karelzak/util-linux/issues/949 |
| 15 | Signed-off-by: Karel Zak <kzak@redhat.com> |
| 16 | Signed-off-by: Pierre-Jean Texier <pjtexier@koncepto.io> |
| 17 | --- |
| 18 | disk-utils/sfdisk.c | 6 +++++- |
| 19 | libfdisk/src/script.c | 49 ++++++++++++++++++++++++++----------------------- |
| 20 | 2 files changed, 31 insertions(+), 24 deletions(-) |
| 21 | |
| 22 | diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c |
| 23 | index bb6e1c6..c0bea70 100644 |
| 24 | --- a/disk-utils/sfdisk.c |
| 25 | +++ b/disk-utils/sfdisk.c |
| 26 | @@ -1782,7 +1782,11 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) |
| 27 | } |
| 28 | |
| 29 | rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf)); |
| 30 | - if (rc < 0) { |
| 31 | + if (rc == -ENOTSUP) { |
| 32 | + buf[sizeof(buf) - 1] = '\0'; |
| 33 | + fdisk_warnx(sf->cxt, _("Unknown script header '%s' -- ignore."), buf); |
| 34 | + continue; |
| 35 | + } else if (rc < 0) { |
| 36 | DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands")); |
| 37 | buf[sizeof(buf) - 1] = '\0'; |
| 38 | rc = loop_control_commands(sf, dp, buf); |
| 39 | diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c |
| 40 | index a21771b..d3e67fa 100644 |
| 41 | --- a/libfdisk/src/script.c |
| 42 | +++ b/libfdisk/src/script.c |
| 43 | @@ -805,8 +805,12 @@ static inline int is_header_line(const char *s) |
| 44 | /* parses "<name>: value", note modifies @s*/ |
| 45 | static int parse_line_header(struct fdisk_script *dp, char *s) |
| 46 | { |
| 47 | - int rc = -EINVAL; |
| 48 | + size_t i; |
| 49 | char *name, *value; |
| 50 | + static const char *supported[] = { |
| 51 | + "label", "unit", "label-id", "device", "grain", |
| 52 | + "first-lba", "last-lba", "table-length", "sector-size" |
| 53 | + }; |
| 54 | |
| 55 | DBG(SCRIPT, ul_debugobj(dp, " parse header '%s'", s)); |
| 56 | |
| 57 | @@ -816,7 +820,7 @@ static int parse_line_header(struct fdisk_script *dp, char *s) |
| 58 | name = s; |
| 59 | value = strchr(s, ':'); |
| 60 | if (!value) |
| 61 | - goto done; |
| 62 | + return -EINVAL; |
| 63 | *value = '\0'; |
| 64 | value++; |
| 65 | |
| 66 | @@ -825,32 +829,30 @@ static int parse_line_header(struct fdisk_script *dp, char *s) |
| 67 | ltrim_whitespace((unsigned char *) value); |
| 68 | rtrim_whitespace((unsigned char *) value); |
| 69 | |
| 70 | + if (!*name || !*value) |
| 71 | + return -EINVAL; |
| 72 | + |
| 73 | + /* check header name */ |
| 74 | + for (i = 0; i < ARRAY_SIZE(supported); i++) { |
| 75 | + if (strcmp(name, supported[i]) == 0) |
| 76 | + break; |
| 77 | + } |
| 78 | + if (i == ARRAY_SIZE(supported)) |
| 79 | + return -ENOTSUP; |
| 80 | + |
| 81 | + /* header specific actions */ |
| 82 | if (strcmp(name, "label") == 0) { |
| 83 | if (dp->cxt && !fdisk_get_label(dp->cxt, value)) |
| 84 | - goto done; /* unknown label name */ |
| 85 | + return -EINVAL; /* unknown label name */ |
| 86 | dp->force_label = 1; |
| 87 | + |
| 88 | } else if (strcmp(name, "unit") == 0) { |
| 89 | if (strcmp(value, "sectors") != 0) |
| 90 | - goto done; /* only "sectors" supported */ |
| 91 | - } else if (strcmp(name, "label-id") == 0 |
| 92 | - || strcmp(name, "device") == 0 |
| 93 | - || strcmp(name, "grain") == 0 |
| 94 | - || strcmp(name, "first-lba") == 0 |
| 95 | - || strcmp(name, "last-lba") == 0 |
| 96 | - || strcmp(name, "table-length") == 0) { |
| 97 | - ; /* whatever is possible */ |
| 98 | - } else |
| 99 | - goto done; /* unknown header */ |
| 100 | + return -EINVAL; /* only "sectors" supported */ |
| 101 | |
| 102 | - if (*name && *value) |
| 103 | - rc = fdisk_script_set_header(dp, name, value); |
| 104 | -done: |
| 105 | - if (rc) |
| 106 | - DBG(SCRIPT, ul_debugobj(dp, "header parse error: " |
| 107 | - "[rc=%d, name='%s', value='%s']", |
| 108 | - rc, name, value)); |
| 109 | - return rc; |
| 110 | + } |
| 111 | |
| 112 | + return fdisk_script_set_header(dp, name, value); |
| 113 | } |
| 114 | |
| 115 | /* returns zero terminated string with next token and @str is updated */ |
| 116 | @@ -1363,7 +1365,8 @@ int fdisk_script_set_fgets(struct fdisk_script *dp, |
| 117 | * |
| 118 | * Reads next line into dump. |
| 119 | * |
| 120 | - * Returns: 0 on success, <0 on error, 1 when nothing to read. |
| 121 | + * Returns: 0 on success, <0 on error, 1 when nothing to read. For unknown headers |
| 122 | + * returns -ENOTSUP, it's usually safe to ignore this error. |
| 123 | */ |
| 124 | int fdisk_script_read_line(struct fdisk_script *dp, FILE *f, char *buf, size_t bufsz) |
| 125 | { |
| 126 | @@ -1428,7 +1431,7 @@ int fdisk_script_read_file(struct fdisk_script *dp, FILE *f) |
| 127 | |
| 128 | while (!feof(f)) { |
| 129 | rc = fdisk_script_read_line(dp, f, buf, sizeof(buf)); |
| 130 | - if (rc) |
| 131 | + if (rc && rc != -ENOTSUP) |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | -- |
| 136 | 2.7.4 |
| 137 | |