test: add t7301 for git-clean--interactive
[alt-git.git] / path.c
blob7f3324aeea8050c082f9bb52d769301cdede4805
1 /*
2 * Utilities for paths and pathnames
3 */
4 #include "cache.h"
5 #include "strbuf.h"
6 #include "string-list.h"
8 #ifndef get_st_mode_bits
9 /*
10 * The replacement lstat(2) we use on Cygwin is incomplete and
11 * may return wrong permission bits. Most of the time we do not care,
12 * but the callsites of this wrapper do care.
14 int get_st_mode_bits(const char *path, int *mode)
16 struct stat st;
17 if (lstat(path, &st) < 0)
18 return -1;
19 *mode = st.st_mode;
20 return 0;
22 #endif
24 static char bad_path[] = "/bad-path/";
26 static char *get_pathname(void)
28 static char pathname_array[4][PATH_MAX];
29 static int index;
30 return pathname_array[3 & ++index];
33 static char *cleanup_path(char *path)
35 /* Clean it up */
36 if (!memcmp(path, "./", 2)) {
37 path += 2;
38 while (*path == '/')
39 path++;
41 return path;
44 char *mksnpath(char *buf, size_t n, const char *fmt, ...)
46 va_list args;
47 unsigned len;
49 va_start(args, fmt);
50 len = vsnprintf(buf, n, fmt, args);
51 va_end(args);
52 if (len >= n) {
53 strlcpy(buf, bad_path, n);
54 return buf;
56 return cleanup_path(buf);
59 static char *vsnpath(char *buf, size_t n, const char *fmt, va_list args)
61 const char *git_dir = get_git_dir();
62 size_t len;
64 len = strlen(git_dir);
65 if (n < len + 1)
66 goto bad;
67 memcpy(buf, git_dir, len);
68 if (len && !is_dir_sep(git_dir[len-1]))
69 buf[len++] = '/';
70 len += vsnprintf(buf + len, n - len, fmt, args);
71 if (len >= n)
72 goto bad;
73 return cleanup_path(buf);
74 bad:
75 strlcpy(buf, bad_path, n);
76 return buf;
79 char *git_snpath(char *buf, size_t n, const char *fmt, ...)
81 char *ret;
82 va_list args;
83 va_start(args, fmt);
84 ret = vsnpath(buf, n, fmt, args);
85 va_end(args);
86 return ret;
89 char *git_pathdup(const char *fmt, ...)
91 char path[PATH_MAX], *ret;
92 va_list args;
93 va_start(args, fmt);
94 ret = vsnpath(path, sizeof(path), fmt, args);
95 va_end(args);
96 return xstrdup(ret);
99 char *mkpathdup(const char *fmt, ...)
101 char *path;
102 struct strbuf sb = STRBUF_INIT;
103 va_list args;
105 va_start(args, fmt);
106 strbuf_vaddf(&sb, fmt, args);
107 va_end(args);
108 path = xstrdup(cleanup_path(sb.buf));
110 strbuf_release(&sb);
111 return path;
114 char *mkpath(const char *fmt, ...)
116 va_list args;
117 unsigned len;
118 char *pathname = get_pathname();
120 va_start(args, fmt);
121 len = vsnprintf(pathname, PATH_MAX, fmt, args);
122 va_end(args);
123 if (len >= PATH_MAX)
124 return bad_path;
125 return cleanup_path(pathname);
128 char *git_path(const char *fmt, ...)
130 char *pathname = get_pathname();
131 va_list args;
132 char *ret;
134 va_start(args, fmt);
135 ret = vsnpath(pathname, PATH_MAX, fmt, args);
136 va_end(args);
137 return ret;
140 void home_config_paths(char **global, char **xdg, char *file)
142 char *xdg_home = getenv("XDG_CONFIG_HOME");
143 char *home = getenv("HOME");
144 char *to_free = NULL;
146 if (!home) {
147 if (global)
148 *global = NULL;
149 } else {
150 if (!xdg_home) {
151 to_free = mkpathdup("%s/.config", home);
152 xdg_home = to_free;
154 if (global)
155 *global = mkpathdup("%s/.gitconfig", home);
158 if (!xdg_home)
159 *xdg = NULL;
160 else
161 *xdg = mkpathdup("%s/git/%s", xdg_home, file);
163 free(to_free);
166 char *git_path_submodule(const char *path, const char *fmt, ...)
168 char *pathname = get_pathname();
169 struct strbuf buf = STRBUF_INIT;
170 const char *git_dir;
171 va_list args;
172 unsigned len;
174 len = strlen(path);
175 if (len > PATH_MAX-100)
176 return bad_path;
178 strbuf_addstr(&buf, path);
179 if (len && path[len-1] != '/')
180 strbuf_addch(&buf, '/');
181 strbuf_addstr(&buf, ".git");
183 git_dir = read_gitfile(buf.buf);
184 if (git_dir) {
185 strbuf_reset(&buf);
186 strbuf_addstr(&buf, git_dir);
188 strbuf_addch(&buf, '/');
190 if (buf.len >= PATH_MAX)
191 return bad_path;
192 memcpy(pathname, buf.buf, buf.len + 1);
194 strbuf_release(&buf);
195 len = strlen(pathname);
197 va_start(args, fmt);
198 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
199 va_end(args);
200 if (len >= PATH_MAX)
201 return bad_path;
202 return cleanup_path(pathname);
205 int validate_headref(const char *path)
207 struct stat st;
208 char *buf, buffer[256];
209 unsigned char sha1[20];
210 int fd;
211 ssize_t len;
213 if (lstat(path, &st) < 0)
214 return -1;
216 /* Make sure it is a "refs/.." symlink */
217 if (S_ISLNK(st.st_mode)) {
218 len = readlink(path, buffer, sizeof(buffer)-1);
219 if (len >= 5 && !memcmp("refs/", buffer, 5))
220 return 0;
221 return -1;
225 * Anything else, just open it and try to see if it is a symbolic ref.
227 fd = open(path, O_RDONLY);
228 if (fd < 0)
229 return -1;
230 len = read_in_full(fd, buffer, sizeof(buffer)-1);
231 close(fd);
234 * Is it a symbolic ref?
236 if (len < 4)
237 return -1;
238 if (!memcmp("ref:", buffer, 4)) {
239 buf = buffer + 4;
240 len -= 4;
241 while (len && isspace(*buf))
242 buf++, len--;
243 if (len >= 5 && !memcmp("refs/", buf, 5))
244 return 0;
248 * Is this a detached HEAD?
250 if (!get_sha1_hex(buffer, sha1))
251 return 0;
253 return -1;
256 static struct passwd *getpw_str(const char *username, size_t len)
258 struct passwd *pw;
259 char *username_z = xmalloc(len + 1);
260 memcpy(username_z, username, len);
261 username_z[len] = '\0';
262 pw = getpwnam(username_z);
263 free(username_z);
264 return pw;
268 * Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
269 * then it is a newly allocated string. Returns NULL on getpw failure or
270 * if path is NULL.
272 char *expand_user_path(const char *path)
274 struct strbuf user_path = STRBUF_INIT;
275 const char *first_slash = strchrnul(path, '/');
276 const char *to_copy = path;
278 if (path == NULL)
279 goto return_null;
280 if (path[0] == '~') {
281 const char *username = path + 1;
282 size_t username_len = first_slash - username;
283 if (username_len == 0) {
284 const char *home = getenv("HOME");
285 if (!home)
286 goto return_null;
287 strbuf_add(&user_path, home, strlen(home));
288 } else {
289 struct passwd *pw = getpw_str(username, username_len);
290 if (!pw)
291 goto return_null;
292 strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
294 to_copy = first_slash;
296 strbuf_add(&user_path, to_copy, strlen(to_copy));
297 return strbuf_detach(&user_path, NULL);
298 return_null:
299 strbuf_release(&user_path);
300 return NULL;
304 * First, one directory to try is determined by the following algorithm.
306 * (0) If "strict" is given, the path is used as given and no DWIM is
307 * done. Otherwise:
308 * (1) "~/path" to mean path under the running user's home directory;
309 * (2) "~user/path" to mean path under named user's home directory;
310 * (3) "relative/path" to mean cwd relative directory; or
311 * (4) "/absolute/path" to mean absolute directory.
313 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
314 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
315 * what we try.
317 * Second, we try chdir() to that. Upon failure, we return NULL.
319 * Then, we try if the current directory is a valid git repository.
320 * Upon failure, we return NULL.
322 * If all goes well, we return the directory we used to chdir() (but
323 * before ~user is expanded), avoiding getcwd() resolving symbolic
324 * links. User relative paths are also returned as they are given,
325 * except DWIM suffixing.
327 const char *enter_repo(const char *path, int strict)
329 static char used_path[PATH_MAX];
330 static char validated_path[PATH_MAX];
332 if (!path)
333 return NULL;
335 if (!strict) {
336 static const char *suffix[] = {
337 "/.git", "", ".git/.git", ".git", NULL,
339 const char *gitfile;
340 int len = strlen(path);
341 int i;
342 while ((1 < len) && (path[len-1] == '/'))
343 len--;
345 if (PATH_MAX <= len)
346 return NULL;
347 strncpy(used_path, path, len); used_path[len] = 0 ;
348 strcpy(validated_path, used_path);
350 if (used_path[0] == '~') {
351 char *newpath = expand_user_path(used_path);
352 if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
353 free(newpath);
354 return NULL;
357 * Copy back into the static buffer. A pity
358 * since newpath was not bounded, but other
359 * branches of the if are limited by PATH_MAX
360 * anyway.
362 strcpy(used_path, newpath); free(newpath);
364 else if (PATH_MAX - 10 < len)
365 return NULL;
366 len = strlen(used_path);
367 for (i = 0; suffix[i]; i++) {
368 struct stat st;
369 strcpy(used_path + len, suffix[i]);
370 if (!stat(used_path, &st) &&
371 (S_ISREG(st.st_mode) ||
372 (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) {
373 strcat(validated_path, suffix[i]);
374 break;
377 if (!suffix[i])
378 return NULL;
379 gitfile = read_gitfile(used_path) ;
380 if (gitfile)
381 strcpy(used_path, gitfile);
382 if (chdir(used_path))
383 return NULL;
384 path = validated_path;
386 else if (chdir(path))
387 return NULL;
389 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
390 validate_headref("HEAD") == 0) {
391 set_git_dir(".");
392 check_repository_format();
393 return path;
396 return NULL;
399 static int calc_shared_perm(int mode)
401 int tweak;
403 if (shared_repository < 0)
404 tweak = -shared_repository;
405 else
406 tweak = shared_repository;
408 if (!(mode & S_IWUSR))
409 tweak &= ~0222;
410 if (mode & S_IXUSR)
411 /* Copy read bits to execute bits */
412 tweak |= (tweak & 0444) >> 2;
413 if (shared_repository < 0)
414 mode = (mode & ~0777) | tweak;
415 else
416 mode |= tweak;
418 return mode;
422 int adjust_shared_perm(const char *path)
424 int old_mode, new_mode;
426 if (!shared_repository)
427 return 0;
428 if (get_st_mode_bits(path, &old_mode) < 0)
429 return -1;
431 new_mode = calc_shared_perm(old_mode);
432 if (S_ISDIR(old_mode)) {
433 /* Copy read bits to execute bits */
434 new_mode |= (new_mode & 0444) >> 2;
435 new_mode |= FORCE_DIR_SET_GID;
438 if (((old_mode ^ new_mode) & ~S_IFMT) &&
439 chmod(path, (new_mode & ~S_IFMT)) < 0)
440 return -2;
441 return 0;
445 * Give path as relative to prefix.
447 * The strbuf may or may not be used, so do not assume it contains the
448 * returned path.
450 const char *relative_path(const char *in, const char *prefix,
451 struct strbuf *sb)
453 int in_len = in ? strlen(in) : 0;
454 int prefix_len = prefix ? strlen(prefix) : 0;
455 int in_off = 0;
456 int prefix_off = 0;
457 int i = 0, j = 0;
459 if (!in_len)
460 return "./";
461 else if (!prefix_len)
462 return in;
464 while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
465 if (is_dir_sep(prefix[i])) {
466 while (is_dir_sep(prefix[i]))
467 i++;
468 while (is_dir_sep(in[j]))
469 j++;
470 prefix_off = i;
471 in_off = j;
472 } else {
473 i++;
474 j++;
478 if (
479 /* "prefix" seems like prefix of "in" */
480 i >= prefix_len &&
482 * but "/foo" is not a prefix of "/foobar"
483 * (i.e. prefix not end with '/')
485 prefix_off < prefix_len) {
486 if (j >= in_len) {
487 /* in="/a/b", prefix="/a/b" */
488 in_off = in_len;
489 } else if (is_dir_sep(in[j])) {
490 /* in="/a/b/c", prefix="/a/b" */
491 while (is_dir_sep(in[j]))
492 j++;
493 in_off = j;
494 } else {
495 /* in="/a/bbb/c", prefix="/a/b" */
496 i = prefix_off;
498 } else if (
499 /* "in" is short than "prefix" */
500 j >= in_len &&
501 /* "in" not end with '/' */
502 in_off < in_len) {
503 if (is_dir_sep(prefix[i])) {
504 /* in="/a/b", prefix="/a/b/c/" */
505 while (is_dir_sep(prefix[i]))
506 i++;
507 in_off = in_len;
510 in += in_off;
511 in_len -= in_off;
513 if (i >= prefix_len) {
514 if (!in_len)
515 return "./";
516 else
517 return in;
520 strbuf_reset(sb);
521 strbuf_grow(sb, in_len);
523 while (i < prefix_len) {
524 if (is_dir_sep(prefix[i])) {
525 strbuf_addstr(sb, "../");
526 while (is_dir_sep(prefix[i]))
527 i++;
528 continue;
530 i++;
532 if (!is_dir_sep(prefix[prefix_len - 1]))
533 strbuf_addstr(sb, "../");
535 strbuf_addstr(sb, in);
537 return sb->buf;
541 * It is okay if dst == src, but they should not overlap otherwise.
543 * Performs the following normalizations on src, storing the result in dst:
544 * - Ensures that components are separated by '/' (Windows only)
545 * - Squashes sequences of '/'.
546 * - Removes "." components.
547 * - Removes ".." components, and the components the precede them.
548 * Returns failure (non-zero) if a ".." component appears as first path
549 * component anytime during the normalization. Otherwise, returns success (0).
551 * Note that this function is purely textual. It does not follow symlinks,
552 * verify the existence of the path, or make any system calls.
554 int normalize_path_copy(char *dst, const char *src)
556 char *dst0;
558 if (has_dos_drive_prefix(src)) {
559 *dst++ = *src++;
560 *dst++ = *src++;
562 dst0 = dst;
564 if (is_dir_sep(*src)) {
565 *dst++ = '/';
566 while (is_dir_sep(*src))
567 src++;
570 for (;;) {
571 char c = *src;
574 * A path component that begins with . could be
575 * special:
576 * (1) "." and ends -- ignore and terminate.
577 * (2) "./" -- ignore them, eat slash and continue.
578 * (3) ".." and ends -- strip one and terminate.
579 * (4) "../" -- strip one, eat slash and continue.
581 if (c == '.') {
582 if (!src[1]) {
583 /* (1) */
584 src++;
585 } else if (is_dir_sep(src[1])) {
586 /* (2) */
587 src += 2;
588 while (is_dir_sep(*src))
589 src++;
590 continue;
591 } else if (src[1] == '.') {
592 if (!src[2]) {
593 /* (3) */
594 src += 2;
595 goto up_one;
596 } else if (is_dir_sep(src[2])) {
597 /* (4) */
598 src += 3;
599 while (is_dir_sep(*src))
600 src++;
601 goto up_one;
606 /* copy up to the next '/', and eat all '/' */
607 while ((c = *src++) != '\0' && !is_dir_sep(c))
608 *dst++ = c;
609 if (is_dir_sep(c)) {
610 *dst++ = '/';
611 while (is_dir_sep(c))
612 c = *src++;
613 src--;
614 } else if (!c)
615 break;
616 continue;
618 up_one:
620 * dst0..dst is prefix portion, and dst[-1] is '/';
621 * go up one level.
623 dst--; /* go to trailing '/' */
624 if (dst <= dst0)
625 return -1;
626 /* Windows: dst[-1] cannot be backslash anymore */
627 while (dst0 < dst && dst[-1] != '/')
628 dst--;
630 *dst = '\0';
631 return 0;
635 * path = Canonical absolute path
636 * prefixes = string_list containing normalized, absolute paths without
637 * trailing slashes (except for the root directory, which is denoted by "/").
639 * Determines, for each path in prefixes, whether the "prefix"
640 * is an ancestor directory of path. Returns the length of the longest
641 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
642 * is an ancestor. (Note that this means 0 is returned if prefixes is
643 * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
644 * are not considered to be their own ancestors. path must be in a
645 * canonical form: empty components, or "." or ".." components are not
646 * allowed.
648 int longest_ancestor_length(const char *path, struct string_list *prefixes)
650 int i, max_len = -1;
652 if (!strcmp(path, "/"))
653 return -1;
655 for (i = 0; i < prefixes->nr; i++) {
656 const char *ceil = prefixes->items[i].string;
657 int len = strlen(ceil);
659 if (len == 1 && ceil[0] == '/')
660 len = 0; /* root matches anything, with length 0 */
661 else if (!strncmp(path, ceil, len) && path[len] == '/')
662 ; /* match of length len */
663 else
664 continue; /* no match */
666 if (len > max_len)
667 max_len = len;
670 return max_len;
673 /* strip arbitrary amount of directory separators at end of path */
674 static inline int chomp_trailing_dir_sep(const char *path, int len)
676 while (len && is_dir_sep(path[len - 1]))
677 len--;
678 return len;
682 * If path ends with suffix (complete path components), returns the
683 * part before suffix (sans trailing directory separators).
684 * Otherwise returns NULL.
686 char *strip_path_suffix(const char *path, const char *suffix)
688 int path_len = strlen(path), suffix_len = strlen(suffix);
690 while (suffix_len) {
691 if (!path_len)
692 return NULL;
694 if (is_dir_sep(path[path_len - 1])) {
695 if (!is_dir_sep(suffix[suffix_len - 1]))
696 return NULL;
697 path_len = chomp_trailing_dir_sep(path, path_len);
698 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
700 else if (path[--path_len] != suffix[--suffix_len])
701 return NULL;
704 if (path_len && !is_dir_sep(path[path_len - 1]))
705 return NULL;
706 return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
709 int daemon_avoid_alias(const char *p)
711 int sl, ndot;
714 * This resurrects the belts and suspenders paranoia check by HPA
715 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
716 * does not do getcwd() based path canonicalization.
718 * sl becomes true immediately after seeing '/' and continues to
719 * be true as long as dots continue after that without intervening
720 * non-dot character.
722 if (!p || (*p != '/' && *p != '~'))
723 return -1;
724 sl = 1; ndot = 0;
725 p++;
727 while (1) {
728 char ch = *p++;
729 if (sl) {
730 if (ch == '.')
731 ndot++;
732 else if (ch == '/') {
733 if (ndot < 3)
734 /* reject //, /./ and /../ */
735 return -1;
736 ndot = 0;
738 else if (ch == 0) {
739 if (0 < ndot && ndot < 3)
740 /* reject /.$ and /..$ */
741 return -1;
742 return 0;
744 else
745 sl = ndot = 0;
747 else if (ch == 0)
748 return 0;
749 else if (ch == '/') {
750 sl = 1;
751 ndot = 0;
756 int offset_1st_component(const char *path)
758 if (has_dos_drive_prefix(path))
759 return 2 + is_dir_sep(path[2]);
760 return is_dir_sep(path[0]);