]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add asktype "string" and support UUID partition change
authorKarel Zak <kzak@redhat.com>
Wed, 15 May 2013 11:33:24 +0000 (13:33 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:46:54 +0000 (16:46 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/ask.c
libfdisk/src/fdiskP.h
libfdisk/src/gpt.c
libfdisk/src/libfdisk.h

index 0127d645a07085dc2157de0cb2e85b871a326148..9beccb4bfc9ddc521601b68c7b3d5c20a037230f 100644 (file)
@@ -368,6 +368,52 @@ int fdisk_ask_number(struct fdisk_context *cxt,
        return rc;
 }
 
+char *fdisk_ask_string_get_result(struct fdisk_ask *ask)
+{
+       assert(ask);
+       assert(fdisk_is_ask(ask, STRING));
+       return ask->data.str.result;
+}
+
+/*
+ * The @result has to be poiter to the allocated buffer.
+ */
+int fdisk_ask_string_set_result(struct fdisk_ask *ask, char *result)
+{
+       assert(ask);
+       ask->data.str.result = result;
+       return 0;
+}
+
+/*
+ * Don't forget to deallocate @result.
+ */
+int fdisk_ask_string(struct fdisk_context *cxt,
+                    const char *query,
+                    char **result)
+{
+       struct fdisk_ask *ask;
+       int rc;
+
+       assert(cxt);
+
+       ask = fdisk_new_ask();
+       if (!ask)
+               return -ENOMEM;
+
+       rc = fdisk_ask_set_type(ask, FDISK_ASKTYPE_STRING);
+       if (!rc)
+               fdisk_ask_set_query(ask, query);
+       if (!rc)
+               rc = fdisk_do_ask(cxt, ask);
+       if (!rc)
+               *result = fdisk_ask_string_get_result(ask);
+
+       fdisk_free_ask(ask);
+       DBG(ASK, dbgprint("result: %s [rc=%d]\n", *result, rc));
+       return rc;
+}
+
 int fdisk_ask_yesno(struct fdisk_context *cxt,
                     const char *query,
                     int *result)
index e05bb86287d0e0d77522a272cb317103eadba984..1114f8cf106636f19778ace2c06d2def41aaa963 100644 (file)
@@ -241,6 +241,10 @@ struct fdisk_ask {
                struct ask_yesno {
                        int             result;         /* TRUE or FALSE */
                } yesno;
+               /* FDISK_ASKTYPE_STRING */
+               struct ask_string {
+                       char            *result;        /* allocated */
+               } str;
                /* FDISK_ASKTYPE_TABLE, see include/tt.h  */
                struct tt *table;
        } data;
index b9fec81943d034b83ddc18a8f407bf054b6f2421..85c9bcadfc2fd2dd9ec1367b242d0c37c70f21d8 100644 (file)
@@ -1834,6 +1834,49 @@ static int gpt_get_partition_status(
        return 0;
 }
 
+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;
+
+       assert(cxt);
+       assert(cxt->label);
+       assert(fdisk_is_disklabel(cxt, GPT));
+
+       DBG(LABEL, dbgprint("UUID change requested parno=%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 UUID (in 8-4-4-4-12 format)"), &str))
+               return -EINVAL;
+
+       rc = string_to_guid(str, &uuid);
+       free(str);
+
+       if (rc)
+               return rc;
+
+       e = &gpt->ents[i];
+
+       guid_to_string(&e->unique_partition_guid, old_u);
+       guid_to_string(&uuid, new_u);
+       fdisk_info(cxt, _("Changing partition UUID from %s to %s"),
+                       old_u, new_u);
+
+       e->unique_partition_guid = uuid;
+       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
index b414ff7bcb680e959e306586b6f3195a7ad3357e..284e24e026558d899519f6e91b9ac688e02f1d34 100644 (file)
@@ -60,7 +60,8 @@ enum {
        FDISK_ASKTYPE_WARNX,
        FDISK_ASKTYPE_INFO,
        FDISK_ASKTYPE_YESNO,
-       FDISK_ASKTYPE_TABLE
+       FDISK_ASKTYPE_TABLE,
+       FDISK_ASKTYPE_STRING
 };
 
 
@@ -141,6 +142,9 @@ extern int fdisk_reset_alignment(struct fdisk_context *cxt);
 extern int fdisk_dos_enable_compatible(struct fdisk_label *lb, int enable);
 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);
+
 /* ask.c */
 #define fdisk_is_ask(a, x) (fdisk_ask_get_type(a) == FDISK_ASKTYPE_ ## x)
 
@@ -178,6 +182,13 @@ extern int fdisk_ask_number(struct fdisk_context *cxt,
                     const char *query,
                     uintmax_t *result);
 
+extern int fdisk_ask_string(struct fdisk_context *cxt,
+                    const char *query,
+                    char **result);
+
+extern char *fdisk_ask_string_get_result(struct fdisk_ask *ask);
+extern int fdisk_ask_string_set_result(struct fdisk_ask *ask, char *result);
+
 extern int fdisk_ask_yesno(struct fdisk_context *cxt, const char *query, int *result);
 extern uint64_t fdisk_ask_yesno_get_result(struct fdisk_ask *ask);
 extern int fdisk_ask_yesno_set_result(struct fdisk_ask *ask, uint64_t result);