From f38db0cfb1a78dd1a189a486d1382c7ba284b928 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 29 Sep 2009 21:46:30 +0200 Subject: [PATCH] libblkid: add blkid_new_probe_from_filename() Signed-off-by: Karel Zak --- shlibs/blkid/docs/libblkid-sections.txt | 1 + shlibs/blkid/samples/mkfs.c | 15 ++------ shlibs/blkid/samples/partitions.c | 16 ++------ shlibs/blkid/samples/topology.c | 16 ++------ shlibs/blkid/src/blkid.h | 1 + shlibs/blkid/src/blkid.sym | 1 + shlibs/blkid/src/blkidP.h | 11 +++--- shlibs/blkid/src/probe.c | 50 ++++++++++++++++++++++++- 8 files changed, 70 insertions(+), 41 deletions(-) diff --git a/shlibs/blkid/docs/libblkid-sections.txt b/shlibs/blkid/docs/libblkid-sections.txt index af94607e..8f643d8b 100644 --- a/shlibs/blkid/docs/libblkid-sections.txt +++ b/shlibs/blkid/docs/libblkid-sections.txt @@ -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 diff --git a/shlibs/blkid/samples/mkfs.c b/shlibs/blkid/samples/mkfs.c index 866ca6e3..eb9e27f5 100644 --- a/shlibs/blkid/samples/mkfs.c +++ b/shlibs/blkid/samples/mkfs.c @@ -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 diff --git a/shlibs/blkid/samples/partitions.c b/shlibs/blkid/samples/partitions.c index 8c000482..c9587fe5 100644 --- a/shlibs/blkid/samples/partitions.c +++ b/shlibs/blkid/samples/partitions.c @@ -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) diff --git a/shlibs/blkid/samples/topology.c b/shlibs/blkid/samples/topology.c index 6d3bc82e..17fcbbdc 100644 --- a/shlibs/blkid/samples/topology.c +++ b/shlibs/blkid/samples/topology.c @@ -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 */ diff --git a/shlibs/blkid/src/blkid.h b/shlibs/blkid/src/blkid.h index 9ab99c2f..6b495b69 100644 --- a/shlibs/blkid/src/blkid.h +++ b/shlibs/blkid/src/blkid.h @@ -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); diff --git a/shlibs/blkid/src/blkid.sym b/shlibs/blkid/src/blkid.sym index 8ac2081a..992d4e9f 100644 --- a/shlibs/blkid/src/blkid.sym +++ b/shlibs/blkid/src/blkid.sym @@ -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; diff --git a/shlibs/blkid/src/blkidP.h b/shlibs/blkid/src/blkidP.h index 86b2b096..2e7a7ed9 100644 --- a/shlibs/blkid/src/blkidP.h +++ b/shlibs/blkid/src/blkidP.h @@ -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) */ diff --git a/shlibs/blkid/src/probe.c b/shlibs/blkid/src/probe.c index 86557d00..25be36ed 100644 --- a/shlibs/blkid/src/probe.c +++ b/shlibs/blkid/src/probe.c @@ -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; -- 2.39.5