diff options
author | Johannes Sixt <johannes.sixt@telecom.at> | 2008-07-21 21:19:55 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2008-07-25 17:41:13 -0700 |
commit | 10c4c881c4d2cb0ece0508e7142e189e68445257 (patch) | |
tree | 32a3cc326352f5a1244144a987b8659b6fcc2bc5 /abspath.c | |
parent | Allow the built-in exec path to be relative to the command invocation path (diff) | |
download | tgif-10c4c881c4d2cb0ece0508e7142e189e68445257.tar.xz |
Allow add_path() to add non-existent directories to the path
This function had used make_absolute_path(); but this function dies if
the directory that contains the entry whose relative path was supplied in
the argument does not exist. This is a problem if the argument is, for
example, "../libexec/git-core", and that "../libexec" does not exist.
Since the resolution of symbolic links is not required for elements in
PATH, we can fall back to using make_nonrelative_path(), which simply
prepends $PWD to the path.
We have to move make_nonrelative_path() alongside make_absolute_path() in
abspath.c so that git-shell can be linked. See 5b8e6f85f.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'abspath.c')
-rw-r--r-- | abspath.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -66,3 +66,39 @@ const char *make_absolute_path(const char *path) return buf; } + +static const char *get_pwd_cwd(void) +{ + static char cwd[PATH_MAX + 1]; + char *pwd; + struct stat cwd_stat, pwd_stat; + if (getcwd(cwd, PATH_MAX) == NULL) + return NULL; + pwd = getenv("PWD"); + if (pwd && strcmp(pwd, cwd)) { + stat(cwd, &cwd_stat); + if (!stat(pwd, &pwd_stat) && + pwd_stat.st_dev == cwd_stat.st_dev && + pwd_stat.st_ino == cwd_stat.st_ino) { + strlcpy(cwd, pwd, PATH_MAX); + } + } + return cwd; +} + +const char *make_nonrelative_path(const char *path) +{ + static char buf[PATH_MAX + 1]; + + if (is_absolute_path(path)) { + if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) + die("Too long path: %.*s", 60, path); + } else { + const char *cwd = get_pwd_cwd(); + if (!cwd) + die("Cannot determine the current working directory"); + if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX) + die("Too long path: %.*s", 60, path); + } + return buf; +} |