From: Karel Zak Date: Wed, 13 Aug 2014 22:27:28 +0000 (+0200) Subject: libfdisk: make it possible to get fields for all labes X-Git-Tag: v2.26-rc1~515 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11ee1177ffa723200f77cbb640da49122ef04ec9;p=thirdparty%2Futil-linux.git libfdisk: make it possible to get fields for all labes Signed-off-by: Karel Zak --- diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index 7f02c06256..b86b609efb 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -212,7 +212,7 @@ static int cols_init(struct cfdisk *cf) cf->fields = NULL; cf->nfields = 0; - return fdisk_get_fields_ids(cf->cxt, 0, &cf->fields, &cf->nfields); + return fdisk_label_get_fields_ids(NULL, cf->cxt, &cf->fields, &cf->nfields); } static void resize(void) diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index 4a9262905f..ed008e7038 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -554,7 +554,7 @@ void list_disklabel(struct fdisk_context *cxt) if (fdisk_get_partitions(cxt, &tb) || fdisk_table_get_nents(tb) <= 0) goto done; - if (fdisk_get_fields_ids(cxt, 0, &ids, &nids)) + if (fdisk_label_get_fields_ids(NULL, cxt, &ids, &nids)) goto done; itr = fdisk_new_iter(FDISK_ITER_FORWARD); diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index c69bc170a1..186ffba6b5 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -66,66 +66,50 @@ int fdisk_label_require_geometry(struct fdisk_label *lb) return lb->flags & FDISK_LABEL_FL_REQUIRE_GEOMETRY ? 1 : 0; } - /** - * fdisk_write_disklabel: - * @cxt: fdisk context - * - * Write in-memory changes to disk - * - * Returns 0 on success, otherwise, a corresponding error. - */ -int fdisk_write_disklabel(struct fdisk_context *cxt) -{ - if (!cxt || !cxt->label || cxt->readonly) - return -EINVAL; - if (!cxt->label->op->write) - return -ENOSYS; - return cxt->label->op->write(cxt); -} - - -/** - * fdisk_get_fields: - * @cxt: fdisk context - * @all: 1 or 0 + * fdisk_label_get_fields_ids + * @lb: label (or NULL for the current label) * @ids: returns allocated array with FDISK_FIELD_* IDs * @nids: returns number of items in fields * - * This function returns the default or all fields for the current label. - * Note that the set of the default fields depends on - * fdisk_enable_details() function. If the details are enabled then - * this function usually returns more fields. + * This function returns the default fields for the label. + * + * Note that the set of the default fields depends on fdisk_enable_details() + * function. If the details are enabled then this function usually returns more + * fields. * * Returns 0 on success, otherwise, a corresponding error. */ -int fdisk_get_fields_ids(struct fdisk_context *cxt, int all, - int **ids, size_t *nids) +int fdisk_label_get_fields_ids( + struct fdisk_label *lb, + struct fdisk_context *cxt, + int **ids, size_t *nids) { size_t i, n; int *c; assert(cxt); - if (!cxt->label) + if (!lb) + lb = cxt->label; + if (!lb) return -EINVAL; - if (!cxt->label->fields || !cxt->label->nfields) + if (!lb->fields || !lb->nfields) return -ENOSYS; - c = calloc(cxt->label->nfields, sizeof(int)); + c = calloc(lb->nfields, sizeof(int)); if (!c) return -ENOMEM; - for (n = 0, i = 0; i < cxt->label->nfields; i++) { - int id = cxt->label->fields[i].id; + for (n = 0, i = 0; i < lb->nfields; i++) { + int id = lb->fields[i].id; - if (!all && - ((fdisk_is_details(cxt) && - (cxt->label->fields[i].flags & FDISK_FIELDFL_EYECANDY)) + if ((fdisk_is_details(cxt) && + (lb->fields[i].flags & FDISK_FIELDFL_EYECANDY)) || (!fdisk_is_details(cxt) && - (cxt->label->fields[i].flags & FDISK_FIELDFL_DETAIL)) + (lb->fields[i].flags & FDISK_FIELDFL_DETAIL)) || (id == FDISK_FIELD_SECTORS && fdisk_use_cylinders(cxt)) || (id == FDISK_FIELD_CYLINDERS && - !fdisk_use_cylinders(cxt)))) + !fdisk_use_cylinders(cxt))) continue; c[n++] = id; @@ -174,6 +158,26 @@ int fdisk_field_is_number(const struct fdisk_field *field) return field->flags ? field->flags & FDISK_FIELDFL_NUMBER : 0; } + +/** + * fdisk_write_disklabel: + * @cxt: fdisk context + * + * Write in-memory changes to disk + * + * Returns 0 on success, otherwise, a corresponding error. + */ +int fdisk_write_disklabel(struct fdisk_context *cxt) +{ + if (!cxt || !cxt->label || cxt->readonly) + return -EINVAL; + if (!cxt->label->op->write) + return -ENOSYS; + return cxt->label->op->write(cxt); +} + + + /** * fdisk_verify_disklabel: * @cxt: fdisk context diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 1db04b7796..3d9d58efd4 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -181,8 +181,10 @@ extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum, struct fdisk_parttype *t); -extern int fdisk_get_fields_ids(struct fdisk_context *cxt, int all, - int **ids, size_t *nids); +extern int fdisk_label_get_fields_ids( + struct fdisk_label *lb, + struct fdisk_context *cxt, + int **ids, size_t *nids); extern const struct fdisk_field *fdisk_label_get_field(struct fdisk_label *lb, int id); extern int fdisk_field_get_id(const struct fdisk_field *fl);