]> err.no Git - util-linux/commitdiff
libblkid: add blkid_new_probe_from_filename()
authorKarel Zak <kzak@redhat.com>
Tue, 29 Sep 2009 19:46:30 +0000 (21:46 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 29 Sep 2009 19:46:30 +0000 (21:46 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
shlibs/blkid/docs/libblkid-sections.txt
shlibs/blkid/samples/mkfs.c
shlibs/blkid/samples/partitions.c
shlibs/blkid/samples/topology.c
shlibs/blkid/src/blkid.h
shlibs/blkid/src/blkid.sym
shlibs/blkid/src/blkidP.h
shlibs/blkid/src/probe.c

index af94607e22be112862bfd1f946216a5668a31563..8f643d8b2aef5379b9c106bb0797f17572aa591e 100644 (file)
@@ -39,6 +39,7 @@ blkid_tag_next
 blkid_probe
 blkid_free_probe
 blkid_new_probe
+blkid_new_probe_from_filename
 blkid_probe_get_devno
 blkid_probe_get_sectorsize
 blkid_probe_get_size
index 866ca6e367ca42b0bb415b5c6b50b468eff58ca6..eb9e27f5f11df34cf4af8c4c29c377a9f7ef149c 100644 (file)
@@ -22,7 +22,7 @@
 
 int main(int argc, char *argv[])
 {
-       int fd, rc;
+       int rc;
        char *devname;
        blkid_probe pr;
        blkid_topology tp;
@@ -35,17 +35,10 @@ int main(int argc, char *argv[])
        }
 
        devname = argv[1];
-
-       fd = open(devname, O_RDONLY);
-       if (fd < 0)
-               errx(EXIT_FAILURE, "%s: open() failed", devname);
-
-       pr = blkid_new_probe();
+       pr = blkid_new_probe_from_filename(devname);
        if (!pr)
-               errx(EXIT_FAILURE, "faild to allocate a new libblkid probe");
-
-       if (blkid_probe_set_device(pr, fd, 0, 0) != 0)
-               errx(EXIT_FAILURE, "failed to assign device to libblkid probe");
+               err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
+                               devname);
 
        /*
         * check Filesystems / Partitions overwrite
index 8c0004822a417513e42cdf9547478508ba2ffa6f..c9587fe544defbf8c792fc1ac45f50bb262f42a0 100644 (file)
@@ -17,7 +17,7 @@
 
 int main(int argc, char *argv[])
 {
-       int fd, i, nparts;
+       int i, nparts;
        char *devname;
        blkid_probe pr;
        blkid_partlist ls;
@@ -31,18 +31,10 @@ int main(int argc, char *argv[])
        }
 
        devname = argv[1];
-
-       fd = open(devname, O_RDONLY);
-       if (fd < 0)
-               err(EXIT_FAILURE, "%s: open() failed", devname);
-
-       pr = blkid_new_probe();
+       pr = blkid_new_probe_from_filename(devname);
        if (!pr)
-               errx(EXIT_FAILURE, "failed to allocate a new libblkid probe");
-
-       if (blkid_probe_set_device(pr, fd, 0, 0) != 0)
-               errx(EXIT_FAILURE, "failed to assign device to libblkid probe");
-
+               err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
+                               devname);
        /* Binary interface */
        ls = blkid_probe_get_partitions(pr);
        if (!ls)
index 6d3bc82ef9cfabebc91c47ee9282da3f66b63c38..17fcbbdc8c410237edd4097279157f9235df7d11 100644 (file)
@@ -22,7 +22,7 @@
 
 int main(int argc, char *argv[])
 {
-       int fd, rc;
+       int rc;
        char *devname;
        blkid_probe pr;
        blkid_topology tp;
@@ -35,18 +35,10 @@ int main(int argc, char *argv[])
        }
 
        devname = argv[1];
-
-       fd = open(devname, O_RDONLY);
-       if (fd < 0)
-               err(EXIT_FAILURE, "%s: open() failed", devname);
-
-       pr = blkid_new_probe();
+       pr = blkid_new_probe_from_filename(devname);
        if (!pr)
-               errx(EXIT_FAILURE, "faild to allocate a new libblkid probe");
-
-       if (blkid_probe_set_device(pr, fd, 0, 0) != 0)
-               errx(EXIT_FAILURE, "failed to assign device to libblkid probe");
-
+               err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
+                               devname);
        /*
         * Binary interface
         */
index 9ab99c2ff385bb030d9f4d1ea61b44b0b8672f95..6b495b69969e9dc34aef38ed78ea119f32a7b1fe 100644 (file)
@@ -185,6 +185,7 @@ extern char *blkid_evaluate_tag(const char *token, const char *value,
 
 /* probe.c */
 extern blkid_probe blkid_new_probe(void);
+extern blkid_probe blkid_new_probe_from_filename(const char *filename);
 extern void blkid_free_probe(blkid_probe pr);
 extern void blkid_reset_probe(blkid_probe pr);
 
index 8ac2081aa13fea20cb247fd593b0d8d717f537da..992d4e9fe85e9a031bb536280c4deac80b2727d0 100644 (file)
@@ -74,6 +74,7 @@ global:
        blkid_devno_to_wholedisk;
        blkid_do_fullprobe;
        blkid_known_pttype;
+       blkid_new_probe_from_filename;
        blkid_partition_get_name;
        blkid_partition_get_partno;
        blkid_partition_get_size;
index 86b2b096106bc11a6df9d8ca9fd10975099f04de..2e7a7ed9669f91ed63100cdfbdd5e5bade3f986d 100644 (file)
@@ -198,10 +198,13 @@ struct blkid_struct_probe
        int                     fd;             /* device file descriptor */
        blkid_loff_t            off;            /* begin of data on the device */
        blkid_loff_t            size;           /* end of data on the device */
+
        dev_t                   devno;          /* device number (st.st_rdev) */
        unsigned int            blkssz;         /* sector size (BLKSSZGET ioctl) */
        mode_t                  mode;           /* struct stat.sb_mode */
 
+       int                     flags;          /* private libray flags */
+
        unsigned char           *sbbuf;         /* superblok buffer */
        size_t                  sbbuf_len;      /* size of data in superblock buffer */
 
@@ -215,13 +218,11 @@ struct blkid_struct_probe
 
        struct blkid_prval      vals[BLKID_NVALS];      /* results */
        int                     nvals;          /* number of assigned vals */
-
-       /* obsolete */
-       int                     probreq;        /* BLKID_PROBREQ_* flags */
-       int                     idx;            /* index of the last prober */
-       unsigned long           *fltr;          /* filter */
 };
 
+/* flags */
+#define BLKID_PRIVATE_FD       (1 << 1)        /* see blkid_new_probe_from_filename() */
+
 /*
  * Evaluation methods (for blkid_eval_* API)
  */
index 86557d00c1ebf5002677613a36da5858fbe4ee8d..25be36ed0a1d8994686c23a8acaad2a65a7b2906 100644 (file)
@@ -140,6 +140,47 @@ blkid_probe blkid_new_probe(void)
        return pr;
 }
 
+/**
+ * blkid_new_probe_from_filename:
+ * @filename: device or regular file
+ *
+ * This function is same as call open(filename), blkid_new_probe() and
+ * blkid_probe_set_device(pr, fd, 0, 0).
+ *
+ * The @filename is closed by blkid_free_probe() or by the
+ * blkid_probe_set_device() call.
+ *
+ * Returns: a pointer to the newly allocated probe struct or NULL in case of
+ * error.
+ */
+blkid_probe blkid_new_probe_from_filename(const char *filename)
+{
+       int fd = -1;
+       blkid_probe pr = NULL;
+
+       if (!filename)
+               return NULL;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0)
+               return NULL;
+
+       pr = blkid_new_probe();
+       if (!pr)
+               goto err;
+
+       if (blkid_probe_set_device(pr, fd, 0, 0))
+               goto err;
+
+       pr->flags |= BLKID_PRIVATE_FD;
+       return pr;
+err:
+       if (fd >= 0)
+               close(fd);
+       blkid_free_probe(pr);
+       return NULL;
+}
+
 /**
  * blkid_free_probe:
  * @pr: probe
@@ -163,6 +204,9 @@ void blkid_free_probe(blkid_probe pr)
        }
        free(pr->buf);
        free(pr->sbbuf);
+
+       if ((pr->flags & BLKID_PRIVATE_FD) && pr->fd >= 0)
+               close(pr->fd);
        free(pr);
 }
 
@@ -490,7 +534,7 @@ unsigned char *blkid_probe_get_buffer(blkid_probe pr,
  * @pr: probe
  * @fd: device file descriptor
  * @off: begin of probing area
- * @size: size of probing area
+ * @size: size of probing area (zero means whole device/file)
  *
  * Assigns the device to probe control struct, resets internal buffers and
  * reads 512 bytes from device to the buffers.
@@ -505,6 +549,10 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
 
        blkid_reset_probe(pr);
 
+       if ((pr->flags & BLKID_PRIVATE_FD) && pr->fd >= 0)
+               close(pr->fd);
+
+       pr->flags &= ~BLKID_PRIVATE_FD;
        pr->fd = fd;
        pr->off = off;
        pr->size = 0;