From: Karel Zak Date: Tue, 30 Jun 2026 13:40:46 +0000 (+0200) Subject: libblkid: use blkid_probe_open_device() in evaluate and verify X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=52c4ec23c5a2240220e7301bb92fc13ea3aeadf9;p=thirdparty%2Futil-linux.git libblkid: use blkid_probe_open_device() in evaluate and verify 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 --- diff --git a/libblkid/src/evaluate.c b/libblkid/src/evaluate.c index 2618b9192..0bbe680e1 100644 --- a/libblkid/src/evaluate.c +++ b/libblkid/src/evaluate.c @@ -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 */ diff --git a/libblkid/src/verify.c b/libblkid/src/verify.c index abd3d1246..7ca267dc7 100644 --- a/libblkid/src/verify.c +++ b/libblkid/src/verify.c @@ -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