]> err.no Git - util-linux/commitdiff
libblkid: add blkid_partlist_get_table()
authorKarel Zak <kzak@redhat.com>
Fri, 23 Apr 2010 11:35:20 +0000 (13:35 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 23 Apr 2010 11:35:20 +0000 (13:35 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
shlibs/blkid/samples/partitions.c
shlibs/blkid/src/blkid.h.in
shlibs/blkid/src/blkid.sym
shlibs/blkid/src/partitions/partitions.c

index c9587fe544defbf8c792fc1ac45f50bb262f42a0..db1b5ff2ee5d58c39da04a2c98024c8c0eedd0a0 100644 (file)
@@ -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 <device|file>  "
@@ -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;
 }
index fc744b13a5c2b8ce361092627795a6e0896f94f7..7fa2fde6bd06d5fa34526f364ab6639f167cf73d 100644 (file)
@@ -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);
 
index e6a1e36e075d0a6444f7a971085eedea733b5c9b..4085d2cee5f5f0c43119e66bb72d762a449880d7 100644 (file)
@@ -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;
index 259284d84026671b45d705580122944bc36e29d9..9c151ef627db60d147f5a0b98348c628e9134dd1 100644 (file)
@@ -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.
  */