]>
Commit | Line | Data |
---|---|---|
26c8a533 | 1 | /* |
3a429d3b | 2 | * Utilities for paths and pathnames |
26c8a533 LT |
3 | */ |
4 | #include "cache.h" | |
395de250 | 5 | #include "strbuf.h" |
a5ccdbe4 | 6 | #include "string-list.h" |
77a6d840 | 7 | #include "dir.h" |
26c8a533 | 8 | |
f66450ae | 9 | static int get_st_mode_bits(const char *path, int *mode) |
0117c2f0 TB |
10 | { |
11 | struct stat st; | |
12 | if (lstat(path, &st) < 0) | |
13 | return -1; | |
14 | *mode = st.st_mode; | |
15 | return 0; | |
16 | } | |
0117c2f0 | 17 | |
26c8a533 LT |
18 | static char bad_path[] = "/bad-path/"; |
19 | ||
4ef9caf5 | 20 | static struct strbuf *get_pathname(void) |
e7676d2f | 21 | { |
4ef9caf5 NTND |
22 | static struct strbuf pathname_array[4] = { |
23 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT | |
24 | }; | |
e7676d2f | 25 | static int index; |
4ef9caf5 NTND |
26 | struct strbuf *sb = &pathname_array[3 & ++index]; |
27 | strbuf_reset(sb); | |
28 | return sb; | |
e7676d2f LT |
29 | } |
30 | ||
26c8a533 LT |
31 | static char *cleanup_path(char *path) |
32 | { | |
33 | /* Clean it up */ | |
34 | if (!memcmp(path, "./", 2)) { | |
35 | path += 2; | |
36 | while (*path == '/') | |
37 | path++; | |
38 | } | |
39 | return path; | |
40 | } | |
41 | ||
4ef9caf5 NTND |
42 | static void strbuf_cleanup_path(struct strbuf *sb) |
43 | { | |
44 | char *path = cleanup_path(sb->buf); | |
45 | if (path > sb->buf) | |
46 | strbuf_remove(sb, 0, path - sb->buf); | |
47 | } | |
48 | ||
108bebea AR |
49 | char *mksnpath(char *buf, size_t n, const char *fmt, ...) |
50 | { | |
51 | va_list args; | |
52 | unsigned len; | |
53 | ||
54 | va_start(args, fmt); | |
55 | len = vsnprintf(buf, n, fmt, args); | |
56 | va_end(args); | |
57 | if (len >= n) { | |
9db56f71 | 58 | strlcpy(buf, bad_path, n); |
108bebea AR |
59 | return buf; |
60 | } | |
61 | return cleanup_path(buf); | |
62 | } | |
63 | ||
557bd833 | 64 | static int dir_prefix(const char *buf, const char *dir) |
fe2d7776 | 65 | { |
557bd833 NTND |
66 | int len = strlen(dir); |
67 | return !strncmp(buf, dir, len) && | |
68 | (is_dir_sep(buf[len]) || buf[len] == '\0'); | |
69 | } | |
fe2d7776 | 70 | |
557bd833 NTND |
71 | /* $buf =~ m|$dir/+$file| but without regex */ |
72 | static int is_dir_file(const char *buf, const char *dir, const char *file) | |
73 | { | |
74 | int len = strlen(dir); | |
75 | if (strncmp(buf, dir, len) || !is_dir_sep(buf[len])) | |
76 | return 0; | |
77 | while (is_dir_sep(buf[len])) | |
78 | len++; | |
79 | return !strcmp(buf + len, file); | |
80 | } | |
81 | ||
82 | static void replace_dir(struct strbuf *buf, int len, const char *newdir) | |
83 | { | |
84 | int newlen = strlen(newdir); | |
85 | int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) && | |
86 | !is_dir_sep(newdir[newlen - 1]); | |
87 | if (need_sep) | |
88 | len--; /* keep one char, to be replaced with '/' */ | |
89 | strbuf_splice(buf, 0, len, newdir, newlen); | |
90 | if (need_sep) | |
91 | buf->buf[newlen] = '/'; | |
92 | } | |
93 | ||
c7b3a3d2 | 94 | static const char *common_list[] = { |
df56607d | 95 | "/branches", "/hooks", "/info", "!/logs", "/lost-found", |
529fef20 | 96 | "/objects", "/refs", "/remotes", "/worktrees", "/rr-cache", "/svn", |
77a6d840 | 97 | "config", "!gc.pid", "packed-refs", "shallow", |
c7b3a3d2 NTND |
98 | NULL |
99 | }; | |
100 | ||
101 | static void update_common_dir(struct strbuf *buf, int git_dir_len) | |
102 | { | |
103 | char *base = buf->buf + git_dir_len; | |
104 | const char **p; | |
105 | ||
6cfbdcb2 NTND |
106 | if (is_dir_file(base, "logs", "HEAD") || |
107 | is_dir_file(base, "info", "sparse-checkout")) | |
c7b3a3d2 NTND |
108 | return; /* keep this in $GIT_DIR */ |
109 | for (p = common_list; *p; p++) { | |
110 | const char *path = *p; | |
111 | int is_dir = 0; | |
77a6d840 NTND |
112 | if (*path == '!') |
113 | path++; | |
c7b3a3d2 NTND |
114 | if (*path == '/') { |
115 | path++; | |
116 | is_dir = 1; | |
117 | } | |
118 | if (is_dir && dir_prefix(base, path)) { | |
119 | replace_dir(buf, git_dir_len, get_git_common_dir()); | |
120 | return; | |
121 | } | |
122 | if (!is_dir && !strcmp(base, path)) { | |
123 | replace_dir(buf, git_dir_len, get_git_common_dir()); | |
124 | return; | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
77a6d840 NTND |
129 | void report_linked_checkout_garbage(void) |
130 | { | |
131 | struct strbuf sb = STRBUF_INIT; | |
132 | const char **p; | |
133 | int len; | |
134 | ||
135 | if (!git_common_dir_env) | |
136 | return; | |
137 | strbuf_addf(&sb, "%s/", get_git_dir()); | |
138 | len = sb.len; | |
139 | for (p = common_list; *p; p++) { | |
140 | const char *path = *p; | |
141 | if (*path == '!') | |
142 | continue; | |
143 | strbuf_setlen(&sb, len); | |
144 | strbuf_addstr(&sb, path); | |
145 | if (file_exists(sb.buf)) | |
146 | report_garbage("unused in linked checkout", sb.buf); | |
147 | } | |
148 | strbuf_release(&sb); | |
fe2d7776 AR |
149 | } |
150 | ||
557bd833 NTND |
151 | static void adjust_git_path(struct strbuf *buf, int git_dir_len) |
152 | { | |
153 | const char *base = buf->buf + git_dir_len; | |
154 | if (git_graft_env && is_dir_file(base, "info", "grafts")) | |
155 | strbuf_splice(buf, 0, buf->len, | |
156 | get_graft_file(), strlen(get_graft_file())); | |
157 | else if (git_index_env && !strcmp(base, "index")) | |
158 | strbuf_splice(buf, 0, buf->len, | |
159 | get_index_file(), strlen(get_index_file())); | |
160 | else if (git_db_env && dir_prefix(base, "objects")) | |
161 | replace_dir(buf, git_dir_len + 7, get_object_directory()); | |
c7b3a3d2 NTND |
162 | else if (git_common_dir_env) |
163 | update_common_dir(buf, git_dir_len); | |
557bd833 NTND |
164 | } |
165 | ||
8afdaf39 | 166 | static void do_git_path(struct strbuf *buf, const char *fmt, va_list args) |
fe2d7776 | 167 | { |
557bd833 NTND |
168 | int gitdir_len; |
169 | strbuf_addstr(buf, get_git_dir()); | |
4ef9caf5 NTND |
170 | if (buf->len && !is_dir_sep(buf->buf[buf->len - 1])) |
171 | strbuf_addch(buf, '/'); | |
557bd833 | 172 | gitdir_len = buf->len; |
4ef9caf5 | 173 | strbuf_vaddf(buf, fmt, args); |
557bd833 | 174 | adjust_git_path(buf, gitdir_len); |
4ef9caf5 | 175 | strbuf_cleanup_path(buf); |
fe2d7776 AR |
176 | } |
177 | ||
1a83c240 | 178 | void strbuf_git_path(struct strbuf *sb, const char *fmt, ...) |
aba13e7c AR |
179 | { |
180 | va_list args; | |
181 | va_start(args, fmt); | |
8afdaf39 | 182 | do_git_path(sb, fmt, args); |
aba13e7c | 183 | va_end(args); |
aba13e7c AR |
184 | } |
185 | ||
57a23b77 | 186 | const char *git_path(const char *fmt, ...) |
aba13e7c | 187 | { |
57a23b77 | 188 | struct strbuf *pathname = get_pathname(); |
aba13e7c AR |
189 | va_list args; |
190 | va_start(args, fmt); | |
57a23b77 | 191 | do_git_path(pathname, fmt, args); |
aba13e7c | 192 | va_end(args); |
57a23b77 | 193 | return pathname->buf; |
aba13e7c AR |
194 | } |
195 | ||
aba13e7c | 196 | char *git_pathdup(const char *fmt, ...) |
21cf3227 | 197 | { |
4ef9caf5 | 198 | struct strbuf path = STRBUF_INIT; |
21cf3227 | 199 | va_list args; |
21cf3227 | 200 | va_start(args, fmt); |
8afdaf39 | 201 | do_git_path(&path, fmt, args); |
21cf3227 | 202 | va_end(args); |
4ef9caf5 | 203 | return strbuf_detach(&path, NULL); |
21cf3227 HKNN |
204 | } |
205 | ||
21cf3227 | 206 | char *mkpathdup(const char *fmt, ...) |
26c8a533 | 207 | { |
21cf3227 | 208 | struct strbuf sb = STRBUF_INIT; |
26c8a533 | 209 | va_list args; |
26c8a533 | 210 | va_start(args, fmt); |
21cf3227 | 211 | strbuf_vaddf(&sb, fmt, args); |
26c8a533 | 212 | va_end(args); |
4ef9caf5 NTND |
213 | strbuf_cleanup_path(&sb); |
214 | return strbuf_detach(&sb, NULL); | |
26c8a533 LT |
215 | } |
216 | ||
dcf69262 | 217 | const char *mkpath(const char *fmt, ...) |
26c8a533 | 218 | { |
26c8a533 | 219 | va_list args; |
4ef9caf5 | 220 | struct strbuf *pathname = get_pathname(); |
26c8a533 | 221 | va_start(args, fmt); |
4ef9caf5 | 222 | strbuf_vaddf(pathname, fmt, args); |
26c8a533 | 223 | va_end(args); |
4ef9caf5 | 224 | return cleanup_path(pathname->buf); |
26c8a533 | 225 | } |
f2db68ed | 226 | |
21cf3227 HKNN |
227 | void home_config_paths(char **global, char **xdg, char *file) |
228 | { | |
229 | char *xdg_home = getenv("XDG_CONFIG_HOME"); | |
230 | char *home = getenv("HOME"); | |
231 | char *to_free = NULL; | |
232 | ||
233 | if (!home) { | |
234 | if (global) | |
235 | *global = NULL; | |
236 | } else { | |
237 | if (!xdg_home) { | |
238 | to_free = mkpathdup("%s/.config", home); | |
239 | xdg_home = to_free; | |
240 | } | |
241 | if (global) | |
242 | *global = mkpathdup("%s/.gitconfig", home); | |
243 | } | |
244 | ||
06b2d872 MM |
245 | if (xdg) { |
246 | if (!xdg_home) | |
247 | *xdg = NULL; | |
248 | else | |
249 | *xdg = mkpathdup("%s/git/%s", xdg_home, file); | |
250 | } | |
21cf3227 HKNN |
251 | |
252 | free(to_free); | |
253 | } | |
254 | ||
dcf69262 | 255 | const char *git_path_submodule(const char *path, const char *fmt, ...) |
0bad611b | 256 | { |
4ef9caf5 | 257 | struct strbuf *buf = get_pathname(); |
0bad611b HV |
258 | const char *git_dir; |
259 | va_list args; | |
0bad611b | 260 | |
4ef9caf5 NTND |
261 | strbuf_addstr(buf, path); |
262 | if (buf->len && buf->buf[buf->len - 1] != '/') | |
263 | strbuf_addch(buf, '/'); | |
264 | strbuf_addstr(buf, ".git"); | |
0bad611b | 265 | |
4ef9caf5 | 266 | git_dir = read_gitfile(buf->buf); |
0bad611b | 267 | if (git_dir) { |
4ef9caf5 NTND |
268 | strbuf_reset(buf); |
269 | strbuf_addstr(buf, git_dir); | |
0bad611b | 270 | } |
4ef9caf5 | 271 | strbuf_addch(buf, '/'); |
0bad611b HV |
272 | |
273 | va_start(args, fmt); | |
4ef9caf5 | 274 | strbuf_vaddf(buf, fmt, args); |
0bad611b | 275 | va_end(args); |
4ef9caf5 NTND |
276 | strbuf_cleanup_path(buf); |
277 | return buf->buf; | |
0bad611b | 278 | } |
f2db68ed | 279 | |
c847f537 | 280 | int validate_headref(const char *path) |
0870ca7f JH |
281 | { |
282 | struct stat st; | |
283 | char *buf, buffer[256]; | |
c847f537 | 284 | unsigned char sha1[20]; |
0104ca09 HO |
285 | int fd; |
286 | ssize_t len; | |
0870ca7f JH |
287 | |
288 | if (lstat(path, &st) < 0) | |
289 | return -1; | |
290 | ||
291 | /* Make sure it is a "refs/.." symlink */ | |
292 | if (S_ISLNK(st.st_mode)) { | |
293 | len = readlink(path, buffer, sizeof(buffer)-1); | |
222b1673 | 294 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
0870ca7f JH |
295 | return 0; |
296 | return -1; | |
297 | } | |
298 | ||
299 | /* | |
300 | * Anything else, just open it and try to see if it is a symbolic ref. | |
301 | */ | |
302 | fd = open(path, O_RDONLY); | |
303 | if (fd < 0) | |
304 | return -1; | |
93d26e4c | 305 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
0870ca7f JH |
306 | close(fd); |
307 | ||
308 | /* | |
309 | * Is it a symbolic ref? | |
310 | */ | |
c847f537 | 311 | if (len < 4) |
0870ca7f | 312 | return -1; |
c847f537 JH |
313 | if (!memcmp("ref:", buffer, 4)) { |
314 | buf = buffer + 4; | |
315 | len -= 4; | |
316 | while (len && isspace(*buf)) | |
317 | buf++, len--; | |
222b1673 | 318 | if (len >= 5 && !memcmp("refs/", buf, 5)) |
c847f537 JH |
319 | return 0; |
320 | } | |
321 | ||
322 | /* | |
323 | * Is this a detached HEAD? | |
324 | */ | |
325 | if (!get_sha1_hex(buffer, sha1)) | |
0870ca7f | 326 | return 0; |
c847f537 | 327 | |
0870ca7f JH |
328 | return -1; |
329 | } | |
330 | ||
395de250 | 331 | static struct passwd *getpw_str(const char *username, size_t len) |
54f4b874 | 332 | { |
d79374c7 | 333 | struct passwd *pw; |
5c0b13f8 | 334 | char *username_z = xmemdupz(username, len); |
395de250 MM |
335 | pw = getpwnam(username_z); |
336 | free(username_z); | |
337 | return pw; | |
338 | } | |
54f4b874 | 339 | |
395de250 MM |
340 | /* |
341 | * Return a string with ~ and ~user expanded via getpw*. If buf != NULL, | |
342 | * then it is a newly allocated string. Returns NULL on getpw failure or | |
343 | * if path is NULL. | |
344 | */ | |
345 | char *expand_user_path(const char *path) | |
346 | { | |
347 | struct strbuf user_path = STRBUF_INIT; | |
395de250 MM |
348 | const char *to_copy = path; |
349 | ||
350 | if (path == NULL) | |
351 | goto return_null; | |
352 | if (path[0] == '~') { | |
53ec551c | 353 | const char *first_slash = strchrnul(path, '/'); |
395de250 MM |
354 | const char *username = path + 1; |
355 | size_t username_len = first_slash - username; | |
df2a79f4 MM |
356 | if (username_len == 0) { |
357 | const char *home = getenv("HOME"); | |
79bf1490 JN |
358 | if (!home) |
359 | goto return_null; | |
cedc61a9 | 360 | strbuf_addstr(&user_path, home); |
df2a79f4 MM |
361 | } else { |
362 | struct passwd *pw = getpw_str(username, username_len); | |
363 | if (!pw) | |
364 | goto return_null; | |
cedc61a9 | 365 | strbuf_addstr(&user_path, pw->pw_dir); |
54f4b874 | 366 | } |
395de250 | 367 | to_copy = first_slash; |
d79374c7 | 368 | } |
cedc61a9 | 369 | strbuf_addstr(&user_path, to_copy); |
395de250 MM |
370 | return strbuf_detach(&user_path, NULL); |
371 | return_null: | |
372 | strbuf_release(&user_path); | |
373 | return NULL; | |
54f4b874 AE |
374 | } |
375 | ||
d79374c7 JH |
376 | /* |
377 | * First, one directory to try is determined by the following algorithm. | |
378 | * | |
379 | * (0) If "strict" is given, the path is used as given and no DWIM is | |
380 | * done. Otherwise: | |
381 | * (1) "~/path" to mean path under the running user's home directory; | |
382 | * (2) "~user/path" to mean path under named user's home directory; | |
383 | * (3) "relative/path" to mean cwd relative directory; or | |
384 | * (4) "/absolute/path" to mean absolute directory. | |
385 | * | |
c8c3f1d0 PT |
386 | * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git" |
387 | * in this order. We select the first one that is a valid git repository, and | |
388 | * chdir() to it. If none match, or we fail to chdir, we return NULL. | |
d79374c7 JH |
389 | * |
390 | * If all goes well, we return the directory we used to chdir() (but | |
391 | * before ~user is expanded), avoiding getcwd() resolving symbolic | |
392 | * links. User relative paths are also returned as they are given, | |
393 | * except DWIM suffixing. | |
394 | */ | |
1c64b48e | 395 | const char *enter_repo(const char *path, int strict) |
54f4b874 | 396 | { |
d79374c7 JH |
397 | static char used_path[PATH_MAX]; |
398 | static char validated_path[PATH_MAX]; | |
399 | ||
400 | if (!path) | |
54f4b874 AE |
401 | return NULL; |
402 | ||
d79374c7 JH |
403 | if (!strict) { |
404 | static const char *suffix[] = { | |
b3256eb8 | 405 | "/.git", "", ".git/.git", ".git", NULL, |
d79374c7 | 406 | }; |
03106768 | 407 | const char *gitfile; |
d79374c7 JH |
408 | int len = strlen(path); |
409 | int i; | |
1c64b48e | 410 | while ((1 < len) && (path[len-1] == '/')) |
d79374c7 | 411 | len--; |
1c64b48e | 412 | |
d79374c7 | 413 | if (PATH_MAX <= len) |
54f4b874 | 414 | return NULL; |
1c64b48e EFL |
415 | strncpy(used_path, path, len); used_path[len] = 0 ; |
416 | strcpy(validated_path, used_path); | |
417 | ||
418 | if (used_path[0] == '~') { | |
419 | char *newpath = expand_user_path(used_path); | |
395de250 MM |
420 | if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { |
421 | free(newpath); | |
d79374c7 | 422 | return NULL; |
395de250 MM |
423 | } |
424 | /* | |
425 | * Copy back into the static buffer. A pity | |
426 | * since newpath was not bounded, but other | |
427 | * branches of the if are limited by PATH_MAX | |
428 | * anyway. | |
429 | */ | |
430 | strcpy(used_path, newpath); free(newpath); | |
d79374c7 JH |
431 | } |
432 | else if (PATH_MAX - 10 < len) | |
433 | return NULL; | |
1c64b48e | 434 | len = strlen(used_path); |
d79374c7 | 435 | for (i = 0; suffix[i]; i++) { |
b3256eb8 | 436 | struct stat st; |
1c64b48e | 437 | strcpy(used_path + len, suffix[i]); |
b3256eb8 JK |
438 | if (!stat(used_path, &st) && |
439 | (S_ISREG(st.st_mode) || | |
440 | (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) { | |
d79374c7 JH |
441 | strcat(validated_path, suffix[i]); |
442 | break; | |
443 | } | |
444 | } | |
03106768 PH |
445 | if (!suffix[i]) |
446 | return NULL; | |
447 | gitfile = read_gitfile(used_path) ; | |
448 | if (gitfile) | |
449 | strcpy(used_path, gitfile); | |
450 | if (chdir(used_path)) | |
0870ca7f | 451 | return NULL; |
d79374c7 | 452 | path = validated_path; |
0870ca7f | 453 | } |
d79374c7 JH |
454 | else if (chdir(path)) |
455 | return NULL; | |
54f4b874 | 456 | |
d79374c7 | 457 | if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && |
c847f537 | 458 | validate_headref("HEAD") == 0) { |
717c3972 | 459 | set_git_dir("."); |
1644162a | 460 | check_repository_format(); |
d79374c7 | 461 | return path; |
54f4b874 AE |
462 | } |
463 | ||
464 | return NULL; | |
465 | } | |
138086a7 | 466 | |
cbe43b84 | 467 | static int calc_shared_perm(int mode) |
138086a7 | 468 | { |
cbe43b84 | 469 | int tweak; |
138086a7 | 470 | |
5a688fe4 | 471 | if (shared_repository < 0) |
cbe43b84 | 472 | tweak = -shared_repository; |
5a688fe4 | 473 | else |
cbe43b84 | 474 | tweak = shared_repository; |
5a688fe4 JH |
475 | |
476 | if (!(mode & S_IWUSR)) | |
477 | tweak &= ~0222; | |
478 | if (mode & S_IXUSR) | |
479 | /* Copy read bits to execute bits */ | |
480 | tweak |= (tweak & 0444) >> 2; | |
481 | if (shared_repository < 0) | |
482 | mode = (mode & ~0777) | tweak; | |
483 | else | |
8c6202d8 | 484 | mode |= tweak; |
06cbe855 | 485 | |
cbe43b84 TB |
486 | return mode; |
487 | } | |
488 | ||
489 | ||
490 | int adjust_shared_perm(const char *path) | |
491 | { | |
492 | int old_mode, new_mode; | |
493 | ||
494 | if (!shared_repository) | |
495 | return 0; | |
496 | if (get_st_mode_bits(path, &old_mode) < 0) | |
497 | return -1; | |
498 | ||
499 | new_mode = calc_shared_perm(old_mode); | |
500 | if (S_ISDIR(old_mode)) { | |
06cbe855 | 501 | /* Copy read bits to execute bits */ |
cbe43b84 TB |
502 | new_mode |= (new_mode & 0444) >> 2; |
503 | new_mode |= FORCE_DIR_SET_GID; | |
06cbe855 HO |
504 | } |
505 | ||
cbe43b84 TB |
506 | if (((old_mode ^ new_mode) & ~S_IFMT) && |
507 | chmod(path, (new_mode & ~S_IFMT)) < 0) | |
138086a7 JH |
508 | return -2; |
509 | return 0; | |
510 | } | |
e5392c51 | 511 | |
7fbd4221 JX |
512 | static int have_same_root(const char *path1, const char *path2) |
513 | { | |
514 | int is_abs1, is_abs2; | |
515 | ||
516 | is_abs1 = is_absolute_path(path1); | |
517 | is_abs2 = is_absolute_path(path2); | |
518 | return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) || | |
519 | (!is_abs1 && !is_abs2); | |
520 | } | |
521 | ||
e02ca72f JX |
522 | /* |
523 | * Give path as relative to prefix. | |
524 | * | |
525 | * The strbuf may or may not be used, so do not assume it contains the | |
526 | * returned path. | |
527 | */ | |
528 | const char *relative_path(const char *in, const char *prefix, | |
529 | struct strbuf *sb) | |
044bbbcb | 530 | { |
e02ca72f JX |
531 | int in_len = in ? strlen(in) : 0; |
532 | int prefix_len = prefix ? strlen(prefix) : 0; | |
533 | int in_off = 0; | |
534 | int prefix_off = 0; | |
288123f0 JH |
535 | int i = 0, j = 0; |
536 | ||
e02ca72f JX |
537 | if (!in_len) |
538 | return "./"; | |
539 | else if (!prefix_len) | |
540 | return in; | |
541 | ||
7fbd4221 JX |
542 | if (have_same_root(in, prefix)) { |
543 | /* bypass dos_drive, for "c:" is identical to "C:" */ | |
544 | if (has_dos_drive_prefix(in)) { | |
545 | i = 2; | |
546 | j = 2; | |
547 | } | |
548 | } else { | |
549 | return in; | |
550 | } | |
551 | ||
e02ca72f JX |
552 | while (i < prefix_len && j < in_len && prefix[i] == in[j]) { |
553 | if (is_dir_sep(prefix[i])) { | |
554 | while (is_dir_sep(prefix[i])) | |
288123f0 | 555 | i++; |
e02ca72f JX |
556 | while (is_dir_sep(in[j])) |
557 | j++; | |
558 | prefix_off = i; | |
559 | in_off = j; | |
560 | } else { | |
561 | i++; | |
562 | j++; | |
563 | } | |
564 | } | |
565 | ||
566 | if ( | |
567 | /* "prefix" seems like prefix of "in" */ | |
568 | i >= prefix_len && | |
569 | /* | |
570 | * but "/foo" is not a prefix of "/foobar" | |
571 | * (i.e. prefix not end with '/') | |
572 | */ | |
573 | prefix_off < prefix_len) { | |
574 | if (j >= in_len) { | |
575 | /* in="/a/b", prefix="/a/b" */ | |
576 | in_off = in_len; | |
577 | } else if (is_dir_sep(in[j])) { | |
578 | /* in="/a/b/c", prefix="/a/b" */ | |
579 | while (is_dir_sep(in[j])) | |
288123f0 | 580 | j++; |
e02ca72f JX |
581 | in_off = j; |
582 | } else { | |
583 | /* in="/a/bbb/c", prefix="/a/b" */ | |
584 | i = prefix_off; | |
585 | } | |
586 | } else if ( | |
587 | /* "in" is short than "prefix" */ | |
588 | j >= in_len && | |
589 | /* "in" not end with '/' */ | |
590 | in_off < in_len) { | |
591 | if (is_dir_sep(prefix[i])) { | |
592 | /* in="/a/b", prefix="/a/b/c/" */ | |
593 | while (is_dir_sep(prefix[i])) | |
594 | i++; | |
595 | in_off = in_len; | |
596 | } | |
597 | } | |
598 | in += in_off; | |
599 | in_len -= in_off; | |
600 | ||
601 | if (i >= prefix_len) { | |
602 | if (!in_len) | |
603 | return "./"; | |
604 | else | |
605 | return in; | |
606 | } | |
607 | ||
608 | strbuf_reset(sb); | |
609 | strbuf_grow(sb, in_len); | |
610 | ||
611 | while (i < prefix_len) { | |
612 | if (is_dir_sep(prefix[i])) { | |
613 | strbuf_addstr(sb, "../"); | |
614 | while (is_dir_sep(prefix[i])) | |
615 | i++; | |
288123f0 | 616 | continue; |
288123f0 JH |
617 | } |
618 | i++; | |
288123f0 | 619 | } |
e02ca72f JX |
620 | if (!is_dir_sep(prefix[prefix_len - 1])) |
621 | strbuf_addstr(sb, "../"); | |
622 | ||
623 | strbuf_addstr(sb, in); | |
624 | ||
625 | return sb->buf; | |
044bbbcb | 626 | } |
ae299be0 | 627 | |
41894ae3 JX |
628 | /* |
629 | * A simpler implementation of relative_path | |
630 | * | |
631 | * Get relative path by removing "prefix" from "in". This function | |
632 | * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter | |
633 | * to increase performance when traversing the path to work_tree. | |
634 | */ | |
635 | const char *remove_leading_path(const char *in, const char *prefix) | |
636 | { | |
637 | static char buf[PATH_MAX + 1]; | |
638 | int i = 0, j = 0; | |
639 | ||
640 | if (!prefix || !prefix[0]) | |
641 | return in; | |
642 | while (prefix[i]) { | |
643 | if (is_dir_sep(prefix[i])) { | |
644 | if (!is_dir_sep(in[j])) | |
645 | return in; | |
646 | while (is_dir_sep(prefix[i])) | |
647 | i++; | |
648 | while (is_dir_sep(in[j])) | |
649 | j++; | |
650 | continue; | |
651 | } else if (in[j] != prefix[i]) { | |
652 | return in; | |
653 | } | |
654 | i++; | |
655 | j++; | |
656 | } | |
657 | if ( | |
658 | /* "/foo" is a prefix of "/foo" */ | |
659 | in[j] && | |
660 | /* "/foo" is not a prefix of "/foobar" */ | |
661 | !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j]) | |
662 | ) | |
663 | return in; | |
664 | while (is_dir_sep(in[j])) | |
665 | j++; | |
666 | if (!in[j]) | |
667 | strcpy(buf, "."); | |
668 | else | |
669 | strcpy(buf, in + j); | |
670 | return buf; | |
671 | } | |
672 | ||
ae299be0 | 673 | /* |
f2a782b8 | 674 | * It is okay if dst == src, but they should not overlap otherwise. |
ae299be0 | 675 | * |
f2a782b8 JS |
676 | * Performs the following normalizations on src, storing the result in dst: |
677 | * - Ensures that components are separated by '/' (Windows only) | |
678 | * - Squashes sequences of '/'. | |
ae299be0 DR |
679 | * - Removes "." components. |
680 | * - Removes ".." components, and the components the precede them. | |
f2a782b8 JS |
681 | * Returns failure (non-zero) if a ".." component appears as first path |
682 | * component anytime during the normalization. Otherwise, returns success (0). | |
ae299be0 DR |
683 | * |
684 | * Note that this function is purely textual. It does not follow symlinks, | |
685 | * verify the existence of the path, or make any system calls. | |
645a29c4 NTND |
686 | * |
687 | * prefix_len != NULL is for a specific case of prefix_pathspec(): | |
688 | * assume that src == dst and src[0..prefix_len-1] is already | |
689 | * normalized, any time "../" eats up to the prefix_len part, | |
690 | * prefix_len is reduced. In the end prefix_len is the remaining | |
691 | * prefix that has not been overridden by user pathspec. | |
ae299be0 | 692 | */ |
645a29c4 | 693 | int normalize_path_copy_len(char *dst, const char *src, int *prefix_len) |
ae299be0 | 694 | { |
f3cad0ad | 695 | char *dst0; |
ae299be0 | 696 | |
f3cad0ad JS |
697 | if (has_dos_drive_prefix(src)) { |
698 | *dst++ = *src++; | |
699 | *dst++ = *src++; | |
ae299be0 | 700 | } |
f3cad0ad | 701 | dst0 = dst; |
ae299be0 | 702 | |
f3cad0ad | 703 | if (is_dir_sep(*src)) { |
ae299be0 | 704 | *dst++ = '/'; |
f3cad0ad JS |
705 | while (is_dir_sep(*src)) |
706 | src++; | |
707 | } | |
708 | ||
709 | for (;;) { | |
710 | char c = *src; | |
711 | ||
712 | /* | |
713 | * A path component that begins with . could be | |
714 | * special: | |
715 | * (1) "." and ends -- ignore and terminate. | |
716 | * (2) "./" -- ignore them, eat slash and continue. | |
717 | * (3) ".." and ends -- strip one and terminate. | |
718 | * (4) "../" -- strip one, eat slash and continue. | |
719 | */ | |
720 | if (c == '.') { | |
721 | if (!src[1]) { | |
722 | /* (1) */ | |
723 | src++; | |
724 | } else if (is_dir_sep(src[1])) { | |
725 | /* (2) */ | |
726 | src += 2; | |
727 | while (is_dir_sep(*src)) | |
728 | src++; | |
729 | continue; | |
730 | } else if (src[1] == '.') { | |
731 | if (!src[2]) { | |
732 | /* (3) */ | |
733 | src += 2; | |
734 | goto up_one; | |
735 | } else if (is_dir_sep(src[2])) { | |
736 | /* (4) */ | |
737 | src += 3; | |
738 | while (is_dir_sep(*src)) | |
739 | src++; | |
740 | goto up_one; | |
741 | } | |
742 | } | |
743 | } | |
ae299be0 | 744 | |
f3cad0ad JS |
745 | /* copy up to the next '/', and eat all '/' */ |
746 | while ((c = *src++) != '\0' && !is_dir_sep(c)) | |
747 | *dst++ = c; | |
748 | if (is_dir_sep(c)) { | |
749 | *dst++ = '/'; | |
750 | while (is_dir_sep(c)) | |
751 | c = *src++; | |
752 | src--; | |
753 | } else if (!c) | |
754 | break; | |
755 | continue; | |
756 | ||
757 | up_one: | |
758 | /* | |
759 | * dst0..dst is prefix portion, and dst[-1] is '/'; | |
760 | * go up one level. | |
761 | */ | |
f42302b4 JS |
762 | dst--; /* go to trailing '/' */ |
763 | if (dst <= dst0) | |
f3cad0ad | 764 | return -1; |
f42302b4 JS |
765 | /* Windows: dst[-1] cannot be backslash anymore */ |
766 | while (dst0 < dst && dst[-1] != '/') | |
767 | dst--; | |
645a29c4 NTND |
768 | if (prefix_len && *prefix_len > dst - dst0) |
769 | *prefix_len = dst - dst0; | |
f3cad0ad | 770 | } |
ae299be0 | 771 | *dst = '\0'; |
f3cad0ad | 772 | return 0; |
ae299be0 | 773 | } |
0454dd93 | 774 | |
645a29c4 NTND |
775 | int normalize_path_copy(char *dst, const char *src) |
776 | { | |
777 | return normalize_path_copy_len(dst, src, NULL); | |
778 | } | |
779 | ||
0454dd93 DR |
780 | /* |
781 | * path = Canonical absolute path | |
9e2326c7 MH |
782 | * prefixes = string_list containing normalized, absolute paths without |
783 | * trailing slashes (except for the root directory, which is denoted by "/"). | |
0454dd93 | 784 | * |
9e2326c7 | 785 | * Determines, for each path in prefixes, whether the "prefix" |
0454dd93 DR |
786 | * is an ancestor directory of path. Returns the length of the longest |
787 | * ancestor directory, excluding any trailing slashes, or -1 if no prefix | |
31171d9e MH |
788 | * is an ancestor. (Note that this means 0 is returned if prefixes is |
789 | * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories | |
0454dd93 DR |
790 | * are not considered to be their own ancestors. path must be in a |
791 | * canonical form: empty components, or "." or ".." components are not | |
9e2326c7 | 792 | * allowed. |
0454dd93 | 793 | */ |
31171d9e | 794 | int longest_ancestor_length(const char *path, struct string_list *prefixes) |
0454dd93 | 795 | { |
a5ccdbe4 | 796 | int i, max_len = -1; |
0454dd93 | 797 | |
31171d9e | 798 | if (!strcmp(path, "/")) |
0454dd93 DR |
799 | return -1; |
800 | ||
31171d9e MH |
801 | for (i = 0; i < prefixes->nr; i++) { |
802 | const char *ceil = prefixes->items[i].string; | |
a5ccdbe4 MH |
803 | int len = strlen(ceil); |
804 | ||
9e2326c7 MH |
805 | if (len == 1 && ceil[0] == '/') |
806 | len = 0; /* root matches anything, with length 0 */ | |
807 | else if (!strncmp(path, ceil, len) && path[len] == '/') | |
808 | ; /* match of length len */ | |
809 | else | |
810 | continue; /* no match */ | |
0454dd93 | 811 | |
9e2326c7 | 812 | if (len > max_len) |
0454dd93 | 813 | max_len = len; |
0454dd93 DR |
814 | } |
815 | ||
816 | return max_len; | |
817 | } | |
4fcc86b0 JS |
818 | |
819 | /* strip arbitrary amount of directory separators at end of path */ | |
820 | static inline int chomp_trailing_dir_sep(const char *path, int len) | |
821 | { | |
822 | while (len && is_dir_sep(path[len - 1])) | |
823 | len--; | |
824 | return len; | |
825 | } | |
826 | ||
827 | /* | |
828 | * If path ends with suffix (complete path components), returns the | |
829 | * part before suffix (sans trailing directory separators). | |
830 | * Otherwise returns NULL. | |
831 | */ | |
832 | char *strip_path_suffix(const char *path, const char *suffix) | |
833 | { | |
834 | int path_len = strlen(path), suffix_len = strlen(suffix); | |
835 | ||
836 | while (suffix_len) { | |
837 | if (!path_len) | |
838 | return NULL; | |
839 | ||
840 | if (is_dir_sep(path[path_len - 1])) { | |
841 | if (!is_dir_sep(suffix[suffix_len - 1])) | |
842 | return NULL; | |
843 | path_len = chomp_trailing_dir_sep(path, path_len); | |
844 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); | |
845 | } | |
846 | else if (path[--path_len] != suffix[--suffix_len]) | |
847 | return NULL; | |
848 | } | |
849 | ||
850 | if (path_len && !is_dir_sep(path[path_len - 1])) | |
851 | return NULL; | |
852 | return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); | |
853 | } | |
34b6cb8b SP |
854 | |
855 | int daemon_avoid_alias(const char *p) | |
856 | { | |
857 | int sl, ndot; | |
858 | ||
859 | /* | |
860 | * This resurrects the belts and suspenders paranoia check by HPA | |
861 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() | |
9517e6b8 | 862 | * does not do getcwd() based path canonicalization. |
34b6cb8b SP |
863 | * |
864 | * sl becomes true immediately after seeing '/' and continues to | |
865 | * be true as long as dots continue after that without intervening | |
866 | * non-dot character. | |
867 | */ | |
868 | if (!p || (*p != '/' && *p != '~')) | |
869 | return -1; | |
870 | sl = 1; ndot = 0; | |
871 | p++; | |
872 | ||
873 | while (1) { | |
874 | char ch = *p++; | |
875 | if (sl) { | |
876 | if (ch == '.') | |
877 | ndot++; | |
878 | else if (ch == '/') { | |
879 | if (ndot < 3) | |
880 | /* reject //, /./ and /../ */ | |
881 | return -1; | |
882 | ndot = 0; | |
883 | } | |
884 | else if (ch == 0) { | |
885 | if (0 < ndot && ndot < 3) | |
886 | /* reject /.$ and /..$ */ | |
887 | return -1; | |
888 | return 0; | |
889 | } | |
890 | else | |
891 | sl = ndot = 0; | |
892 | } | |
893 | else if (ch == 0) | |
894 | return 0; | |
895 | else if (ch == '/') { | |
896 | sl = 1; | |
897 | ndot = 0; | |
898 | } | |
899 | } | |
900 | } | |
4bb43de2 | 901 | |
1d1d69bc JS |
902 | static int only_spaces_and_periods(const char *path, size_t len, size_t skip) |
903 | { | |
904 | if (len < skip) | |
905 | return 0; | |
906 | len -= skip; | |
907 | path += skip; | |
908 | while (len-- > 0) { | |
909 | char c = *(path++); | |
910 | if (c != ' ' && c != '.') | |
911 | return 0; | |
912 | } | |
913 | return 1; | |
914 | } | |
915 | ||
916 | int is_ntfs_dotgit(const char *name) | |
917 | { | |
918 | int len; | |
919 | ||
920 | for (len = 0; ; len++) | |
921 | if (!name[len] || name[len] == '\\' || is_dir_sep(name[len])) { | |
922 | if (only_spaces_and_periods(name, len, 4) && | |
923 | !strncasecmp(name, ".git", 4)) | |
924 | return 1; | |
925 | if (only_spaces_and_periods(name, len, 5) && | |
926 | !strncasecmp(name, "git~1", 5)) | |
927 | return 1; | |
928 | if (name[len] != '\\') | |
929 | return 0; | |
930 | name += len + 1; | |
931 | len = -1; | |
932 | } | |
933 | } |