]> git.ipfire.org Git - thirdparty/git.git/blame - path.c
Introduce new function real_path_if_valid()
[thirdparty/git.git] / path.c
CommitLineData
26c8a533
LT
1/*
2 * I'm tired of doing "vsnprintf()" etc just to open a
3 * file, so here's a "return static buffer with printf"
4 * interface for paths.
5 *
6 * It's obviously not thread-safe. Sue me. But it's quite
7 * useful for doing things like
8 *
9 * f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
10 *
11 * which is what it's designed for.
12 */
13#include "cache.h"
395de250 14#include "strbuf.h"
26c8a533 15
26c8a533
LT
16static char bad_path[] = "/bad-path/";
17
e7676d2f
LT
18static char *get_pathname(void)
19{
20 static char pathname_array[4][PATH_MAX];
21 static int index;
22 return pathname_array[3 & ++index];
23}
24
26c8a533
LT
25static char *cleanup_path(char *path)
26{
27 /* Clean it up */
28 if (!memcmp(path, "./", 2)) {
29 path += 2;
30 while (*path == '/')
31 path++;
32 }
33 return path;
34}
35
108bebea
AR
36char *mksnpath(char *buf, size_t n, const char *fmt, ...)
37{
38 va_list args;
39 unsigned len;
40
41 va_start(args, fmt);
42 len = vsnprintf(buf, n, fmt, args);
43 va_end(args);
44 if (len >= n) {
9db56f71 45 strlcpy(buf, bad_path, n);
108bebea
AR
46 return buf;
47 }
48 return cleanup_path(buf);
49}
50
5b3b8fa2 51static char *vsnpath(char *buf, size_t n, const char *fmt, va_list args)
fe2d7776
AR
52{
53 const char *git_dir = get_git_dir();
fe2d7776
AR
54 size_t len;
55
56 len = strlen(git_dir);
57 if (n < len + 1)
58 goto bad;
59 memcpy(buf, git_dir, len);
60 if (len && !is_dir_sep(git_dir[len-1]))
61 buf[len++] = '/';
fe2d7776 62 len += vsnprintf(buf + len, n - len, fmt, args);
fe2d7776
AR
63 if (len >= n)
64 goto bad;
65 return cleanup_path(buf);
66bad:
9db56f71 67 strlcpy(buf, bad_path, n);
fe2d7776
AR
68 return buf;
69}
70
aba13e7c
AR
71char *git_snpath(char *buf, size_t n, const char *fmt, ...)
72{
66a51a9a 73 char *ret;
aba13e7c
AR
74 va_list args;
75 va_start(args, fmt);
66a51a9a 76 ret = vsnpath(buf, n, fmt, args);
aba13e7c 77 va_end(args);
66a51a9a 78 return ret;
aba13e7c
AR
79}
80
81char *git_pathdup(const char *fmt, ...)
82{
66a51a9a 83 char path[PATH_MAX], *ret;
aba13e7c
AR
84 va_list args;
85 va_start(args, fmt);
66a51a9a 86 ret = vsnpath(path, sizeof(path), fmt, args);
aba13e7c 87 va_end(args);
66a51a9a 88 return xstrdup(ret);
aba13e7c
AR
89}
90
21cf3227
HKNN
91char *mkpathdup(const char *fmt, ...)
92{
93 char *path;
94 struct strbuf sb = STRBUF_INIT;
95 va_list args;
96
97 va_start(args, fmt);
98 strbuf_vaddf(&sb, fmt, args);
99 va_end(args);
100 path = xstrdup(cleanup_path(sb.buf));
101
102 strbuf_release(&sb);
103 return path;
104}
105
26c8a533
LT
106char *mkpath(const char *fmt, ...)
107{
108 va_list args;
109 unsigned len;
e7676d2f 110 char *pathname = get_pathname();
26c8a533
LT
111
112 va_start(args, fmt);
113 len = vsnprintf(pathname, PATH_MAX, fmt, args);
114 va_end(args);
115 if (len >= PATH_MAX)
116 return bad_path;
117 return cleanup_path(pathname);
118}
119
120char *git_path(const char *fmt, ...)
121{
e7676d2f 122 char *pathname = get_pathname();
26c8a533 123 va_list args;
5c44252e 124 char *ret;
26c8a533 125
26c8a533 126 va_start(args, fmt);
5c44252e 127 ret = vsnpath(pathname, PATH_MAX, fmt, args);
26c8a533 128 va_end(args);
5c44252e 129 return ret;
26c8a533 130}
f2db68ed 131
21cf3227
HKNN
132void home_config_paths(char **global, char **xdg, char *file)
133{
134 char *xdg_home = getenv("XDG_CONFIG_HOME");
135 char *home = getenv("HOME");
136 char *to_free = NULL;
137
138 if (!home) {
139 if (global)
140 *global = NULL;
141 } else {
142 if (!xdg_home) {
143 to_free = mkpathdup("%s/.config", home);
144 xdg_home = to_free;
145 }
146 if (global)
147 *global = mkpathdup("%s/.gitconfig", home);
148 }
149
150 if (!xdg_home)
151 *xdg = NULL;
152 else
153 *xdg = mkpathdup("%s/git/%s", xdg_home, file);
154
155 free(to_free);
156}
157
0bad611b
HV
158char *git_path_submodule(const char *path, const char *fmt, ...)
159{
160 char *pathname = get_pathname();
161 struct strbuf buf = STRBUF_INIT;
162 const char *git_dir;
163 va_list args;
164 unsigned len;
165
166 len = strlen(path);
167 if (len > PATH_MAX-100)
168 return bad_path;
169
170 strbuf_addstr(&buf, path);
171 if (len && path[len-1] != '/')
172 strbuf_addch(&buf, '/');
173 strbuf_addstr(&buf, ".git");
174
13d6ec91 175 git_dir = read_gitfile(buf.buf);
0bad611b
HV
176 if (git_dir) {
177 strbuf_reset(&buf);
178 strbuf_addstr(&buf, git_dir);
179 }
180 strbuf_addch(&buf, '/');
181
182 if (buf.len >= PATH_MAX)
183 return bad_path;
184 memcpy(pathname, buf.buf, buf.len + 1);
185
186 strbuf_release(&buf);
187 len = strlen(pathname);
188
189 va_start(args, fmt);
190 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
191 va_end(args);
192 if (len >= PATH_MAX)
193 return bad_path;
194 return cleanup_path(pathname);
195}
f2db68ed 196
c847f537 197int validate_headref(const char *path)
0870ca7f
JH
198{
199 struct stat st;
200 char *buf, buffer[256];
c847f537 201 unsigned char sha1[20];
0104ca09
HO
202 int fd;
203 ssize_t len;
0870ca7f
JH
204
205 if (lstat(path, &st) < 0)
206 return -1;
207
208 /* Make sure it is a "refs/.." symlink */
209 if (S_ISLNK(st.st_mode)) {
210 len = readlink(path, buffer, sizeof(buffer)-1);
222b1673 211 if (len >= 5 && !memcmp("refs/", buffer, 5))
0870ca7f
JH
212 return 0;
213 return -1;
214 }
215
216 /*
217 * Anything else, just open it and try to see if it is a symbolic ref.
218 */
219 fd = open(path, O_RDONLY);
220 if (fd < 0)
221 return -1;
93d26e4c 222 len = read_in_full(fd, buffer, sizeof(buffer)-1);
0870ca7f
JH
223 close(fd);
224
225 /*
226 * Is it a symbolic ref?
227 */
c847f537 228 if (len < 4)
0870ca7f 229 return -1;
c847f537
JH
230 if (!memcmp("ref:", buffer, 4)) {
231 buf = buffer + 4;
232 len -= 4;
233 while (len && isspace(*buf))
234 buf++, len--;
222b1673 235 if (len >= 5 && !memcmp("refs/", buf, 5))
c847f537
JH
236 return 0;
237 }
238
239 /*
240 * Is this a detached HEAD?
241 */
242 if (!get_sha1_hex(buffer, sha1))
0870ca7f 243 return 0;
c847f537 244
0870ca7f
JH
245 return -1;
246}
247
395de250 248static struct passwd *getpw_str(const char *username, size_t len)
54f4b874 249{
d79374c7 250 struct passwd *pw;
395de250
MM
251 char *username_z = xmalloc(len + 1);
252 memcpy(username_z, username, len);
253 username_z[len] = '\0';
254 pw = getpwnam(username_z);
255 free(username_z);
256 return pw;
257}
54f4b874 258
395de250
MM
259/*
260 * Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
261 * then it is a newly allocated string. Returns NULL on getpw failure or
262 * if path is NULL.
263 */
264char *expand_user_path(const char *path)
265{
266 struct strbuf user_path = STRBUF_INIT;
267 const char *first_slash = strchrnul(path, '/');
268 const char *to_copy = path;
269
270 if (path == NULL)
271 goto return_null;
272 if (path[0] == '~') {
273 const char *username = path + 1;
274 size_t username_len = first_slash - username;
df2a79f4
MM
275 if (username_len == 0) {
276 const char *home = getenv("HOME");
79bf1490
JN
277 if (!home)
278 goto return_null;
df2a79f4
MM
279 strbuf_add(&user_path, home, strlen(home));
280 } else {
281 struct passwd *pw = getpw_str(username, username_len);
282 if (!pw)
283 goto return_null;
284 strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
54f4b874 285 }
395de250 286 to_copy = first_slash;
d79374c7 287 }
395de250
MM
288 strbuf_add(&user_path, to_copy, strlen(to_copy));
289 return strbuf_detach(&user_path, NULL);
290return_null:
291 strbuf_release(&user_path);
292 return NULL;
54f4b874
AE
293}
294
d79374c7
JH
295/*
296 * First, one directory to try is determined by the following algorithm.
297 *
298 * (0) If "strict" is given, the path is used as given and no DWIM is
299 * done. Otherwise:
300 * (1) "~/path" to mean path under the running user's home directory;
301 * (2) "~user/path" to mean path under named user's home directory;
302 * (3) "relative/path" to mean cwd relative directory; or
303 * (4) "/absolute/path" to mean absolute directory.
304 *
305 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
306 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
307 * what we try.
308 *
309 * Second, we try chdir() to that. Upon failure, we return NULL.
310 *
311 * Then, we try if the current directory is a valid git repository.
312 * Upon failure, we return NULL.
313 *
314 * If all goes well, we return the directory we used to chdir() (but
315 * before ~user is expanded), avoiding getcwd() resolving symbolic
316 * links. User relative paths are also returned as they are given,
317 * except DWIM suffixing.
318 */
1c64b48e 319const char *enter_repo(const char *path, int strict)
54f4b874 320{
d79374c7
JH
321 static char used_path[PATH_MAX];
322 static char validated_path[PATH_MAX];
323
324 if (!path)
54f4b874
AE
325 return NULL;
326
d79374c7
JH
327 if (!strict) {
328 static const char *suffix[] = {
b3256eb8 329 "/.git", "", ".git/.git", ".git", NULL,
d79374c7 330 };
03106768 331 const char *gitfile;
d79374c7
JH
332 int len = strlen(path);
333 int i;
1c64b48e 334 while ((1 < len) && (path[len-1] == '/'))
d79374c7 335 len--;
1c64b48e 336
d79374c7 337 if (PATH_MAX <= len)
54f4b874 338 return NULL;
1c64b48e
EFL
339 strncpy(used_path, path, len); used_path[len] = 0 ;
340 strcpy(validated_path, used_path);
341
342 if (used_path[0] == '~') {
343 char *newpath = expand_user_path(used_path);
395de250
MM
344 if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
345 free(newpath);
d79374c7 346 return NULL;
395de250
MM
347 }
348 /*
349 * Copy back into the static buffer. A pity
350 * since newpath was not bounded, but other
351 * branches of the if are limited by PATH_MAX
352 * anyway.
353 */
354 strcpy(used_path, newpath); free(newpath);
d79374c7
JH
355 }
356 else if (PATH_MAX - 10 < len)
357 return NULL;
1c64b48e 358 len = strlen(used_path);
d79374c7 359 for (i = 0; suffix[i]; i++) {
b3256eb8 360 struct stat st;
1c64b48e 361 strcpy(used_path + len, suffix[i]);
b3256eb8
JK
362 if (!stat(used_path, &st) &&
363 (S_ISREG(st.st_mode) ||
364 (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) {
d79374c7
JH
365 strcat(validated_path, suffix[i]);
366 break;
367 }
368 }
03106768
PH
369 if (!suffix[i])
370 return NULL;
371 gitfile = read_gitfile(used_path) ;
372 if (gitfile)
373 strcpy(used_path, gitfile);
374 if (chdir(used_path))
0870ca7f 375 return NULL;
d79374c7 376 path = validated_path;
0870ca7f 377 }
d79374c7
JH
378 else if (chdir(path))
379 return NULL;
54f4b874 380
d79374c7 381 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
c847f537 382 validate_headref("HEAD") == 0) {
717c3972 383 set_git_dir(".");
1644162a 384 check_repository_format();
d79374c7 385 return path;
54f4b874
AE
386 }
387
388 return NULL;
389}
138086a7 390
17e61b82 391int set_shared_perm(const char *path, int mode)
138086a7
JH
392{
393 struct stat st;
17e61b82 394 int tweak, shared, orig_mode;
138086a7 395
17e61b82
JH
396 if (!shared_repository) {
397 if (mode)
398 return chmod(path, mode & ~S_IFMT);
138086a7 399 return 0;
17e61b82
JH
400 }
401 if (!mode) {
402 if (lstat(path, &st) < 0)
403 return -1;
404 mode = st.st_mode;
405 orig_mode = mode;
406 } else
407 orig_mode = 0;
5a688fe4
JH
408 if (shared_repository < 0)
409 shared = -shared_repository;
410 else
411 shared = shared_repository;
412 tweak = shared;
413
414 if (!(mode & S_IWUSR))
415 tweak &= ~0222;
416 if (mode & S_IXUSR)
417 /* Copy read bits to execute bits */
418 tweak |= (tweak & 0444) >> 2;
419 if (shared_repository < 0)
420 mode = (mode & ~0777) | tweak;
421 else
8c6202d8 422 mode |= tweak;
06cbe855
HO
423
424 if (S_ISDIR(mode)) {
06cbe855 425 /* Copy read bits to execute bits */
5a688fe4
JH
426 mode |= (shared & 0444) >> 2;
427 mode |= FORCE_DIR_SET_GID;
06cbe855
HO
428 }
429
5a688fe4 430 if (((shared_repository < 0
17e61b82
JH
431 ? (orig_mode & (FORCE_DIR_SET_GID | 0777))
432 : (orig_mode & mode)) != mode) &&
433 chmod(path, (mode & ~S_IFMT)) < 0)
138086a7
JH
434 return -2;
435 return 0;
436}
e5392c51 437
e2a57aac 438const char *relative_path(const char *abs, const char *base)
044bbbcb
LT
439{
440 static char buf[PATH_MAX + 1];
288123f0
JH
441 int i = 0, j = 0;
442
443 if (!base || !base[0])
044bbbcb 444 return abs;
288123f0
JH
445 while (base[i]) {
446 if (is_dir_sep(base[i])) {
447 if (!is_dir_sep(abs[j]))
448 return abs;
449 while (is_dir_sep(base[i]))
450 i++;
451 while (is_dir_sep(abs[j]))
452 j++;
453 continue;
454 } else if (abs[j] != base[i]) {
455 return abs;
456 }
457 i++;
458 j++;
459 }
460 if (
461 /* "/foo" is a prefix of "/foo" */
462 abs[j] &&
463 /* "/foo" is not a prefix of "/foobar" */
464 !is_dir_sep(base[i-1]) && !is_dir_sep(abs[j])
465 )
044bbbcb 466 return abs;
288123f0
JH
467 while (is_dir_sep(abs[j]))
468 j++;
469 if (!abs[j])
470 strcpy(buf, ".");
471 else
472 strcpy(buf, abs + j);
044bbbcb
LT
473 return buf;
474}
ae299be0
DR
475
476/*
f2a782b8 477 * It is okay if dst == src, but they should not overlap otherwise.
ae299be0 478 *
f2a782b8
JS
479 * Performs the following normalizations on src, storing the result in dst:
480 * - Ensures that components are separated by '/' (Windows only)
481 * - Squashes sequences of '/'.
ae299be0
DR
482 * - Removes "." components.
483 * - Removes ".." components, and the components the precede them.
f2a782b8
JS
484 * Returns failure (non-zero) if a ".." component appears as first path
485 * component anytime during the normalization. Otherwise, returns success (0).
ae299be0
DR
486 *
487 * Note that this function is purely textual. It does not follow symlinks,
488 * verify the existence of the path, or make any system calls.
489 */
f3cad0ad 490int normalize_path_copy(char *dst, const char *src)
ae299be0 491{
f3cad0ad 492 char *dst0;
ae299be0 493
f3cad0ad
JS
494 if (has_dos_drive_prefix(src)) {
495 *dst++ = *src++;
496 *dst++ = *src++;
ae299be0 497 }
f3cad0ad 498 dst0 = dst;
ae299be0 499
f3cad0ad 500 if (is_dir_sep(*src)) {
ae299be0 501 *dst++ = '/';
f3cad0ad
JS
502 while (is_dir_sep(*src))
503 src++;
504 }
505
506 for (;;) {
507 char c = *src;
508
509 /*
510 * A path component that begins with . could be
511 * special:
512 * (1) "." and ends -- ignore and terminate.
513 * (2) "./" -- ignore them, eat slash and continue.
514 * (3) ".." and ends -- strip one and terminate.
515 * (4) "../" -- strip one, eat slash and continue.
516 */
517 if (c == '.') {
518 if (!src[1]) {
519 /* (1) */
520 src++;
521 } else if (is_dir_sep(src[1])) {
522 /* (2) */
523 src += 2;
524 while (is_dir_sep(*src))
525 src++;
526 continue;
527 } else if (src[1] == '.') {
528 if (!src[2]) {
529 /* (3) */
530 src += 2;
531 goto up_one;
532 } else if (is_dir_sep(src[2])) {
533 /* (4) */
534 src += 3;
535 while (is_dir_sep(*src))
536 src++;
537 goto up_one;
538 }
539 }
540 }
ae299be0 541
f3cad0ad
JS
542 /* copy up to the next '/', and eat all '/' */
543 while ((c = *src++) != '\0' && !is_dir_sep(c))
544 *dst++ = c;
545 if (is_dir_sep(c)) {
546 *dst++ = '/';
547 while (is_dir_sep(c))
548 c = *src++;
549 src--;
550 } else if (!c)
551 break;
552 continue;
553
554 up_one:
555 /*
556 * dst0..dst is prefix portion, and dst[-1] is '/';
557 * go up one level.
558 */
f42302b4
JS
559 dst--; /* go to trailing '/' */
560 if (dst <= dst0)
f3cad0ad 561 return -1;
f42302b4
JS
562 /* Windows: dst[-1] cannot be backslash anymore */
563 while (dst0 < dst && dst[-1] != '/')
564 dst--;
f3cad0ad 565 }
ae299be0 566 *dst = '\0';
f3cad0ad 567 return 0;
ae299be0 568}
0454dd93
DR
569
570/*
571 * path = Canonical absolute path
572 * prefix_list = Colon-separated list of absolute paths
573 *
2860b57a 574 * Determines, for each path in prefix_list, whether the "prefix" really
0454dd93
DR
575 * is an ancestor directory of path. Returns the length of the longest
576 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
577 * is an ancestor. (Note that this means 0 is returned if prefix_list is
578 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
579 * are not considered to be their own ancestors. path must be in a
580 * canonical form: empty components, or "." or ".." components are not
581 * allowed. prefix_list may be null, which is like "".
582 */
583int longest_ancestor_length(const char *path, const char *prefix_list)
584{
585 char buf[PATH_MAX+1];
586 const char *ceil, *colon;
587 int len, max_len = -1;
588
589 if (prefix_list == NULL || !strcmp(path, "/"))
590 return -1;
591
592 for (colon = ceil = prefix_list; *colon; ceil = colon+1) {
43a7ddb5 593 for (colon = ceil; *colon && *colon != PATH_SEP; colon++);
0454dd93
DR
594 len = colon - ceil;
595 if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
596 continue;
597 strlcpy(buf, ceil, len+1);
43a7ddb5
RS
598 if (normalize_path_copy(buf, buf) < 0)
599 continue;
600 len = strlen(buf);
601 if (len > 0 && buf[len-1] == '/')
602 buf[--len] = '\0';
0454dd93
DR
603
604 if (!strncmp(path, buf, len) &&
605 path[len] == '/' &&
606 len > max_len) {
607 max_len = len;
608 }
609 }
610
611 return max_len;
612}
4fcc86b0
JS
613
614/* strip arbitrary amount of directory separators at end of path */
615static inline int chomp_trailing_dir_sep(const char *path, int len)
616{
617 while (len && is_dir_sep(path[len - 1]))
618 len--;
619 return len;
620}
621
622/*
623 * If path ends with suffix (complete path components), returns the
624 * part before suffix (sans trailing directory separators).
625 * Otherwise returns NULL.
626 */
627char *strip_path_suffix(const char *path, const char *suffix)
628{
629 int path_len = strlen(path), suffix_len = strlen(suffix);
630
631 while (suffix_len) {
632 if (!path_len)
633 return NULL;
634
635 if (is_dir_sep(path[path_len - 1])) {
636 if (!is_dir_sep(suffix[suffix_len - 1]))
637 return NULL;
638 path_len = chomp_trailing_dir_sep(path, path_len);
639 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
640 }
641 else if (path[--path_len] != suffix[--suffix_len])
642 return NULL;
643 }
644
645 if (path_len && !is_dir_sep(path[path_len - 1]))
646 return NULL;
647 return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
648}
34b6cb8b
SP
649
650int daemon_avoid_alias(const char *p)
651{
652 int sl, ndot;
653
654 /*
655 * This resurrects the belts and suspenders paranoia check by HPA
656 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
9517e6b8 657 * does not do getcwd() based path canonicalization.
34b6cb8b
SP
658 *
659 * sl becomes true immediately after seeing '/' and continues to
660 * be true as long as dots continue after that without intervening
661 * non-dot character.
662 */
663 if (!p || (*p != '/' && *p != '~'))
664 return -1;
665 sl = 1; ndot = 0;
666 p++;
667
668 while (1) {
669 char ch = *p++;
670 if (sl) {
671 if (ch == '.')
672 ndot++;
673 else if (ch == '/') {
674 if (ndot < 3)
675 /* reject //, /./ and /../ */
676 return -1;
677 ndot = 0;
678 }
679 else if (ch == 0) {
680 if (0 < ndot && ndot < 3)
681 /* reject /.$ and /..$ */
682 return -1;
683 return 0;
684 }
685 else
686 sl = ndot = 0;
687 }
688 else if (ch == 0)
689 return 0;
690 else if (ch == '/') {
691 sl = 1;
692 ndot = 0;
693 }
694 }
695}
4bb43de2
NTND
696
697int offset_1st_component(const char *path)
698{
699 if (has_dos_drive_prefix(path))
700 return 2 + is_dir_sep(path[2]);
701 return is_dir_sep(path[0]);
702}