From: Karel Zak Date: Thu, 6 Dec 2012 14:13:23 +0000 (+0100) Subject: libfdisk: add probing function X-Git-Tag: v2.23-rc1~160 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7ce10975339ca231953050a6cb6f518d88e64f28;p=thirdparty%2Futil-linux.git libfdisk: add probing function Signed-off-by: Karel Zak --- diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index 91855a66aa..c8d5aab914 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -63,17 +63,8 @@ enum failure { /* * labels */ -extern const struct fdisk_label aix_label; -extern const struct fdisk_label dos_label; -extern const struct fdisk_label bsd_label; -extern const struct fdisk_label mac_label; -extern const struct fdisk_label sun_label; -extern const struct fdisk_label sgi_label; -extern const struct fdisk_label gpt_label; - extern struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int readonly); extern void fdisk_free_context(struct fdisk_context *cxt); -extern int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name); extern struct fdisk_parttype *fdisk_get_partition_type(struct fdisk_context *cxt, int partnum); extern int fdisk_set_partition_type(struct fdisk_context *cxt, int partnum, struct fdisk_parttype *t); diff --git a/fdisks/utils.c b/fdisks/utils.c index 5f4aaccb3f..3a6332f2ea 100644 --- a/fdisks/utils.c +++ b/fdisks/utils.c @@ -35,86 +35,6 @@ int fdisk_debug_mask; -/* - * Label probing functions. - */ -static const struct fdisk_label *labels[] = -{ - &gpt_label, - &dos_label, - &sun_label, - &sgi_label, - &aix_label, - &bsd_label, - &mac_label, -}; - - -static int __probe_labels(struct fdisk_context *cxt) -{ - size_t i; - - cxt->disklabel = FDISK_DISKLABEL_ANY; - - for (i = 0; i < ARRAY_SIZE(labels); i++) { - if (!labels[i]->probe || labels[i]->probe(cxt) != 1) - continue; - - cxt->label = labels[i]; - - DBG(LABEL, dbgprint("detected a %s label", cxt->label->name)); - return 0; - } - - return 1; /* not found */ -} - -/** - * fdisk_create_disklabel: - * @cxt: fdisk context - * @name: label name - * - * Creates a new disk label of type @name. If @name is NULL, then it - * will create a default system label type, either SUN or DOS. - * - * Returns 0 on success, otherwise, a corresponding error. - */ -int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name) -{ - if (!cxt) - return -EINVAL; - - cxt->label = NULL; - - if (!name) { /* use default label creation */ -#ifdef __sparc__ - cxt->label = &sun_label; -#else - cxt->label = &dos_label; -#endif - } else { - size_t i; - - for (i = 0; i < ARRAY_SIZE(labels); i++) { - if (strcmp(name, labels[i]->name) != 0) - continue; - - cxt->label = labels[i]; - DBG(LABEL, dbgprint("changing to %s label\n", cxt->label->name)); - break; - } - } - - if (!cxt->label) - return -EINVAL; - if (!cxt->label->create) - return -ENOSYS; - - fdisk_reset_alignment(cxt); - - return cxt->label->create(cxt); -} - /** * fdisk_new_context: * @fname: path to the device to be handled @@ -156,7 +76,7 @@ struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int rea /* detect labels and apply labes specific stuff (e.g geomery) * to the context */ - __probe_labels(cxt); + fdisk_probe_labels(cxt); fdisk_reset_alignment(cxt); diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index e9c21ca898..eac127ee6c 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -204,4 +204,7 @@ extern int fdisk_discover_topology(struct fdisk_context *cxt); extern void fdisk_zeroize_firstsector(struct fdisk_context *cxt); extern int fdisk_read_firstsector(struct fdisk_context *cxt); +/* label.c */ +extern int fdisk_probe_labels(struct fdisk_context *cxt); + #endif /* _LIBFDISK_PRIVATE_H */ diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index 86c30990f1..fa7218c0f8 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -1,6 +1,51 @@ #include "fdiskP.h" +/* + * Label probing functions. + */ +extern const struct fdisk_label aix_label; +extern const struct fdisk_label dos_label; +extern const struct fdisk_label bsd_label; +extern const struct fdisk_label mac_label; +extern const struct fdisk_label sun_label; +extern const struct fdisk_label sgi_label; +extern const struct fdisk_label gpt_label; + +static const struct fdisk_label *labels[] = +{ + &gpt_label, + &dos_label, + &sun_label, + &sgi_label, + &aix_label, + &bsd_label, + &mac_label, +}; + +/* + * Don't use this function derectly, use fdisk_new_context_from_filename() + */ +int fdisk_probe_labels(struct fdisk_context *cxt) +{ + size_t i; + + cxt->disklabel = FDISK_DISKLABEL_ANY; + + for (i = 0; i < ARRAY_SIZE(labels); i++) { + if (!labels[i]->probe || labels[i]->probe(cxt) != 1) + continue; + + cxt->label = labels[i]; + + DBG(LABEL, dbgprint("detected a %s label", cxt->label->name)); + return 0; + } + + return 1; /* not found */ +} + + /** * fdisk_dev_has_disklabel: * @cxt: fdisk context @@ -103,3 +148,49 @@ int fdisk_delete_partition(struct fdisk_context *cxt, int partnum) cxt->label->name, partnum)); return cxt->label->part_delete(cxt, partnum); } + +/** + * fdisk_create_disklabel: + * @cxt: fdisk context + * @name: label name + * + * Creates a new disk label of type @name. If @name is NULL, then it + * will create a default system label type, either SUN or DOS. + * + * Returns 0 on success, otherwise, a corresponding error. + */ +int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name) +{ + if (!cxt) + return -EINVAL; + + cxt->label = NULL; + + if (!name) { /* use default label creation */ +#ifdef __sparc__ + cxt->label = &sun_label; +#else + cxt->label = &dos_label; +#endif + } else { + size_t i; + + for (i = 0; i < ARRAY_SIZE(labels); i++) { + if (strcmp(name, labels[i]->name) != 0) + continue; + + cxt->label = labels[i]; + DBG(LABEL, dbgprint("changing to %s label\n", cxt->label->name)); + break; + } + } + + if (!cxt->label) + return -EINVAL; + if (!cxt->label->create) + return -ENOSYS; + + fdisk_reset_alignment(cxt); + + return cxt->label->create(cxt); +}