From: Karel Zak Date: Thu, 11 Sep 2014 11:53:35 +0000 (+0200) Subject: libfdisk: (gpt) use generic API to change UUID and name X-Git-Tag: v2.26-rc1~382 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6936c081aba8cc590fdebfcc38574f09aecb5503;p=thirdparty%2Futil-linux.git libfdisk: (gpt) use generic API to change UUID and name Signed-off-by: Karel Zak --- diff --git a/disk-utils/fdisk-menu.c b/disk-utils/fdisk-menu.c index e9bdc45542..c973833a04 100644 --- a/disk-utils/fdisk-menu.c +++ b/disk-utils/fdisk-menu.c @@ -563,6 +563,7 @@ static int gpt_menu_cb(struct fdisk_context **cxt0, { struct fdisk_context *cxt = *cxt0; struct fdisk_context *mbr; + struct fdisk_partition *pa = NULL; size_t n; int rc = 0; @@ -594,10 +595,34 @@ static int gpt_menu_cb(struct fdisk_context **cxt0, switch(ent->key) { case 'u': - rc = fdisk_gpt_partition_set_uuid(cxt, n); + pa = fdisk_new_partition(); /* new template */ + if (!pa) + rc = -ENOMEM; + else { + char *str = NULL; + rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str); + if (!rc) + rc = fdisk_partition_set_uuid(pa, str); + if (!rc) + rc = fdisk_set_partition(cxt, n, pa); + free(str); + fdisk_unref_partition(pa); + } break; case 'n': - rc = fdisk_gpt_partition_set_name(cxt, n); + pa = fdisk_new_partition(); /* new template */ + if (!pa) + rc = -ENOMEM; + else { + char *str = NULL; + rc = fdisk_ask_string(cxt, _("New name"), &str); + if (!rc) + rc = fdisk_partition_set_name(pa, str); + if (!rc) + rc = fdisk_set_partition(cxt, n, pa); + free(str); + fdisk_unref_partition(pa); + } break; case 'A': rc = fdisk_partition_toggle_flag(cxt, n, GPT_FLAG_LEGACYBOOT); @@ -613,6 +638,7 @@ static int gpt_menu_cb(struct fdisk_context **cxt0, break; } } + return rc; } diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index ee91b8981d..469e36e154 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1440,14 +1440,28 @@ static int gpt_set_partition(struct fdisk_context *cxt, size_t n, e = &gpt->ents[n]; if (pa->uuid) { + char new_u[37], old_u[37]; + + guid_to_string(&e->partition_guid, old_u); rc = gpt_entry_set_uuid(e, pa->uuid); if (rc) return rc; + guid_to_string(&e->partition_guid, new_u); + fdisk_sinfo(cxt, FDISK_INFO_SUCCESS, + _("Partition UUID changed from %s to %s."), + old_u, new_u); } - if (pa->name) + if (pa->name) { + char *old = encode_to_utf8((unsigned char *)e->name, sizeof(e->name)); gpt_entry_set_name(e, pa->name); + fdisk_sinfo(cxt, FDISK_INFO_SUCCESS, + _("Partition name changed from '%s' to '%.*s'."), + old, (int) GPT_PART_NAME_LEN, pa->name); + free(old); + } + if (pa->type && pa->type->typestr) { struct gpt_guid typeid; @@ -2181,91 +2195,6 @@ static int gpt_part_is_used(struct fdisk_context *cxt, size_t i) return !partition_unused(e) || gpt_partition_start(e); } -int fdisk_gpt_partition_set_uuid(struct fdisk_context *cxt, size_t i) -{ - struct fdisk_gpt_label *gpt; - struct gpt_entry *e; - char *str, new_u[37], old_u[37]; - int rc; - - assert(cxt); - assert(cxt->label); - assert(fdisk_is_label(cxt, GPT)); - - DBG(LABEL, ul_debug("UUID change requested partno=%zu", i)); - - gpt = self_label(cxt); - - if ((uint32_t) i >= le32_to_cpu(gpt->pheader->npartition_entries)) - return -EINVAL; - - if (fdisk_ask_string(cxt, - _("New UUID (in 8-4-4-4-12 format)"), &str)) - return -EINVAL; - - e = &gpt->ents[i]; - guid_to_string(&e->partition_guid, old_u); - - rc = gpt_entry_set_uuid(e, str); - - free(str); - - if (rc) { - fdisk_warnx(cxt, _("Failed to parse your UUID.")); - return rc; - } - - guid_to_string(&e->partition_guid, new_u); - - gpt_recompute_crc(gpt->pheader, gpt->ents); - gpt_recompute_crc(gpt->bheader, gpt->ents); - fdisk_label_set_changed(cxt->label, 1); - - fdisk_sinfo(cxt, FDISK_INFO_SUCCESS, - _("Partition UUID changed from %s to %s."), - old_u, new_u); - 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; - - assert(cxt); - assert(cxt->label); - assert(fdisk_is_label(cxt, GPT)); - - DBG(LABEL, ul_debug("NAME change requested partno=%zu", 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)); - - gpt_entry_set_name(e, str); - - gpt_recompute_crc(gpt->pheader, gpt->ents); - gpt_recompute_crc(gpt->bheader, gpt->ents); - - fdisk_label_set_changed(cxt->label, 1); - - fdisk_sinfo(cxt, FDISK_INFO_SUCCESS, - _("Partition name changed from '%s' to '%.*s'."), - old, (int) GPT_PART_NAME_LEN, str); - free(str); - free(old); - - return 0; -} - int fdisk_gpt_is_hybrid(struct fdisk_context *cxt) { assert(cxt); diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 099bfca1bb..2404d6758a 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -396,8 +396,6 @@ enum { GPT_FLAG_GUIDSPECIFIC }; -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); extern int fdisk_gpt_is_hybrid(struct fdisk_context *cxt); /* dos.c */