diff options
author | Karel Zak | 2011-06-23 16:06:27 +0200 |
---|---|---|
committer | Karel Zak | 2011-06-23 16:06:27 +0200 |
commit | 2576b4e7821339f79bc25660f86291b55c0c6486 (patch) | |
tree | 98ce8e58f7eabc379a811df968d4a3a9b3e55ab4 /libmount | |
parent | libmount: cleanup return codes in mount sample (diff) | |
download | kernel-qcow2-util-linux-2576b4e7821339f79bc25660f86291b55c0c6486.tar.gz kernel-qcow2-util-linux-2576b4e7821339f79bc25660f86291b55c0c6486.tar.xz kernel-qcow2-util-linux-2576b4e7821339f79bc25660f86291b55c0c6486.zip |
libmount: allow to convert /dev/loopN to backing filename
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount')
-rw-r--r-- | libmount/samples/mount.c | 7 | ||||
-rw-r--r-- | libmount/src/cache.c | 51 | ||||
-rw-r--r-- | libmount/src/libmount.h.in | 2 | ||||
-rw-r--r-- | libmount/src/libmount.sym | 1 |
4 files changed, 58 insertions, 3 deletions
diff --git a/libmount/samples/mount.c b/libmount/samples/mount.c index 76956d5ab..e01612144 100644 --- a/libmount/samples/mount.c +++ b/libmount/samples/mount.c @@ -118,13 +118,13 @@ static void print_all(struct libmnt_context *cxt, char *pattern, int show_label) const char *type = mnt_fs_get_fstype(fs); const char *src = mnt_fs_get_source(fs); const char *optstr = mnt_fs_get_options(fs); + char *xsrc; if (type && pattern && !mnt_match_fstype(type, pattern)) continue; - /* TODO: print loop backing file instead of device name */ - - printf ("%s on %s", src ? : "none", mnt_fs_get_target(fs)); + xsrc = mnt_pretty_path(src, cache); + printf ("%s on %s", xsrc, mnt_fs_get_target(fs)); if (type) printf (" type %s", type); if (optstr) @@ -135,6 +135,7 @@ static void print_all(struct libmnt_context *cxt, char *pattern, int show_label) printf (" [%s]", lb); } fputc('\n', stdout); + free(xsrc); } mnt_free_cache(cache); diff --git a/libmount/src/cache.c b/libmount/src/cache.c index 2738eee80..e3bdcd089 100644 --- a/libmount/src/cache.c +++ b/libmount/src/cache.c @@ -27,6 +27,7 @@ #include "canonicalize.h" #include "mountP.h" +#include "loopdev.h" /* * Canonicalized (resolved) paths & tags cache @@ -447,6 +448,10 @@ char *mnt_get_fstype(const char *devname, int *ambi, struct libmnt_cache *cache) * @path: "native" path * @cache: cache for results or NULL * + * Converts path: + * - to the absolute path + * - /dev/dm-N to /dev/mapper/<name> + * * Returns: absolute path or NULL in case of error. The result has to be * deallocated by free() if @cache is NULL. */ @@ -490,6 +495,52 @@ error: } /** + * mnt_pretty_path: + * @path: any path + * @cache: NULL or pointer to the cache + * + * Converts path: + * - to the absolute path + * - /dev/dm-N to /dev/mapper/<name> + * - /dev/loopN to the loop backing filename + * - empty path (NULL) to 'none' + * + * Returns: new allocated string with path, result has to be always deallocated + * by free(). + */ +char *mnt_pretty_path(const char *path, struct libmnt_cache *cache) +{ + char *pretty = mnt_resolve_path(path, cache); + + if (!pretty) + return strdup("none"); + + /* users assume backing file name rather than /dev/loopN in + * output if the device has been initialized by mount(8). + */ + if (strncmp(pretty, "/dev/loop", 9) == 0) { + struct loopdev_cxt lc; + + loopcxt_init(&lc, 0); + loopcxt_set_device(&lc, pretty); + + if (loopcxt_is_autoclear(&lc)) { + char *tmp = loopcxt_get_backing_file(&lc); + if (tmp) { + if (!cache) + free(pretty); /* not cached, deallocate */ + return tmp; /* return backing file */ + } + } + loopcxt_deinit(&lc); + + } + + /* don't return pointer to the cache, allocate a new string */ + return cache ? strdup(pretty) : pretty; +} + +/** * mnt_resolve_tag: * @token: tag name * @value: tag value diff --git a/libmount/src/libmount.h.in b/libmount/src/libmount.h.in index 111b3a683..160c7049d 100644 --- a/libmount/src/libmount.h.in +++ b/libmount/src/libmount.h.in @@ -148,6 +148,8 @@ extern char *mnt_resolve_tag(const char *token, const char *value, struct libmnt_cache *cache); extern char *mnt_resolve_spec(const char *spec, struct libmnt_cache *cache); +extern char *mnt_pretty_path(const char *path, struct libmnt_cache *cache); + /* optstr.c */ extern int mnt_optstr_next_option(char **optstr, char **name, size_t *namesz, char **value, size_t *valuesz); diff --git a/libmount/src/libmount.sym b/libmount/src/libmount.sym index 34727a668..8b5cb61dd 100644 --- a/libmount/src/libmount.sym +++ b/libmount/src/libmount.sym @@ -157,6 +157,7 @@ global: mnt_optstr_remove_option; mnt_optstr_set_option; mnt_parse_version_string; + mnt_pretty_path; mnt_reset_context; mnt_reset_fs; mnt_reset_iter; |