From: Karel Zak Date: Thu, 2 Oct 2014 13:49:05 +0000 (+0200) Subject: libfdisk: (gpt) parse attr bits X-Git-Tag: v2.26-rc1~352 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c77ba5316f60ed0bdd255b18400f1e8347f3bfb2;p=thirdparty%2Futil-linux.git libfdisk: (gpt) parse attr bits Signed-off-by: Karel Zak --- diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index 6720ff0cfe..857318b00c 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1370,6 +1370,75 @@ static int gpt_entry_attrs_to_string(struct gpt_entry *e, char **res) return 0; } +static int gpt_entry_attrs_from_string( + struct fdisk_context *cxt, + struct gpt_entry *e, + const char *str) +{ + const char *p = str; + uint64_t attrs = 0; + char *bits; + + assert(e); + assert(p); + + DBG(LABEL, ul_debug("GPT: parsing string attributes '%s'", p)); + + bits = (char *) &attrs; + + while (p && *p) { + int bit = -1; + + while (isblank(*p)) p++; + if (!*p) + break; + + DBG(LABEL, ul_debug(" parsing item '%s'", p)); + + if (strncmp(p, "GUID:", 5) == 0) { + p += 5; + continue; + } else if (strncmp(p, GPT_ATTRSTR_REQ, + sizeof(GPT_ATTRSTR_REQ) - 1) == 0) { + bit = GPT_ATTRBIT_REQ; + p += sizeof(GPT_ATTRSTR_REQ) - 1; + } else if (strncmp(p, GPT_ATTRSTR_LEGACY, + sizeof(GPT_ATTRSTR_LEGACY) - 1) == 0) { + bit = GPT_ATTRBIT_LEGACY; + p += sizeof(GPT_ATTRSTR_LEGACY) - 1; + } else if (strncmp(p, GPT_ATTRSTR_NOBLOCK, + sizeof(GPT_ATTRSTR_NOBLOCK) - 1) == 0) { + bit = GPT_ATTRBIT_NOBLOCK; + p += sizeof(GPT_ATTRSTR_NOBLOCK) - 1; + } else if (isdigit((unsigned int) *p)) { + char *end = NULL; + + errno = 0; + bit = strtol(p, &end, 0); + if (errno || !end || end == str + || bit < GPT_ATTRBIT_GUID_FIRST + || bit >= GPT_ATTRBIT_GUID_FIRST + GPT_ATTRBIT_GUID_COUNT) + bit = -1; + else + p = end; + } + + if (bit < 0) { + fdisk_warnx(cxt, _("unssuported GPT attribute bit '%s'"), p); + return -EINVAL; + } + + setbit(bits, bit); + + while (isblank(*p)) p++; + if (*p == ',') + p++; + } + + e->attrs = cpu_to_le64(attrs); + return 0; +} + static int gpt_get_partition(struct fdisk_context *cxt, size_t n, struct fdisk_partition *pa) { @@ -1470,6 +1539,11 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n, return rc; gpt_entry_set_type(e, &typeid); } + if (pa->attrs) { + rc = gpt_entry_attrs_from_string(cxt, e, pa->attrs); + if (rc) + return rc; + } if (pa->start) e->lba_start = cpu_to_le64(pa->start); @@ -1991,6 +2065,8 @@ static int gpt_add_partition( if (pa && pa->name && *pa->name) gpt_entry_set_name(e, pa->name); + if (pa && pa->attrs) + gpt_entry_attrs_from_string(cxt, e, pa->attrs); DBG(LABEL, ul_debug("GPT new partition: partno=%zu, start=%ju, end=%ju, size=%ju", partnum, diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index ad19c41c67..ac0aad263d 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -256,6 +256,7 @@ extern const struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partit extern int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name); extern const char *fdisk_partition_get_name(struct fdisk_partition *pa); extern int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid); +extern int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs); extern const char *fdisk_partition_get_uuid(struct fdisk_partition *pa); extern const char *fdisk_partition_get_attrs(struct fdisk_partition *pa); extern int fdisk_partition_is_nested(struct fdisk_partition *pa); diff --git a/libfdisk/src/partition.c b/libfdisk/src/partition.c index 4f83f72754..c9b596b6bb 100644 --- a/libfdisk/src/partition.c +++ b/libfdisk/src/partition.c @@ -347,6 +347,22 @@ const char *fdisk_partition_get_attrs(struct fdisk_partition *pa) return pa ? pa->attrs : NULL; } +int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs) +{ + char *p = NULL; + + if (!pa) + return -EINVAL; + if (attrs) { + p = strdup(attrs); + if (!p) + return -ENOMEM; + } + free(pa->attrs); + pa->attrs = p; + return 0; +} + int fdisk_partition_is_nested(struct fdisk_partition *pa) { return pa && pa->parent_partno != FDISK_EMPTY_PARTNO;