diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/canonicalize.c | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/lib/canonicalize.c b/lib/canonicalize.c index b888fbb8..e54cf0c0 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -20,6 +20,9 @@ * * TODO: use canonicalize_file_name() when exist in glibc */ +#include <stdio.h> +#include <string.h> +#include <ctype.h> #include <unistd.h> #include <string.h> #include <errno.h> @@ -136,17 +139,54 @@ myrealpath(const char *path, char *resolved_path, int maxreslth) { return NULL; } +/* + * Converts private "dm-N" names to "/dev/mapper/<name>" + * + * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs + * provides the real DM device names in /sys/block/<ptname>/dm/name + */ char * -canonicalize_path(const char *path) { +canonicalize_dm_name(const char *ptname) +{ + FILE *f; + size_t sz; + char path[256], name[256], *res = NULL; + + snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname); + if (!(f = fopen(path, "r"))) + return NULL; + + /* read "<name>\n" from sysfs */ + if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) { + name[sz - 1] = '\0'; + snprintf(path, sizeof(path), "/dev/mapper/%s", name); + res = strdup(path); + } + fclose(f); + return res; +} + +char * +canonicalize_path(const char *path) +{ char canonical[PATH_MAX+2]; + char *p; if (path == NULL) return NULL; - if (myrealpath (path, canonical, PATH_MAX+1)) - return strdup(canonical); + if (!myrealpath(path, canonical, PATH_MAX+1)) + return strdup(path); + + + p = strrchr(canonical, '/'); + if (p && strncmp(p, "/dm-", 4) == 0 && isdigit(*(p + 4))) { + p = canonicalize_dm_name(p+1); + if (p) + return p; + } - return strdup(path); + return strdup(canonical); } |