From b3ee97a3c2c29be58219356979d17095b62d76f4 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 15 Sep 2009 22:50:11 +0200 Subject: [PATCH] libblkid: add blkid_probe_get_{size,sectorsize,devno} Signed-off-by: Karel Zak --- shlibs/blkid/src/blkid.h | 5 ++++ shlibs/blkid/src/probe.c | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/shlibs/blkid/src/blkid.h b/shlibs/blkid/src/blkid.h index 40ada4dc..a1cfe90c 100644 --- a/shlibs/blkid/src/blkid.h +++ b/shlibs/blkid/src/blkid.h @@ -128,6 +128,11 @@ extern void blkid_reset_probe(blkid_probe pr); extern int blkid_probe_set_device(blkid_probe pr, int fd, blkid_loff_t off, blkid_loff_t size); + +extern dev_t blkid_probe_get_devno(blkid_probe pr); +extern blkid_loff_t blkid_probe_get_size(blkid_probe pr); +extern unsigned int blkid_probe_get_sectorsize(blkid_probe pr); + /* * superblocks probing */ diff --git a/shlibs/blkid/src/probe.c b/shlibs/blkid/src/probe.c index a68881db..a3d08114 100644 --- a/shlibs/blkid/src/probe.c +++ b/shlibs/blkid/src/probe.c @@ -744,6 +744,65 @@ int blkid_probe_vsprintf_value(blkid_probe pr, const char *name, return 0; } +/** + * blkid_probe_get_devno: + * @pr: probe + * + * Returns: block device number, or 0 for regilar files. + */ +dev_t blkid_probe_get_devno(blkid_probe pr) +{ + if (!pr->devno) { + struct stat sb; + + if (fstat(pr->fd, &sb) == 0 && S_ISBLK(sb.st_mode)) + pr->devno = sb.st_rdev; + } + return pr->devno; +} + +/** + * blkid_probe_get_size: + * @pr: probe + * + * Returns: block device (or file) size in bytes or -1 in case of error. + */ +blkid_loff_t blkid_probe_get_size(blkid_probe pr) +{ + return pr ? pr->size : -1; +} + +/** + * blkid_probe_get_sectorsize: + * @pr: probe + * + * Returns: block device hardware sector size (BLKSSZGET ioctl, default 512). + */ +unsigned int blkid_probe_get_sectorsize(blkid_probe pr) +{ + if (!pr) + return DEFAULT_SECTOR_SIZE; /*... and good luck! */ + if (pr->blkssz) + return pr->blkssz; + if (!pr->mode) { + struct stat st; + + if (fstat(pr->fd, &st)) + goto fallback; + pr->mode = st.st_mode; + } + if (S_ISBLK(pr->mode)) { + if (blkdev_get_sector_size(pr->fd, (int *) &pr->blkssz)) + goto fallback; + + return pr->blkssz; + } + +fallback: + pr->blkssz = DEFAULT_SECTOR_SIZE; + return pr->blkssz; +} + /** * blkid_probe_numof_values: * @pr: probe -- 2.39.5