From: Karel Zak Date: Fri, 23 Apr 2010 11:35:20 +0000 (+0200) Subject: libblkid: add blkid_partlist_get_table() X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19f87d1d366694d0ad8b504f7fd88476e3cdc018;p=util-linux libblkid: add blkid_partlist_get_table() Signed-off-by: Karel Zak --- diff --git a/shlibs/blkid/samples/partitions.c b/shlibs/blkid/samples/partitions.c index c9587fe5..db1b5ff2 100644 --- a/shlibs/blkid/samples/partitions.c +++ b/shlibs/blkid/samples/partitions.c @@ -21,7 +21,7 @@ int main(int argc, char *argv[]) char *devname; blkid_probe pr; blkid_partlist ls; - blkid_parttable root_tab = NULL; + blkid_parttable root_tab; if (argc < 2) { fprintf(stderr, "usage: %s " @@ -40,25 +40,32 @@ int main(int argc, char *argv[]) if (!ls) errx(EXIT_FAILURE, "%s: failed to read partitions\n", devname); + /* + * Print info about the primary (root) partition table + */ + root_tab = blkid_partlist_get_table(ls); + if (!root_tab) + errx(EXIT_FAILURE, "%s: does not contains any " + "known partition table\n", devname); + + printf("size: %jd, sector size: %u, PT: %s, offset: %jd\n---\n", + blkid_probe_get_size(pr), + blkid_probe_get_sectorsize(pr), + blkid_parttable_get_type(root_tab), + blkid_parttable_get_offset(root_tab)); + + /* + * List partitions + */ nparts = blkid_partlist_numof_partitions(ls); if (!nparts) - errx(EXIT_FAILURE, "%s: does not contains any " - "known partition table\n", devname); + goto done; for (i = 0; i < nparts; i++) { const char *p; blkid_partition par = blkid_partlist_get_partition(ls, i); blkid_parttable tab = blkid_partition_get_table(par); - if (i == 0) { - root_tab = tab; - printf("size: %llu, sector size: %u, " - "PT: %s, offset: %llu\n---\n", - (unsigned long long) blkid_probe_get_size(pr), - blkid_probe_get_sectorsize(pr), - blkid_parttable_get_type(tab), - (unsigned long long) blkid_parttable_get_offset(tab)); - } printf("#%d: %10llu %10llu 0x%x", blkid_partition_get_partno(par), (unsigned long long) blkid_partition_get_start(par), @@ -66,7 +73,7 @@ int main(int argc, char *argv[]) blkid_partition_get_type(par)); if (root_tab != tab) - /* subpartition */ + /* subpartition (BSD, Minix, ...) */ printf(" (%s)", blkid_parttable_get_type(tab)); p = blkid_partition_get_name(par); @@ -82,7 +89,7 @@ int main(int argc, char *argv[]) putc('\n', stdout); } +done: blkid_free_probe(pr); - return EXIT_SUCCESS; } diff --git a/shlibs/blkid/src/blkid.h.in b/shlibs/blkid/src/blkid.h.in index fc744b13..7fa2fde6 100644 --- a/shlibs/blkid/src/blkid.h.in +++ b/shlibs/blkid/src/blkid.h.in @@ -274,6 +274,7 @@ extern int blkid_probe_set_partitions_flags(blkid_probe pr, int flags); extern blkid_partlist blkid_probe_get_partitions(blkid_probe pr); extern int blkid_partlist_numof_partitions(blkid_partlist ls); +extern blkid_parttable blkid_partlist_get_table(blkid_partlist ls); extern blkid_partition blkid_partlist_get_partition(blkid_partlist ls, int n); extern blkid_partition blkid_partlist_devno_to_partition(blkid_partlist ls, dev_t devno); diff --git a/shlibs/blkid/src/blkid.sym b/shlibs/blkid/src/blkid.sym index e6a1e36e..4085d2ce 100644 --- a/shlibs/blkid/src/blkid.sym +++ b/shlibs/blkid/src/blkid.sym @@ -122,6 +122,7 @@ BLKID_2.18 { global: blkid_partition_get_flags; blkid_partlist_devno_to_partition; + blkid_partlist_get_table; blkid_probe_all_removable; blkid_probe_get_fd; blkid_probe_get_offset; diff --git a/shlibs/blkid/src/partitions/partitions.c b/shlibs/blkid/src/partitions/partitions.c index 259284d8..9c151ef6 100644 --- a/shlibs/blkid/src/partitions/partitions.c +++ b/shlibs/blkid/src/partitions/partitions.c @@ -278,9 +278,6 @@ int blkid_probe_filter_partitions_type(blkid_probe pr, int flag, char *names[]) * use more blkid_partlist objects in the same time you have to create * more blkid_probe handlers (see blkid_new_probe()). * - * TODO: add blkid_ref() and blkid_unref() to allows to use blkid_partlist - * independently on libblkid probing stuff. - * * Returns: list of partitions, or NULL in case of error. */ blkid_partlist blkid_probe_get_partitions(blkid_probe pr) @@ -607,9 +604,10 @@ static int partitions_probe(blkid_probe pr, struct blkid_chain *chn) name = idinfos[i]->name; /* all checks passed */ - blkid_probe_set_value(pr, "PTTYPE", - (unsigned char *) name, strlen(name) + 1); - + if (!chn->binary) + blkid_probe_set_value(pr, "PTTYPE", + (unsigned char *) name, + strlen(name) + 1); DBG(DEBUG_LOWPROBE, printf("<-- leaving probing loop (type=%s) [PARTS idx=%d]\n", name, chn->idx)); @@ -878,6 +876,22 @@ int blkid_partlist_numof_partitions(blkid_partlist ls) return ls ? ls->nparts : -1; } +/** + * blkid_partlist_get_table: + * + * Returns top-level partition table or NULL of there is not a partition table + * on the device. + */ +blkid_parttable blkid_partlist_get_table(blkid_partlist ls) +{ + if (!ls || list_empty(&ls->l_tabs)) + return NULL; + + return list_entry(ls->l_tabs.next, + struct blkid_struct_parttable, t_tabs); +} + + /** * blkid_partlist_get_partition: * @ls: partitions list @@ -885,11 +899,8 @@ int blkid_partlist_numof_partitions(blkid_partlist ls) * * It's possible that the list of partitions is *empty*, but there is a valid * partition table on the disk. This happen when on-disk details about - * partitions are unknown, but we are able to detect partition table magic - * string only. The nice example is AIX. If your question is: "Is there any - * partition table?", use: - * - * blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL); + * partitions are unknown or the partition table is empty. The nice example is + * AIX. See also blkid_partlist_get_table(). * * Returns: partition object or NULL in case or error. */