]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: (gpt) use generic API to change UUID and name
authorKarel Zak <kzak@redhat.com>
Thu, 11 Sep 2014 11:53:35 +0000 (13:53 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 Oct 2014 12:55:31 +0000 (14:55 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/fdisk-menu.c
libfdisk/src/gpt.c
libfdisk/src/libfdisk.h

index e9bdc45542679aa91c11236f3bf0d7833670419a..c973833a041565cfc5b9bad05dd76cccf2d61577 100644 (file)
@@ -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;
 }
 
index ee91b8981dc5a8c999d9928fb1253f5b3451e002..469e36e15476a97ad690db45e27febee67c60bbc 100644 (file)
@@ -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);
index 099bfca1bb41cde599219962e80509a00ce5b98a..2404d6758a1d1445b0980457fee2bdf3cd826f28 100644 (file)
@@ -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 */