]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: remove fdisk_get_partition_type()
authorKarel Zak <kzak@redhat.com>
Thu, 28 Nov 2013 11:56:12 +0000 (12:56 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:12 +0000 (11:35 +0100)
Let's use more generic:

fdisk_get_partition()
        fdisk_partition_get_parttype()

rather than fdisk_get_partition_type().

The patch also improves fdisk_get_partition() semantic to allocate
a new partition struct if the argument is NULL.

Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
libfdisk/src/label.c
libfdisk/src/libfdisk.h
libfdisk/src/partition.c

index a5c1fe63431618339fd12ea65599415650788828..db3b298701627572b628e464d1757abf5f145b69 100644 (file)
@@ -160,7 +160,9 @@ void toggle_dos_compatibility_flag(struct fdisk_context *cxt)
 void change_partition_type(struct fdisk_context *cxt)
 {
        size_t i;
-       struct fdisk_parttype *t = NULL, *org_t = NULL;
+       struct fdisk_parttype *t = NULL;
+       struct fdisk_partition *pa = NULL;
+       const char *old = NULL;
 
        assert(cxt);
        assert(cxt->label);
@@ -168,28 +170,30 @@ void change_partition_type(struct fdisk_context *cxt)
        if (fdisk_ask_partnum(cxt, &i, FALSE))
                return;
 
-       org_t = t = fdisk_get_partition_type(cxt, i);
-       if (!t)
-                fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
-        else {
-               do {
-                       t = ask_partition_type(cxt);
-               } while (!t);
-
-               if (fdisk_set_partition_type(cxt, i, t) == 0)
-                       fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
-                               _("Changed type of partition '%s' to '%s'."),
-                               org_t ? org_t->name : _("Unknown"),
-                                   t ?     t->name : _("Unknown"));
-               else
-                       fdisk_info(cxt,
-                               _("Type of partition %zu is unchanged: %s."),
-                               i + 1,
-                               org_t ? org_t->name : _("Unknown"));
-        }
-
-       fdisk_free_parttype(t);
-       fdisk_free_parttype(org_t);
+       if (fdisk_get_partition(cxt, i, &pa)) {
+               fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
+               return;
+       }
+
+       t = (struct fdisk_parttype *) fdisk_partition_get_type(pa);
+       old = t ? t->name : _("Unknown");
+
+       do {
+               t = ask_partition_type(cxt);
+       } while (!t);
+
+       fdisk_partition_set_type(pa, t);
+
+       if (fdisk_set_partition_type(cxt, i, t) == 0)
+               fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
+                       _("Changed type of partition '%s' to '%s'."),
+                       old, t ? t->name : _("Unknown"));
+       else
+               fdisk_info(cxt,
+                       _("Type of partition %zu is unchanged: %s."),
+                       i + 1, old);
+
+       fdisk_free_partition(pa);
 }
 
 void list_disk_geometry(struct fdisk_context *cxt)
index 0ed7e4ec9acf33d9d1271379e7c0a759b591a4f8..bf2542a16723763b02ad7509bbd7d3ff08715dc2 100644 (file)
@@ -199,7 +199,7 @@ int fdisk_verify_disklabel(struct fdisk_context *cxt)
  * Returns: 0 on success, otherwise, a corresponding error.
  */
 int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
-                       struct fdisk_partition *pa)
+                       struct fdisk_partition **pa)
 {
        int rc;
 
@@ -208,12 +208,17 @@ int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
        if (!cxt->label->op->get_part)
                return -ENOSYS;
 
-       fdisk_reset_partition(pa);
-       pa->cxt = cxt;
-       pa->partno = partno;
-
-       rc = cxt->label->op->get_part(cxt, partno, pa);
-       if (rc == 0 && fdisk_partition_is_used(pa))
+       if (!*pa) {
+               *pa = fdisk_new_partition();
+               if (!*pa)
+                       return -ENOMEM;
+       } else
+               fdisk_reset_partition(*pa);
+       (*pa)->cxt = cxt;
+       (*pa)->partno = partno;
+
+       rc = cxt->label->op->get_part(cxt, partno, *pa);
+       if (rc == 0 && fdisk_partition_is_used(*pa))
                DBG(LABEL, dbgprint("get partition %zu", partno));
        return rc;
 }
@@ -294,11 +299,6 @@ int fdisk_list_partitions(struct fdisk_context *cxt, int *cols, size_t ncols)
                rc = -ENOMEM;
                goto done;
        }
-       pa = fdisk_new_partition();
-       if (!pa) {
-               rc = -ENOMEM;
-               goto done;
-       }
 
        /* define table columns */
        for (j = 0; j < ncols; j++) {
@@ -312,7 +312,7 @@ int fdisk_list_partitions(struct fdisk_context *cxt, int *cols, size_t ncols)
        for (i = 0; i < cxt->label->nparts_max; i++) {
                struct tt_line *ln;
 
-               rc = fdisk_get_partition(cxt, i, pa);
+               rc = fdisk_get_partition(cxt, i, &pa);
                if (rc)
                        continue;
                if (!fdisk_partition_is_used(pa))
@@ -498,23 +498,6 @@ int fdisk_set_disklabel_id(struct fdisk_context *cxt)
        return cxt->label->op->set_id(cxt);
 }
 
-/**
- * fdisk_get_partition_type:
- * @cxt: fdisk context
- * @partnum: partition number
- *
- * Returns partition type or NULL upon failure.
- */
-struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt,
-                                               size_t partnum)
-{
-       if (!cxt || !cxt->label || !cxt->label->op->part_get_type)
-               return NULL;
-
-       DBG(LABEL, dbgprint("partition: %zd: get type", partnum));
-       return cxt->label->op->part_get_type(cxt, partnum);
-}
-
 /**
  * fdisk_set_partition_type:
  * @cxt: fdisk context
index 7f84c3ebf89dc6065aa76a6ec81445b18c68e8ef..d3b646007e735590f7e72f202ad9e592beb006ac 100644 (file)
@@ -153,12 +153,11 @@ extern int fdisk_locate_disklabel(struct fdisk_context *cxt, int n, const char *
 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_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition *pa);
+extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct fdisk_partition **pa);
 
 extern int fdisk_add_partition(struct fdisk_context *cxt, struct fdisk_parttype *t);
 extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum);
 
-extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, size_t partnum);
 extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum,
                             struct fdisk_parttype *t);
 
index b7a63a5e62903867e8fb978442901cf90fadac14..add505fba4599e75001adc6313ccbb062a5e3a57 100644 (file)
@@ -92,6 +92,7 @@ int fdisk_partition_set_type(struct fdisk_partition *pa, struct fdisk_parttype *
 {
        if (!pa)
                return -EINVAL;
+       fdisk_free_parttype(pa->type);
        pa->type = type;
        return 0;
 }