]> err.no Git - util-linux/commitdiff
libblkid: add blkid_driver_has_major()
authorKarel Zak <kzak@redhat.com>
Fri, 18 Sep 2009 11:07:05 +0000 (13:07 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 18 Sep 2009 11:07:05 +0000 (13:07 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/pathnames.h
shlibs/blkid/src/blkidP.h
shlibs/blkid/src/devno.c

index f958a13bc9ec1c8ef878a966551deca9da1ffe61..efe5c3bc8b2caf9d0d115ca21e1009a335f8baa4 100644 (file)
@@ -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 */
index 64ebeee83ee7828ba078d0670cd1581f03121715..10c29776bc54ecc2d6f3ce149c4377a52cdf1e3f 100644 (file)
@@ -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);
index c976228af39bd3829e3bde6fd6fdffc898975886..8518f9fd99d02b87656efaf302b05eac548cef2b 100644 (file)
@@ -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)