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" |
| 14 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 15 | static char bad_path[] = "/bad-path/"; |
| 16 | |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 17 | static char *get_pathname(void) |
| 18 | { |
| 19 | static char pathname_array[4][PATH_MAX]; |
| 20 | static int index; |
| 21 | return pathname_array[3 & ++index]; |
| 22 | } |
| 23 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 24 | static char *cleanup_path(char *path) |
| 25 | { |
| 26 | /* Clean it up */ |
| 27 | if (!memcmp(path, "./", 2)) { |
| 28 | path += 2; |
| 29 | while (*path == '/') |
| 30 | path++; |
| 31 | } |
| 32 | return path; |
| 33 | } |
| 34 | |
Alex Riesen | 108bebe | 2008-10-26 22:59:13 +0100 | [diff] [blame] | 35 | char *mksnpath(char *buf, size_t n, const char *fmt, ...) |
| 36 | { |
| 37 | va_list args; |
| 38 | unsigned len; |
| 39 | |
| 40 | va_start(args, fmt); |
| 41 | len = vsnprintf(buf, n, fmt, args); |
| 42 | va_end(args); |
| 43 | if (len >= n) { |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 44 | strlcpy(buf, bad_path, n); |
Alex Riesen | 108bebe | 2008-10-26 22:59:13 +0100 | [diff] [blame] | 45 | return buf; |
| 46 | } |
| 47 | return cleanup_path(buf); |
| 48 | } |
| 49 | |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 50 | 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] | 51 | { |
| 52 | const char *git_dir = get_git_dir(); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 53 | size_t len; |
| 54 | |
| 55 | len = strlen(git_dir); |
| 56 | if (n < len + 1) |
| 57 | goto bad; |
| 58 | memcpy(buf, git_dir, len); |
| 59 | if (len && !is_dir_sep(git_dir[len-1])) |
| 60 | buf[len++] = '/'; |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 61 | len += vsnprintf(buf + len, n - len, fmt, args); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 62 | if (len >= n) |
| 63 | goto bad; |
| 64 | return cleanup_path(buf); |
| 65 | bad: |
Daniel Lowe | 9db56f7 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 66 | strlcpy(buf, bad_path, n); |
Alex Riesen | fe2d777 | 2008-10-27 10:22:21 +0100 | [diff] [blame] | 67 | return buf; |
| 68 | } |
| 69 | |
Alex Riesen | aba13e7 | 2008-10-27 11:17:51 +0100 | [diff] [blame] | 70 | char *git_snpath(char *buf, size_t n, const char *fmt, ...) |
| 71 | { |
| 72 | va_list args; |
| 73 | va_start(args, fmt); |
| 74 | (void)git_vsnpath(buf, n, fmt, args); |
| 75 | va_end(args); |
| 76 | return buf; |
| 77 | } |
| 78 | |
| 79 | char *git_pathdup(const char *fmt, ...) |
| 80 | { |
| 81 | char path[PATH_MAX]; |
| 82 | va_list args; |
| 83 | va_start(args, fmt); |
| 84 | (void)git_vsnpath(path, sizeof(path), fmt, args); |
| 85 | va_end(args); |
| 86 | return xstrdup(path); |
| 87 | } |
| 88 | |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 89 | char *mkpath(const char *fmt, ...) |
| 90 | { |
| 91 | va_list args; |
| 92 | unsigned len; |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 93 | char *pathname = get_pathname(); |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 94 | |
| 95 | va_start(args, fmt); |
| 96 | len = vsnprintf(pathname, PATH_MAX, fmt, args); |
| 97 | va_end(args); |
| 98 | if (len >= PATH_MAX) |
| 99 | return bad_path; |
| 100 | return cleanup_path(pathname); |
| 101 | } |
| 102 | |
| 103 | char *git_path(const char *fmt, ...) |
| 104 | { |
Sven Verdoolaege | 5da1606 | 2005-09-26 22:54:01 +0200 | [diff] [blame] | 105 | const char *git_dir = get_git_dir(); |
Linus Torvalds | e7676d2 | 2006-09-11 12:03:15 -0700 | [diff] [blame] | 106 | char *pathname = get_pathname(); |
Linus Torvalds | 26c8a53 | 2005-07-08 16:20:59 -0700 | [diff] [blame] | 107 | va_list args; |
| 108 | unsigned len; |
| 109 | |
| 110 | len = strlen(git_dir); |
| 111 | if (len > PATH_MAX-100) |
| 112 | return bad_path; |
| 113 | memcpy(pathname, git_dir, len); |
| 114 | if (len && git_dir[len-1] != '/') |
| 115 | pathname[len++] = '/'; |
| 116 | va_start(args, fmt); |
| 117 | len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args); |
| 118 | va_end(args); |
| 119 | if (len >= PATH_MAX) |
| 120 | return bad_path; |
| 121 | return cleanup_path(pathname); |
| 122 | } |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 123 | |
| 124 | |
| 125 | /* git_mkstemp() - create tmp file honoring TMPDIR variable */ |
| 126 | int git_mkstemp(char *path, size_t len, const char *template) |
| 127 | { |
Junio C Hamano | e7a7be8 | 2007-07-25 21:34:53 -0700 | [diff] [blame] | 128 | const char *tmp; |
| 129 | size_t n; |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 130 | |
Junio C Hamano | e7a7be8 | 2007-07-25 21:34:53 -0700 | [diff] [blame] | 131 | tmp = getenv("TMPDIR"); |
| 132 | if (!tmp) |
| 133 | tmp = "/tmp"; |
| 134 | n = snprintf(path, len, "%s/%s", tmp, template); |
| 135 | if (len <= n) { |
| 136 | errno = ENAMETOOLONG; |
| 137 | return -1; |
Holger Eitzenberger | 35c3c62 | 2005-08-08 22:33:08 +0200 | [diff] [blame] | 138 | } |
Holger Eitzenberger | f2db68e | 2005-08-04 22:43:03 +0200 | [diff] [blame] | 139 | return mkstemp(path); |
| 140 | } |
| 141 | |
| 142 | |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 143 | int validate_headref(const char *path) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 144 | { |
| 145 | struct stat st; |
| 146 | char *buf, buffer[256]; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 147 | unsigned char sha1[20]; |
Heikki Orsila | 0104ca0 | 2008-04-27 21:21:58 +0300 | [diff] [blame] | 148 | int fd; |
| 149 | ssize_t len; |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 150 | |
| 151 | if (lstat(path, &st) < 0) |
| 152 | return -1; |
| 153 | |
| 154 | /* Make sure it is a "refs/.." symlink */ |
| 155 | if (S_ISLNK(st.st_mode)) { |
| 156 | len = readlink(path, buffer, sizeof(buffer)-1); |
Junio C Hamano | 222b167 | 2009-02-12 13:02:09 -0800 | [diff] [blame] | 157 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 158 | return 0; |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Anything else, just open it and try to see if it is a symbolic ref. |
| 164 | */ |
| 165 | fd = open(path, O_RDONLY); |
| 166 | if (fd < 0) |
| 167 | return -1; |
Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 +0000 | [diff] [blame] | 168 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 169 | close(fd); |
| 170 | |
| 171 | /* |
| 172 | * Is it a symbolic ref? |
| 173 | */ |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 174 | if (len < 4) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 175 | return -1; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 176 | if (!memcmp("ref:", buffer, 4)) { |
| 177 | buf = buffer + 4; |
| 178 | len -= 4; |
| 179 | while (len && isspace(*buf)) |
| 180 | buf++, len--; |
Junio C Hamano | 222b167 | 2009-02-12 13:02:09 -0800 | [diff] [blame] | 181 | if (len >= 5 && !memcmp("refs/", buf, 5)) |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Is this a detached HEAD? |
| 187 | */ |
| 188 | if (!get_sha1_hex(buffer, sha1)) |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 189 | return 0; |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 190 | |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 194 | static char *user_path(char *buf, char *path, int sz) |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 195 | { |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 196 | struct passwd *pw; |
| 197 | char *slash; |
| 198 | int len, baselen; |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 199 | |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 200 | if (!path || path[0] != '~') |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 201 | return NULL; |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 202 | path++; |
| 203 | slash = strchr(path, '/'); |
| 204 | if (path[0] == '/' || !path[0]) { |
| 205 | pw = getpwuid(getuid()); |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 206 | } |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 207 | else { |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 208 | if (slash) { |
| 209 | *slash = 0; |
| 210 | pw = getpwnam(path); |
| 211 | *slash = '/'; |
| 212 | } |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 213 | else |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 214 | pw = getpwnam(path); |
Junio C Hamano | 0870ca7 | 2005-11-18 14:59:34 -0800 | [diff] [blame] | 215 | } |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 216 | if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir)) |
| 217 | return NULL; |
| 218 | baselen = strlen(pw->pw_dir); |
| 219 | memcpy(buf, pw->pw_dir, baselen); |
| 220 | while ((1 < baselen) && (buf[baselen-1] == '/')) { |
| 221 | buf[baselen-1] = 0; |
| 222 | baselen--; |
| 223 | } |
| 224 | if (slash && slash[1]) { |
| 225 | len = strlen(slash); |
| 226 | if (sz <= baselen + len) |
| 227 | return NULL; |
| 228 | memcpy(buf + baselen, slash, len + 1); |
| 229 | } |
| 230 | return buf; |
| 231 | } |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 232 | |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 233 | /* |
| 234 | * First, one directory to try is determined by the following algorithm. |
| 235 | * |
| 236 | * (0) If "strict" is given, the path is used as given and no DWIM is |
| 237 | * done. Otherwise: |
| 238 | * (1) "~/path" to mean path under the running user's home directory; |
| 239 | * (2) "~user/path" to mean path under named user's home directory; |
| 240 | * (3) "relative/path" to mean cwd relative directory; or |
| 241 | * (4) "/absolute/path" to mean absolute directory. |
| 242 | * |
| 243 | * Unless "strict" is given, we try access() for existence of "%s.git/.git", |
| 244 | * "%s/.git", "%s.git", "%s" in this order. The first one that exists is |
| 245 | * what we try. |
| 246 | * |
| 247 | * Second, we try chdir() to that. Upon failure, we return NULL. |
| 248 | * |
| 249 | * Then, we try if the current directory is a valid git repository. |
| 250 | * Upon failure, we return NULL. |
| 251 | * |
| 252 | * If all goes well, we return the directory we used to chdir() (but |
| 253 | * before ~user is expanded), avoiding getcwd() resolving symbolic |
| 254 | * links. User relative paths are also returned as they are given, |
| 255 | * except DWIM suffixing. |
| 256 | */ |
| 257 | char *enter_repo(char *path, int strict) |
| 258 | { |
| 259 | static char used_path[PATH_MAX]; |
| 260 | static char validated_path[PATH_MAX]; |
| 261 | |
| 262 | if (!path) |
| 263 | return NULL; |
| 264 | |
| 265 | if (!strict) { |
| 266 | static const char *suffix[] = { |
| 267 | ".git/.git", "/.git", ".git", "", NULL, |
| 268 | }; |
| 269 | int len = strlen(path); |
| 270 | int i; |
| 271 | while ((1 < len) && (path[len-1] == '/')) { |
| 272 | path[len-1] = 0; |
| 273 | len--; |
| 274 | } |
| 275 | if (PATH_MAX <= len) |
| 276 | return NULL; |
| 277 | if (path[0] == '~') { |
| 278 | if (!user_path(used_path, path, PATH_MAX)) |
| 279 | return NULL; |
| 280 | strcpy(validated_path, path); |
| 281 | path = used_path; |
| 282 | } |
| 283 | else if (PATH_MAX - 10 < len) |
| 284 | return NULL; |
| 285 | else { |
| 286 | path = strcpy(used_path, path); |
| 287 | strcpy(validated_path, path); |
| 288 | } |
| 289 | len = strlen(path); |
| 290 | for (i = 0; suffix[i]; i++) { |
| 291 | strcpy(path + len, suffix[i]); |
| 292 | if (!access(path, F_OK)) { |
| 293 | strcat(validated_path, suffix[i]); |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | if (!suffix[i] || chdir(path)) |
| 298 | return NULL; |
| 299 | path = validated_path; |
| 300 | } |
| 301 | else if (chdir(path)) |
| 302 | return NULL; |
| 303 | |
| 304 | if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && |
Junio C Hamano | c847f53 | 2007-01-01 23:31:08 -0800 | [diff] [blame] | 305 | validate_headref("HEAD") == 0) { |
Matthias Lederhofer | 7627943 | 2007-06-28 16:15:25 +0200 | [diff] [blame] | 306 | setenv(GIT_DIR_ENVIRONMENT, ".", 1); |
Junio C Hamano | 1644162 | 2005-11-25 10:48:26 -0800 | [diff] [blame] | 307 | check_repository_format(); |
Junio C Hamano | d79374c | 2005-12-03 01:45:57 -0800 | [diff] [blame] | 308 | return path; |
Andreas Ericsson | 54f4b87 | 2005-11-17 20:37:14 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return NULL; |
| 312 | } |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 313 | |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame^] | 314 | int set_shared_perm(const char *path, int mode) |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 315 | { |
| 316 | struct stat st; |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame^] | 317 | int tweak, shared, orig_mode; |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 318 | |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame^] | 319 | if (!shared_repository) { |
| 320 | if (mode) |
| 321 | return chmod(path, mode & ~S_IFMT); |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 322 | return 0; |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame^] | 323 | } |
| 324 | if (!mode) { |
| 325 | if (lstat(path, &st) < 0) |
| 326 | return -1; |
| 327 | mode = st.st_mode; |
| 328 | orig_mode = mode; |
| 329 | } else |
| 330 | orig_mode = 0; |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 331 | if (shared_repository < 0) |
| 332 | shared = -shared_repository; |
| 333 | else |
| 334 | shared = shared_repository; |
| 335 | tweak = shared; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 336 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 337 | if (!(mode & S_IWUSR)) |
| 338 | tweak &= ~0222; |
| 339 | if (mode & S_IXUSR) |
| 340 | /* Copy read bits to execute bits */ |
| 341 | tweak |= (tweak & 0444) >> 2; |
| 342 | if (shared_repository < 0) |
| 343 | mode = (mode & ~0777) | tweak; |
| 344 | else |
Petr Baudis | 8c6202d | 2008-07-12 03:15:03 +0200 | [diff] [blame] | 345 | mode |= tweak; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 346 | |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 347 | if (S_ISDIR(mode)) { |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 348 | /* Copy read bits to execute bits */ |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 349 | mode |= (shared & 0444) >> 2; |
| 350 | mode |= FORCE_DIR_SET_GID; |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 351 | } |
| 352 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 353 | if (((shared_repository < 0 |
Junio C Hamano | 17e61b8 | 2009-03-27 23:21:00 -0700 | [diff] [blame^] | 354 | ? (orig_mode & (FORCE_DIR_SET_GID | 0777)) |
| 355 | : (orig_mode & mode)) != mode) && |
| 356 | chmod(path, (mode & ~S_IFMT)) < 0) |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 357 | return -2; |
| 358 | return 0; |
| 359 | } |
Johannes Schindelin | e5392c5 | 2007-08-01 01:28:59 +0100 | [diff] [blame] | 360 | |
Linus Torvalds | 044bbbc | 2008-06-19 12:34:06 -0700 | [diff] [blame] | 361 | const char *make_relative_path(const char *abs, const char *base) |
| 362 | { |
| 363 | static char buf[PATH_MAX + 1]; |
| 364 | int baselen; |
| 365 | if (!base) |
| 366 | return abs; |
| 367 | baselen = strlen(base); |
| 368 | if (prefixcmp(abs, base)) |
| 369 | return abs; |
| 370 | if (abs[baselen] == '/') |
| 371 | baselen++; |
| 372 | else if (base[baselen - 1] != '/') |
| 373 | return abs; |
| 374 | strcpy(buf, abs + baselen); |
| 375 | return buf; |
| 376 | } |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 377 | |
| 378 | /* |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 379 | * It is okay if dst == src, but they should not overlap otherwise. |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 380 | * |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 381 | * Performs the following normalizations on src, storing the result in dst: |
| 382 | * - Ensures that components are separated by '/' (Windows only) |
| 383 | * - Squashes sequences of '/'. |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 384 | * - Removes "." components. |
| 385 | * - Removes ".." components, and the components the precede them. |
Johannes Sixt | f2a782b | 2009-02-07 16:08:31 +0100 | [diff] [blame] | 386 | * Returns failure (non-zero) if a ".." component appears as first path |
| 387 | * component anytime during the normalization. Otherwise, returns success (0). |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 388 | * |
| 389 | * Note that this function is purely textual. It does not follow symlinks, |
| 390 | * verify the existence of the path, or make any system calls. |
| 391 | */ |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 392 | int normalize_path_copy(char *dst, const char *src) |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 393 | { |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 394 | char *dst0; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 395 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 396 | if (has_dos_drive_prefix(src)) { |
| 397 | *dst++ = *src++; |
| 398 | *dst++ = *src++; |
| 399 | } |
| 400 | dst0 = dst; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 401 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 402 | if (is_dir_sep(*src)) { |
| 403 | *dst++ = '/'; |
| 404 | while (is_dir_sep(*src)) |
| 405 | src++; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 408 | for (;;) { |
| 409 | char c = *src; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 410 | |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 411 | /* |
| 412 | * A path component that begins with . could be |
| 413 | * special: |
| 414 | * (1) "." and ends -- ignore and terminate. |
| 415 | * (2) "./" -- ignore them, eat slash and continue. |
| 416 | * (3) ".." and ends -- strip one and terminate. |
| 417 | * (4) "../" -- strip one, eat slash and continue. |
| 418 | */ |
| 419 | if (c == '.') { |
| 420 | if (!src[1]) { |
| 421 | /* (1) */ |
| 422 | src++; |
| 423 | } else if (is_dir_sep(src[1])) { |
| 424 | /* (2) */ |
| 425 | src += 2; |
| 426 | while (is_dir_sep(*src)) |
| 427 | src++; |
| 428 | continue; |
| 429 | } else if (src[1] == '.') { |
| 430 | if (!src[2]) { |
| 431 | /* (3) */ |
| 432 | src += 2; |
| 433 | goto up_one; |
| 434 | } else if (is_dir_sep(src[2])) { |
| 435 | /* (4) */ |
| 436 | src += 3; |
| 437 | while (is_dir_sep(*src)) |
| 438 | src++; |
| 439 | goto up_one; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* copy up to the next '/', and eat all '/' */ |
| 445 | while ((c = *src++) != '\0' && !is_dir_sep(c)) |
| 446 | *dst++ = c; |
| 447 | if (is_dir_sep(c)) { |
| 448 | *dst++ = '/'; |
| 449 | while (is_dir_sep(c)) |
| 450 | c = *src++; |
| 451 | src--; |
| 452 | } else if (!c) |
| 453 | break; |
| 454 | continue; |
| 455 | |
| 456 | up_one: |
| 457 | /* |
| 458 | * dst0..dst is prefix portion, and dst[-1] is '/'; |
| 459 | * go up one level. |
| 460 | */ |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 461 | dst--; /* go to trailing '/' */ |
| 462 | if (dst <= dst0) |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 463 | return -1; |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 464 | /* Windows: dst[-1] cannot be backslash anymore */ |
| 465 | while (dst0 < dst && dst[-1] != '/') |
| 466 | dst--; |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 467 | } |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 468 | *dst = '\0'; |
Johannes Sixt | f3cad0a | 2009-02-07 16:08:28 +0100 | [diff] [blame] | 469 | return 0; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 470 | } |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 471 | |
| 472 | /* |
| 473 | * path = Canonical absolute path |
| 474 | * prefix_list = Colon-separated list of absolute paths |
| 475 | * |
Nguyễn Thái Ngọc Duy | 2860b57 | 2008-08-10 22:26:23 +0700 | [diff] [blame] | 476 | * Determines, for each path in prefix_list, whether the "prefix" really |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 477 | * is an ancestor directory of path. Returns the length of the longest |
| 478 | * ancestor directory, excluding any trailing slashes, or -1 if no prefix |
| 479 | * is an ancestor. (Note that this means 0 is returned if prefix_list is |
| 480 | * "/".) "/foo" is not considered an ancestor of "/foobar". Directories |
| 481 | * are not considered to be their own ancestors. path must be in a |
| 482 | * canonical form: empty components, or "." or ".." components are not |
| 483 | * allowed. prefix_list may be null, which is like "". |
| 484 | */ |
| 485 | int longest_ancestor_length(const char *path, const char *prefix_list) |
| 486 | { |
| 487 | char buf[PATH_MAX+1]; |
| 488 | const char *ceil, *colon; |
| 489 | int len, max_len = -1; |
| 490 | |
| 491 | if (prefix_list == NULL || !strcmp(path, "/")) |
| 492 | return -1; |
| 493 | |
| 494 | for (colon = ceil = prefix_list; *colon; ceil = colon+1) { |
René Scharfe | 43a7ddb | 2009-02-07 16:08:29 +0100 | [diff] [blame] | 495 | for (colon = ceil; *colon && *colon != PATH_SEP; colon++); |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 496 | len = colon - ceil; |
| 497 | if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil)) |
| 498 | continue; |
| 499 | strlcpy(buf, ceil, len+1); |
René Scharfe | 43a7ddb | 2009-02-07 16:08:29 +0100 | [diff] [blame] | 500 | if (normalize_path_copy(buf, buf) < 0) |
| 501 | continue; |
| 502 | len = strlen(buf); |
| 503 | if (len > 0 && buf[len-1] == '/') |
| 504 | buf[--len] = '\0'; |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 505 | |
| 506 | if (!strncmp(path, buf, len) && |
| 507 | path[len] == '/' && |
| 508 | len > max_len) { |
| 509 | max_len = len; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return max_len; |
| 514 | } |
Johannes Schindelin | 4fcc86b | 2009-02-19 20:10:49 +0100 | [diff] [blame] | 515 | |
| 516 | /* strip arbitrary amount of directory separators at end of path */ |
| 517 | static inline int chomp_trailing_dir_sep(const char *path, int len) |
| 518 | { |
| 519 | while (len && is_dir_sep(path[len - 1])) |
| 520 | len--; |
| 521 | return len; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * If path ends with suffix (complete path components), returns the |
| 526 | * part before suffix (sans trailing directory separators). |
| 527 | * Otherwise returns NULL. |
| 528 | */ |
| 529 | char *strip_path_suffix(const char *path, const char *suffix) |
| 530 | { |
| 531 | int path_len = strlen(path), suffix_len = strlen(suffix); |
| 532 | |
| 533 | while (suffix_len) { |
| 534 | if (!path_len) |
| 535 | return NULL; |
| 536 | |
| 537 | if (is_dir_sep(path[path_len - 1])) { |
| 538 | if (!is_dir_sep(suffix[suffix_len - 1])) |
| 539 | return NULL; |
| 540 | path_len = chomp_trailing_dir_sep(path, path_len); |
| 541 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); |
| 542 | } |
| 543 | else if (path[--path_len] != suffix[--suffix_len]) |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | if (path_len && !is_dir_sep(path[path_len - 1])) |
| 548 | return NULL; |
| 549 | return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); |
| 550 | } |