2 * Utilities for paths and pathnames
6 #include "string-list.h"
8 static int get_st_mode_bits(const char *path, int *mode)
11 if (lstat(path, &st) < 0)
17 static char bad_path[] = "/bad-path/";
19 static char *get_pathname(void)
21 static char pathname_array[4][PATH_MAX];
23 return pathname_array[3 & ++index];
26 static char *cleanup_path(char *path)
29 if (!memcmp(path, "./", 2)) {
37 char *mksnpath(char *buf, size_t n, const char *fmt, ...)
43 len = vsnprintf(buf, n, fmt, args);
46 strlcpy(buf, bad_path, n);
49 return cleanup_path(buf);
52 static char *vsnpath(char *buf, size_t n, const char *fmt, va_list args)
54 const char *git_dir = get_git_dir();
57 len = strlen(git_dir);
60 memcpy(buf, git_dir, len);
61 if (len && !is_dir_sep(git_dir[len-1]))
63 len += vsnprintf(buf + len, n - len, fmt, args);
66 return cleanup_path(buf);
68 strlcpy(buf, bad_path, n);
72 char *git_snpath(char *buf, size_t n, const char *fmt, ...)
77 ret = vsnpath(buf, n, fmt, args);
82 char *git_pathdup(const char *fmt, ...)
84 char path[PATH_MAX], *ret;
87 ret = vsnpath(path, sizeof(path), fmt, args);
92 char *mkpathdup(const char *fmt, ...)
95 struct strbuf sb = STRBUF_INIT;
99 strbuf_vaddf(&sb, fmt, args);
101 path = xstrdup(cleanup_path(sb.buf));
107 char *mkpath(const char *fmt, ...)
111 char *pathname = get_pathname();
114 len = vsnprintf(pathname, PATH_MAX, fmt, args);
118 return cleanup_path(pathname);
121 char *git_path(const char *fmt, ...)
123 char *pathname = get_pathname();
128 ret = vsnpath(pathname, PATH_MAX, fmt, args);
133 void home_config_paths(char **global, char **xdg, char *file)
135 char *xdg_home = getenv("XDG_CONFIG_HOME");
136 char *home = getenv("HOME");
137 char *to_free = NULL;
144 to_free = mkpathdup("%s/.config", home);
148 *global = mkpathdup("%s/.gitconfig", home);
154 *xdg = mkpathdup("%s/git/%s", xdg_home, file);
159 char *git_path_submodule(const char *path, const char *fmt, ...)
161 char *pathname = get_pathname();
162 struct strbuf buf = STRBUF_INIT;
168 if (len > PATH_MAX-100)
171 strbuf_addstr(&buf, path);
172 if (len && path[len-1] != '/')
173 strbuf_addch(&buf, '/');
174 strbuf_addstr(&buf, ".git");
176 git_dir = read_gitfile(buf.buf);
179 strbuf_addstr(&buf, git_dir);
181 strbuf_addch(&buf, '/');
183 if (buf.len >= PATH_MAX)
185 memcpy(pathname, buf.buf, buf.len + 1);
187 strbuf_release(&buf);
188 len = strlen(pathname);
191 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
195 return cleanup_path(pathname);
198 int validate_headref(const char *path)
201 char *buf, buffer[256];
202 unsigned char sha1[20];
206 if (lstat(path, &st) < 0)
209 /* Make sure it is a "refs/.." symlink */
210 if (S_ISLNK(st.st_mode)) {
211 len = readlink(path, buffer, sizeof(buffer)-1);
212 if (len >= 5 && !memcmp("refs/", buffer, 5))
218 * Anything else, just open it and try to see if it is a symbolic ref.
220 fd = open(path, O_RDONLY);
223 len = read_in_full(fd, buffer, sizeof(buffer)-1);
227 * Is it a symbolic ref?
231 if (!memcmp("ref:", buffer, 4)) {
234 while (len && isspace(*buf))
236 if (len >= 5 && !memcmp("refs/", buf, 5))
241 * Is this a detached HEAD?
243 if (!get_sha1_hex(buffer, sha1))
249 static struct passwd *getpw_str(const char *username, size_t len)
252 char *username_z = xmemdupz(username, len);
253 pw = getpwnam(username_z);
259 * Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
260 * then it is a newly allocated string. Returns NULL on getpw failure or
263 char *expand_user_path(const char *path)
265 struct strbuf user_path = STRBUF_INIT;
266 const char *to_copy = path;
270 if (path[0] == '~') {
271 const char *first_slash = strchrnul(path, '/');
272 const char *username = path + 1;
273 size_t username_len = first_slash - username;
274 if (username_len == 0) {
275 const char *home = getenv("HOME");
278 strbuf_add(&user_path, home, strlen(home));
280 struct passwd *pw = getpw_str(username, username_len);
283 strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir));
285 to_copy = first_slash;
287 strbuf_add(&user_path, to_copy, strlen(to_copy));
288 return strbuf_detach(&user_path, NULL);
290 strbuf_release(&user_path);
295 * First, one directory to try is determined by the following algorithm.
297 * (0) If "strict" is given, the path is used as given and no DWIM is
299 * (1) "~/path" to mean path under the running user's home directory;
300 * (2) "~user/path" to mean path under named user's home directory;
301 * (3) "relative/path" to mean cwd relative directory; or
302 * (4) "/absolute/path" to mean absolute directory.
304 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
305 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
308 * Second, we try chdir() to that. Upon failure, we return NULL.
310 * Then, we try if the current directory is a valid git repository.
311 * Upon failure, we return NULL.
313 * If all goes well, we return the directory we used to chdir() (but
314 * before ~user is expanded), avoiding getcwd() resolving symbolic
315 * links. User relative paths are also returned as they are given,
316 * except DWIM suffixing.
318 const char *enter_repo(const char *path, int strict)
320 static char used_path[PATH_MAX];
321 static char validated_path[PATH_MAX];
327 static const char *suffix[] = {
328 "/.git", "", ".git/.git", ".git", NULL,
331 int len = strlen(path);
333 while ((1 < len) && (path[len-1] == '/'))
338 strncpy(used_path, path, len); used_path[len] = 0 ;
339 strcpy(validated_path, used_path);
341 if (used_path[0] == '~') {
342 char *newpath = expand_user_path(used_path);
343 if (!newpath || (PATH_MAX - 10 < strlen(newpath))) {
348 * Copy back into the static buffer. A pity
349 * since newpath was not bounded, but other
350 * branches of the if are limited by PATH_MAX
353 strcpy(used_path, newpath); free(newpath);
355 else if (PATH_MAX - 10 < len)
357 len = strlen(used_path);
358 for (i = 0; suffix[i]; i++) {
360 strcpy(used_path + len, suffix[i]);
361 if (!stat(used_path, &st) &&
362 (S_ISREG(st.st_mode) ||
363 (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) {
364 strcat(validated_path, suffix[i]);
370 gitfile = read_gitfile(used_path) ;
372 strcpy(used_path, gitfile);
373 if (chdir(used_path))
375 path = validated_path;
377 else if (chdir(path))
380 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
381 validate_headref("HEAD") == 0) {
383 check_repository_format();
390 static int calc_shared_perm(int mode)
394 if (shared_repository < 0)
395 tweak = -shared_repository;
397 tweak = shared_repository;
399 if (!(mode & S_IWUSR))
402 /* Copy read bits to execute bits */
403 tweak |= (tweak & 0444) >> 2;
404 if (shared_repository < 0)
405 mode = (mode & ~0777) | tweak;
413 int adjust_shared_perm(const char *path)
415 int old_mode, new_mode;
417 if (!shared_repository)
419 if (get_st_mode_bits(path, &old_mode) < 0)
422 new_mode = calc_shared_perm(old_mode);
423 if (S_ISDIR(old_mode)) {
424 /* Copy read bits to execute bits */
425 new_mode |= (new_mode & 0444) >> 2;
426 new_mode |= FORCE_DIR_SET_GID;
429 if (((old_mode ^ new_mode) & ~S_IFMT) &&
430 chmod(path, (new_mode & ~S_IFMT)) < 0)
435 static int have_same_root(const char *path1, const char *path2)
437 int is_abs1, is_abs2;
439 is_abs1 = is_absolute_path(path1);
440 is_abs2 = is_absolute_path(path2);
441 return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) ||
442 (!is_abs1 && !is_abs2);
446 * Give path as relative to prefix.
448 * The strbuf may or may not be used, so do not assume it contains the
451 const char *relative_path(const char *in, const char *prefix,
454 int in_len = in ? strlen(in) : 0;
455 int prefix_len = prefix ? strlen(prefix) : 0;
462 else if (!prefix_len)
465 if (have_same_root(in, prefix)) {
466 /* bypass dos_drive, for "c:" is identical to "C:" */
467 if (has_dos_drive_prefix(in)) {
475 while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
476 if (is_dir_sep(prefix[i])) {
477 while (is_dir_sep(prefix[i]))
479 while (is_dir_sep(in[j]))
490 /* "prefix" seems like prefix of "in" */
493 * but "/foo" is not a prefix of "/foobar"
494 * (i.e. prefix not end with '/')
496 prefix_off < prefix_len) {
498 /* in="/a/b", prefix="/a/b" */
500 } else if (is_dir_sep(in[j])) {
501 /* in="/a/b/c", prefix="/a/b" */
502 while (is_dir_sep(in[j]))
506 /* in="/a/bbb/c", prefix="/a/b" */
510 /* "in" is short than "prefix" */
512 /* "in" not end with '/' */
514 if (is_dir_sep(prefix[i])) {
515 /* in="/a/b", prefix="/a/b/c/" */
516 while (is_dir_sep(prefix[i]))
524 if (i >= prefix_len) {
532 strbuf_grow(sb, in_len);
534 while (i < prefix_len) {
535 if (is_dir_sep(prefix[i])) {
536 strbuf_addstr(sb, "../");
537 while (is_dir_sep(prefix[i]))
543 if (!is_dir_sep(prefix[prefix_len - 1]))
544 strbuf_addstr(sb, "../");
546 strbuf_addstr(sb, in);
552 * A simpler implementation of relative_path
554 * Get relative path by removing "prefix" from "in". This function
555 * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter
556 * to increase performance when traversing the path to work_tree.
558 const char *remove_leading_path(const char *in, const char *prefix)
560 static char buf[PATH_MAX + 1];
563 if (!prefix || !prefix[0])
566 if (is_dir_sep(prefix[i])) {
567 if (!is_dir_sep(in[j]))
569 while (is_dir_sep(prefix[i]))
571 while (is_dir_sep(in[j]))
574 } else if (in[j] != prefix[i]) {
581 /* "/foo" is a prefix of "/foo" */
583 /* "/foo" is not a prefix of "/foobar" */
584 !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j])
587 while (is_dir_sep(in[j]))
597 * It is okay if dst == src, but they should not overlap otherwise.
599 * Performs the following normalizations on src, storing the result in dst:
600 * - Ensures that components are separated by '/' (Windows only)
601 * - Squashes sequences of '/'.
602 * - Removes "." components.
603 * - Removes ".." components, and the components the precede them.
604 * Returns failure (non-zero) if a ".." component appears as first path
605 * component anytime during the normalization. Otherwise, returns success (0).
607 * Note that this function is purely textual. It does not follow symlinks,
608 * verify the existence of the path, or make any system calls.
610 * prefix_len != NULL is for a specific case of prefix_pathspec():
611 * assume that src == dst and src[0..prefix_len-1] is already
612 * normalized, any time "../" eats up to the prefix_len part,
613 * prefix_len is reduced. In the end prefix_len is the remaining
614 * prefix that has not been overridden by user pathspec.
616 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
620 if (has_dos_drive_prefix(src)) {
626 if (is_dir_sep(*src)) {
628 while (is_dir_sep(*src))
636 * A path component that begins with . could be
638 * (1) "." and ends -- ignore and terminate.
639 * (2) "./" -- ignore them, eat slash and continue.
640 * (3) ".." and ends -- strip one and terminate.
641 * (4) "../" -- strip one, eat slash and continue.
647 } else if (is_dir_sep(src[1])) {
650 while (is_dir_sep(*src))
653 } else if (src[1] == '.') {
658 } else if (is_dir_sep(src[2])) {
661 while (is_dir_sep(*src))
668 /* copy up to the next '/', and eat all '/' */
669 while ((c = *src++) != '\0' && !is_dir_sep(c))
673 while (is_dir_sep(c))
682 * dst0..dst is prefix portion, and dst[-1] is '/';
685 dst--; /* go to trailing '/' */
688 /* Windows: dst[-1] cannot be backslash anymore */
689 while (dst0 < dst && dst[-1] != '/')
691 if (prefix_len && *prefix_len > dst - dst0)
692 *prefix_len = dst - dst0;
698 int normalize_path_copy(char *dst, const char *src)
700 return normalize_path_copy_len(dst, src, NULL);
704 * path = Canonical absolute path
705 * prefixes = string_list containing normalized, absolute paths without
706 * trailing slashes (except for the root directory, which is denoted by "/").
708 * Determines, for each path in prefixes, whether the "prefix"
709 * is an ancestor directory of path. Returns the length of the longest
710 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
711 * is an ancestor. (Note that this means 0 is returned if prefixes is
712 * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
713 * are not considered to be their own ancestors. path must be in a
714 * canonical form: empty components, or "." or ".." components are not
717 int longest_ancestor_length(const char *path, struct string_list *prefixes)
721 if (!strcmp(path, "/"))
724 for (i = 0; i < prefixes->nr; i++) {
725 const char *ceil = prefixes->items[i].string;
726 int len = strlen(ceil);
728 if (len == 1 && ceil[0] == '/')
729 len = 0; /* root matches anything, with length 0 */
730 else if (!strncmp(path, ceil, len) && path[len] == '/')
731 ; /* match of length len */
733 continue; /* no match */
742 /* strip arbitrary amount of directory separators at end of path */
743 static inline int chomp_trailing_dir_sep(const char *path, int len)
745 while (len && is_dir_sep(path[len - 1]))
751 * If path ends with suffix (complete path components), returns the
752 * part before suffix (sans trailing directory separators).
753 * Otherwise returns NULL.
755 char *strip_path_suffix(const char *path, const char *suffix)
757 int path_len = strlen(path), suffix_len = strlen(suffix);
763 if (is_dir_sep(path[path_len - 1])) {
764 if (!is_dir_sep(suffix[suffix_len - 1]))
766 path_len = chomp_trailing_dir_sep(path, path_len);
767 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
769 else if (path[--path_len] != suffix[--suffix_len])
773 if (path_len && !is_dir_sep(path[path_len - 1]))
775 return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
778 int daemon_avoid_alias(const char *p)
783 * This resurrects the belts and suspenders paranoia check by HPA
784 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
785 * does not do getcwd() based path canonicalization.
787 * sl becomes true immediately after seeing '/' and continues to
788 * be true as long as dots continue after that without intervening
791 if (!p || (*p != '/' && *p != '~'))
801 else if (ch == '/') {
803 /* reject //, /./ and /../ */
808 if (0 < ndot && ndot < 3)
809 /* reject /.$ and /..$ */
818 else if (ch == '/') {
825 int offset_1st_component(const char *path)
827 if (has_dos_drive_prefix(path))
828 return 2 + is_dir_sep(path[2]);
829 return is_dir_sep(path[0]);