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