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);
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
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;
+}
+
* 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.
*/
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;
+}
+