]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sysfs: move blkid_devno_to_wholedisk to sysfs_devno_to_wholedisk
authorMichal Luscon <mluscon@redhat.com>
Wed, 29 Feb 2012 16:24:40 +0000 (17:24 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 27 Mar 2012 11:16:19 +0000 (13:16 +0200)
Signed-off-by: Michal Luscon <mluscon@redhat.com>
include/sysfs.h
lib/sysfs.c
libblkid/src/devno.c

index affd2bb7655f153b62f3c432fed50b6094aa9c41..8604aca5e7a9f70ee0e9c795023d60f0e55c0541 100644 (file)
@@ -62,4 +62,6 @@ extern char *sysfs_get_slave(struct sysfs_cxt *cxt);
 extern int sysfs_is_partition_dirent(DIR *dir, struct dirent *d,
                        const char *parent_name);
 
+extern int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
+            size_t len, dev_t *diskdevno);
 #endif /* UTIL_LINUX_SYSFS_H */
index a57c0a6baf57be2bc06471a2aa99430133ee6525..fb03dd999a8401703c946794c55b5f150e758a7f 100644 (file)
@@ -439,6 +439,135 @@ char *sysfs_get_devname(struct sysfs_cxt *cxt, char *buf, size_t bufsiz)
        return buf;
 }
 
+/* returns basename and keeps dirname in the @path */
+static char *stripoff_last_component(char *path)
+{
+    char *p = strrchr(path, '/');
+
+    if (!p)
+        return NULL;
+    *p = '\0';
+    return ++p;
+}
+
+static int get_dm_wholedisk(struct sysfs_cxt *cxt, char *diskname,
+                size_t len, dev_t *diskdevno)
+{
+    int rc = 0;
+    char *name;
+
+    /* Note, sysfs_get_slave() returns the first slave only,
+     * if there is more slaves, then return NULL
+     */
+    name = sysfs_get_slave(cxt);
+    if (!name)
+        return -1;
+
+    if (diskname && len) {
+        strncpy(diskname, name, len);
+        diskname[len - 1] = '\0';
+    }
+
+    if (diskdevno) {
+        *diskdevno = sysfs_devname_to_devno(name, NULL);
+        if (!*diskdevno)
+            rc = -1;
+    }
+
+    free(name);
+    return rc;
+}
+
+int sysfs_devno_to_wholedisk(dev_t dev, char *diskname,
+            size_t len, dev_t *diskdevno)
+{
+    struct sysfs_cxt cxt;
+    int is_part = 0;
+
+    if (!dev || sysfs_init(&cxt, dev, NULL) != 0)
+        return -1;
+
+    is_part = sysfs_has_attribute(&cxt, "partition");
+    if (!is_part) {
+        /*
+         * Extra case for partitions mapped by device-mapper.
+         *
+         * All regualar partitions (added by BLKPG ioctl or kernel PT
+         * parser) have the /sys/.../partition file. The partitions
+         * mapped by DM don't have such file, but they have "part"
+         * prefix in DM UUID.
+         */
+        char *uuid = sysfs_strdup(&cxt, "dm/uuid");
+        char *tmp = uuid;
+        char *prefix = uuid ? strsep(&tmp, "-") : NULL;
+
+        if (prefix && strncasecmp(prefix, "part", 4) == 0)
+            is_part = 1;
+        free(uuid);
+
+        if (is_part &&
+            get_dm_wholedisk(&cxt, diskname, len, diskdevno) == 0)
+            /*
+             * partitioned device, mapped by DM
+             */
+            goto done;
+
+        is_part = 0;
+    }
+
+    if (!is_part) {
+        /*
+         * unpartitioned device
+         */
+        if (diskname && len) {
+            if (!sysfs_get_devname(&cxt, diskname, len))
+                goto err;
+        }
+        if (diskdevno)
+            *diskdevno = dev;
+
+    } else {
+        /*
+         * partitioned device
+         *  - readlink /sys/dev/block/8:1   = ../../block/sda/sda1
+         *  - dirname  ../../block/sda/sda1 = ../../block/sda
+         *  - basename ../../block/sda      = sda
+         */
+        char linkpath[PATH_MAX];
+        char *name;
+        int linklen;
+
+        linklen = sysfs_readlink(&cxt, NULL,
+                linkpath, sizeof(linkpath) - 1);
+        if (linklen < 0)
+            goto err;
+        linkpath[linklen] = '\0';
+
+        stripoff_last_component(linkpath);      /* dirname */
+        name = stripoff_last_component(linkpath);   /* basename */
+        if (!name)
+            goto err;
+
+        if (diskname && len) {
+            strncpy(diskname, name, len);
+            diskname[len - 1] = '\0';
+        }
+
+        if (diskdevno) {
+            *diskdevno = sysfs_devname_to_devno(name, NULL);
+            if (!*diskdevno)
+                goto err;
+        }
+    }
+
+done:
+    sysfs_deinit(&cxt);
+    return 0;
+err:
+    sysfs_deinit(&cxt);
+    return -1;
+}
+
 #ifdef TEST_PROGRAM_SYSFS
 #include <errno.h>
 #include <err.h>
index 59edfd85cefdc82bd3b79437c775f284559c6d48..2996343a0583e5ffa90106f97547ac2e1f58ba19 100644 (file)
@@ -197,16 +197,7 @@ static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
  * @short_description: mix of various utils for low-level and high-level API
  */
 
-/* returns basename and keeps dirname in the @path */
-static char *stripoff_last_component(char *path)
-{
-       char *p = strrchr(path, '/');
 
-       if (!p)
-               return NULL;
-       *p = '\0';
-       return ++p;
-}
 
 static char *scandev_devno_to_devpath(dev_t devno)
 {
@@ -279,33 +270,6 @@ char *blkid_devno_to_devname(dev_t devno)
        return path;
 }
 
-static int get_dm_wholedisk(struct sysfs_cxt *cxt, char *diskname,
-                           size_t len, dev_t *diskdevno)
-{
-       int rc = 0;
-       char *name;
-
-       /* Note, sysfs_get_slave() returns the first slave only,
-        * if there is more slaves, then return NULL
-        */
-       name = sysfs_get_slave(cxt);
-       if (!name)
-               return -1;
-
-       if (diskname && len) {
-               strncpy(diskname, name, len);
-               diskname[len - 1] = '\0';
-       }
-
-       if (diskdevno) {
-               *diskdevno = sysfs_devname_to_devno(name, NULL);
-               if (!*diskdevno)
-                       rc = -1;
-       }
-
-       free(name);
-       return rc;
-}
 
 /**
  * blkid_devno_to_wholedisk:
@@ -346,99 +310,7 @@ static int get_dm_wholedisk(struct sysfs_cxt *cxt, char *diskname,
 int blkid_devno_to_wholedisk(dev_t dev, char *diskname,
                        size_t len, dev_t *diskdevno)
 {
-       struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
-       int is_part = 0;
-
-       if (!dev || sysfs_init(&cxt, dev, NULL) != 0)
-               return -1;
-
-       is_part = sysfs_has_attribute(&cxt, "partition");
-       if (!is_part) {
-               /*
-                * Extra case for partitions mapped by device-mapper.
-                *
-                * All regualar partitions (added by BLKPG ioctl or kernel PT
-                * parser) have the /sys/.../partition file. The partitions
-                * mapped by DM don't have such file, but they have "part"
-                * prefix in DM UUID.
-                */
-               char *uuid = sysfs_strdup(&cxt, "dm/uuid");
-               char *tmp = uuid;
-               char *prefix = uuid ? strsep(&tmp, "-") : NULL;
-
-               if (prefix && strncasecmp(prefix, "part", 4) == 0)
-                       is_part = 1;
-               free(uuid);
-
-               if (is_part &&
-                   get_dm_wholedisk(&cxt, diskname, len, diskdevno) == 0)
-                       /*
-                        * partitioned device, mapped by DM
-                        */
-                       goto done;
-
-               is_part = 0;
-       }
-
-       if (!is_part) {
-               /*
-                * unpartitioned device
-                */
-               if (diskname && len) {
-                       if (!sysfs_get_devname(&cxt, diskname, len))
-                               goto err;
-               }
-               if (diskdevno)
-                       *diskdevno = dev;
-
-       } else {
-               /*
-                * partitioned device
-                *      - readlink /sys/dev/block/8:1   = ../../block/sda/sda1
-                *      - dirname  ../../block/sda/sda1 = ../../block/sda
-                *      - basename ../../block/sda      = sda
-                */
-               char linkpath[PATH_MAX];
-               char *name;
-               int linklen;
-
-               linklen = sysfs_readlink(&cxt, NULL,
-                               linkpath, sizeof(linkpath) - 1);
-               if (linklen < 0)
-                       goto err;
-               linkpath[linklen] = '\0';
-
-               stripoff_last_component(linkpath);              /* dirname */
-               name = stripoff_last_component(linkpath);       /* basename */
-               if (!name)
-                       goto err;
-
-               if (diskname && len) {
-                       strncpy(diskname, name, len);
-                       diskname[len - 1] = '\0';
-               }
-
-               if (diskdevno) {
-                       *diskdevno = sysfs_devname_to_devno(name, NULL);
-                       if (!*diskdevno)
-                               goto err;
-               }
-       }
-
-done:
-       sysfs_deinit(&cxt);
-
-       DBG(DEBUG_DEVNO,
-           printf("found entire diskname for devno 0x%04llx %s\n",
-           (long long) dev, diskname ? diskname : ""));
-       return 0;
-err:
-       sysfs_deinit(&cxt);
-
-       DBG(DEBUG_DEVNO,
-           printf("failed to convert 0x%04llx to wholedisk name, errno=%d\n",
-           (long long) dev, errno));
-       return -1;
+       return sysfs_devno_to_wholedisk( dev, diskname, len, diskdevno);
 }
 
 /*