| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 1 | /* |
| Torsten Bögershausen | 3a429d3 | 2013-03-30 09:53:32 | [diff] [blame] | 2 | * Utilities for paths and pathnames |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 3 | */ |
| 4 | #include "cache.h" |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 5 | #include "strbuf.h" |
| Michael Haggerty | a5ccdbe | 2012-10-28 16:16:23 | [diff] [blame] | 6 | #include "string-list.h" |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 7 | |
| Ramsay Jones | f66450a | 2013-06-22 19:42:47 | [diff] [blame] | 8 | static int get_st_mode_bits(const char *path, int *mode) |
| Torsten Bögershausen | 0117c2f | 2013-03-23 12:40:29 | [diff] [blame] | 9 | { |
| 10 | struct stat st; |
| 11 | if (lstat(path, &st) < 0) |
| 12 | return -1; |
| 13 | *mode = st.st_mode; |
| 14 | return 0; |
| 15 | } |
| Torsten Bögershausen | 0117c2f | 2013-03-23 12:40:29 | [diff] [blame] | 16 | |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 17 | static char bad_path[] = "/bad-path/"; |
| 18 | |
| Linus Torvalds | e7676d2 | 2006-09-11 19:03:15 | [diff] [blame] | 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 | |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 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 | |
| Alex Riesen | 108bebe | 2008-10-26 21:59:13 | [diff] [blame] | 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) { |
| Daniel Lowe | 9db56f7 | 2008-11-10 21:07:52 | [diff] [blame] | 46 | strlcpy(buf, bad_path, n); |
| Alex Riesen | 108bebe | 2008-10-26 21:59:13 | [diff] [blame] | 47 | return buf; |
| 48 | } |
| 49 | return cleanup_path(buf); |
| 50 | } |
| 51 | |
| Ramsay Jones | 5b3b8fa | 2012-09-04 17:26:30 | [diff] [blame] | 52 | static char *vsnpath(char *buf, size_t n, const char *fmt, va_list args) |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 53 | { |
| 54 | const char *git_dir = get_git_dir(); |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 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++] = '/'; |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 63 | len += vsnprintf(buf + len, n - len, fmt, args); |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 64 | if (len >= n) |
| 65 | goto bad; |
| 66 | return cleanup_path(buf); |
| 67 | bad: |
| Daniel Lowe | 9db56f7 | 2008-11-10 21:07:52 | [diff] [blame] | 68 | strlcpy(buf, bad_path, n); |
| Alex Riesen | fe2d777 | 2008-10-27 09:22:21 | [diff] [blame] | 69 | return buf; |
| 70 | } |
| 71 | |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 72 | char *git_snpath(char *buf, size_t n, const char *fmt, ...) |
| 73 | { |
| Ramsay Jones | 66a51a9 | 2012-09-04 17:27:54 | [diff] [blame] | 74 | char *ret; |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 75 | va_list args; |
| 76 | va_start(args, fmt); |
| Ramsay Jones | 66a51a9 | 2012-09-04 17:27:54 | [diff] [blame] | 77 | ret = vsnpath(buf, n, fmt, args); |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 78 | va_end(args); |
| Ramsay Jones | 66a51a9 | 2012-09-04 17:27:54 | [diff] [blame] | 79 | return ret; |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | char *git_pathdup(const char *fmt, ...) |
| 83 | { |
| Ramsay Jones | 66a51a9 | 2012-09-04 17:27:54 | [diff] [blame] | 84 | char path[PATH_MAX], *ret; |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 85 | va_list args; |
| 86 | va_start(args, fmt); |
| Ramsay Jones | 66a51a9 | 2012-09-04 17:27:54 | [diff] [blame] | 87 | ret = vsnpath(path, sizeof(path), fmt, args); |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 88 | va_end(args); |
| Ramsay Jones | 66a51a9 | 2012-09-04 17:27:54 | [diff] [blame] | 89 | return xstrdup(ret); |
| Alex Riesen | aba13e7 | 2008-10-27 10:17:51 | [diff] [blame] | 90 | } |
| 91 | |
| Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 09:03:23 | [diff] [blame] | 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 | |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 107 | char *mkpath(const char *fmt, ...) |
| 108 | { |
| 109 | va_list args; |
| 110 | unsigned len; |
| Linus Torvalds | e7676d2 | 2006-09-11 19:03:15 | [diff] [blame] | 111 | char *pathname = get_pathname(); |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 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 | { |
| Linus Torvalds | e7676d2 | 2006-09-11 19:03:15 | [diff] [blame] | 123 | char *pathname = get_pathname(); |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 124 | va_list args; |
| Ramsay Jones | 5c44252 | 2012-09-04 17:29:22 | [diff] [blame] | 125 | char *ret; |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 126 | |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 127 | va_start(args, fmt); |
| Ramsay Jones | 5c44252 | 2012-09-04 17:29:22 | [diff] [blame] | 128 | ret = vsnpath(pathname, PATH_MAX, fmt, args); |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 129 | va_end(args); |
| Ramsay Jones | 5c44252 | 2012-09-04 17:29:22 | [diff] [blame] | 130 | return ret; |
| Linus Torvalds | 26c8a53 | 2005-07-08 23:20:59 | [diff] [blame] | 131 | } |
| Holger Eitzenberger | f2db68e | 2005-08-04 20:43:03 | [diff] [blame] | 132 | |
| Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 09:03:23 | [diff] [blame] | 133 | void home_config_paths(char **global, char **xdg, char *file) |
| 134 | { |
| 135 | char *xdg_home = getenv("XDG_CONFIG_HOME"); |
| 136 | char *home = getenv("HOME"); |
| 137 | char *to_free = NULL; |
| 138 | |
| 139 | if (!home) { |
| 140 | if (global) |
| 141 | *global = NULL; |
| 142 | } else { |
| 143 | if (!xdg_home) { |
| 144 | to_free = mkpathdup("%s/.config", home); |
| 145 | xdg_home = to_free; |
| 146 | } |
| 147 | if (global) |
| 148 | *global = mkpathdup("%s/.gitconfig", home); |
| 149 | } |
| 150 | |
| 151 | if (!xdg_home) |
| 152 | *xdg = NULL; |
| 153 | else |
| 154 | *xdg = mkpathdup("%s/git/%s", xdg_home, file); |
| 155 | |
| 156 | free(to_free); |
| 157 | } |
| 158 | |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 159 | char *git_path_submodule(const char *path, const char *fmt, ...) |
| 160 | { |
| 161 | char *pathname = get_pathname(); |
| 162 | struct strbuf buf = STRBUF_INIT; |
| 163 | const char *git_dir; |
| 164 | va_list args; |
| 165 | unsigned len; |
| 166 | |
| 167 | len = strlen(path); |
| 168 | if (len > PATH_MAX-100) |
| 169 | return bad_path; |
| 170 | |
| 171 | strbuf_addstr(&buf, path); |
| 172 | if (len && path[len-1] != '/') |
| 173 | strbuf_addch(&buf, '/'); |
| 174 | strbuf_addstr(&buf, ".git"); |
| 175 | |
| Junio C Hamano | 13d6ec9 | 2011-08-22 21:04:56 | [diff] [blame] | 176 | git_dir = read_gitfile(buf.buf); |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 177 | if (git_dir) { |
| 178 | strbuf_reset(&buf); |
| 179 | strbuf_addstr(&buf, git_dir); |
| 180 | } |
| 181 | strbuf_addch(&buf, '/'); |
| 182 | |
| 183 | if (buf.len >= PATH_MAX) |
| 184 | return bad_path; |
| 185 | memcpy(pathname, buf.buf, buf.len + 1); |
| 186 | |
| 187 | strbuf_release(&buf); |
| 188 | len = strlen(pathname); |
| 189 | |
| 190 | va_start(args, fmt); |
| 191 | len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args); |
| 192 | va_end(args); |
| 193 | if (len >= PATH_MAX) |
| 194 | return bad_path; |
| 195 | return cleanup_path(pathname); |
| 196 | } |
| Holger Eitzenberger | f2db68e | 2005-08-04 20:43:03 | [diff] [blame] | 197 | |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 198 | int validate_headref(const char *path) |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 199 | { |
| 200 | struct stat st; |
| 201 | char *buf, buffer[256]; |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 202 | unsigned char sha1[20]; |
| Heikki Orsila | 0104ca0 | 2008-04-27 18:21:58 | [diff] [blame] | 203 | int fd; |
| 204 | ssize_t len; |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 205 | |
| 206 | if (lstat(path, &st) < 0) |
| 207 | return -1; |
| 208 | |
| 209 | /* Make sure it is a "refs/.." symlink */ |
| 210 | if (S_ISLNK(st.st_mode)) { |
| 211 | len = readlink(path, buffer, sizeof(buffer)-1); |
| Junio C Hamano | 222b167 | 2009-02-12 21:02:09 | [diff] [blame] | 212 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 213 | return 0; |
| 214 | return -1; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Anything else, just open it and try to see if it is a symbolic ref. |
| 219 | */ |
| 220 | fd = open(path, O_RDONLY); |
| 221 | if (fd < 0) |
| 222 | return -1; |
| Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 | [diff] [blame] | 223 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 224 | close(fd); |
| 225 | |
| 226 | /* |
| 227 | * Is it a symbolic ref? |
| 228 | */ |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 229 | if (len < 4) |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 230 | return -1; |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 231 | if (!memcmp("ref:", buffer, 4)) { |
| 232 | buf = buffer + 4; |
| 233 | len -= 4; |
| 234 | while (len && isspace(*buf)) |
| 235 | buf++, len--; |
| Junio C Hamano | 222b167 | 2009-02-12 21:02:09 | [diff] [blame] | 236 | if (len >= 5 && !memcmp("refs/", buf, 5)) |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Is this a detached HEAD? |
| 242 | */ |
| 243 | if (!get_sha1_hex(buffer, sha1)) |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 244 | return 0; |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 245 | |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 246 | return -1; |
| 247 | } |
| 248 | |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 249 | static struct passwd *getpw_str(const char *username, size_t len) |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 250 | { |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 251 | struct passwd *pw; |
| René Scharfe | 5c0b13f | 2014-07-19 15:35:34 | [diff] [blame] | 252 | char *username_z = xmemdupz(username, len); |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 253 | pw = getpwnam(username_z); |
| 254 | free(username_z); |
| 255 | return pw; |
| 256 | } |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 257 | |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 258 | /* |
| 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 |
| 261 | * if path is NULL. |
| 262 | */ |
| 263 | char *expand_user_path(const char *path) |
| 264 | { |
| 265 | struct strbuf user_path = STRBUF_INIT; |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 266 | const char *to_copy = path; |
| 267 | |
| 268 | if (path == NULL) |
| 269 | goto return_null; |
| 270 | if (path[0] == '~') { |
| Jeff King | 53ec551 | 2014-01-28 01:36:12 | [diff] [blame] | 271 | const char *first_slash = strchrnul(path, '/'); |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 272 | const char *username = path + 1; |
| 273 | size_t username_len = first_slash - username; |
| Matthieu Moy | df2a79f | 2009-11-19 15:21:15 | [diff] [blame] | 274 | if (username_len == 0) { |
| 275 | const char *home = getenv("HOME"); |
| Jonathan Nieder | 79bf149 | 2010-07-26 15:06:51 | [diff] [blame] | 276 | if (!home) |
| 277 | goto return_null; |
| René Scharfe | cedc61a | 2014-07-16 23:38:18 | [diff] [blame] | 278 | strbuf_addstr(&user_path, home); |
| Matthieu Moy | df2a79f | 2009-11-19 15:21:15 | [diff] [blame] | 279 | } else { |
| 280 | struct passwd *pw = getpw_str(username, username_len); |
| 281 | if (!pw) |
| 282 | goto return_null; |
| René Scharfe | cedc61a | 2014-07-16 23:38:18 | [diff] [blame] | 283 | strbuf_addstr(&user_path, pw->pw_dir); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 284 | } |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 285 | to_copy = first_slash; |
| Junio C Hamano | 0870ca7 | 2005-11-18 22:59:34 | [diff] [blame] | 286 | } |
| René Scharfe | cedc61a | 2014-07-16 23:38:18 | [diff] [blame] | 287 | strbuf_addstr(&user_path, to_copy); |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 288 | return strbuf_detach(&user_path, NULL); |
| 289 | return_null: |
| 290 | strbuf_release(&user_path); |
| 291 | return NULL; |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 292 | } |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 293 | |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 294 | /* |
| 295 | * First, one directory to try is determined by the following algorithm. |
| 296 | * |
| 297 | * (0) If "strict" is given, the path is used as given and no DWIM is |
| 298 | * done. Otherwise: |
| 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. |
| 303 | * |
| 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 |
| 306 | * what we try. |
| 307 | * |
| 308 | * Second, we try chdir() to that. Upon failure, we return NULL. |
| 309 | * |
| 310 | * Then, we try if the current directory is a valid git repository. |
| 311 | * Upon failure, we return NULL. |
| 312 | * |
| 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. |
| 317 | */ |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 318 | const char *enter_repo(const char *path, int strict) |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 319 | { |
| 320 | static char used_path[PATH_MAX]; |
| 321 | static char validated_path[PATH_MAX]; |
| 322 | |
| 323 | if (!path) |
| 324 | return NULL; |
| 325 | |
| 326 | if (!strict) { |
| 327 | static const char *suffix[] = { |
| Jeff King | b3256eb | 2012-02-02 21:59:13 | [diff] [blame] | 328 | "/.git", "", ".git/.git", ".git", NULL, |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 329 | }; |
| Phil Hord | 0310676 | 2011-10-04 20:05:17 | [diff] [blame] | 330 | const char *gitfile; |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 331 | int len = strlen(path); |
| 332 | int i; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 333 | while ((1 < len) && (path[len-1] == '/')) |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 334 | len--; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 335 | |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 336 | if (PATH_MAX <= len) |
| 337 | return NULL; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 338 | strncpy(used_path, path, len); used_path[len] = 0 ; |
| 339 | strcpy(validated_path, used_path); |
| 340 | |
| 341 | if (used_path[0] == '~') { |
| 342 | char *newpath = expand_user_path(used_path); |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 343 | if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { |
| 344 | free(newpath); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 345 | return NULL; |
| Matthieu Moy | 395de25 | 2009-11-17 17:24:25 | [diff] [blame] | 346 | } |
| 347 | /* |
| 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 |
| 351 | * anyway. |
| 352 | */ |
| 353 | strcpy(used_path, newpath); free(newpath); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 354 | } |
| 355 | else if (PATH_MAX - 10 < len) |
| 356 | return NULL; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 357 | len = strlen(used_path); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 358 | for (i = 0; suffix[i]; i++) { |
| Jeff King | b3256eb | 2012-02-02 21:59:13 | [diff] [blame] | 359 | struct stat st; |
| Erik Faye-Lund | 1c64b48 | 2011-10-04 20:02:00 | [diff] [blame] | 360 | strcpy(used_path + len, suffix[i]); |
| Jeff King | b3256eb | 2012-02-02 21:59:13 | [diff] [blame] | 361 | if (!stat(used_path, &st) && |
| 362 | (S_ISREG(st.st_mode) || |
| 363 | (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) { |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 364 | strcat(validated_path, suffix[i]); |
| 365 | break; |
| 366 | } |
| 367 | } |
| Phil Hord | 0310676 | 2011-10-04 20:05:17 | [diff] [blame] | 368 | if (!suffix[i]) |
| 369 | return NULL; |
| 370 | gitfile = read_gitfile(used_path) ; |
| 371 | if (gitfile) |
| 372 | strcpy(used_path, gitfile); |
| 373 | if (chdir(used_path)) |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 374 | return NULL; |
| 375 | path = validated_path; |
| 376 | } |
| 377 | else if (chdir(path)) |
| 378 | return NULL; |
| 379 | |
| 380 | if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && |
| Junio C Hamano | c847f53 | 2007-01-02 07:31:08 | [diff] [blame] | 381 | validate_headref("HEAD") == 0) { |
| René Scharfe | 717c397 | 2010-02-06 09:35:19 | [diff] [blame] | 382 | set_git_dir("."); |
| Junio C Hamano | 1644162 | 2005-11-25 18:48:26 | [diff] [blame] | 383 | check_repository_format(); |
| Junio C Hamano | d79374c | 2005-12-03 09:45:57 | [diff] [blame] | 384 | return path; |
| Andreas Ericsson | 54f4b87 | 2005-11-17 19:37:14 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | return NULL; |
| 388 | } |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 389 | |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 390 | static int calc_shared_perm(int mode) |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 391 | { |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 392 | int tweak; |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 393 | |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 394 | if (shared_repository < 0) |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 395 | tweak = -shared_repository; |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 396 | else |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 397 | tweak = shared_repository; |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 398 | |
| Junio C Hamano | 5a688fe | 2009-03-25 23:19:36 | [diff] [blame] | 399 | if (!(mode & S_IWUSR)) |
| 400 | tweak &= ~0222; |
| 401 | if (mode & S_IXUSR) |
| 402 | /* Copy read bits to execute bits */ |
| 403 | tweak |= (tweak & 0444) >> 2; |
| 404 | if (shared_repository < 0) |
| 405 | mode = (mode & ~0777) | tweak; |
| 406 | else |
| Petr Baudis | 8c6202d | 2008-07-12 01:15:03 | [diff] [blame] | 407 | mode |= tweak; |
| Junio C Hamano | 94df250 | 2006-06-10 06:09:49 | [diff] [blame] | 408 | |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 409 | return mode; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | int adjust_shared_perm(const char *path) |
| 414 | { |
| 415 | int old_mode, new_mode; |
| 416 | |
| 417 | if (!shared_repository) |
| 418 | return 0; |
| 419 | if (get_st_mode_bits(path, &old_mode) < 0) |
| 420 | return -1; |
| 421 | |
| 422 | new_mode = calc_shared_perm(old_mode); |
| 423 | if (S_ISDIR(old_mode)) { |
| Heikki Orsila | 06cbe85 | 2008-04-16 08:34:24 | [diff] [blame] | 424 | /* Copy read bits to execute bits */ |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 425 | new_mode |= (new_mode & 0444) >> 2; |
| 426 | new_mode |= FORCE_DIR_SET_GID; |
| Heikki Orsila | 06cbe85 | 2008-04-16 08:34:24 | [diff] [blame] | 427 | } |
| 428 | |
| Torsten Bögershausen | cbe43b8 | 2013-03-30 09:53:47 | [diff] [blame] | 429 | if (((old_mode ^ new_mode) & ~S_IFMT) && |
| 430 | chmod(path, (new_mode & ~S_IFMT)) < 0) |
| Junio C Hamano | 138086a | 2006-06-10 05:07:23 | [diff] [blame] | 431 | return -2; |
| 432 | return 0; |
| 433 | } |
| Johannes Schindelin | e5392c5 | 2007-08-01 00:28:59 | [diff] [blame] | 434 | |
| Jiang Xin | 7fbd422 | 2013-10-14 02:29:39 | [diff] [blame] | 435 | static int have_same_root(const char *path1, const char *path2) |
| 436 | { |
| 437 | int is_abs1, is_abs2; |
| 438 | |
| 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); |
| 443 | } |
| 444 | |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 445 | /* |
| 446 | * Give path as relative to prefix. |
| 447 | * |
| 448 | * The strbuf may or may not be used, so do not assume it contains the |
| 449 | * returned path. |
| 450 | */ |
| 451 | const char *relative_path(const char *in, const char *prefix, |
| 452 | struct strbuf *sb) |
| Linus Torvalds | 044bbbc | 2008-06-19 19:34:06 | [diff] [blame] | 453 | { |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 454 | int in_len = in ? strlen(in) : 0; |
| 455 | int prefix_len = prefix ? strlen(prefix) : 0; |
| 456 | int in_off = 0; |
| 457 | int prefix_off = 0; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 458 | int i = 0, j = 0; |
| 459 | |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 460 | if (!in_len) |
| 461 | return "./"; |
| 462 | else if (!prefix_len) |
| 463 | return in; |
| 464 | |
| Jiang Xin | 7fbd422 | 2013-10-14 02:29:39 | [diff] [blame] | 465 | if (have_same_root(in, prefix)) { |
| 466 | /* bypass dos_drive, for "c:" is identical to "C:" */ |
| 467 | if (has_dos_drive_prefix(in)) { |
| 468 | i = 2; |
| 469 | j = 2; |
| 470 | } |
| 471 | } else { |
| 472 | return in; |
| 473 | } |
| 474 | |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 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])) |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 478 | i++; |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 479 | while (is_dir_sep(in[j])) |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 480 | j++; |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 481 | prefix_off = i; |
| 482 | in_off = j; |
| 483 | } else { |
| 484 | i++; |
| 485 | j++; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if ( |
| 490 | /* "prefix" seems like prefix of "in" */ |
| 491 | i >= prefix_len && |
| 492 | /* |
| 493 | * but "/foo" is not a prefix of "/foobar" |
| 494 | * (i.e. prefix not end with '/') |
| 495 | */ |
| 496 | prefix_off < prefix_len) { |
| 497 | if (j >= in_len) { |
| 498 | /* in="/a/b", prefix="/a/b" */ |
| 499 | in_off = in_len; |
| 500 | } else if (is_dir_sep(in[j])) { |
| 501 | /* in="/a/b/c", prefix="/a/b" */ |
| 502 | while (is_dir_sep(in[j])) |
| 503 | j++; |
| 504 | in_off = j; |
| 505 | } else { |
| 506 | /* in="/a/bbb/c", prefix="/a/b" */ |
| 507 | i = prefix_off; |
| 508 | } |
| 509 | } else if ( |
| 510 | /* "in" is short than "prefix" */ |
| 511 | j >= in_len && |
| 512 | /* "in" not end with '/' */ |
| 513 | in_off < in_len) { |
| 514 | if (is_dir_sep(prefix[i])) { |
| 515 | /* in="/a/b", prefix="/a/b/c/" */ |
| 516 | while (is_dir_sep(prefix[i])) |
| 517 | i++; |
| 518 | in_off = in_len; |
| 519 | } |
| 520 | } |
| 521 | in += in_off; |
| 522 | in_len -= in_off; |
| 523 | |
| 524 | if (i >= prefix_len) { |
| 525 | if (!in_len) |
| 526 | return "./"; |
| 527 | else |
| 528 | return in; |
| 529 | } |
| 530 | |
| 531 | strbuf_reset(sb); |
| 532 | strbuf_grow(sb, in_len); |
| 533 | |
| 534 | while (i < prefix_len) { |
| 535 | if (is_dir_sep(prefix[i])) { |
| 536 | strbuf_addstr(sb, "../"); |
| 537 | while (is_dir_sep(prefix[i])) |
| 538 | i++; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 539 | continue; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 540 | } |
| 541 | i++; |
| Junio C Hamano | 288123f | 2010-01-22 03:05:19 | [diff] [blame] | 542 | } |
| Jiang Xin | e02ca72 | 2013-06-25 15:53:43 | [diff] [blame] | 543 | if (!is_dir_sep(prefix[prefix_len - 1])) |
| 544 | strbuf_addstr(sb, "../"); |
| 545 | |
| 546 | strbuf_addstr(sb, in); |
| 547 | |
| 548 | return sb->buf; |
| Linus Torvalds | 044bbbc | 2008-06-19 19:34:06 | [diff] [blame] | 549 | } |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 550 | |
| 551 | /* |
| Jiang Xin | 41894ae | 2013-10-14 02:29:40 | [diff] [blame] | 552 | * A simpler implementation of relative_path |
| 553 | * |
| 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. |
| 557 | */ |
| 558 | const char *remove_leading_path(const char *in, const char *prefix) |
| 559 | { |
| 560 | static char buf[PATH_MAX + 1]; |
| 561 | int i = 0, j = 0; |
| 562 | |
| 563 | if (!prefix || !prefix[0]) |
| 564 | return in; |
| 565 | while (prefix[i]) { |
| 566 | if (is_dir_sep(prefix[i])) { |
| 567 | if (!is_dir_sep(in[j])) |
| 568 | return in; |
| 569 | while (is_dir_sep(prefix[i])) |
| 570 | i++; |
| 571 | while (is_dir_sep(in[j])) |
| 572 | j++; |
| 573 | continue; |
| 574 | } else if (in[j] != prefix[i]) { |
| 575 | return in; |
| 576 | } |
| 577 | i++; |
| 578 | j++; |
| 579 | } |
| 580 | if ( |
| 581 | /* "/foo" is a prefix of "/foo" */ |
| 582 | in[j] && |
| 583 | /* "/foo" is not a prefix of "/foobar" */ |
| 584 | !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j]) |
| 585 | ) |
| 586 | return in; |
| 587 | while (is_dir_sep(in[j])) |
| 588 | j++; |
| 589 | if (!in[j]) |
| 590 | strcpy(buf, "."); |
| 591 | else |
| 592 | strcpy(buf, in + j); |
| 593 | return buf; |
| 594 | } |
| 595 | |
| 596 | /* |
| Johannes Sixt | f2a782b | 2009-02-07 15:08:31 | [diff] [blame] | 597 | * It is okay if dst == src, but they should not overlap otherwise. |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 598 | * |
| Johannes Sixt | f2a782b | 2009-02-07 15:08:31 | [diff] [blame] | 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 '/'. |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 602 | * - Removes "." components. |
| 603 | * - Removes ".." components, and the components the precede them. |
| Johannes Sixt | f2a782b | 2009-02-07 15:08:31 | [diff] [blame] | 604 | * Returns failure (non-zero) if a ".." component appears as first path |
| 605 | * component anytime during the normalization. Otherwise, returns success (0). |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 606 | * |
| 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. |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 609 | * |
| 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. |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 615 | */ |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 616 | int normalize_path_copy_len(char *dst, const char *src, int *prefix_len) |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 617 | { |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 618 | char *dst0; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 619 | |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 620 | if (has_dos_drive_prefix(src)) { |
| 621 | *dst++ = *src++; |
| 622 | *dst++ = *src++; |
| 623 | } |
| 624 | dst0 = dst; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 625 | |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 626 | if (is_dir_sep(*src)) { |
| 627 | *dst++ = '/'; |
| 628 | while (is_dir_sep(*src)) |
| 629 | src++; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 630 | } |
| 631 | |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 632 | for (;;) { |
| 633 | char c = *src; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 634 | |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 635 | /* |
| 636 | * A path component that begins with . could be |
| 637 | * special: |
| 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. |
| 642 | */ |
| 643 | if (c == '.') { |
| 644 | if (!src[1]) { |
| 645 | /* (1) */ |
| 646 | src++; |
| 647 | } else if (is_dir_sep(src[1])) { |
| 648 | /* (2) */ |
| 649 | src += 2; |
| 650 | while (is_dir_sep(*src)) |
| 651 | src++; |
| 652 | continue; |
| 653 | } else if (src[1] == '.') { |
| 654 | if (!src[2]) { |
| 655 | /* (3) */ |
| 656 | src += 2; |
| 657 | goto up_one; |
| 658 | } else if (is_dir_sep(src[2])) { |
| 659 | /* (4) */ |
| 660 | src += 3; |
| 661 | while (is_dir_sep(*src)) |
| 662 | src++; |
| 663 | goto up_one; |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /* copy up to the next '/', and eat all '/' */ |
| 669 | while ((c = *src++) != '\0' && !is_dir_sep(c)) |
| 670 | *dst++ = c; |
| 671 | if (is_dir_sep(c)) { |
| 672 | *dst++ = '/'; |
| 673 | while (is_dir_sep(c)) |
| 674 | c = *src++; |
| 675 | src--; |
| 676 | } else if (!c) |
| 677 | break; |
| 678 | continue; |
| 679 | |
| 680 | up_one: |
| 681 | /* |
| 682 | * dst0..dst is prefix portion, and dst[-1] is '/'; |
| 683 | * go up one level. |
| 684 | */ |
| Johannes Sixt | f42302b | 2009-02-07 15:08:30 | [diff] [blame] | 685 | dst--; /* go to trailing '/' */ |
| 686 | if (dst <= dst0) |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 687 | return -1; |
| Johannes Sixt | f42302b | 2009-02-07 15:08:30 | [diff] [blame] | 688 | /* Windows: dst[-1] cannot be backslash anymore */ |
| 689 | while (dst0 < dst && dst[-1] != '/') |
| 690 | dst--; |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 691 | if (prefix_len && *prefix_len > dst - dst0) |
| 692 | *prefix_len = dst - dst0; |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 693 | } |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 694 | *dst = '\0'; |
| Johannes Sixt | f3cad0a | 2009-02-07 15:08:28 | [diff] [blame] | 695 | return 0; |
| David Reiss | ae299be | 2008-05-20 06:48:54 | [diff] [blame] | 696 | } |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 697 | |
| Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 08:36:03 | [diff] [blame] | 698 | int normalize_path_copy(char *dst, const char *src) |
| 699 | { |
| 700 | return normalize_path_copy_len(dst, src, NULL); |
| 701 | } |
| 702 | |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 703 | /* |
| 704 | * path = Canonical absolute path |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 705 | * prefixes = string_list containing normalized, absolute paths without |
| 706 | * trailing slashes (except for the root directory, which is denoted by "/"). |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 707 | * |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 708 | * Determines, for each path in prefixes, whether the "prefix" |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 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 |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 711 | * is an ancestor. (Note that this means 0 is returned if prefixes is |
| 712 | * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 713 | * are not considered to be their own ancestors. path must be in a |
| 714 | * canonical form: empty components, or "." or ".." components are not |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 715 | * allowed. |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 716 | */ |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 717 | int longest_ancestor_length(const char *path, struct string_list *prefixes) |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 718 | { |
| Michael Haggerty | a5ccdbe | 2012-10-28 16:16:23 | [diff] [blame] | 719 | int i, max_len = -1; |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 720 | |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 721 | if (!strcmp(path, "/")) |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 722 | return -1; |
| 723 | |
| Michael Haggerty | 31171d9 | 2012-10-28 16:16:24 | [diff] [blame] | 724 | for (i = 0; i < prefixes->nr; i++) { |
| 725 | const char *ceil = prefixes->items[i].string; |
| Michael Haggerty | a5ccdbe | 2012-10-28 16:16:23 | [diff] [blame] | 726 | int len = strlen(ceil); |
| 727 | |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 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 */ |
| 732 | else |
| 733 | continue; /* no match */ |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 734 | |
| Michael Haggerty | 9e2326c | 2012-10-28 16:16:25 | [diff] [blame] | 735 | if (len > max_len) |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 736 | max_len = len; |
| David Reiss | 0454dd9 | 2008-05-20 06:49:26 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | return max_len; |
| 740 | } |
| Johannes Schindelin | 4fcc86b | 2009-02-19 19:10:49 | [diff] [blame] | 741 | |
| 742 | /* strip arbitrary amount of directory separators at end of path */ |
| 743 | static inline int chomp_trailing_dir_sep(const char *path, int len) |
| 744 | { |
| 745 | while (len && is_dir_sep(path[len - 1])) |
| 746 | len--; |
| 747 | return len; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * If path ends with suffix (complete path components), returns the |
| 752 | * part before suffix (sans trailing directory separators). |
| 753 | * Otherwise returns NULL. |
| 754 | */ |
| 755 | char *strip_path_suffix(const char *path, const char *suffix) |
| 756 | { |
| 757 | int path_len = strlen(path), suffix_len = strlen(suffix); |
| 758 | |
| 759 | while (suffix_len) { |
| 760 | if (!path_len) |
| 761 | return NULL; |
| 762 | |
| 763 | if (is_dir_sep(path[path_len - 1])) { |
| 764 | if (!is_dir_sep(suffix[suffix_len - 1])) |
| 765 | return NULL; |
| 766 | path_len = chomp_trailing_dir_sep(path, path_len); |
| 767 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); |
| 768 | } |
| 769 | else if (path[--path_len] != suffix[--suffix_len]) |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | if (path_len && !is_dir_sep(path[path_len - 1])) |
| 774 | return NULL; |
| 775 | return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); |
| 776 | } |
| Shawn O. Pearce | 34b6cb8 | 2009-11-09 19:26:43 | [diff] [blame] | 777 | |
| 778 | int daemon_avoid_alias(const char *p) |
| 779 | { |
| 780 | int sl, ndot; |
| 781 | |
| 782 | /* |
| 783 | * This resurrects the belts and suspenders paranoia check by HPA |
| 784 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() |
| Junio C Hamano | 9517e6b | 2010-02-04 05:23:18 | [diff] [blame] | 785 | * does not do getcwd() based path canonicalization. |
| Shawn O. Pearce | 34b6cb8 | 2009-11-09 19:26:43 | [diff] [blame] | 786 | * |
| 787 | * sl becomes true immediately after seeing '/' and continues to |
| 788 | * be true as long as dots continue after that without intervening |
| 789 | * non-dot character. |
| 790 | */ |
| 791 | if (!p || (*p != '/' && *p != '~')) |
| 792 | return -1; |
| 793 | sl = 1; ndot = 0; |
| 794 | p++; |
| 795 | |
| 796 | while (1) { |
| 797 | char ch = *p++; |
| 798 | if (sl) { |
| 799 | if (ch == '.') |
| 800 | ndot++; |
| 801 | else if (ch == '/') { |
| 802 | if (ndot < 3) |
| 803 | /* reject //, /./ and /../ */ |
| 804 | return -1; |
| 805 | ndot = 0; |
| 806 | } |
| 807 | else if (ch == 0) { |
| 808 | if (0 < ndot && ndot < 3) |
| 809 | /* reject /.$ and /..$ */ |
| 810 | return -1; |
| 811 | return 0; |
| 812 | } |
| 813 | else |
| 814 | sl = ndot = 0; |
| 815 | } |
| 816 | else if (ch == 0) |
| 817 | return 0; |
| 818 | else if (ch == '/') { |
| 819 | sl = 1; |
| 820 | ndot = 0; |
| 821 | } |
| 822 | } |
| 823 | } |