]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add fdisk_apply_table() and fdisk_delete_all_partitions()
authorKarel Zak <kzak@redhat.com>
Fri, 29 Aug 2014 09:18:29 +0000 (11:18 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 29 Aug 2014 09:18:29 +0000 (11:18 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/libfdisk.h
libfdisk/src/partition.c
libfdisk/src/table.c

index 676ca5c5b9556f1064643afb671f86724a12fe27..716a8e2267857f5fb8ccaf9a505a6c0719027839 100644 (file)
@@ -196,6 +196,7 @@ extern int fdisk_get_partition(struct fdisk_context *cxt, size_t partno, struct
 
 extern int fdisk_add_partition(struct fdisk_context *cxt, struct fdisk_partition *pa);
 extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum);
+extern int fdisk_delete_all_partitions(struct fdisk_context *cxt);
 
 extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum,
                             struct fdisk_parttype *t);
@@ -294,6 +295,7 @@ extern int fdisk_table_next_partition(
 extern struct fdisk_partition *fdisk_table_get_partition(
                        struct fdisk_table *tb,
                        size_t n);
+extern int fdisk_apply_table(struct fdisk_context *cxt, struct fdisk_table *tb);
 
 /* alignment.c */
 #define FDISK_ALIGN_UP         1
index 822ca23d417bd82ac05c7025b1366bb678f9b7a2..f84968f21b5f1356420cf416f3eb83e64f8aaead 100644 (file)
@@ -631,3 +631,31 @@ int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum)
                                cxt->label->name, partnum));
        return cxt->label->op->part_delete(cxt, partnum);
 }
+
+/**
+ * fdisk_delete_all_partitions:
+ * @cxt: fdisk context
+ *
+ * Delete all used partitions.
+ *
+ * Returns: 0 on success, otherwise, a corresponding error.
+ */
+int fdisk_delete_all_partitions(struct fdisk_context *cxt)
+{
+       size_t i;
+
+       if (!cxt || !cxt->label)
+               return -EINVAL;
+
+       for (i = 0; i < cxt->label->nparts_max; i++) {
+
+               if (!fdisk_is_partition_used(cxt, i))
+                       continue;
+               rc = fdisk_delete_partition(cxt, i);
+               if (rc)
+                       break;
+       }
+
+       return rc;
+}
+
index 8de8a754269a99ce58e86bc844bf2fc122ec8a15..c6c51c837ee2cc3644448907e099b91c4bb660f8 100644 (file)
@@ -28,8 +28,9 @@ struct fdisk_table *fdisk_new_table(void)
  * fdisk_reset_table:
  * @tb: tab pointer
  *
- * Removes all entries (filesystems) from the table. The filesystems with zero
- * reference count will be deallocated.
+ * Removes all entries (partitions) from the table. The parititons with zero
+ * reference count will be deallocated. This function does not modify partition
+ * table.
  *
  * Returns: 0 on success or negative number in case of error.
  */
@@ -530,3 +531,34 @@ int fdisk_table_wrong_order(struct fdisk_table *tb)
        return 0;
 }
 
+/**
+ * fdisk_apply_table:
+ * @cxt: context
+ * @tb: table
+ *
+ * Add partitions from table @tb to the in-memory disk label. See
+ * fdisk_add_partition(), fdisk_delete_all_partitions().
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int fdisk_apply_table(struct fdisk_context *cxt, struct fdisk_table *tb)
+{
+       struct fdisk_partition *pa;
+       struct fdisk_iter itr;
+       int rc = 0;
+
+       assert(cxt);
+       assert(tb);
+
+       DBG(TAB, ul_debugobj(tb, "applying to context %p", cxt));
+
+       fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
+       while (tb && fdisk_table_next_partition(tb, &itr, &pa) == 0) {
+               rc = fdisk_add_partition(cxt, pa);
+               if (rc)
+                       break;
+       }
+
+       return rc;
+}
+