From: Karel Zak Date: Mon, 24 Nov 2025 08:42:09 +0000 (+0100) Subject: libmount: refactor mnt_get_fstype() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08a0f0ae66e799d8f0f19fb8f5bbd73691af6880;p=thirdparty%2Futil-linux.git libmount: refactor mnt_get_fstype() Move the current code into two small functions to improve readability and facilitate future extensions. Signed-off-by: Karel Zak --- diff --git a/libmount/src/cache.c b/libmount/src/cache.c index 294265449..a03d6d6d6 100644 --- a/libmount/src/cache.c +++ b/libmount/src/cache.c @@ -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) {