Merge branch 'mg/mailmap-update'
[git/jnareb-git/bp-gitweb.git] / abspath.c
blobc91a29cb298a3ad792ff8745f3e8e0eb28d71678
1 #include "cache.h"
3 /*
4 * Do not use this for inspecting *tracked* content. When path is a
5 * symlink to a directory, we do not want to say it is a directory when
6 * dealing with tracked content in the working tree.
7 */
8 int is_directory(const char *path)
10 struct stat st;
11 return (!stat(path, &st) && S_ISDIR(st.st_mode));
14 /* We allow "recursive" symbolic links. Only within reason, though. */
15 #define MAXDEPTH 5
17 const char *make_absolute_path(const char *path)
19 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
20 char cwd[1024] = "";
21 int buf_index = 1;
23 int depth = MAXDEPTH;
24 char *last_elem = NULL;
25 struct stat st;
27 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
28 die ("Too long path: %.*s", 60, path);
30 while (depth--) {
31 if (!is_directory(buf)) {
32 char *last_slash = strrchr(buf, '/');
33 if (last_slash) {
34 *last_slash = '\0';
35 last_elem = xstrdup(last_slash + 1);
36 } else {
37 last_elem = xstrdup(buf);
38 *buf = '\0';
42 if (*buf) {
43 if (!*cwd && !getcwd(cwd, sizeof(cwd)))
44 die_errno ("Could not get current working directory");
46 if (chdir(buf))
47 die_errno ("Could not switch to '%s'", buf);
49 if (!getcwd(buf, PATH_MAX))
50 die_errno ("Could not get current working directory");
52 if (last_elem) {
53 size_t len = strlen(buf);
54 if (len + strlen(last_elem) + 2 > PATH_MAX)
55 die ("Too long path name: '%s/%s'",
56 buf, last_elem);
57 if (len && buf[len-1] != '/')
58 buf[len++] = '/';
59 strcpy(buf + len, last_elem);
60 free(last_elem);
61 last_elem = NULL;
64 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
65 ssize_t len = readlink(buf, next_buf, PATH_MAX);
66 if (len < 0)
67 die_errno ("Invalid symlink '%s'", buf);
68 if (PATH_MAX <= len)
69 die("symbolic link too long: %s", buf);
70 next_buf[len] = '\0';
71 buf = next_buf;
72 buf_index = 1 - buf_index;
73 next_buf = bufs[buf_index];
74 } else
75 break;
78 if (*cwd && chdir(cwd))
79 die_errno ("Could not change back to '%s'", cwd);
81 return buf;
84 static const char *get_pwd_cwd(void)
86 static char cwd[PATH_MAX + 1];
87 char *pwd;
88 struct stat cwd_stat, pwd_stat;
89 if (getcwd(cwd, PATH_MAX) == NULL)
90 return NULL;
91 pwd = getenv("PWD");
92 if (pwd && strcmp(pwd, cwd)) {
93 stat(cwd, &cwd_stat);
94 if (!stat(pwd, &pwd_stat) &&
95 pwd_stat.st_dev == cwd_stat.st_dev &&
96 pwd_stat.st_ino == cwd_stat.st_ino) {
97 strlcpy(cwd, pwd, PATH_MAX);
100 return cwd;
103 const char *make_nonrelative_path(const char *path)
105 static char buf[PATH_MAX + 1];
107 if (is_absolute_path(path)) {
108 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
109 die("Too long path: %.*s", 60, path);
110 } else {
111 const char *cwd = get_pwd_cwd();
112 if (!cwd)
113 die_errno("Cannot determine the current working directory");
114 if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
115 die("Too long path: %.*s", 60, path);
117 return buf;