Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 1 | /* |
Torsten Bögershausen | 3a429d3 | 2013-03-30 10:53:32 +0100 | [diff] [blame] | 2 | * Utilities for paths and pathnames |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 3 | */ |
| 4 | #include "cache.h" |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 5 | #include "strbuf.h" |
Michael Haggerty | a5ccdbe | 2012-10-28 17:16:23 +0100 | [diff] [blame] | 6 | #include "string-list.h" |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 7 | |
Torsten Bögershausen | 0117c2f | 2013-03-23 13:40:29 +0100 | [diff] [blame] | 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. |
| 13 | */ |
| 14 | int get_st_mode_bits(const char *path, int *mode) |
| 15 | { |
| 16 | struct stat st; |
| 17 | if (lstat(path, &st) < 0) |
| 18 | return -1; |
| 19 | *mode = st.st_mode; |
| 20 | return 0; |
| 21 | } |
| 22 | #endif |
| 23 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 24 | static char bad_path[] = "/bad-path/"; |
| 25 | |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 26 | static char *get_pathname(void) |
| 27 | { |
| 28 | static char pathname_array[4][PATH_MAX]; |
| 29 | static int index; |
| 30 | return pathname_array[3 & ++index]; |
| 31 | } |
| 32 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 33 | static char *cleanup_path(char *path) |
| 34 | { |
| 35 | /* Clean it up */ |
| 36 | if (!memcmp(path, "./", 2)) { |
| 37 | path += 2; |
| 38 | while (*path == '/') |
| 39 | path++; |
| 40 | } |
| 41 | return path; |
| 42 | } |
| 43 | |
Alex Riesen | 108bebe | 2008-10-26 22:59:13 +0100 | [diff] [blame] | 44 | char *mksnpath(char *buf, size_t n, const char *fmt, ...) |
| 45 | { |
| 46 | va_list args; |
| 47 | unsigned len; |
| 48 | |
| 49 | va_start(args, fmt); |
| 50 | len = vsnprintf(buf, n, fmt, args); |
| 51 | va_end(args); |
| 52 | if (len >= n) { |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 53 | strlcpy(buf, bad_path, n); |
Alex Riesen | 108bebe | 2008-10-26 22:59:13 +0100 | [diff] [blame] | 54 | return buf; |
| 55 | } |
| 56 | return cleanup_path(buf); |
| 57 | } |
| 58 | |
Ramsay Jones | 5b3b8fa | 2012-09-04 18:26:30 +0100 | [diff] [blame] | 59 | static char *vsnpath(char *buf, size_t n, const char *fmt, va_list args) |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 60 | { |
| 61 | const char *git_dir = get_git_dir(); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 62 | size_t len; |
| 63 | |
| 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++] = '/'; |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 70 | len += vsnprintf(buf + len, n - len, fmt, args); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 71 | if (len >= n) |
| 72 | goto bad; |
| 73 | return cleanup_path(buf); |
| 74 | bad: |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 75 | strlcpy(buf, bad_path, n); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 76 | return buf; |
| 77 | } |
| 78 | |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 79 | char *git_snpath(char *buf, size_t n, const char *fmt, ...) |
| 80 | { |
Ramsay Jones | 66a51a9 | 2012-09-04 18:27:54 +0100 | [diff] [blame] | 81 | char *ret; |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 82 | va_list args; |
| 83 | va_start(args, fmt); |
Ramsay Jones | 66a51a9 | 2012-09-04 18:27:54 +0100 | [diff] [blame] | 84 | ret = vsnpath(buf, n, fmt, args); |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 85 | va_end(args); |
Ramsay Jones | 66a51a9 | 2012-09-04 18:27:54 +0100 | [diff] [blame] | 86 | return ret; |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | char *git_pathdup(const char *fmt, ...) |
| 90 | { |
Ramsay Jones | 66a51a9 | 2012-09-04 18:27:54 +0100 | [diff] [blame] | 91 | char path[PATH_MAX], *ret; |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 92 | va_list args; |
| 93 | va_start(args, fmt); |
Ramsay Jones | 66a51a9 | 2012-09-04 18:27:54 +0100 | [diff] [blame] | 94 | ret = vsnpath(path, sizeof(path), fmt, args); |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 95 | va_end(args); |
Ramsay Jones | 66a51a9 | 2012-09-04 18:27:54 +0100 | [diff] [blame] | 96 | return xstrdup(ret); |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 99 | char *mkpathdup(const char *fmt, ...) |
| 100 | { |
| 101 | char *path; |
| 102 | struct strbuf sb = STRBUF_INIT; |
| 103 | va_list args; |
| 104 | |
| 105 | va_start(args, fmt); |
| 106 | strbuf_vaddf(&sb, fmt, args); |
| 107 | va_end(args); |
| 108 | path = xstrdup(cleanup_path(sb.buf)); |
| 109 | |
| 110 | strbuf_release(&sb); |
| 111 | return path; |
| 112 | } |
| 113 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 114 | char *mkpath(const char *fmt, ...) |
| 115 | { |
| 116 | va_list args; |
| 117 | unsigned len; |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 118 | char *pathname = get_pathname(); |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 119 | |
| 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); |
| 126 | } |
| 127 | |
| 128 | char *git_path(const char *fmt, ...) |
| 129 | { |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 130 | char *pathname = get_pathname(); |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 131 | va_list args; |
Ramsay Jones | 5c44252 | 2012-09-04 18:29:22 +0100 | [diff] [blame] | 132 | char *ret; |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 133 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 134 | va_start(args, fmt); |
Ramsay Jones | 5c44252 | 2012-09-04 18:29:22 +0100 | [diff] [blame] | 135 | ret = vsnpath(pathname, PATH_MAX, fmt, args); |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 136 | va_end(args); |
Ramsay Jones | 5c44252 | 2012-09-04 18:29:22 +0100 | [diff] [blame] | 137 | return ret; |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 138 | } |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 139 | |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 140 | void home_config_paths(char **global, char **xdg, char *file) |
| 141 | { |
| 142 | char *xdg_home = getenv("XDG_CONFIG_HOME"); |
| 143 | char *home = getenv("HOME"); |
| 144 | char *to_free = NULL; |
| 145 | |
| 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; |
| 153 | } |
| 154 | if (global) |
| 155 | *global = mkpathdup("%s/.gitconfig", home); |
| 156 | } |
| 157 | |
| 158 | if (!xdg_home) |
| 159 | *xdg = NULL; |
| 160 | else |
| 161 | *xdg = mkpathdup("%s/git/%s", xdg_home, file); |
| 162 | |
| 163 | free(to_free); |
| 164 | } |
| 165 | |
Heiko Voigt | 0bad611 | 2010-07-07 15:39:11 +0200 | [diff] [blame] | 166 | char *git_path_submodule(const char *path, const char *fmt, ...) |
| 167 | { |
| 168 | char *pathname = get_pathname(); |
| 169 | struct strbuf buf = STRBUF_INIT; |
| 170 | const char *git_dir; |
| 171 | va_list args; |
| 172 | unsigned len; |
| 173 | |
| 174 | len = strlen(path); |
| 175 | if (len > PATH_MAX-100) |
| 176 | return bad_path; |
| 177 | |
| 178 | strbuf_addstr(&buf, path); |
| 179 | if (len && path[len-1] != '/') |
| 180 | strbuf_addch(&buf, '/'); |
| 181 | strbuf_addstr(&buf, ".git"); |
| 182 | |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 183 | git_dir = read_gitfile(buf.buf); |
Heiko Voigt | 0bad611 | 2010-07-07 15:39:11 +0200 | [diff] [blame] | 184 | if (git_dir) { |
| 185 | strbuf_reset(&buf); |
| 186 | strbuf_addstr(&buf, git_dir); |
| 187 | } |
| 188 | strbuf_addch(&buf, '/'); |
| 189 | |
| 190 | if (buf.len >= PATH_MAX) |
| 191 | return bad_path; |
| 192 | memcpy(pathname, buf.buf, buf.len + 1); |
| 193 | |
| 194 | strbuf_release(&buf); |
| 195 | len = strlen(pathname); |
| 196 | |
| 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); |
| 203 | } |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 204 | |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 205 | int validate_headref(const char *path) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 206 | { |
| 207 | struct stat st; |
| 208 | char *buf, buffer[256]; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 209 | unsigned char sha1[20]; |
Heikki Orsila | 0104ca0 | 2008-04-27 21:21:58 +0300 | [diff] [blame] | 210 | int fd; |
| 211 | ssize_t len; |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 212 | |
| 213 | if (lstat(path, &st) < 0) |
| 214 | return -1; |
| 215 | |
| 216 | /* Make sure it is a "refs/.." symlink */ |
| 217 | if (S_ISLNK(st.st_mode)) { |
| 218 | len = readlink(path, buffer, sizeof(buffer)-1); |
Junio C Hamano | 222b167 | 2009-02-12 13:02:09 -0800 | [diff] [blame] | 219 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 220 | return 0; |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Anything else, just open it and try to see if it is a symbolic ref. |
| 226 | */ |
| 227 | fd = open(path, O_RDONLY); |
| 228 | if (fd < 0) |
| 229 | return -1; |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 230 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 231 | close(fd); |
| 232 | |
| 233 | /* |
| 234 | * Is it a symbolic ref? |
| 235 | */ |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 236 | if (len < 4) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 237 | return -1; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 238 | if (!memcmp("ref:", buffer, 4)) { |
| 239 | buf = buffer + 4; |
| 240 | len -= 4; |
| 241 | while (len && isspace(*buf)) |
| 242 | buf++, len--; |
Junio C Hamano | 222b167 | 2009-02-12 13:02:09 -0800 | [diff] [blame] | 243 | if (len >= 5 && !memcmp("refs/", buf, 5)) |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Is this a detached HEAD? |
| 249 | */ |
| 250 | if (!get_sha1_hex(buffer, sha1)) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 251 | return 0; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 252 | |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 256 | static struct passwd *getpw_str(const char *username, size_t len) |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 257 | { |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 258 | struct passwd *pw; |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 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; |
| 265 | } |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 266 | |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 267 | /* |
| 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. |
| 271 | */ |
| 272 | char *expand_user_path(const char *path) |
| 273 | { |
| 274 | struct strbuf user_path = STRBUF_INIT; |
| 275 | const char *first_slash = strchrnul(path, '/'); |
| 276 | const char *to_copy = path; |
| 277 | |
| 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; |
Matthieu Moy | df2a79f | 2009-11-19 16:21:15 +0100 | [diff] [blame] | 283 | if (username_len == 0) { |
| 284 | const char *home = getenv("HOME"); |
Jonathan Nieder | 79bf149 | 2010-07-26 10:06:51 -0500 | [diff] [blame] | 285 | if (!home) |
| 286 | goto return_null; |
Matthieu Moy | df2a79f | 2009-11-19 16:21:15 +0100 | [diff] [blame] | 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)); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 293 | } |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 294 | to_copy = first_slash; |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 295 | } |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 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; |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 301 | } |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 302 | |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 303 | /* |
| 304 | * First, one directory to try is determined by the following algorithm. |
| 305 | * |
| 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. |
| 312 | * |
| 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. |
| 316 | * |
| 317 | * Second, we try chdir() to that. Upon failure, we return NULL. |
| 318 | * |
| 319 | * Then, we try if the current directory is a valid git repository. |
| 320 | * Upon failure, we return NULL. |
| 321 | * |
| 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. |
| 326 | */ |
Erik Faye-Lund | 1c64b48 | 2011-10-04 16:02:00 -0400 | [diff] [blame] | 327 | const char *enter_repo(const char *path, int strict) |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 328 | { |
| 329 | static char used_path[PATH_MAX]; |
| 330 | static char validated_path[PATH_MAX]; |
| 331 | |
| 332 | if (!path) |
| 333 | return NULL; |
| 334 | |
| 335 | if (!strict) { |
| 336 | static const char *suffix[] = { |
Jeff King | b3256eb | 2012-02-02 16:59:13 -0500 | [diff] [blame] | 337 | "/.git", "", ".git/.git", ".git", NULL, |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 338 | }; |
Phil Hord | 0310676 | 2011-10-04 16:05:17 -0400 | [diff] [blame] | 339 | const char *gitfile; |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 340 | int len = strlen(path); |
| 341 | int i; |
Erik Faye-Lund | 1c64b48 | 2011-10-04 16:02:00 -0400 | [diff] [blame] | 342 | while ((1 < len) && (path[len-1] == '/')) |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 343 | len--; |
Erik Faye-Lund | 1c64b48 | 2011-10-04 16:02:00 -0400 | [diff] [blame] | 344 | |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 345 | if (PATH_MAX <= len) |
| 346 | return NULL; |
Erik Faye-Lund | 1c64b48 | 2011-10-04 16:02:00 -0400 | [diff] [blame] | 347 | strncpy(used_path, path, len); used_path[len] = 0 ; |
| 348 | strcpy(validated_path, used_path); |
| 349 | |
| 350 | if (used_path[0] == '~') { |
| 351 | char *newpath = expand_user_path(used_path); |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 352 | if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { |
| 353 | free(newpath); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 354 | return NULL; |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 355 | } |
| 356 | /* |
| 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. |
| 361 | */ |
| 362 | strcpy(used_path, newpath); free(newpath); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 363 | } |
| 364 | else if (PATH_MAX - 10 < len) |
| 365 | return NULL; |
Erik Faye-Lund | 1c64b48 | 2011-10-04 16:02:00 -0400 | [diff] [blame] | 366 | len = strlen(used_path); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 367 | for (i = 0; suffix[i]; i++) { |
Jeff King | b3256eb | 2012-02-02 16:59:13 -0500 | [diff] [blame] | 368 | struct stat st; |
Erik Faye-Lund | 1c64b48 | 2011-10-04 16:02:00 -0400 | [diff] [blame] | 369 | strcpy(used_path + len, suffix[i]); |
Jeff King | b3256eb | 2012-02-02 16:59:13 -0500 | [diff] [blame] | 370 | if (!stat(used_path, &st) && |
| 371 | (S_ISREG(st.st_mode) || |
| 372 | (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) { |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 373 | strcat(validated_path, suffix[i]); |
| 374 | break; |
| 375 | } |
| 376 | } |
Phil Hord | 0310676 | 2011-10-04 16:05:17 -0400 | [diff] [blame] | 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)) |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 383 | return NULL; |
| 384 | path = validated_path; |
| 385 | } |
| 386 | else if (chdir(path)) |
| 387 | return NULL; |
| 388 | |
| 389 | if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 390 | validate_headref("HEAD") == 0) { |
René Scharfe | 717c397 | 2010-02-06 10:35:19 +0100 | [diff] [blame] | 391 | set_git_dir("."); |
Junio C Hamano | 1644162 | 2005-11-25 10:48:26 -0800 | [diff] [blame] | 392 | check_repository_format(); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 393 | return path; |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | return NULL; |
| 397 | } |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 398 | |
Torsten Bögershausen | cbe43b8 | 2013-03-30 10:53:47 +0100 | [diff] [blame] | 399 | static int calc_shared_perm(int mode) |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 400 | { |
Torsten Bögershausen | cbe43b8 | 2013-03-30 10:53:47 +0100 | [diff] [blame] | 401 | int tweak; |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 402 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 403 | if (shared_repository < 0) |
Torsten Bögershausen | cbe43b8 | 2013-03-30 10:53:47 +0100 | [diff] [blame] | 404 | tweak = -shared_repository; |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 405 | else |
Torsten Bögershausen | cbe43b8 | 2013-03-30 10:53:47 +0100 | [diff] [blame] | 406 | tweak = shared_repository; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 407 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 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 |
Petr Baudis | 8c6202d | 2008-07-12 03:15:03 +0200 | [diff] [blame] | 416 | mode |= tweak; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 417 | |
Torsten Bögershausen | cbe43b8 | 2013-03-30 10:53:47 +0100 | [diff] [blame] | 418 | return mode; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | int adjust_shared_perm(const char *path) |
| 423 | { |
| 424 | int old_mode, new_mode; |
| 425 | |
| 426 | if (!shared_repository) |
| 427 | return 0; |
| 428 | if (get_st_mode_bits(path, &old_mode) < 0) |
| 429 | return -1; |
| 430 | |
| 431 | new_mode = calc_shared_perm(old_mode); |
| 432 | if (S_ISDIR(old_mode)) { |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 433 | /* Copy read bits to execute bits */ |
Torsten Bögershausen | cbe43b8 | 2013-03-30 10:53:47 +0100 | [diff] [blame] | 434 | new_mode |= (new_mode & 0444) >> 2; |
| 435 | new_mode |= FORCE_DIR_SET_GID; |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 436 | } |
| 437 | |
Torsten Bögershausen | cbe43b8 | 2013-03-30 10:53:47 +0100 | [diff] [blame] | 438 | if (((old_mode ^ new_mode) & ~S_IFMT) && |
| 439 | chmod(path, (new_mode & ~S_IFMT)) < 0) |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 440 | return -2; |
| 441 | return 0; |
| 442 | } |
Johannes Schindelin | e5392c5 | 2007-08-01 01:28:59 +0100 | [diff] [blame] | 443 | |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 444 | const char *relative_path(const char *abs, const char *base) |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 445 | { |
| 446 | static char buf[PATH_MAX + 1]; |
Junio C Hamano | 288123f | 2010-01-21 19:05:19 -0800 | [diff] [blame] | 447 | int i = 0, j = 0; |
| 448 | |
| 449 | if (!base || !base[0]) |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 450 | return abs; |
Junio C Hamano | 288123f | 2010-01-21 19:05:19 -0800 | [diff] [blame] | 451 | while (base[i]) { |
| 452 | if (is_dir_sep(base[i])) { |
| 453 | if (!is_dir_sep(abs[j])) |
| 454 | return abs; |
| 455 | while (is_dir_sep(base[i])) |
| 456 | i++; |
| 457 | while (is_dir_sep(abs[j])) |
| 458 | j++; |
| 459 | continue; |
| 460 | } else if (abs[j] != base[i]) { |
| 461 | return abs; |
| 462 | } |
| 463 | i++; |
| 464 | j++; |
| 465 | } |
| 466 | if ( |
| 467 | /* "/foo" is a prefix of "/foo" */ |
| 468 | abs[j] && |
| 469 | /* "/foo" is not a prefix of "/foobar" */ |
| 470 | !is_dir_sep(base[i-1]) && !is_dir_sep(abs[j]) |
| 471 | ) |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 472 | return abs; |
Junio C Hamano | 288123f | 2010-01-21 19:05:19 -0800 | [diff] [blame] | 473 | while (is_dir_sep(abs[j])) |
| 474 | j++; |
| 475 | if (!abs[j]) |
| 476 | strcpy(buf, "."); |
| 477 | else |
| 478 | strcpy(buf, abs + j); |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 479 | return buf; |
| 480 | } |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 481 | |
| 482 | /* |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 483 | * It is okay if dst == src, but they should not overlap otherwise. |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 484 | * |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 485 | * Performs the following normalizations on src, storing the result in dst: |
| 486 | * - Ensures that components are separated by '/' (Windows only) |
| 487 | * - Squashes sequences of '/'. |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 488 | * - Removes "." components. |
| 489 | * - Removes ".." components, and the components the precede them. |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 490 | * Returns failure (non-zero) if a ".." component appears as first path |
| 491 | * component anytime during the normalization. Otherwise, returns success (0). |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 492 | * |
| 493 | * Note that this function is purely textual. It does not follow symlinks, |
| 494 | * verify the existence of the path, or make any system calls. |
| 495 | */ |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 496 | int normalize_path_copy(char *dst, const char *src) |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 497 | { |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 498 | char *dst0; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 499 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 500 | if (has_dos_drive_prefix(src)) { |
| 501 | *dst++ = *src++; |
| 502 | *dst++ = *src++; |
| 503 | } |
| 504 | dst0 = dst; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 505 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 506 | if (is_dir_sep(*src)) { |
| 507 | *dst++ = '/'; |
| 508 | while (is_dir_sep(*src)) |
| 509 | src++; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 512 | for (;;) { |
| 513 | char c = *src; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 514 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 515 | /* |
| 516 | * A path component that begins with . could be |
| 517 | * special: |
| 518 | * (1) "." and ends -- ignore and terminate. |
| 519 | * (2) "./" -- ignore them, eat slash and continue. |
| 520 | * (3) ".." and ends -- strip one and terminate. |
| 521 | * (4) "../" -- strip one, eat slash and continue. |
| 522 | */ |
| 523 | if (c == '.') { |
| 524 | if (!src[1]) { |
| 525 | /* (1) */ |
| 526 | src++; |
| 527 | } else if (is_dir_sep(src[1])) { |
| 528 | /* (2) */ |
| 529 | src += 2; |
| 530 | while (is_dir_sep(*src)) |
| 531 | src++; |
| 532 | continue; |
| 533 | } else if (src[1] == '.') { |
| 534 | if (!src[2]) { |
| 535 | /* (3) */ |
| 536 | src += 2; |
| 537 | goto up_one; |
| 538 | } else if (is_dir_sep(src[2])) { |
| 539 | /* (4) */ |
| 540 | src += 3; |
| 541 | while (is_dir_sep(*src)) |
| 542 | src++; |
| 543 | goto up_one; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* copy up to the next '/', and eat all '/' */ |
| 549 | while ((c = *src++) != '\0' && !is_dir_sep(c)) |
| 550 | *dst++ = c; |
| 551 | if (is_dir_sep(c)) { |
| 552 | *dst++ = '/'; |
| 553 | while (is_dir_sep(c)) |
| 554 | c = *src++; |
| 555 | src--; |
| 556 | } else if (!c) |
| 557 | break; |
| 558 | continue; |
| 559 | |
| 560 | up_one: |
| 561 | /* |
| 562 | * dst0..dst is prefix portion, and dst[-1] is '/'; |
| 563 | * go up one level. |
| 564 | */ |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 565 | dst--; /* go to trailing '/' */ |
| 566 | if (dst <= dst0) |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 567 | return -1; |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 568 | /* Windows: dst[-1] cannot be backslash anymore */ |
| 569 | while (dst0 < dst && dst[-1] != '/') |
| 570 | dst--; |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 571 | } |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 572 | *dst = '\0'; |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 573 | return 0; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 574 | } |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 575 | |
| 576 | /* |
| 577 | * path = Canonical absolute path |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 578 | * prefixes = string_list containing normalized, absolute paths without |
| 579 | * trailing slashes (except for the root directory, which is denoted by "/"). |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 580 | * |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 581 | * Determines, for each path in prefixes, whether the "prefix" |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 582 | * is an ancestor directory of path. Returns the length of the longest |
| 583 | * ancestor directory, excluding any trailing slashes, or -1 if no prefix |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 584 | * is an ancestor. (Note that this means 0 is returned if prefixes is |
| 585 | * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 586 | * are not considered to be their own ancestors. path must be in a |
| 587 | * canonical form: empty components, or "." or ".." components are not |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 588 | * allowed. |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 589 | */ |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 590 | int longest_ancestor_length(const char *path, struct string_list *prefixes) |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 591 | { |
Michael Haggerty | a5ccdbe | 2012-10-28 17:16:23 +0100 | [diff] [blame] | 592 | int i, max_len = -1; |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 593 | |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 594 | if (!strcmp(path, "/")) |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 595 | return -1; |
| 596 | |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 597 | for (i = 0; i < prefixes->nr; i++) { |
| 598 | const char *ceil = prefixes->items[i].string; |
Michael Haggerty | a5ccdbe | 2012-10-28 17:16:23 +0100 | [diff] [blame] | 599 | int len = strlen(ceil); |
| 600 | |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 601 | if (len == 1 && ceil[0] == '/') |
| 602 | len = 0; /* root matches anything, with length 0 */ |
| 603 | else if (!strncmp(path, ceil, len) && path[len] == '/') |
| 604 | ; /* match of length len */ |
| 605 | else |
| 606 | continue; /* no match */ |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 607 | |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 608 | if (len > max_len) |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 609 | max_len = len; |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | return max_len; |
| 613 | } |
Johannes Schindelin | 4fcc86b | 2009-02-19 20:10:49 +0100 | [diff] [blame] | 614 | |
| 615 | /* strip arbitrary amount of directory separators at end of path */ |
| 616 | static inline int chomp_trailing_dir_sep(const char *path, int len) |
| 617 | { |
| 618 | while (len && is_dir_sep(path[len - 1])) |
| 619 | len--; |
| 620 | return len; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * If path ends with suffix (complete path components), returns the |
| 625 | * part before suffix (sans trailing directory separators). |
| 626 | * Otherwise returns NULL. |
| 627 | */ |
| 628 | char *strip_path_suffix(const char *path, const char *suffix) |
| 629 | { |
| 630 | int path_len = strlen(path), suffix_len = strlen(suffix); |
| 631 | |
| 632 | while (suffix_len) { |
| 633 | if (!path_len) |
| 634 | return NULL; |
| 635 | |
| 636 | if (is_dir_sep(path[path_len - 1])) { |
| 637 | if (!is_dir_sep(suffix[suffix_len - 1])) |
| 638 | return NULL; |
| 639 | path_len = chomp_trailing_dir_sep(path, path_len); |
| 640 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); |
| 641 | } |
| 642 | else if (path[--path_len] != suffix[--suffix_len]) |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | if (path_len && !is_dir_sep(path[path_len - 1])) |
| 647 | return NULL; |
| 648 | return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); |
| 649 | } |
Shawn O. Pearce | 34b6cb8 | 2009-11-09 11:26:43 -0800 | [diff] [blame] | 650 | |
| 651 | int daemon_avoid_alias(const char *p) |
| 652 | { |
| 653 | int sl, ndot; |
| 654 | |
| 655 | /* |
| 656 | * This resurrects the belts and suspenders paranoia check by HPA |
| 657 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() |
Junio C Hamano | 9517e6b | 2010-02-03 21:23:18 -0800 | [diff] [blame] | 658 | * does not do getcwd() based path canonicalization. |
Shawn O. Pearce | 34b6cb8 | 2009-11-09 11:26:43 -0800 | [diff] [blame] | 659 | * |
| 660 | * sl becomes true immediately after seeing '/' and continues to |
| 661 | * be true as long as dots continue after that without intervening |
| 662 | * non-dot character. |
| 663 | */ |
| 664 | if (!p || (*p != '/' && *p != '~')) |
| 665 | return -1; |
| 666 | sl = 1; ndot = 0; |
| 667 | p++; |
| 668 | |
| 669 | while (1) { |
| 670 | char ch = *p++; |
| 671 | if (sl) { |
| 672 | if (ch == '.') |
| 673 | ndot++; |
| 674 | else if (ch == '/') { |
| 675 | if (ndot < 3) |
| 676 | /* reject //, /./ and /../ */ |
| 677 | return -1; |
| 678 | ndot = 0; |
| 679 | } |
| 680 | else if (ch == 0) { |
| 681 | if (0 < ndot && ndot < 3) |
| 682 | /* reject /.$ and /..$ */ |
| 683 | return -1; |
| 684 | return 0; |
| 685 | } |
| 686 | else |
| 687 | sl = ndot = 0; |
| 688 | } |
| 689 | else if (ch == 0) |
| 690 | return 0; |
| 691 | else if (ch == '/') { |
| 692 | sl = 1; |
| 693 | ndot = 0; |
| 694 | } |
| 695 | } |
| 696 | } |
Nguyễn Thái Ngọc Duy | 4bb43de | 2010-02-16 12:22:08 +0700 | [diff] [blame] | 697 | |
| 698 | int offset_1st_component(const char *path) |
| 699 | { |
| 700 | if (has_dos_drive_prefix(path)) |
| 701 | return 2 + is_dir_sep(path[2]); |
| 702 | return is_dir_sep(path[0]); |
| 703 | } |