From: Karel Zak Date: Thu, 18 Feb 2010 10:09:46 +0000 (+0100) Subject: libblkid: add generic functions for sysfs attributes X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=220c60151dad64bb79fef28552f02003cf735a36;p=util-linux libblkid: add generic functions for sysfs attributes Signed-off-by: Karel Zak --- diff --git a/shlibs/blkid/src/blkidP.h b/shlibs/blkid/src/blkidP.h index 377cf0e5..7dc39fc3 100644 --- a/shlibs/blkid/src/blkidP.h +++ b/shlibs/blkid/src/blkidP.h @@ -21,6 +21,7 @@ #include #include #include +#include #include "c.h" #include "bitops.h" /* $(top_srcdir)/include/ */ @@ -331,6 +332,9 @@ struct dir_list { }; extern void blkid__scan_dir(char *, dev_t, struct dir_list **, char **); extern int blkid_driver_has_major(const char *drvname, int major); +extern int blkid_devno_has_attribute(dev_t devno, const char *attribute); +extern int blkid_devno_get_attribute(dev_t devno, const char *attribute, + uint64_t *result); /* lseek.c */ extern blkid_loff_t blkid_llseek(int fd, blkid_loff_t offset, int whence); diff --git a/shlibs/blkid/src/devno.c b/shlibs/blkid/src/devno.c index 288e82ee..1b64ec1e 100644 --- a/shlibs/blkid/src/devno.c +++ b/shlibs/blkid/src/devno.c @@ -30,6 +30,7 @@ #include #endif #include +#include #include "blkidP.h" #include "pathnames.h" @@ -451,6 +452,50 @@ int blkid_driver_has_major(const char *drvname, int major) return match; } +static char *mk_devno_attribute_path(char *buf, size_t buflen, + dev_t devno, const char *attr) +{ + int len = snprintf(buf, buflen, "/sys/dev/block/%d:%d/%s", + major(devno), minor(devno), attr); + + if (len < 0 || len + 1 > buflen) + return NULL; + + return buf; +} + +int blkid_devno_has_attribute(dev_t devno, const char *attribute) +{ + char path[PATH_MAX]; + struct stat info; + + if (!mk_devno_attribute_path(path, sizeof(path), devno, attribute)) + return 0; + + if (stat(path, &info) == 0) + return 1; + + return 0; +} + +int blkid_devno_get_attribute(dev_t devno, const char *attribute, uint64_t *result) +{ + FILE *f; + char path[PATH_MAX]; + int rc = 0; + + if (!mk_devno_attribute_path(path, sizeof(path), devno, attribute)) + return -1; + + f = fopen(path, "r"); + if (f) { + rc = fscanf(f, "%" SCNu64, result); + fclose(f); + } + + return rc == 1 ? 0 : -1; +} + #ifdef TEST_PROGRAM int main(int argc, char** argv) { diff --git a/shlibs/blkid/src/topology/sysfs.c b/shlibs/blkid/src/topology/sysfs.c index 20c507f1..ce870f22 100644 --- a/shlibs/blkid/src/topology/sysfs.c +++ b/shlibs/blkid/src/topology/sysfs.c @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -22,57 +22,30 @@ static unsigned long dev_topology_attribute(const char *attribute, dev_t dev, dev_t *primary) { - const char *sysfs_fmt_str = "/sys/dev/block/%d:%d/%s"; - char path[PATH_MAX]; - int len; - FILE *fp = NULL; - struct stat info; - unsigned long result = 0UL; - - len = snprintf(path, sizeof(path), sysfs_fmt_str, - major(dev), minor(dev), attribute); - if (len < 0 || len + 1 > sizeof(path)) - goto err; + uint64_t result; /* * check if the desired sysfs attribute exists * - if not: either the kernel doesn't have topology support or the * device could be a partition */ - if (stat(path, &info) < 0) { + if (!blkid_devno_has_attribute(dev, attribute)) { if (!*primary && blkid_devno_to_wholedisk(dev, NULL, 0, primary)) goto err; /* get attribute from partition's primary device */ - len = snprintf(path, sizeof(path), sysfs_fmt_str, - major(*primary), minor(*primary), attribute); - if (len < 0 || len + 1 > sizeof(path)) - goto err; + dev = *primary; } - fp = fopen(path, "r"); - if (!fp) { - DBG(DEBUG_LOWPROBE, printf( - "topology: %s: fopen failed, errno=%d\n", path, errno)); + if (blkid_devno_get_attribute(dev, attribute, &result)) goto err; - } - - if (fscanf(fp, "%lu", &result) != 1) { - DBG(DEBUG_LOWPROBE, printf( - "topology: %s: unexpected file format\n", path)); - goto err; - } - - fclose(fp); DBG(DEBUG_LOWPROBE, - printf("topology: attribute %s = %lu\n", attribute, result)); + printf("topology: attribute %s = %" PRIu64 "\n", attribute, result)); return result; err: - if (fp) - fclose(fp); DBG(DEBUG_LOWPROBE, printf("topology: failed to read %s attribute\n", attribute)); return 0;