We have fdisk_set_disklabel_id(), but it's old ask-API based function.
It's not comfortable if you want to avoid dialog or template.
Addresses: https://github.com/karelzak/util-linux/issues/916
Signed-off-by: Karel Zak <kzak@redhat.com>
fdisk_locate_disklabel
fdisk_reorder_partitions
fdisk_set_disklabel_id
+fdisk_set_disklabel_id_from_string
fdisk_set_partition_type
fdisk_toggle_partition_flag
fdisk_verify_disklabel
return 0;
}
-static int dos_set_disklabel_id(struct fdisk_context *cxt)
+static int dos_set_disklabel_id(struct fdisk_context *cxt, const char *str)
{
- char *end = NULL, *str = NULL;
+ char *str0 = str;
unsigned int id, old;
struct fdisk_dos_label *l;
- int rc;
+ int rc = 0;
assert(cxt);
assert(cxt->label);
l = self_label(cxt);
old = mbr_get_id(cxt->firstsector);
- rc = fdisk_ask_string(cxt,
+
+ if (!str)
+ rc = fdisk_ask_string(cxt,
_("Enter the new disk identifier"), &str);
- if (rc)
- return rc;
+ if (!rc) {
+ char *end = NULL;
- errno = 0;
- id = strtoul(str, &end, 0);
- if (errno || str == end || (end && *end)) {
- fdisk_warnx(cxt, _("Incorrect value."));
- return -EINVAL;
+ errno = 0;
+ id = strtoul(str, &end, 0);
+ if (errno || str == end || (end && *end)) {
+ fdisk_warnx(cxt, _("Incorrect value."));
+ rc = -EINVAL;
+ }
}
+ if (!str0)
+ free(str);
+ if (rc)
+ return -EINVAL;
mbr_set_id(cxt->firstsector, id);
l->non_pt_changed = 1;
/* get details from label */
int (*get_item)(struct fdisk_context *cxt, struct fdisk_labelitem *item);
/* set disk label ID */
- int (*set_id)(struct fdisk_context *cxt);
+ int (*set_id)(struct fdisk_context *cxt, const char *str);
/* new partition */
return rc;
}
-static int gpt_set_disklabel_id(struct fdisk_context *cxt)
+static int gpt_set_disklabel_id(struct fdisk_context *cxt, const char *str)
{
struct fdisk_gpt_label *gpt;
struct gpt_guid uuid;
- char *str, *old, *new;
+ char *old, *new;
int rc;
assert(cxt);
assert(fdisk_is_label(cxt, GPT));
gpt = self_label(cxt);
- if (fdisk_ask_string(cxt,
- _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str))
- return -EINVAL;
-
- rc = string_to_guid(str, &uuid);
- free(str);
+ if (!str) {
+ if (fdisk_ask_string(cxt,
+ _("Enter new disk UUID (in 8-4-4-4-12 format)"), &str))
+ return -EINVAL;
+ rc = string_to_guid(str, &uuid);
+ free(str);
+ } else
+ rc = string_to_guid(str, &uuid);
if (rc) {
fdisk_warnx(cxt, _("Failed to parse your UUID."));
return -ENOSYS;
DBG(CXT, ul_debugobj(cxt, "setting %s disk ID", cxt->label->name));
- return cxt->label->op->set_id(cxt);
+ return cxt->label->op->set_id(cxt, NULL);
+}
+
+/**
+ * fdisk_set_disklabel_id_from_string
+ * @cxt: fdisk context
+ *
+ * Returns: 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_set_disklabel_id_from_string(struct fdisk_context *cxt, const char *str)
+{
+ if (!cxt || !cxt->label || !str)
+ return -EINVAL;
+ if (!cxt->label->op->set_id)
+ return -ENOSYS;
+
+ DBG(CXT, ul_debugobj(cxt, "setting %s disk ID from '%s'", cxt->label->name, str));
+ return cxt->label->op->set_id(cxt, str);
}
/**
extern int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id);
extern int fdisk_set_disklabel_id(struct fdisk_context *cxt);
+extern int fdisk_set_disklabel_id_from_string(struct fdisk_context *cxt, const char *str);
extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition **pa);
extern int fdisk_set_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition *pa);
fdisk_script_set_table;
fdisk_assign_device_by_fd;
} FDISK_2.33;
+FDISK_2.36 {
+ fdisk_set_disklabel_id_from_string;
+} FDISK_2.35;