diff options
author | Jürgen Bubeck <bubeck@xkrug.com> | 2016-11-09 17:14:42 +0100 |
---|---|---|
committer | Przemyslaw Pawelczyk <przemoc@gmail.com> | 2021-04-07 18:24:27 +0200 |
commit | 45f104b60356b096deaed0fd184f1c8b04a40d75 (patch) | |
tree | 555fa7bfd9040d12cec549f52cc2508ebd2ee03a /src/metaentry.c | |
parent | f8d5fc70290d19441ea513a10dbfe8cc074571f7 (diff) |
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 <przemoc@gmail.com>
Diffstat (limited to 'src/metaentry.c')
-rw-r--r-- | src/metaentry.c | 11 |
1 files 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); } |