diff options
author | Karel Zak | 2012-12-06 15:13:23 +0100 |
---|---|---|
committer | Karel Zak | 2013-03-11 11:20:41 +0100 |
commit | 7ce10975339ca231953050a6cb6f518d88e64f28 (patch) | |
tree | c61c70d1876fd1c48b4542045a2e936507c739a9 /libfdisk/src | |
parent | libfdisk: add fdisk_reset_alignment() (diff) | |
download | kernel-qcow2-util-linux-7ce10975339ca231953050a6cb6f518d88e64f28.tar.gz kernel-qcow2-util-linux-7ce10975339ca231953050a6cb6f518d88e64f28.tar.xz kernel-qcow2-util-linux-7ce10975339ca231953050a6cb6f518d88e64f28.zip |
libfdisk: add probing function
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src')
-rw-r--r-- | libfdisk/src/fdiskP.h | 3 | ||||
-rw-r--r-- | libfdisk/src/label.c | 91 |
2 files changed, 94 insertions, 0 deletions
diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index e9c21ca89..eac127ee6 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 86c30990f..fa7218c0f 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); +} |