From 1054699ccd358a42c4869e523b4835151f4c4804 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 21 May 2013 16:12:10 +0200 Subject: [PATCH] libfdisk: support GPT partition name (label) change Signed-off-by: Karel Zak --- fdisks/fdisk-menu.c | 3 +-- libfdisk/src/gpt.c | 54 +++++++++++++++++++++++++++++++++++------ libfdisk/src/libfdisk.h | 1 + 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/fdisks/fdisk-menu.c b/fdisks/fdisk-menu.c index 0ea3dc1d31..474a06af36 100644 --- a/fdisks/fdisk-menu.c +++ b/fdisks/fdisk-menu.c @@ -381,8 +381,7 @@ static int gpt_menu_cb(struct fdisk_context *cxt, rc = fdisk_gpt_partition_set_uuid(cxt, n); break; case 'n': - /* not implemented yet - rc = fdisk_gpt_partition_set_name(cxt, n);*/ + rc = fdisk_gpt_partition_set_name(cxt, n); break; } return rc; diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index 4a992f0c38..9cce053f8c 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1566,12 +1566,6 @@ static int gpt_create_new_partition(struct fdisk_context *cxt, gpt_entry_set_type(e, type); - /* deal with partition name - for (i = 0; i < GPT_PART_NAME_LEN; i++) - e->name[i] = - cpu_to_le16((uint16_t) gpt_sys_types[sys].name[i]); - */ - /* * Any time a new partition entry is created a new GUID must be * generated for that partition, and every partition is guaranteed @@ -1875,7 +1869,7 @@ int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i) assert(cxt->label); assert(fdisk_is_disklabel(cxt, GPT)); - DBG(LABEL, dbgprint("UUID change requested parno=%zd", i)); + DBG(LABEL, dbgprint("UUID change requested partno=%zd", i)); gpt = self_label(cxt); @@ -1907,6 +1901,52 @@ int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i) return 0; } +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; + + assert(cxt); + assert(cxt->label); + assert(fdisk_is_disklabel(cxt, GPT)); + + DBG(LABEL, dbgprint("NAME change requested partno=%zd", i)); + + gpt = self_label(cxt); + + if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) + return -EINVAL; + + if (fdisk_ask_string(cxt, _("New name"), &str)) + return -EINVAL; + + e = &gpt->ents[i]; + old = encode_to_utf8((unsigned char *)e->name, sizeof(e->name)); + + fdisk_info(cxt, _("Changing partition name from '%s' to '%.*s'"), + old, GPT_PART_NAME_LEN, str); + sz = strlen(str); + if (sz) { + if (sz > GPT_PART_NAME_LEN) + sz = GPT_PART_NAME_LEN; + memcpy(name, str, sz); + } + free(str); + free(old); + + for (i = 0; i < GPT_PART_NAME_LEN; i++) + e->name[i] = cpu_to_le16((uint16_t) name[i]); + + gpt_recompute_crc(gpt->pheader, gpt->ents); + gpt_recompute_crc(gpt->bheader, gpt->ents); + + fdisk_label_set_changed(cxt->label, 1); + return 0; +} + + /* * Deinitialize fdisk-specific variables */ diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 284e24e026..049420d7ea 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -144,6 +144,7 @@ extern int fdisk_dos_is_compatible(struct fdisk_label *lb); /* gpt */ extern int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i); +extern int fdisk_gpt_partition_set_name(struct fdisk_context *cxt, size_t i); /* ask.c */ #define fdisk_is_ask(a, x) (fdisk_ask_get_type(a) == FDISK_ASKTYPE_ ## x) -- 2.47.2