]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: refactor mnt_get_fstype()
authorKarel Zak <kzak@redhat.com>
Mon, 24 Nov 2025 08:42:09 +0000 (09:42 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 9 Dec 2025 10:18:03 +0000 (11:18 +0100)
Move the current code into two small functions to improve readability
and facilitate future extensions.

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/cache.c

index 294265449152b4fb54135d0335aa3c4fecc146ae..a03d6d6d618eae80b950e38b101c235d000dfed2 100644 (file)
@@ -446,7 +446,7 @@ static int __mnt_cache_find_tag_value(struct libmnt_cache *cache,
  * mnt_cache_find_tag_value:
  * @cache: cache for results
  * @devname: device name
- * @token: tag name ("LABEL" or "UUID")
+ * @token: tag name ("LABEL", "UUID", ...)
  *
  * Returns: LABEL or UUID for the @devname or NULL in case of error.
  */
@@ -460,35 +460,24 @@ char *mnt_cache_find_tag_value(struct libmnt_cache *cache,
        return NULL;
 }
 
-/**
- * mnt_get_fstype:
- * @devname: device name
- * @ambi: returns TRUE if probing result is ambivalent (optional argument)
- * @cache: cache for results or NULL
- *
- * Returns: filesystem type or NULL in case of error. The result has to be
- * deallocated by free() if @cache is NULL.
- */
-char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache)
+static char *fstype_from_cache(const char *devname, struct libmnt_cache *cache)
+{
+       char *val = NULL;
+
+       assert(cache);
+
+       if (__mnt_cache_find_tag_value(cache, devname, "TYPE", &val) != 0)
+               return NULL;
+       return val;
+}
+
+static char *fstype_from_blkid(const char *devname, int *ambi)
 {
        blkid_probe pr;
        const char *data;
        char *type = NULL;
        int rc;
 
-       DBG(CACHE, ul_debugobj(cache, "get %s FS type", devname));
-
-       if (cache) {
-               char *val = NULL;
-               rc = __mnt_cache_find_tag_value(cache, devname, "TYPE", &val);
-               if (ambi)
-                       *ambi = rc == -2 ? TRUE : FALSE;
-               return rc ? NULL : val;
-       }
-
-       /*
-        * no cache, probe directly
-        */
        pr =  blkid_new_probe_from_filename(devname);
        if (!pr)
                return NULL;
@@ -498,11 +487,8 @@ char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache)
 
        rc = blkid_do_safeprobe(pr);
 
-       DBG(CACHE, ul_debugobj(cache, "libblkid rc=%d", rc));
-
        if (!rc && !blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
                type = strdup(data);
-
        if (ambi)
                *ambi = rc == -2 ? TRUE : FALSE;
 
@@ -510,6 +496,25 @@ char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache)
        return type;
 }
 
+/**
+ * mnt_get_fstype:
+ * @devname: device name
+ * @ambi: returns TRUE if probing result is ambivalent (optional argument)
+ * @cache: cache for results or NULL
+ *
+ * Returns: filesystem type or NULL in case of error. The result has to be
+ * deallocated by free() if @cache is NULL.
+ */
+char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache)
+{
+       DBG(CACHE, ul_debugobj(cache, "get %s FS type", devname));
+
+       if (cache)
+               return fstype_from_cache(devname, cache);
+
+       return fstype_from_blkid(devname, ambi);
+}
+
 static char *canonicalize_path_and_cache(const char *path,
                                                struct libmnt_cache *cache)
 {