From: Karel Zak Date: Fri, 29 Aug 2014 09:18:29 +0000 (+0200) Subject: libfdisk: add fdisk_apply_table() and fdisk_delete_all_partitions() X-Git-Tag: v2.26-rc1~475 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c0e6b1530a089c592f0149a3e976f0d403eefd7;p=thirdparty%2Futil-linux.git libfdisk: add fdisk_apply_table() and fdisk_delete_all_partitions() Signed-off-by: Karel Zak --- diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 676ca5c5b9..716a8e2267 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -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 diff --git a/libfdisk/src/partition.c b/libfdisk/src/partition.c index 822ca23d41..f84968f21b 100644 --- a/libfdisk/src/partition.c +++ b/libfdisk/src/partition.c @@ -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; +} + diff --git a/libfdisk/src/table.c b/libfdisk/src/table.c index 8de8a75426..c6c51c837e 100644 --- a/libfdisk/src/table.c +++ b/libfdisk/src/table.c @@ -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; +} +