Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * I'm tired of doing "vsnprintf()" etc just to open a |
| 3 | * file, so here's a "return static buffer with printf" |
| 4 | * interface for paths. |
| 5 | * |
| 6 | * It's obviously not thread-safe. Sue me. But it's quite |
| 7 | * useful for doing things like |
| 8 | * |
| 9 | * f = open(mkpath("%s/%s.git", base, name), O_RDONLY); |
| 10 | * |
| 11 | * which is what it's designed for. |
| 12 | */ |
| 13 | #include "cache.h" |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 14 | #include "strbuf.h" |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 16 | static char bad_path[] = "/bad-path/"; |
| 17 | |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 18 | static char *get_pathname(void) |
| 19 | { |
| 20 | static char pathname_array[4][PATH_MAX]; |
| 21 | static int index; |
| 22 | return pathname_array[3 & ++index]; |
| 23 | } |
| 24 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 25 | static char *cleanup_path(char *path) |
| 26 | { |
| 27 | /* Clean it up */ |
| 28 | if (!memcmp(path, "./", 2)) { |
| 29 | path += 2; |
| 30 | while (*path == '/') |
| 31 | path++; |
| 32 | } |
| 33 | return path; |
| 34 | } |
| 35 | |
Alex Riesen | 108bebe | 2008-10-26 22:59:13 +0100 | [diff] [blame] | 36 | char *mksnpath(char *buf, size_t n, const char *fmt, ...) |
| 37 | { |
| 38 | va_list args; |
| 39 | unsigned len; |
| 40 | |
| 41 | va_start(args, fmt); |
| 42 | len = vsnprintf(buf, n, fmt, args); |
| 43 | va_end(args); |
| 44 | if (len >= n) { |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 45 | strlcpy(buf, bad_path, n); |
Alex Riesen | 108bebe | 2008-10-26 22:59:13 +0100 | [diff] [blame] | 46 | return buf; |
| 47 | } |
| 48 | return cleanup_path(buf); |
| 49 | } |
| 50 | |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 51 | static char *git_vsnpath(char *buf, size_t n, const char *fmt, va_list args) |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 52 | { |
| 53 | const char *git_dir = get_git_dir(); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 54 | size_t len; |
| 55 | |
| 56 | len = strlen(git_dir); |
| 57 | if (n < len + 1) |
| 58 | goto bad; |
| 59 | memcpy(buf, git_dir, len); |
| 60 | if (len && !is_dir_sep(git_dir[len-1])) |
| 61 | buf[len++] = '/'; |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 62 | len += vsnprintf(buf + len, n - len, fmt, args); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 63 | if (len >= n) |
| 64 | goto bad; |
| 65 | return cleanup_path(buf); |
| 66 | bad: |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 67 | strlcpy(buf, bad_path, n); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 68 | return buf; |
| 69 | } |
| 70 | |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 71 | char *git_snpath(char *buf, size_t n, const char *fmt, ...) |
| 72 | { |
| 73 | va_list args; |
| 74 | va_start(args, fmt); |
| 75 | (void)git_vsnpath(buf, n, fmt, args); |
| 76 | va_end(args); |
| 77 | return buf; |
| 78 | } |
| 79 | |
| 80 | char *git_pathdup(const char *fmt, ...) |
| 81 | { |
| 82 | char path[PATH_MAX]; |
| 83 | va_list args; |
| 84 | va_start(args, fmt); |
| 85 | (void)git_vsnpath(path, sizeof(path), fmt, args); |
| 86 | va_end(args); |
| 87 | return xstrdup(path); |
| 88 | } |
| 89 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 90 | char *mkpath(const char *fmt, ...) |
| 91 | { |
| 92 | va_list args; |
| 93 | unsigned len; |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 94 | char *pathname = get_pathname(); |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 95 | |
| 96 | va_start(args, fmt); |
| 97 | len = vsnprintf(pathname, PATH_MAX, fmt, args); |
| 98 | va_end(args); |
| 99 | if (len >= PATH_MAX) |
| 100 | return bad_path; |
| 101 | return cleanup_path(pathname); |
| 102 | } |
| 103 | |
| 104 | char *git_path(const char *fmt, ...) |
| 105 | { |
Sven Verdoolaege | 5da1606 | 2005-09-26 22:54:01 +0200 | [diff] [blame] | 106 | const char *git_dir = get_git_dir(); |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 107 | char *pathname = get_pathname(); |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 108 | va_list args; |
| 109 | unsigned len; |
| 110 | |
| 111 | len = strlen(git_dir); |
| 112 | if (len > PATH_MAX-100) |
| 113 | return bad_path; |
| 114 | memcpy(pathname, git_dir, len); |
| 115 | if (len && git_dir[len-1] != '/') |
| 116 | pathname[len++] = '/'; |
| 117 | va_start(args, fmt); |
| 118 | len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args); |
| 119 | va_end(args); |
| 120 | if (len >= PATH_MAX) |
| 121 | return bad_path; |
| 122 | return cleanup_path(pathname); |
| 123 | } |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 124 | |
| 125 | |
| 126 | /* git_mkstemp() - create tmp file honoring TMPDIR variable */ |
| 127 | int git_mkstemp(char *path, size_t len, const char *template) |
| 128 | { |
Junio C Hamano | e7a7be8 | 2007-07-25 21:34:53 -0700 | [diff] [blame] | 129 | const char *tmp; |
| 130 | size_t n; |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 131 | |
Junio C Hamano | e7a7be8 | 2007-07-25 21:34:53 -0700 | [diff] [blame] | 132 | tmp = getenv("TMPDIR"); |
| 133 | if (!tmp) |
| 134 | tmp = "/tmp"; |
| 135 | n = snprintf(path, len, "%s/%s", tmp, template); |
| 136 | if (len <= n) { |
| 137 | errno = ENAMETOOLONG; |
| 138 | return -1; |
Holger Eitzenberger | 35c3c62 | 2005-08-08 22:33:08 +0200 | [diff] [blame] | 139 | } |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 140 | return mkstemp(path); |
| 141 | } |
| 142 | |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 143 | /* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */ |
| 144 | int git_mkstemps(char *path, size_t len, const char *template, int suffix_len) |
| 145 | { |
| 146 | const char *tmp; |
| 147 | size_t n; |
| 148 | |
| 149 | tmp = getenv("TMPDIR"); |
| 150 | if (!tmp) |
| 151 | tmp = "/tmp"; |
| 152 | n = snprintf(path, len, "%s/%s", tmp, template); |
| 153 | if (len <= n) { |
| 154 | errno = ENAMETOOLONG; |
| 155 | return -1; |
| 156 | } |
| 157 | return mkstemps(path, suffix_len); |
| 158 | } |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 159 | |
Matthieu Moy | 00787ed | 2010-02-22 23:32:12 +0100 | [diff] [blame] | 160 | /* Adapted from libiberty's mkstemp.c. */ |
| 161 | |
| 162 | #undef TMP_MAX |
| 163 | #define TMP_MAX 16384 |
| 164 | |
Matthieu Moy | b862b61 | 2010-02-22 23:32:13 +0100 | [diff] [blame] | 165 | int git_mkstemps_mode(char *pattern, int suffix_len, int mode) |
Matthieu Moy | 00787ed | 2010-02-22 23:32:12 +0100 | [diff] [blame] | 166 | { |
| 167 | static const char letters[] = |
| 168 | "abcdefghijklmnopqrstuvwxyz" |
| 169 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 170 | "0123456789"; |
| 171 | static const int num_letters = 62; |
| 172 | uint64_t value; |
| 173 | struct timeval tv; |
| 174 | char *template; |
| 175 | size_t len; |
| 176 | int fd, count; |
| 177 | |
| 178 | len = strlen(pattern); |
| 179 | |
| 180 | if (len < 6 + suffix_len) { |
| 181 | errno = EINVAL; |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) { |
| 186 | errno = EINVAL; |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Replace pattern's XXXXXX characters with randomness. |
| 192 | * Try TMP_MAX different filenames. |
| 193 | */ |
| 194 | gettimeofday(&tv, NULL); |
| 195 | value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid(); |
| 196 | template = &pattern[len - 6 - suffix_len]; |
| 197 | for (count = 0; count < TMP_MAX; ++count) { |
| 198 | uint64_t v = value; |
| 199 | /* Fill in the random bits. */ |
| 200 | template[0] = letters[v % num_letters]; v /= num_letters; |
| 201 | template[1] = letters[v % num_letters]; v /= num_letters; |
| 202 | template[2] = letters[v % num_letters]; v /= num_letters; |
| 203 | template[3] = letters[v % num_letters]; v /= num_letters; |
| 204 | template[4] = letters[v % num_letters]; v /= num_letters; |
| 205 | template[5] = letters[v % num_letters]; v /= num_letters; |
| 206 | |
Matthieu Moy | b862b61 | 2010-02-22 23:32:13 +0100 | [diff] [blame] | 207 | fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode); |
Matthieu Moy | 00787ed | 2010-02-22 23:32:12 +0100 | [diff] [blame] | 208 | if (fd > 0) |
| 209 | return fd; |
| 210 | /* |
| 211 | * Fatal error (EPERM, ENOSPC etc). |
| 212 | * It doesn't make sense to loop. |
| 213 | */ |
| 214 | if (errno != EEXIST) |
| 215 | break; |
| 216 | /* |
| 217 | * This is a random value. It is only necessary that |
| 218 | * the next TMP_MAX values generated by adding 7777 to |
| 219 | * VALUE are different with (module 2^32). |
| 220 | */ |
| 221 | value += 7777; |
| 222 | } |
| 223 | /* We return the null string if we can't find a unique file name. */ |
| 224 | pattern[0] = '\0'; |
Matthieu Moy | 00787ed | 2010-02-22 23:32:12 +0100 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | |
Matthieu Moy | b862b61 | 2010-02-22 23:32:13 +0100 | [diff] [blame] | 228 | int git_mkstemp_mode(char *pattern, int mode) |
| 229 | { |
| 230 | /* mkstemp is just mkstemps with no suffix */ |
| 231 | return git_mkstemps_mode(pattern, 0, mode); |
| 232 | } |
| 233 | |
| 234 | int gitmkstemps(char *pattern, int suffix_len) |
| 235 | { |
| 236 | return git_mkstemps_mode(pattern, suffix_len, 0600); |
| 237 | } |
| 238 | |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 239 | int validate_headref(const char *path) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 240 | { |
| 241 | struct stat st; |
| 242 | char *buf, buffer[256]; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 243 | unsigned char sha1[20]; |
Heikki Orsila | 0104ca0 | 2008-04-27 21:21:58 +0300 | [diff] [blame] | 244 | int fd; |
| 245 | ssize_t len; |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 246 | |
| 247 | if (lstat(path, &st) < 0) |
| 248 | return -1; |
| 249 | |
| 250 | /* Make sure it is a "refs/.." symlink */ |
| 251 | if (S_ISLNK(st.st_mode)) { |
| 252 | len = readlink(path, buffer, sizeof(buffer)-1); |
Junio C Hamano | 222b167 | 2009-02-12 13:02:09 -0800 | [diff] [blame] | 253 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 254 | return 0; |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Anything else, just open it and try to see if it is a symbolic ref. |
| 260 | */ |
| 261 | fd = open(path, O_RDONLY); |
| 262 | if (fd < 0) |
| 263 | return -1; |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 264 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 265 | close(fd); |
| 266 | |
| 267 | /* |
| 268 | * Is it a symbolic ref? |
| 269 | */ |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 270 | if (len < 4) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 271 | return -1; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 272 | if (!memcmp("ref:", buffer, 4)) { |
| 273 | buf = buffer + 4; |
| 274 | len -= 4; |
| 275 | while (len && isspace(*buf)) |
| 276 | buf++, len--; |
Junio C Hamano | 222b167 | 2009-02-12 13:02:09 -0800 | [diff] [blame] | 277 | if (len >= 5 && !memcmp("refs/", buf, 5)) |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Is this a detached HEAD? |
| 283 | */ |
| 284 | if (!get_sha1_hex(buffer, sha1)) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 285 | return 0; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 286 | |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 287 | return -1; |
| 288 | } |
| 289 | |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 290 | static struct passwd *getpw_str(const char *username, size_t len) |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 291 | { |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 292 | struct passwd *pw; |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 293 | char *username_z = xmalloc(len + 1); |
| 294 | memcpy(username_z, username, len); |
| 295 | username_z[len] = '\0'; |
| 296 | pw = getpwnam(username_z); |
| 297 | free(username_z); |
| 298 | return pw; |
| 299 | } |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 300 | |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 301 | /* |
| 302 | * Return a string with ~ and ~user expanded via getpw*. If buf != NULL, |
| 303 | * then it is a newly allocated string. Returns NULL on getpw failure or |
| 304 | * if path is NULL. |
| 305 | */ |
| 306 | char *expand_user_path(const char *path) |
| 307 | { |
| 308 | struct strbuf user_path = STRBUF_INIT; |
| 309 | const char *first_slash = strchrnul(path, '/'); |
| 310 | const char *to_copy = path; |
| 311 | |
| 312 | if (path == NULL) |
| 313 | goto return_null; |
| 314 | if (path[0] == '~') { |
| 315 | const char *username = path + 1; |
| 316 | size_t username_len = first_slash - username; |
Matthieu Moy | df2a79f | 2009-11-19 16:21:15 +0100 | [diff] [blame] | 317 | if (username_len == 0) { |
| 318 | const char *home = getenv("HOME"); |
| 319 | strbuf_add(&user_path, home, strlen(home)); |
| 320 | } else { |
| 321 | struct passwd *pw = getpw_str(username, username_len); |
| 322 | if (!pw) |
| 323 | goto return_null; |
| 324 | strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir)); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 325 | } |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 326 | to_copy = first_slash; |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 327 | } |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 328 | strbuf_add(&user_path, to_copy, strlen(to_copy)); |
| 329 | return strbuf_detach(&user_path, NULL); |
| 330 | return_null: |
| 331 | strbuf_release(&user_path); |
| 332 | return NULL; |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 333 | } |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 334 | |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 335 | /* |
| 336 | * First, one directory to try is determined by the following algorithm. |
| 337 | * |
| 338 | * (0) If "strict" is given, the path is used as given and no DWIM is |
| 339 | * done. Otherwise: |
| 340 | * (1) "~/path" to mean path under the running user's home directory; |
| 341 | * (2) "~user/path" to mean path under named user's home directory; |
| 342 | * (3) "relative/path" to mean cwd relative directory; or |
| 343 | * (4) "/absolute/path" to mean absolute directory. |
| 344 | * |
| 345 | * Unless "strict" is given, we try access() for existence of "%s.git/.git", |
| 346 | * "%s/.git", "%s.git", "%s" in this order. The first one that exists is |
| 347 | * what we try. |
| 348 | * |
| 349 | * Second, we try chdir() to that. Upon failure, we return NULL. |
| 350 | * |
| 351 | * Then, we try if the current directory is a valid git repository. |
| 352 | * Upon failure, we return NULL. |
| 353 | * |
| 354 | * If all goes well, we return the directory we used to chdir() (but |
| 355 | * before ~user is expanded), avoiding getcwd() resolving symbolic |
| 356 | * links. User relative paths are also returned as they are given, |
| 357 | * except DWIM suffixing. |
| 358 | */ |
| 359 | char *enter_repo(char *path, int strict) |
| 360 | { |
| 361 | static char used_path[PATH_MAX]; |
| 362 | static char validated_path[PATH_MAX]; |
| 363 | |
| 364 | if (!path) |
| 365 | return NULL; |
| 366 | |
| 367 | if (!strict) { |
| 368 | static const char *suffix[] = { |
| 369 | ".git/.git", "/.git", ".git", "", NULL, |
| 370 | }; |
| 371 | int len = strlen(path); |
| 372 | int i; |
| 373 | while ((1 < len) && (path[len-1] == '/')) { |
| 374 | path[len-1] = 0; |
| 375 | len--; |
| 376 | } |
| 377 | if (PATH_MAX <= len) |
| 378 | return NULL; |
| 379 | if (path[0] == '~') { |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 380 | char *newpath = expand_user_path(path); |
| 381 | if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { |
| 382 | free(newpath); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 383 | return NULL; |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 384 | } |
| 385 | /* |
| 386 | * Copy back into the static buffer. A pity |
| 387 | * since newpath was not bounded, but other |
| 388 | * branches of the if are limited by PATH_MAX |
| 389 | * anyway. |
| 390 | */ |
| 391 | strcpy(used_path, newpath); free(newpath); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 392 | strcpy(validated_path, path); |
| 393 | path = used_path; |
| 394 | } |
| 395 | else if (PATH_MAX - 10 < len) |
| 396 | return NULL; |
| 397 | else { |
| 398 | path = strcpy(used_path, path); |
| 399 | strcpy(validated_path, path); |
| 400 | } |
| 401 | len = strlen(path); |
| 402 | for (i = 0; suffix[i]; i++) { |
| 403 | strcpy(path + len, suffix[i]); |
| 404 | if (!access(path, F_OK)) { |
| 405 | strcat(validated_path, suffix[i]); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | if (!suffix[i] || chdir(path)) |
| 410 | return NULL; |
| 411 | path = validated_path; |
| 412 | } |
| 413 | else if (chdir(path)) |
| 414 | return NULL; |
| 415 | |
| 416 | if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 417 | validate_headref("HEAD") == 0) { |
René Scharfe | 717c397 | 2010-02-06 10:35:19 +0100 | [diff] [blame] | 418 | set_git_dir("."); |
Junio C Hamano | 1644162 | 2005-11-25 10:48:26 -0800 | [diff] [blame] | 419 | check_repository_format(); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 420 | return path; |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return NULL; |
| 424 | } |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 425 | |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame] | 426 | int set_shared_perm(const char *path, int mode) |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 427 | { |
| 428 | struct stat st; |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame] | 429 | int tweak, shared, orig_mode; |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 430 | |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame] | 431 | if (!shared_repository) { |
| 432 | if (mode) |
| 433 | return chmod(path, mode & ~S_IFMT); |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 434 | return 0; |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame] | 435 | } |
| 436 | if (!mode) { |
| 437 | if (lstat(path, &st) < 0) |
| 438 | return -1; |
| 439 | mode = st.st_mode; |
| 440 | orig_mode = mode; |
| 441 | } else |
| 442 | orig_mode = 0; |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 443 | if (shared_repository < 0) |
| 444 | shared = -shared_repository; |
| 445 | else |
| 446 | shared = shared_repository; |
| 447 | tweak = shared; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 448 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 449 | if (!(mode & S_IWUSR)) |
| 450 | tweak &= ~0222; |
| 451 | if (mode & S_IXUSR) |
| 452 | /* Copy read bits to execute bits */ |
| 453 | tweak |= (tweak & 0444) >> 2; |
| 454 | if (shared_repository < 0) |
| 455 | mode = (mode & ~0777) | tweak; |
| 456 | else |
Petr Baudis | 8c6202d | 2008-07-12 03:15:03 +0200 | [diff] [blame] | 457 | mode |= tweak; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 458 | |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 459 | if (S_ISDIR(mode)) { |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 460 | /* Copy read bits to execute bits */ |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 461 | mode |= (shared & 0444) >> 2; |
| 462 | mode |= FORCE_DIR_SET_GID; |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 463 | } |
| 464 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 465 | if (((shared_repository < 0 |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame] | 466 | ? (orig_mode & (FORCE_DIR_SET_GID | 0777)) |
| 467 | : (orig_mode & mode)) != mode) && |
| 468 | chmod(path, (mode & ~S_IFMT)) < 0) |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 469 | return -2; |
| 470 | return 0; |
| 471 | } |
Johannes Schindelin | e5392c5 | 2007-08-01 01:28:59 +0100 | [diff] [blame] | 472 | |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 473 | const char *make_relative_path(const char *abs, const char *base) |
| 474 | { |
| 475 | static char buf[PATH_MAX + 1]; |
Junio C Hamano | 288123f | 2010-01-21 19:05:19 -0800 | [diff] [blame] | 476 | int i = 0, j = 0; |
| 477 | |
| 478 | if (!base || !base[0]) |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 479 | return abs; |
Junio C Hamano | 288123f | 2010-01-21 19:05:19 -0800 | [diff] [blame] | 480 | while (base[i]) { |
| 481 | if (is_dir_sep(base[i])) { |
| 482 | if (!is_dir_sep(abs[j])) |
| 483 | return abs; |
| 484 | while (is_dir_sep(base[i])) |
| 485 | i++; |
| 486 | while (is_dir_sep(abs[j])) |
| 487 | j++; |
| 488 | continue; |
| 489 | } else if (abs[j] != base[i]) { |
| 490 | return abs; |
| 491 | } |
| 492 | i++; |
| 493 | j++; |
| 494 | } |
| 495 | if ( |
| 496 | /* "/foo" is a prefix of "/foo" */ |
| 497 | abs[j] && |
| 498 | /* "/foo" is not a prefix of "/foobar" */ |
| 499 | !is_dir_sep(base[i-1]) && !is_dir_sep(abs[j]) |
| 500 | ) |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 501 | return abs; |
Junio C Hamano | 288123f | 2010-01-21 19:05:19 -0800 | [diff] [blame] | 502 | while (is_dir_sep(abs[j])) |
| 503 | j++; |
| 504 | if (!abs[j]) |
| 505 | strcpy(buf, "."); |
| 506 | else |
| 507 | strcpy(buf, abs + j); |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 508 | return buf; |
| 509 | } |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 510 | |
| 511 | /* |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 512 | * It is okay if dst == src, but they should not overlap otherwise. |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 513 | * |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 514 | * Performs the following normalizations on src, storing the result in dst: |
| 515 | * - Ensures that components are separated by '/' (Windows only) |
| 516 | * - Squashes sequences of '/'. |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 517 | * - Removes "." components. |
| 518 | * - Removes ".." components, and the components the precede them. |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 519 | * Returns failure (non-zero) if a ".." component appears as first path |
| 520 | * component anytime during the normalization. Otherwise, returns success (0). |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 521 | * |
| 522 | * Note that this function is purely textual. It does not follow symlinks, |
| 523 | * verify the existence of the path, or make any system calls. |
| 524 | */ |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 525 | int normalize_path_copy(char *dst, const char *src) |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 526 | { |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 527 | char *dst0; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 528 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 529 | if (has_dos_drive_prefix(src)) { |
| 530 | *dst++ = *src++; |
| 531 | *dst++ = *src++; |
| 532 | } |
| 533 | dst0 = dst; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 534 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 535 | if (is_dir_sep(*src)) { |
| 536 | *dst++ = '/'; |
| 537 | while (is_dir_sep(*src)) |
| 538 | src++; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 541 | for (;;) { |
| 542 | char c = *src; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 543 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 544 | /* |
| 545 | * A path component that begins with . could be |
| 546 | * special: |
| 547 | * (1) "." and ends -- ignore and terminate. |
| 548 | * (2) "./" -- ignore them, eat slash and continue. |
| 549 | * (3) ".." and ends -- strip one and terminate. |
| 550 | * (4) "../" -- strip one, eat slash and continue. |
| 551 | */ |
| 552 | if (c == '.') { |
| 553 | if (!src[1]) { |
| 554 | /* (1) */ |
| 555 | src++; |
| 556 | } else if (is_dir_sep(src[1])) { |
| 557 | /* (2) */ |
| 558 | src += 2; |
| 559 | while (is_dir_sep(*src)) |
| 560 | src++; |
| 561 | continue; |
| 562 | } else if (src[1] == '.') { |
| 563 | if (!src[2]) { |
| 564 | /* (3) */ |
| 565 | src += 2; |
| 566 | goto up_one; |
| 567 | } else if (is_dir_sep(src[2])) { |
| 568 | /* (4) */ |
| 569 | src += 3; |
| 570 | while (is_dir_sep(*src)) |
| 571 | src++; |
| 572 | goto up_one; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* copy up to the next '/', and eat all '/' */ |
| 578 | while ((c = *src++) != '\0' && !is_dir_sep(c)) |
| 579 | *dst++ = c; |
| 580 | if (is_dir_sep(c)) { |
| 581 | *dst++ = '/'; |
| 582 | while (is_dir_sep(c)) |
| 583 | c = *src++; |
| 584 | src--; |
| 585 | } else if (!c) |
| 586 | break; |
| 587 | continue; |
| 588 | |
| 589 | up_one: |
| 590 | /* |
| 591 | * dst0..dst is prefix portion, and dst[-1] is '/'; |
| 592 | * go up one level. |
| 593 | */ |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 594 | dst--; /* go to trailing '/' */ |
| 595 | if (dst <= dst0) |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 596 | return -1; |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 597 | /* Windows: dst[-1] cannot be backslash anymore */ |
| 598 | while (dst0 < dst && dst[-1] != '/') |
| 599 | dst--; |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 600 | } |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 601 | *dst = '\0'; |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 602 | return 0; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 603 | } |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * path = Canonical absolute path |
| 607 | * prefix_list = Colon-separated list of absolute paths |
| 608 | * |
Nguyễn Thái Ngọc Duy | 2860b57 | 2008-08-10 22:26:23 +0700 | [diff] [blame] | 609 | * Determines, for each path in prefix_list, whether the "prefix" really |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 610 | * is an ancestor directory of path. Returns the length of the longest |
| 611 | * ancestor directory, excluding any trailing slashes, or -1 if no prefix |
| 612 | * is an ancestor. (Note that this means 0 is returned if prefix_list is |
| 613 | * "/".) "/foo" is not considered an ancestor of "/foobar". Directories |
| 614 | * are not considered to be their own ancestors. path must be in a |
| 615 | * canonical form: empty components, or "." or ".." components are not |
| 616 | * allowed. prefix_list may be null, which is like "". |
| 617 | */ |
| 618 | int longest_ancestor_length(const char *path, const char *prefix_list) |
| 619 | { |
| 620 | char buf[PATH_MAX+1]; |
| 621 | const char *ceil, *colon; |
| 622 | int len, max_len = -1; |
| 623 | |
| 624 | if (prefix_list == NULL || !strcmp(path, "/")) |
| 625 | return -1; |
| 626 | |
| 627 | for (colon = ceil = prefix_list; *colon; ceil = colon+1) { |
René Scharfe | 43a7ddb | 2009-02-07 16:08:29 +0100 | [diff] [blame] | 628 | for (colon = ceil; *colon && *colon != PATH_SEP; colon++); |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 629 | len = colon - ceil; |
| 630 | if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil)) |
| 631 | continue; |
| 632 | strlcpy(buf, ceil, len+1); |
René Scharfe | 43a7ddb | 2009-02-07 16:08:29 +0100 | [diff] [blame] | 633 | if (normalize_path_copy(buf, buf) < 0) |
| 634 | continue; |
| 635 | len = strlen(buf); |
| 636 | if (len > 0 && buf[len-1] == '/') |
| 637 | buf[--len] = '\0'; |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 638 | |
| 639 | if (!strncmp(path, buf, len) && |
| 640 | path[len] == '/' && |
| 641 | len > max_len) { |
| 642 | max_len = len; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | return max_len; |
| 647 | } |
Johannes Schindelin | 4fcc86b | 2009-02-19 20:10:49 +0100 | [diff] [blame] | 648 | |
| 649 | /* strip arbitrary amount of directory separators at end of path */ |
| 650 | static inline int chomp_trailing_dir_sep(const char *path, int len) |
| 651 | { |
| 652 | while (len && is_dir_sep(path[len - 1])) |
| 653 | len--; |
| 654 | return len; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * If path ends with suffix (complete path components), returns the |
| 659 | * part before suffix (sans trailing directory separators). |
| 660 | * Otherwise returns NULL. |
| 661 | */ |
| 662 | char *strip_path_suffix(const char *path, const char *suffix) |
| 663 | { |
| 664 | int path_len = strlen(path), suffix_len = strlen(suffix); |
| 665 | |
| 666 | while (suffix_len) { |
| 667 | if (!path_len) |
| 668 | return NULL; |
| 669 | |
| 670 | if (is_dir_sep(path[path_len - 1])) { |
| 671 | if (!is_dir_sep(suffix[suffix_len - 1])) |
| 672 | return NULL; |
| 673 | path_len = chomp_trailing_dir_sep(path, path_len); |
| 674 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); |
| 675 | } |
| 676 | else if (path[--path_len] != suffix[--suffix_len]) |
| 677 | return NULL; |
| 678 | } |
| 679 | |
| 680 | if (path_len && !is_dir_sep(path[path_len - 1])) |
| 681 | return NULL; |
| 682 | return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); |
| 683 | } |
Shawn O. Pearce | 34b6cb8 | 2009-11-09 11:26:43 -0800 | [diff] [blame] | 684 | |
| 685 | int daemon_avoid_alias(const char *p) |
| 686 | { |
| 687 | int sl, ndot; |
| 688 | |
| 689 | /* |
| 690 | * This resurrects the belts and suspenders paranoia check by HPA |
| 691 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() |
Junio C Hamano | 9517e6b | 2010-02-03 21:23:18 -0800 | [diff] [blame] | 692 | * does not do getcwd() based path canonicalization. |
Shawn O. Pearce | 34b6cb8 | 2009-11-09 11:26:43 -0800 | [diff] [blame] | 693 | * |
| 694 | * sl becomes true immediately after seeing '/' and continues to |
| 695 | * be true as long as dots continue after that without intervening |
| 696 | * non-dot character. |
| 697 | */ |
| 698 | if (!p || (*p != '/' && *p != '~')) |
| 699 | return -1; |
| 700 | sl = 1; ndot = 0; |
| 701 | p++; |
| 702 | |
| 703 | while (1) { |
| 704 | char ch = *p++; |
| 705 | if (sl) { |
| 706 | if (ch == '.') |
| 707 | ndot++; |
| 708 | else if (ch == '/') { |
| 709 | if (ndot < 3) |
| 710 | /* reject //, /./ and /../ */ |
| 711 | return -1; |
| 712 | ndot = 0; |
| 713 | } |
| 714 | else if (ch == 0) { |
| 715 | if (0 < ndot && ndot < 3) |
| 716 | /* reject /.$ and /..$ */ |
| 717 | return -1; |
| 718 | return 0; |
| 719 | } |
| 720 | else |
| 721 | sl = ndot = 0; |
| 722 | } |
| 723 | else if (ch == 0) |
| 724 | return 0; |
| 725 | else if (ch == '/') { |
| 726 | sl = 1; |
| 727 | ndot = 0; |
| 728 | } |
| 729 | } |
| 730 | } |
Nguyễn Thái Ngọc Duy | 4bb43de | 2010-02-16 12:22:08 +0700 | [diff] [blame] | 731 | |
| 732 | int offset_1st_component(const char *path) |
| 733 | { |
| 734 | if (has_dos_drive_prefix(path)) |
| 735 | return 2 + is_dir_sep(path[2]); |
| 736 | return is_dir_sep(path[0]); |
| 737 | } |