]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: use new ul_path_* API
authorKarel Zak <kzak@redhat.com>
Wed, 16 May 2018 11:20:39 +0000 (13:20 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2018 11:07:46 +0000 (13:07 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/src/devname.c
libblkid/src/partitions/partitions.c
libblkid/src/topology/sysfs.c

index 59029ec06543944c67527ded16b610c22539cdb8..986bcf2418d6a02d9f80a0411a9d92aff4747dc7 100644 (file)
@@ -565,6 +565,7 @@ static int probe_all(blkid_cache cache, int only_if_new)
  */
 static int probe_all_removable(blkid_cache cache)
 {
+       struct path_cxt *pc;
        DIR *dir;
        struct dirent *d;
 
@@ -575,8 +576,9 @@ static int probe_all_removable(blkid_cache cache)
        if (!dir)
                return -BLKID_ERR_PROC;
 
+       pc = ul_new_path(NULL);
+
        while((d = readdir(dir))) {
-               struct sysfs_cxt sysfs = UL_SYSFSCXT_EMPTY;
                int removable = 0;
                dev_t devno;
 
@@ -589,20 +591,19 @@ static int probe_all_removable(blkid_cache cache)
                     ((d->d_name[1] == '.') && (d->d_name[2] == 0))))
                        continue;
 
-               devno = sysfs_devname_to_devno(d->d_name, NULL);
+               devno = sysfs_devname_to_devno(d->d_name);
                if (!devno)
                        continue;
 
-               if (sysfs_init(&sysfs, devno, NULL) == 0) {
-                       if (sysfs_read_int(&sysfs, "removable", &removable) != 0)
+               if (sysfs_blkdev_init_path(pc, devno, NULL) == 0
+                   && ul_path_read_s32(pc, &removable, "removable") != 0)
                                removable = 0;
-                       sysfs_deinit(&sysfs);
-               }
 
                if (removable)
                        probe_one(cache, d->d_name, devno, 0, 0, 1);
        }
 
+       ul_unref_path(pc);
        closedir(dir);
        return 0;
 }
index 83c3c4f1b536939fda22c8def261224dbb2a5b65..f60f3c6535c8d128d41189da20dad10c9f627792 100644 (file)
@@ -1008,26 +1008,30 @@ blkid_partition blkid_partlist_get_partition_by_partno(blkid_partlist ls, int n)
  */
 blkid_partition blkid_partlist_devno_to_partition(blkid_partlist ls, dev_t devno)
 {
-       struct sysfs_cxt sysfs;
+       struct path_cxt *pc;
        uint64_t start, size;
        int i, rc, partno = 0;
 
        DBG(LOWPROBE, ul_debug("trying to convert devno 0x%llx to partition",
                        (long long) devno));
 
-       if (sysfs_init(&sysfs, devno, NULL)) {
+
+       pc = ul_new_sysfs_path(devno, NULL, NULL);
+       if (!pc) {
                DBG(LOWPROBE, ul_debug("failed t init sysfs context"));
                return NULL;
        }
-       rc = sysfs_read_u64(&sysfs, "size", &size);
+       rc = ul_path_read_u64(pc, &size, "size");
        if (!rc) {
-               rc = sysfs_read_u64(&sysfs, "start", &start);
+               rc = ul_path_read_u64(pc, &start, "start");
                if (rc) {
                        /* try to get partition number from DM uuid.
                         */
-                       char *uuid = sysfs_strdup(&sysfs, "dm/uuid");
-                       char *tmp = uuid;
-                       char *prefix = uuid ? strsep(&tmp, "-") : NULL;
+                       char *uuid = NULL, *tmp, *prefix;
+
+                       ul_path_read_string(pc, &uuid, "dm/uuid");
+                       tmp = uuid;
+                       prefix = uuid ? strsep(&tmp, "-") : NULL;
 
                        if (prefix && strncasecmp(prefix, "part", 4) == 0) {
                                char *end = NULL;
@@ -1042,7 +1046,7 @@ blkid_partition blkid_partlist_devno_to_partition(blkid_partlist ls, dev_t devno
                }
        }
 
-       sysfs_deinit(&sysfs);
+       ul_unref_path(pc);
 
        if (rc)
                return NULL;
index 710e4b6761725bc979e64f71ced647d08092c42d..0605c355861d5f032d716d210db83070e61fee80 100644 (file)
@@ -42,55 +42,61 @@ static struct topology_val {
 static int probe_sysfs_tp(blkid_probe pr,
                const struct blkid_idmag *mag __attribute__((__unused__)))
 {
-       dev_t dev, disk = 0;
-       int rc;
-       struct sysfs_cxt sysfs = UL_SYSFSCXT_EMPTY,
-                        parent = UL_SYSFSCXT_EMPTY;
+       dev_t dev;
+       int rc, set_parent = 1;
+       struct path_cxt *pc;
        size_t i, count = 0;
 
        dev = blkid_probe_get_devno(pr);
-       if (!dev || sysfs_init(&sysfs, dev, NULL) != 0)
+       if (!dev)
+               return 1;
+       pc = ul_new_sysfs_path(dev, NULL, NULL);
+       if (!pc)
                return 1;
 
        rc = 1;         /* nothing (default) */
 
        for (i = 0; i < ARRAY_SIZE(topology_vals); i++) {
                struct topology_val *val = &topology_vals[i];
-               int ok = sysfs_has_attribute(&sysfs, val->attr);
+               int ok = ul_path_access(pc, F_OK, val->attr) == 0;
 
                rc = 1; /* nothing */
 
-               if (!ok) {
-                       if (!disk) {
-                               /*
-                                * Read attributes from "disk" if the current
-                                * device is a partition.
-                                */
-                               disk = blkid_probe_get_wholedisk_devno(pr);
-                               if (disk && disk != dev) {
-                                       if (sysfs_init(&parent, disk, NULL) != 0)
-                                               goto done;
-
-                                       sysfs.parent = &parent;
-                                       ok = sysfs_has_attribute(&sysfs,
-                                                                val->attr);
-                               }
+               if (!ok && set_parent) {
+                       dev_t disk = blkid_probe_get_wholedisk_devno(pr);
+                       set_parent = 0;
+
+                       /*
+                        * Read attributes from "disk" if the current device is
+                        * a partition. Note that sysfs ul_path_* API is able
+                        * to redirect requests to attributes if parent is set.
+                        */
+                       if (disk && disk != dev) {
+                               struct path_cxt *parent = ul_new_sysfs_path(disk, NULL, NULL);
+                               if (!parent)
+                                       goto done;
+
+                               sysfs_blkdev_set_parent(pc, parent);
+                               ul_unref_path(parent);
+
+                               /* try it again */
+                               ok = ul_path_access(pc, F_OK, val->attr) == 0;
                        }
-                       if (!ok)
-                               continue;       /* attribute does not exist */
                }
+               if (!ok)
+                       continue;       /* attribute does not exist */
 
                if (val->set_ulong) {
                        uint64_t data;
 
-                       if (sysfs_read_u64(&sysfs, val->attr, &data) != 0)
+                       if (ul_path_read_u64(pc, &data, val->attr) != 0)
                                continue;
                        rc = val->set_ulong(pr, (unsigned long) data);
 
                } else if (val->set_int) {
                        int64_t data;
 
-                       if (sysfs_read_s64(&sysfs, val->attr, &data) != 0)
+                       if (ul_path_read_s64(pc, &data, val->attr) != 0)
                                continue;
                        rc = val->set_int(pr, (int) data);
                }
@@ -102,9 +108,7 @@ static int probe_sysfs_tp(blkid_probe pr,
        }
 
 done:
-       sysfs_deinit(&sysfs);
-       sysfs_deinit(&parent);
-
+       ul_unref_path(pc);              /* unref pc and parent */
        if (count)
                return 0;               /* success */
        return rc;                      /* error or nothing */