]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: define probing return values
authorKarel Zak <kzak@redhat.com>
Fri, 2 Dec 2022 08:20:18 +0000 (09:20 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 2 Dec 2022 08:20:18 +0000 (09:20 +0100)
Don't use magic numbers in library and applications code.

Fixes: https://github.com/util-linux/util-linux/issues/1939
Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/src/blkid.h.in
libblkid/src/blkidP.h
libblkid/src/probe.c
libblkid/src/superblocks/superblocks.c

index ae4e555e315aeee8fecee426ab0b70643850606b..0f1f1df84f610c8a5604fcf80a7a7782e304dc1c 100644 (file)
@@ -418,6 +418,13 @@ extern int blkid_do_safeprobe(blkid_probe pr)
 extern int blkid_do_fullprobe(blkid_probe pr)
                        __ul_attribute__((nonnull));
 
+enum {
+       BLKID_PROBE_OK          = 0,
+       BLKID_PROBE_NONE        = 1,
+       BLKID_PROBE_ERROR       = -1,
+       BLKID_PROBE_AMBIGUOUS   = -2
+};
+
 extern int blkid_probe_numof_values(blkid_probe pr)
                        __ul_attribute__((nonnull));
 extern int blkid_probe_get_value(blkid_probe pr, int num, const char **name,
index 43ceb9c9811de4cf0199ec62ad4c52a1597676b0..a9308068e66296af0284123931e8237d227d953b 100644 (file)
@@ -313,9 +313,6 @@ struct blkid_struct_cache
 /* old systems */
 #define BLKID_CACHE_FILE_OLD   "/etc/blkid.tab"
 
-#define BLKID_PROBE_OK  0
-#define BLKID_PROBE_NONE 1
-
 #define BLKID_ERR_IO    5
 #define BLKID_ERR_SYSFS         9
 #define BLKID_ERR_MEM  12
index 203f0ae49890eae6b215e746dff9416b97a4af69..34d2646c0f055f679dac946855a81854958d8959 100644 (file)
@@ -71,7 +71,7 @@
  *
  * <informalexample>
  *   <programlisting>
- *     while((blkid_do_probe(pr) == 0)
+ *     while((blkid_do_probe(pr) == BLKID_PROBE_OK)
  *             ... use result ...
  *   </programlisting>
  * </informalexample>
@@ -1218,7 +1218,7 @@ static inline void blkid_probe_end(blkid_probe pr)
  * <example>
  *   <title>basic case - use the first result only</title>
  *   <programlisting>
- *     if (blkid_do_probe(pr) == 0) {
+ *     if (blkid_do_probe(pr) == BLKID_PROBE_OK) {
  *             int nvals = blkid_probe_numof_values(pr);
  *             for (n = 0; n < nvals; n++) {
  *                     if (blkid_probe_get_value(pr, n, &name, &data, &len) == 0)
@@ -1231,7 +1231,7 @@ static inline void blkid_probe_end(blkid_probe pr)
  * <example>
  *   <title>advanced case - probe for all signatures</title>
  *   <programlisting>
- *     while (blkid_do_probe(pr) == 0) {
+ *     while (blkid_do_probe(pr) == BLKID_PROBE_OK) {
  *             int nvals = blkid_probe_numof_values(pr);
  *             ...
  *     }
@@ -1247,7 +1247,7 @@ int blkid_do_probe(blkid_probe pr)
        int rc = 1;
 
        if (pr->flags & BLKID_FL_NOSCAN_DEV)
-               return 1;
+               return BLKID_PROBE_NONE;
 
        do {
                struct blkid_chain *chn = pr->cur_chain;
@@ -1272,7 +1272,7 @@ int blkid_do_probe(blkid_probe pr)
                                chn = pr->cur_chain = &pr->chains[idx];
                        else {
                                blkid_probe_end(pr);
-                               return 1;       /* all chains already probed */
+                               return BLKID_PROBE_NONE;        /* all chains already probed */
                        }
                }
 
@@ -1289,7 +1289,10 @@ int blkid_do_probe(blkid_probe pr)
                /* rc: -1 = error, 0 = success, 1 = no result */
                rc = chn->driver->probe(pr, chn);
 
-       } while (rc == 1);
+       } while (rc == BLKID_PROBE_NONE);
+
+       if (rc < 0)
+              return BLKID_PROBE_ERROR;
 
        return rc;
 }
@@ -1373,7 +1376,7 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
 
        chn = pr->cur_chain;
        if (!chn)
-               return -1;
+               return BLKID_PROBE_ERROR;
 
        switch (chn->driver->id) {
        case BLKID_CHAIN_SUBLKS:
@@ -1387,28 +1390,28 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
                        rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
                break;
        default:
-               return 0;
+               return BLKID_PROBE_OK;
        }
 
        if (rc || len == 0 || off == NULL)
-               return 0;
+               return BLKID_PROBE_OK;
 
        errno = 0;
        magoff = strtoumax(off, NULL, 10);
        if (errno)
-               return 0;
+               return BLKID_PROBE_OK;
 
        offset = magoff + pr->off;
        fd = blkid_probe_get_fd(pr);
        if (fd < 0)
-               return -1;
+               return BLKID_PROBE_ERROR;
 
        if (len > sizeof(buf))
                len = sizeof(buf);
 
        rc = is_conventional(pr, offset);
        if (rc < 0)
-               return rc;
+               return BLKID_PROBE_ERROR;
        conventional = rc == 1;
 
        DBG(LOWPROBE, ul_debug(
@@ -1416,7 +1419,7 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
            offset, offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not"));
 
        if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
-               return -1;
+               return BLKID_PROBE_ERROR;
 
        if (!dryrun && len) {
                if (conventional) {
@@ -1424,9 +1427,9 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
 
                        /* wipen on device */
                        if (write_all(fd, buf, len))
-                               return -1;
+                               return BLKID_PROBE_ERROR;
                        if (fsync(fd) != 0)
-                               return -1;
+                               return BLKID_PROBE_ERROR;
                } else {
 #ifdef HAVE_LINUX_BLKZONED_H
                        uint64_t zone_mask = ~(pr->zone_size - 1);
@@ -1437,7 +1440,7 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
 
                        rc = ioctl(fd, BLKRESETZONE, &range);
                        if (rc < 0)
-                               return -1;
+                               return BLKID_PROBE_ERROR;
 #else
                        /* Should not reach here */
                        assert(0);
@@ -1456,7 +1459,7 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
                return blkid_probe_step_back(pr);
        }
 
-       return 0;
+       return BLKID_PROBE_OK;
 }
 
 /**
@@ -1485,7 +1488,7 @@ int blkid_do_wipe(blkid_probe pr, int dryrun)
  *      blkid_probe_enable_partitions(pr, 1);
  *      blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
  *
- *     while (blkid_do_probe(pr) == 0) {
+ *     while (blkid_do_probe(pr) == BLKID_PROBE_OK) {
  *             const char *ostr = NULL;
  *             size_t len = 0;
  *
@@ -1571,7 +1574,7 @@ int blkid_do_safeprobe(blkid_probe pr)
        int i, count = 0, rc = 0;
 
        if (pr->flags & BLKID_FL_NOSCAN_DEV)
-               return 1;
+               return BLKID_PROBE_NONE;
 
        blkid_probe_start(pr);
 
@@ -1604,8 +1607,9 @@ int blkid_do_safeprobe(blkid_probe pr)
 done:
        blkid_probe_end(pr);
        if (rc < 0)
-               return rc;
-       return count ? 0 : 1;
+               return BLKID_PROBE_ERROR;
+
+       return count == 0 ? BLKID_PROBE_NONE : BLKID_PROBE_OK;
 }
 
 /**
@@ -1625,7 +1629,7 @@ int blkid_do_fullprobe(blkid_probe pr)
        int i, count = 0, rc = 0;
 
        if (pr->flags & BLKID_FL_NOSCAN_DEV)
-               return 1;
+               return BLKID_PROBE_NONE;
 
        blkid_probe_start(pr);
 
@@ -1658,8 +1662,9 @@ int blkid_do_fullprobe(blkid_probe pr)
 done:
        blkid_probe_end(pr);
        if (rc < 0)
-               return rc;
-       return count ? 0 : 1;
+               return BLKID_PROBE_ERROR;
+
+       return count == 0 ? BLKID_PROBE_NONE : BLKID_PROBE_OK;
 }
 
 /* same sa blkid_probe_get_buffer() but works with 512-sectors */
index a25ebc90896d4cfed4505ef0fb5caafadfb7c3c2..b94a2c8ae0807908659bed5ca8f3fb485e5cfeee 100644 (file)
@@ -506,7 +506,7 @@ static int superblocks_safeprobe(blkid_probe pr, struct blkid_chain *chn)
                DBG(LOWPROBE, ul_debug("ERROR: superblocks chain: "
                               "ambivalent result detected (%d filesystems)!",
                               count));
-               rc = -2;                /* error, ambivalent result (more FS) */
+               rc = BLKID_PROBE_AMBIGUOUS;     /* error, ambivalent result (more FS) */
                goto done;
        }
        if (!count) {