From 7ef93b3eff22c6671c12621fef26853c907435ec Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 18 Sep 2009 13:07:05 +0200 Subject: [PATCH] libblkid: add blkid_driver_has_major() Signed-off-by: Karel Zak --- include/pathnames.h | 1 + shlibs/blkid/src/blkidP.h | 1 + shlibs/blkid/src/devno.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/include/pathnames.h b/include/pathnames.h index f958a13b..efe5c3bc 100644 --- a/include/pathnames.h +++ b/include/pathnames.h @@ -74,6 +74,7 @@ #define _PATH_PROC_FILESYSTEMS "/proc/filesystems" #define _PATH_PROC_MOUNTS "/proc/mounts" #define _PATH_PROC_PARTITIONS "/proc/partitions" +#define _PATH_PROC_DEVICES "/proc/devices" #ifndef _PATH_MOUNTED # ifdef MOUNTED /* deprecated */ diff --git a/shlibs/blkid/src/blkidP.h b/shlibs/blkid/src/blkidP.h index 64ebeee8..10c29776 100644 --- a/shlibs/blkid/src/blkidP.h +++ b/shlibs/blkid/src/blkidP.h @@ -339,6 +339,7 @@ struct dir_list { struct dir_list *next; }; extern void blkid__scan_dir(char *, dev_t, struct dir_list **, char **); +extern int blkid_driver_has_major(const char *drvname, int major); /* 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 c976228a..8518f9fd 100644 --- a/shlibs/blkid/src/devno.c +++ b/shlibs/blkid/src/devno.c @@ -32,6 +32,7 @@ #endif #include "blkidP.h" +#include "pathnames.h" char *blkid_strndup(const char *s, int length) { @@ -328,6 +329,43 @@ err: return -1; } +/* + * Returns 1 if the @major number is associated with @drvname. + */ +int blkid_driver_has_major(const char *drvname, int major) +{ + FILE *f; + char buf[128]; + int match = 0; + + f = fopen(_PATH_PROC_DEVICES, "r"); + if (!f) + return 0; + + while (fgets(buf, sizeof(buf), f)) { /* skip to block dev section */ + if (strncmp("Block devices:\n", buf, sizeof(buf)) == 0) + break; + } + + while (fgets(buf, sizeof(buf), f)) { + unsigned int maj; + char name[64]; + + if (sscanf(buf, "%u %64[^\n ]", &maj, name) != 2) + continue; + + if (maj == major && strcmp(name, drvname) == 0) { + match = 1; + break; + } + } + + fclose(f); + + DBG(DEBUG_DEVNO, printf("major %d %s associated with '%s' driver\n", + major, match ? "is" : "is NOT", drvname)); + return match; +} #ifdef TEST_PROGRAM int main(int argc, char** argv) -- 2.39.5