diff options
author | Karel Zak | 2014-08-28 14:16:56 +0200 |
---|---|---|
committer | Karel Zak | 2014-08-28 14:16:56 +0200 |
commit | 080633e43dd8f7af1417b5cf05d0e8330d356b02 (patch) | |
tree | 5a6a0dcecd87ec2e3aafdc2580a56910e0fd0e26 /libfdisk | |
parent | libfdisk: add reference to context to dump (diff) | |
download | kernel-qcow2-util-linux-080633e43dd8f7af1417b5cf05d0e8330d356b02.tar.gz kernel-qcow2-util-linux-080633e43dd8f7af1417b5cf05d0e8330d356b02.tar.xz kernel-qcow2-util-linux-080633e43dd8f7af1417b5cf05d0e8330d356b02.zip |
libfdisk: (gpt) set_{name,uuid} functions refactoring
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk')
-rw-r--r-- | libfdisk/src/gpt.c | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index 27e41ba26..befb2d91d 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1715,6 +1715,35 @@ static void gpt_entry_set_type(struct gpt_entry *e, struct gpt_guid *uuid) DBG(LABEL, gpt_debug_uuid("new type", &(e->type))); } +static void gpt_entry_set_name(struct gpt_entry *e, char *str) +{ + char name[GPT_PART_NAME_LEN] = { 0 }; + size_t i, sz = strlen(str); + + if (sz) { + if (sz > GPT_PART_NAME_LEN) + sz = GPT_PART_NAME_LEN; + memcpy(name, str, sz); + } + + for (i = 0; i < GPT_PART_NAME_LEN; i++) + e->name[i] = cpu_to_le16((uint16_t) name[i]); +} + +static int gpt_entry_set_uuid(struct gpt_entry *e, char *str) +{ + struct gpt_guid uuid; + int rc; + + rc = string_to_guid(str, &uuid); + if (rc) + return rc; + + e->partition_guid = uuid; + return 0; +} + + /* * Create a new GPT partition entry, specified by partnum, and with a range * of fsect to lsenct sectors, of type t. @@ -2108,7 +2137,6 @@ int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i) { struct fdisk_gpt_label *gpt; struct gpt_entry *e; - struct gpt_guid uuid; char *str, new_u[37], old_u[37]; int rc; @@ -2127,7 +2155,11 @@ int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i) _("New UUID (in 8-4-4-4-12 format)"), &str)) return -EINVAL; - rc = string_to_guid(str, &uuid); + e = &gpt->ents[i]; + guid_to_string(&e->partition_guid, old_u); + + rc = gpt_entry_set_uuid(e, str); + free(str); if (rc) { @@ -2135,12 +2167,8 @@ int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i) return rc; } - e = &gpt->ents[i]; + guid_to_string(&e->partition_guid, new_u); - guid_to_string(&e->partition_guid, old_u); - guid_to_string(&uuid, new_u); - - e->partition_guid = uuid; gpt_recompute_crc(gpt->pheader, gpt->ents); gpt_recompute_crc(gpt->bheader, gpt->ents); fdisk_label_set_changed(cxt->label, 1); @@ -2155,8 +2183,7 @@ int fdisk_gpt_partition_set_name(struct fdisk_context *cxt, size_t i) { struct fdisk_gpt_label *gpt; struct gpt_entry *e; - char *str, *old, name[GPT_PART_NAME_LEN] = { 0 }; - size_t sz; + char *str, *old; assert(cxt); assert(cxt->label); @@ -2175,15 +2202,7 @@ int fdisk_gpt_partition_set_name(struct fdisk_context *cxt, size_t i) e = &gpt->ents[i]; old = encode_to_utf8((unsigned char *)e->name, sizeof(e->name)); - sz = strlen(str); - if (sz) { - if (sz > GPT_PART_NAME_LEN) - sz = GPT_PART_NAME_LEN; - memcpy(name, str, sz); - } - - for (i = 0; i < GPT_PART_NAME_LEN; i++) - e->name[i] = cpu_to_le16((uint16_t) name[i]); + gpt_entry_set_name(e, str); gpt_recompute_crc(gpt->pheader, gpt->ents); gpt_recompute_crc(gpt->bheader, gpt->ents); |