diff options
author | Karel Zak | 2014-08-14 12:01:38 +0200 |
---|---|---|
committer | Karel Zak | 2014-08-14 12:01:38 +0200 |
commit | 5ab37600712d476156160ba7fe1b5d51e52b33cd (patch) | |
tree | 179314c95398b411a7ae3b4fd9ef4cdff3f4988f | |
parent | libfdisk: (gpt) allow to specify attr bit by API (diff) | |
download | kernel-qcow2-util-linux-5ab37600712d476156160ba7fe1b5d51e52b33cd.tar.gz kernel-qcow2-util-linux-5ab37600712d476156160ba7fe1b5d51e52b33cd.tar.xz kernel-qcow2-util-linux-5ab37600712d476156160ba7fe1b5d51e52b33cd.zip |
libfdisk: move get_parttypes to label API
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | disk-utils/cfdisk.c | 14 | ||||
-rw-r--r-- | disk-utils/fdisk.c | 3 | ||||
-rw-r--r-- | libfdisk/src/label.c | 71 | ||||
-rw-r--r-- | libfdisk/src/libfdisk.h | 5 | ||||
-rw-r--r-- | libfdisk/src/parttype.c | 23 |
5 files changed, 70 insertions, 46 deletions
diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index b86b609ef..e38052591 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -1509,25 +1509,27 @@ static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf, { struct cfdisk_menuitem *d, *cm; size_t i = 0, nitems, idx = 0; - struct fdisk_parttype *t = NULL; + struct fdisk_parttype *types, *t = NULL; + struct fdisk_label *lb; int has_typestr = 0; DBG(UI, ul_debug("asking for parttype.")); + lb = fdisk_get_label(cf->cxt, NULL); + /* create cfdisk menu according to label types, note that the * last cm[] item has to be empty -- so nitems + 1 */ - nitems = cf->cxt->label->nparttypes; - if (!nitems) + if (fdisk_label_get_parttypes(lb, &types, &nitems) || !nitems) return NULL; + cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem)); if (!cm) return NULL; - has_typestr = cf->cxt->label->parttypes[0].typestr && - *cf->cxt->label->parttypes[0].typestr; + has_typestr = fdisk_label_is_parttype_string(lb); for (i = 0; i < nitems; i++) { - struct fdisk_parttype *x = &cf->cxt->label->parttypes[i]; + struct fdisk_parttype *x = &types[i]; char *name; if (!x || !x->name) diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index ed008e703..6883b6c41 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -354,11 +354,12 @@ int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask, struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt) { const char *q; + struct fdisk_label *lb = fdisk_get_label(cxt, NULL); if (!cxt || !cxt->label || !cxt->label->nparttypes) return NULL; - q = fdisk_is_parttype_string(cxt) ? + q = fdisk_label_is_parttype_string(lb) ? _("Partition type (type L to list all types): ") : _("Hex code (type L to list all codes): "); do { diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index 186ffba6b..7cd48d66a 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -54,6 +54,43 @@ const char *fdisk_label_get_name(struct fdisk_label *lb) } /** + * fdisk_label_get_parttypes: + * @lb: label + * @types: returns array with supported partition types + * @ntypes: returns number of types + * + * Returns: 0 on success, <0 on error. + */ +int fdisk_label_get_parttypes(struct fdisk_label *lb, + struct fdisk_parttype **types, + size_t *ntypes) +{ + if (!lb) + return -EINVAL; + if (types) + *types = lb->parttypes; + if (ntypes) + *ntypes = lb->nparttypes; + return 0; +} + +/** + * fdisk_label_is_parttype_string: + * @lb: label + * + * Returns: 1 if the label uses strings as partition type + * identifiers (e.g. GPT UUIDS) or 0. + */ +int fdisk_label_is_parttype_string(struct fdisk_label *lb) +{ + assert(lb); + + if (lb->parttypes && lb->parttypes[0].typestr) + return 1; + return 0; +} + +/** * fdisk_label_require_geometry: * @lb: label * @@ -176,8 +213,6 @@ int fdisk_write_disklabel(struct fdisk_context *cxt) return cxt->label->op->write(cxt); } - - /** * fdisk_verify_disklabel: * @cxt: fdisk context @@ -198,8 +233,6 @@ int fdisk_verify_disklabel(struct fdisk_context *cxt) return cxt->label->op->verify(cxt); } - - /** * fdisk_list_disklabel: * @cxt: fdisk context @@ -267,7 +300,20 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name) return cxt->label->op->create(cxt); } - +/** + * fdisk_locate_disklabel: + * @cxt: context + * @n: N item + * @name: return item name + * @offset: return offset where is item + * @size: of the item + * + * Locate disklabel and returns info about @n item of the label. For example + * GPT is composed from two items, PMBR and GPT, n=0 return offset to PMBR and n=1 + * return offset to GPT. For more details see 'D' expect fdisk command. + * + * Returns: 0 on succes, <0 on error, 1 no more items. + */ int fdisk_locate_disklabel(struct fdisk_context *cxt, int n, const char **name, off_t *offset, size_t *size) { @@ -300,7 +346,7 @@ int fdisk_get_disklabel_id(struct fdisk_context *cxt, char **id) } /** - * fdisk_get_disklabel_id: + * fdisk_set_disklabel_id: * @cxt: fdisk context * * Returns 0 on success, otherwise, a corresponding error. @@ -337,19 +383,6 @@ int fdisk_set_partition_type(struct fdisk_context *cxt, return cxt->label->op->part_set_type(cxt, partnum, t); } -/** - * fdisk_get_nparttypes: - * @cxt: fdisk context - * - * Returns: number of partition types supported by the current label - */ -size_t fdisk_get_nparttypes(struct fdisk_context *cxt) -{ - if (!cxt || !cxt->label) - return 0; - - return cxt->label->nparttypes; -} /** * fdisk_partition_taggle_flag: diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 8febbdf55..cf65ddaee 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -129,7 +129,6 @@ extern struct fdisk_parttype *fdisk_parse_parttype(struct fdisk_context *cxt, co extern struct fdisk_parttype *fdisk_new_unknown_parttype(unsigned int type, const char *typestr); extern void fdisk_free_parttype(struct fdisk_parttype *type); -extern size_t fdisk_get_nparttypes(struct fdisk_context *cxt); extern int fdisk_is_parttype_string(struct fdisk_context *cxt); @@ -180,6 +179,10 @@ extern int fdisk_delete_partition(struct fdisk_context *cxt, size_t partnum); extern int fdisk_set_partition_type(struct fdisk_context *cxt, size_t partnum, struct fdisk_parttype *t); +extern int fdisk_label_get_parttypes(struct fdisk_label *lb, + struct fdisk_parttype **types, + size_t *ntypes); +extern int fdisk_label_is_parttype_string(struct fdisk_label *lb); extern int fdisk_label_get_fields_ids( struct fdisk_label *lb, diff --git a/libfdisk/src/parttype.c b/libfdisk/src/parttype.c index 5535e2fb3..5d6f5d00c 100644 --- a/libfdisk/src/parttype.c +++ b/libfdisk/src/parttype.c @@ -20,7 +20,7 @@ struct fdisk_parttype *fdisk_get_parttype_from_code( { size_t i; - if (!fdisk_get_nparttypes(cxt)) + if (!cxt->label->nparttypes) return NULL; for (i = 0; i < cxt->label->nparttypes; i++) @@ -45,7 +45,7 @@ struct fdisk_parttype *fdisk_get_parttype_from_string( { size_t i; - if (!fdisk_get_nparttypes(cxt)) + if (!cxt->label->nparttypes) return NULL; for (i = 0; i < cxt->label->nparttypes; i++) @@ -107,7 +107,7 @@ struct fdisk_parttype *fdisk_parse_parttype( unsigned int code = 0; char *typestr = NULL, *end = NULL; - if (!fdisk_get_nparttypes(cxt)) + if (!cxt->label->nparttypes) return NULL; DBG(CXT, ul_debugobj(cxt, "parsing '%s' partition type", str)); @@ -138,7 +138,7 @@ struct fdisk_parttype *fdisk_parse_parttype( errno = 0; i = strtol(str, &end, 0); if (errno == 0 && *end == '\0' && i > 0 - && i - 1 < (int) fdisk_get_nparttypes(cxt)) { + && i - 1 < (int) cxt->label->nparttypes) { ret = &types[i - 1]; goto done; } @@ -165,19 +165,4 @@ void fdisk_free_parttype(struct fdisk_parttype *t) } } -/** - * fdisk_is_parttype_string: - * @cxt: context - * - * Returns: 1 if the current label uses strings as partition type - * identifiers (e.g. GPT UUIDS) or 0. - */ -int fdisk_is_parttype_string(struct fdisk_context *cxt) -{ - assert(cxt); - assert(cxt->label); - if (cxt->label->parttypes && cxt->label->parttypes[0].typestr) - return 1; - return 0; -} |