]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: use blkid_probe_open_device() in evaluate and verify
authorKarel Zak <kzak@redhat.com>
Tue, 30 Jun 2026 13:40:46 +0000 (15:40 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 9 Jul 2026 10:18:04 +0000 (12:18 +0200)
Convert evaluate.c and verify.c to use blkid_probe_open_device()
instead of open() + blkid_probe_set_device(). This makes these
internal callers VFS-aware and simplifies fd ownership.

Clean up verify.c error paths with dev_err/dev_free labels.

Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/src/evaluate.c
libblkid/src/verify.c

index 2618b9192d2766aea0d3f43c6add2764d1a8440a..0bbe680e1036dcb1ebe4c24a6fd26e8918e7460b 100644 (file)
@@ -54,7 +54,7 @@
 static int verify_tag(const char *devname, const char *name, const char *value)
 {
        blkid_probe pr;
-       int fd = -1, rc = -1;
+       int rc = -1;
        size_t len;
        const char *data;
        int errsv = 0;
@@ -73,13 +73,10 @@ static int verify_tag(const char *devname, const char *name, const char *value)
        blkid_probe_enable_partitions(pr, TRUE);
        blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
 
-       fd = open(devname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
-       if (fd < 0) {
+       if (blkid_probe_open_device(pr, devname, 0)) {
                errsv = errno;
                goto done;
        }
-       if (blkid_probe_set_device(pr, fd, 0, 0))
-               goto done;
        rc = blkid_do_safeprobe(pr);
        if (rc)
                goto done;
@@ -89,8 +86,6 @@ static int verify_tag(const char *devname, const char *name, const char *value)
 done:
        DBG(EVALUATE, ul_debug("%s: %s verification %s",
                        devname, name, rc == 0 ? "PASS" : "FAILED"));
-       if (fd >= 0)
-               close(fd);
        blkid_free_probe(pr);
 
        /* for non-root users we use unverified udev links */
index abd3d1246102e6e43d32d5e2104422cea684555a..7ca267dc72b33c752b77da71522be84ef0fa54fa 100644 (file)
@@ -64,7 +64,6 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
        const char *type, *value;
        struct stat st;
        time_t diff, now;
-       int fd;
 
        if (!dev || !cache)
                return NULL;
@@ -76,15 +75,7 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
                DBG(PROBE, ul_debug("blkid_verify: error %m (%d) while "
                           "trying to stat %s", errno,
                           dev->bid_name));
-       open_err:
-               if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
-                       /* We don't have read permission, just return cache data. */
-                       DBG(PROBE, ul_debug("returning unverified data for %s",
-                                               dev->bid_name));
-                       return dev;
-               }
-               blkid_free_dev(dev);
-               return NULL;
+               goto dev_err;
        }
 
        if (now >= dev->bid_time &&
@@ -114,31 +105,19 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
                   (long long)diff));
 #endif
 
-       if (sysfs_devno_is_dm_private(st.st_rdev, NULL)) {
-               blkid_free_dev(dev);
-               return NULL;
-       }
+       if (sysfs_devno_is_dm_private(st.st_rdev, NULL))
+               goto dev_free;
        if (!cache->probe) {
                cache->probe = blkid_new_probe();
-               if (!cache->probe) {
-                       blkid_free_dev(dev);
-                       return NULL;
-               }
+               if (!cache->probe)
+                       goto dev_free;
        }
 
-       fd = open(dev->bid_name, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
-       if (fd < 0) {
+       if (blkid_probe_open_device(cache->probe, dev->bid_name, 0)) {
                DBG(PROBE, ul_debug("blkid_verify: error %m (%d) while "
                                        "opening %s", errno,
                                        dev->bid_name));
-               goto open_err;
-       }
-
-       if (blkid_probe_set_device(cache->probe, fd, 0, 0)) {
-               /* failed to read the device */
-               close(fd);
-               blkid_free_dev(dev);
-               return NULL;
+               goto dev_err;
        }
 
        /* remove old cache info */
@@ -187,9 +166,18 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
        /* reset prober */
        blkid_probe_reset_superblocks_filter(cache->probe);
        blkid_probe_set_device(cache->probe, -1, 0, 0);
-       close(fd);
 
        return dev;
+
+dev_err:
+       if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
+               DBG(PROBE, ul_debug("returning unverified data for %s",
+                                       dev->bid_name));
+               return dev;
+       }
+dev_free:
+       blkid_free_dev(dev);
+       return NULL;
 }
 
 #ifdef TEST_PROGRAM