]>
Commit | Line | Data |
---|---|---|
5b8e6f85 DP |
1 | #include "cache.h" |
2 | ||
90b4a71c JH |
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) | |
9 | { | |
10 | struct stat st; | |
11 | return (!stat(path, &st) && S_ISDIR(st.st_mode)); | |
12 | } | |
13 | ||
5b8e6f85 DP |
14 | /* We allow "recursive" symbolic links. Only within reason, though. */ |
15 | #define MAXDEPTH 5 | |
16 | ||
e2a57aac CMN |
17 | /* |
18 | * Use this to get the real path, i.e. resolve links. If you want an | |
19 | * absolute path but don't mind links, use absolute_path. | |
20 | * | |
21 | * If path is our buffer, then return path, as it's already what the | |
22 | * user wants. | |
23 | */ | |
24 | const char *real_path(const char *path) | |
5b8e6f85 DP |
25 | { |
26 | static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1]; | |
27 | char cwd[1024] = ""; | |
1630726e | 28 | int buf_index = 1; |
5b8e6f85 DP |
29 | |
30 | int depth = MAXDEPTH; | |
31 | char *last_elem = NULL; | |
32 | struct stat st; | |
33 | ||
1d679de5 CMN |
34 | /* We've already done it */ |
35 | if (path == buf || path == next_buf) | |
36 | return path; | |
37 | ||
3efe5d1d MH |
38 | if (!*path) |
39 | die("The empty string is not a valid path"); | |
40 | ||
5b8e6f85 DP |
41 | if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) |
42 | die ("Too long path: %.*s", 60, path); | |
43 | ||
44 | while (depth--) { | |
90b4a71c | 45 | if (!is_directory(buf)) { |
d1c69255 | 46 | char *last_slash = find_last_dir_sep(buf); |
5b8e6f85 | 47 | if (last_slash) { |
5b8e6f85 | 48 | last_elem = xstrdup(last_slash + 1); |
f4c21e89 | 49 | last_slash[1] = '\0'; |
5b8e6f85 DP |
50 | } else { |
51 | last_elem = xstrdup(buf); | |
52 | *buf = '\0'; | |
53 | } | |
54 | } | |
55 | ||
56 | if (*buf) { | |
57 | if (!*cwd && !getcwd(cwd, sizeof(cwd))) | |
0721c314 | 58 | die_errno ("Could not get current working directory"); |
5b8e6f85 DP |
59 | |
60 | if (chdir(buf)) | |
0721c314 | 61 | die_errno ("Could not switch to '%s'", buf); |
5b8e6f85 DP |
62 | } |
63 | if (!getcwd(buf, PATH_MAX)) | |
0721c314 | 64 | die_errno ("Could not get current working directory"); |
5b8e6f85 DP |
65 | |
66 | if (last_elem) { | |
1630726e | 67 | size_t len = strlen(buf); |
5b8e6f85 DP |
68 | if (len + strlen(last_elem) + 2 > PATH_MAX) |
69 | die ("Too long path name: '%s/%s'", | |
70 | buf, last_elem); | |
d1c69255 | 71 | if (len && !is_dir_sep(buf[len-1])) |
ed0cb46e NTND |
72 | buf[len++] = '/'; |
73 | strcpy(buf + len, last_elem); | |
5b8e6f85 DP |
74 | free(last_elem); |
75 | last_elem = NULL; | |
76 | } | |
77 | ||
78 | if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) { | |
1630726e | 79 | ssize_t len = readlink(buf, next_buf, PATH_MAX); |
5b8e6f85 | 80 | if (len < 0) |
0721c314 | 81 | die_errno ("Invalid symlink '%s'", buf); |
737e31af JH |
82 | if (PATH_MAX <= len) |
83 | die("symbolic link too long: %s", buf); | |
5b8e6f85 DP |
84 | next_buf[len] = '\0'; |
85 | buf = next_buf; | |
86 | buf_index = 1 - buf_index; | |
87 | next_buf = bufs[buf_index]; | |
88 | } else | |
89 | break; | |
90 | } | |
91 | ||
92 | if (*cwd && chdir(cwd)) | |
0721c314 | 93 | die_errno ("Could not change back to '%s'", cwd); |
5b8e6f85 DP |
94 | |
95 | return buf; | |
96 | } | |
10c4c881 JS |
97 | |
98 | static const char *get_pwd_cwd(void) | |
99 | { | |
100 | static char cwd[PATH_MAX + 1]; | |
101 | char *pwd; | |
102 | struct stat cwd_stat, pwd_stat; | |
103 | if (getcwd(cwd, PATH_MAX) == NULL) | |
104 | return NULL; | |
105 | pwd = getenv("PWD"); | |
106 | if (pwd && strcmp(pwd, cwd)) { | |
107 | stat(cwd, &cwd_stat); | |
7d092adc JS |
108 | if ((cwd_stat.st_dev || cwd_stat.st_ino) && |
109 | !stat(pwd, &pwd_stat) && | |
10c4c881 JS |
110 | pwd_stat.st_dev == cwd_stat.st_dev && |
111 | pwd_stat.st_ino == cwd_stat.st_ino) { | |
112 | strlcpy(cwd, pwd, PATH_MAX); | |
113 | } | |
114 | } | |
115 | return cwd; | |
116 | } | |
117 | ||
e2a57aac CMN |
118 | /* |
119 | * Use this to get an absolute path from a relative one. If you want | |
120 | * to resolve links, you should use real_path. | |
121 | * | |
122 | * If the path is already absolute, then return path. As the user is | |
123 | * never meant to free the return value, we're safe. | |
124 | */ | |
125 | const char *absolute_path(const char *path) | |
10c4c881 JS |
126 | { |
127 | static char buf[PATH_MAX + 1]; | |
128 | ||
a0601dc1 MH |
129 | if (!*path) { |
130 | die("The empty string is not a valid path"); | |
131 | } else if (is_absolute_path(path)) { | |
10c4c881 JS |
132 | if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) |
133 | die("Too long path: %.*s", 60, path); | |
134 | } else { | |
b248e950 ES |
135 | size_t len; |
136 | const char *fmt; | |
10c4c881 JS |
137 | const char *cwd = get_pwd_cwd(); |
138 | if (!cwd) | |
0721c314 | 139 | die_errno("Cannot determine the current working directory"); |
b248e950 ES |
140 | len = strlen(cwd); |
141 | fmt = (len > 0 && is_dir_sep(cwd[len-1])) ? "%s%s" : "%s/%s"; | |
142 | if (snprintf(buf, PATH_MAX, fmt, cwd, path) >= PATH_MAX) | |
10c4c881 JS |
143 | die("Too long path: %.*s", 60, path); |
144 | } | |
145 | return buf; | |
146 | } | |
06876284 DI |
147 | |
148 | /* | |
149 | * Unlike prefix_path, this should be used if the named file does | |
150 | * not have to interact with index entry; i.e. name of a random file | |
151 | * on the filesystem. | |
152 | */ | |
153 | const char *prefix_filename(const char *pfx, int pfx_len, const char *arg) | |
154 | { | |
155 | static char path[PATH_MAX]; | |
156 | #ifndef WIN32 | |
157 | if (!pfx_len || is_absolute_path(arg)) | |
158 | return arg; | |
159 | memcpy(path, pfx, pfx_len); | |
160 | strcpy(path + pfx_len, arg); | |
161 | #else | |
162 | char *p; | |
163 | /* don't add prefix to absolute paths, but still replace '\' by '/' */ | |
164 | if (is_absolute_path(arg)) | |
165 | pfx_len = 0; | |
166 | else if (pfx_len) | |
167 | memcpy(path, pfx, pfx_len); | |
168 | strcpy(path + pfx_len, arg); | |
169 | for (p = path + pfx_len; *p; p++) | |
170 | if (*p == '\\') | |
171 | *p = '/'; | |
172 | #endif | |
173 | return path; | |
174 | } |