From 45f104b60356b096deaed0fd184f1c8b04a40d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Bubeck?= Date: Wed, 9 Nov 2016 17:14:42 +0100 Subject: Fix path normalization for the case of CWD being the root directory (/). Previously metastore would fail if running from / and paths were given: $ pwd / $ metastore -d bin lstat failed for .bin: No such file or directory Now it works properly: $ metastore -d bin | head -1 lrwxrwxrwx root root 2018-08-17 16:31:23.120322532 +0200 ./bin/hostname Signed-off-by: Przemyslaw Pawelczyk --- src/metaentry.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/metaentry.c b/src/metaentry.c index da28723..fe1c6d1 100644 --- a/src/metaentry.c +++ b/src/metaentry.c @@ -322,16 +322,21 @@ normalize_path(const char *orig) char *real = realpath(orig, NULL); char cwd[PATH_MAX]; char *result; + size_t cwdlen; getcwd(cwd, PATH_MAX); if (!real) return NULL; - if (!strncmp(real, cwd, strlen(cwd))) { - result = xmalloc(strlen(real) - strlen(cwd) + 1 + 1); + cwdlen = strlen(cwd); + /* If CWD=="/", make cwdlen=0 to not omit slash when building rel path. */ + cwdlen -= !strcmp("/", cwd); + + if (!strncmp(real, cwd, cwdlen)) { + result = xmalloc(strlen(real) - cwdlen + 1 + 1); result[0] = '\0'; strcat(result, "."); - strcat(result, real + strlen(cwd)); + strcat(result, real + cwdlen); } else { result = xstrdup(real); } -- cgit v1.2.1