]> git.ipfire.org Git - thirdparty/git.git/blame - path.c
move_temp_to_file(): do not forget to chmod() in "Coda hack" codepath
[thirdparty/git.git] / path.c
CommitLineData
26c8a533
LT
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
26c8a533
LT
15static char bad_path[] = "/bad-path/";
16
e7676d2f
LT
17static 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
26c8a533
LT
24static 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
108bebea
AR
35char *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) {
9db56f71 44 strlcpy(buf, bad_path, n);
108bebea
AR
45 return buf;
46 }
47 return cleanup_path(buf);
48}
49
aba13e7c 50static char *git_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
fe2d7776
AR
51{
52 const char *git_dir = get_git_dir();
fe2d7776
AR
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++] = '/';
fe2d7776 61 len += vsnprintf(buf + len, n - len, fmt, args);
fe2d7776
AR
62 if (len >= n)
63 goto bad;
64 return cleanup_path(buf);
65bad:
9db56f71 66 strlcpy(buf, bad_path, n);
fe2d7776
AR
67 return buf;
68}
69
aba13e7c
AR
70char *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
79char *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
26c8a533
LT
89char *mkpath(const char *fmt, ...)
90{
91 va_list args;
92 unsigned len;
e7676d2f 93 char *pathname = get_pathname();
26c8a533
LT
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
103char *git_path(const char *fmt, ...)
104{
5da1606d 105 const char *git_dir = get_git_dir();
e7676d2f 106 char *pathname = get_pathname();
26c8a533
LT
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}
f2db68ed
HE
123
124
125/* git_mkstemp() - create tmp file honoring TMPDIR variable */
126int git_mkstemp(char *path, size_t len, const char *template)
127{
e7a7be88
JH
128 const char *tmp;
129 size_t n;
130
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;
35c3c629 138 }
f2db68ed
HE
139 return mkstemp(path);
140}
141
142
c847f537 143int validate_headref(const char *path)
0870ca7f
JH
144{
145 struct stat st;
146 char *buf, buffer[256];
c847f537 147 unsigned char sha1[20];
0104ca09
HO
148 int fd;
149 ssize_t len;
0870ca7f
JH
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);
222b1673 157 if (len >= 5 && !memcmp("refs/", buffer, 5))
0870ca7f
JH
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;
93d26e4c 168 len = read_in_full(fd, buffer, sizeof(buffer)-1);
0870ca7f
JH
169 close(fd);
170
171 /*
172 * Is it a symbolic ref?
173 */
c847f537 174 if (len < 4)
0870ca7f 175 return -1;
c847f537
JH
176 if (!memcmp("ref:", buffer, 4)) {
177 buf = buffer + 4;
178 len -= 4;
179 while (len && isspace(*buf))
180 buf++, len--;
222b1673 181 if (len >= 5 && !memcmp("refs/", buf, 5))
c847f537
JH
182 return 0;
183 }
184
185 /*
186 * Is this a detached HEAD?
187 */
188 if (!get_sha1_hex(buffer, sha1))
0870ca7f 189 return 0;
c847f537 190
0870ca7f
JH
191 return -1;
192}
193
d79374c7 194static char *user_path(char *buf, char *path, int sz)
54f4b874 195{
d79374c7
JH
196 struct passwd *pw;
197 char *slash;
198 int len, baselen;
54f4b874 199
d79374c7
JH
200 if (!path || path[0] != '~')
201 return NULL;
202 path++;
203 slash = strchr(path, '/');
204 if (path[0] == '/' || !path[0]) {
205 pw = getpwuid(getuid());
206 }
207 else {
208 if (slash) {
209 *slash = 0;
210 pw = getpwnam(path);
211 *slash = '/';
54f4b874 212 }
d79374c7
JH
213 else
214 pw = getpwnam(path);
54f4b874 215 }
d79374c7
JH
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;
54f4b874
AE
231}
232
d79374c7
JH
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 */
54f4b874
AE
257char *enter_repo(char *path, int strict)
258{
d79374c7
JH
259 static char used_path[PATH_MAX];
260 static char validated_path[PATH_MAX];
261
262 if (!path)
54f4b874
AE
263 return NULL;
264
d79374c7
JH
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)
54f4b874 276 return NULL;
d79374c7
JH
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))
0870ca7f 298 return NULL;
d79374c7 299 path = validated_path;
0870ca7f 300 }
d79374c7
JH
301 else if (chdir(path))
302 return NULL;
54f4b874 303
d79374c7 304 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
c847f537 305 validate_headref("HEAD") == 0) {
7627943a 306 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
1644162a 307 check_repository_format();
d79374c7 308 return path;
54f4b874
AE
309 }
310
311 return NULL;
312}
138086a7
JH
313
314int adjust_shared_perm(const char *path)
315{
316 struct stat st;
5a688fe4 317 int mode, tweak, shared;
138086a7
JH
318
319 if (!shared_repository)
320 return 0;
321 if (lstat(path, &st) < 0)
322 return -1;
323 mode = st.st_mode;
5a688fe4
JH
324 if (shared_repository < 0)
325 shared = -shared_repository;
326 else
327 shared = shared_repository;
328 tweak = shared;
329
330 if (!(mode & S_IWUSR))
331 tweak &= ~0222;
332 if (mode & S_IXUSR)
333 /* Copy read bits to execute bits */
334 tweak |= (tweak & 0444) >> 2;
335 if (shared_repository < 0)
336 mode = (mode & ~0777) | tweak;
337 else
8c6202d8 338 mode |= tweak;
06cbe855
HO
339
340 if (S_ISDIR(mode)) {
06cbe855 341 /* Copy read bits to execute bits */
5a688fe4
JH
342 mode |= (shared & 0444) >> 2;
343 mode |= FORCE_DIR_SET_GID;
06cbe855
HO
344 }
345
5a688fe4
JH
346 if (((shared_repository < 0
347 ? (st.st_mode & (FORCE_DIR_SET_GID | 0777))
348 : (st.st_mode & mode)) != mode) &&
349 chmod(path, mode) < 0)
138086a7
JH
350 return -2;
351 return 0;
352}
e5392c51 353
044bbbcb
LT
354const char *make_relative_path(const char *abs, const char *base)
355{
356 static char buf[PATH_MAX + 1];
357 int baselen;
358 if (!base)
359 return abs;
360 baselen = strlen(base);
361 if (prefixcmp(abs, base))
362 return abs;
363 if (abs[baselen] == '/')
364 baselen++;
365 else if (base[baselen - 1] != '/')
366 return abs;
367 strcpy(buf, abs + baselen);
368 return buf;
369}
ae299be0
DR
370
371/*
f2a782b8 372 * It is okay if dst == src, but they should not overlap otherwise.
ae299be0 373 *
f2a782b8
JS
374 * Performs the following normalizations on src, storing the result in dst:
375 * - Ensures that components are separated by '/' (Windows only)
376 * - Squashes sequences of '/'.
ae299be0
DR
377 * - Removes "." components.
378 * - Removes ".." components, and the components the precede them.
f2a782b8
JS
379 * Returns failure (non-zero) if a ".." component appears as first path
380 * component anytime during the normalization. Otherwise, returns success (0).
ae299be0
DR
381 *
382 * Note that this function is purely textual. It does not follow symlinks,
383 * verify the existence of the path, or make any system calls.
384 */
f3cad0ad 385int normalize_path_copy(char *dst, const char *src)
ae299be0 386{
f3cad0ad 387 char *dst0;
ae299be0 388
f3cad0ad
JS
389 if (has_dos_drive_prefix(src)) {
390 *dst++ = *src++;
391 *dst++ = *src++;
ae299be0 392 }
f3cad0ad 393 dst0 = dst;
ae299be0 394
f3cad0ad 395 if (is_dir_sep(*src)) {
ae299be0 396 *dst++ = '/';
f3cad0ad
JS
397 while (is_dir_sep(*src))
398 src++;
399 }
400
401 for (;;) {
402 char c = *src;
403
404 /*
405 * A path component that begins with . could be
406 * special:
407 * (1) "." and ends -- ignore and terminate.
408 * (2) "./" -- ignore them, eat slash and continue.
409 * (3) ".." and ends -- strip one and terminate.
410 * (4) "../" -- strip one, eat slash and continue.
411 */
412 if (c == '.') {
413 if (!src[1]) {
414 /* (1) */
415 src++;
416 } else if (is_dir_sep(src[1])) {
417 /* (2) */
418 src += 2;
419 while (is_dir_sep(*src))
420 src++;
421 continue;
422 } else if (src[1] == '.') {
423 if (!src[2]) {
424 /* (3) */
425 src += 2;
426 goto up_one;
427 } else if (is_dir_sep(src[2])) {
428 /* (4) */
429 src += 3;
430 while (is_dir_sep(*src))
431 src++;
432 goto up_one;
433 }
434 }
435 }
ae299be0 436
f3cad0ad
JS
437 /* copy up to the next '/', and eat all '/' */
438 while ((c = *src++) != '\0' && !is_dir_sep(c))
439 *dst++ = c;
440 if (is_dir_sep(c)) {
441 *dst++ = '/';
442 while (is_dir_sep(c))
443 c = *src++;
444 src--;
445 } else if (!c)
446 break;
447 continue;
448
449 up_one:
450 /*
451 * dst0..dst is prefix portion, and dst[-1] is '/';
452 * go up one level.
453 */
f42302b4
JS
454 dst--; /* go to trailing '/' */
455 if (dst <= dst0)
f3cad0ad 456 return -1;
f42302b4
JS
457 /* Windows: dst[-1] cannot be backslash anymore */
458 while (dst0 < dst && dst[-1] != '/')
459 dst--;
f3cad0ad 460 }
ae299be0 461 *dst = '\0';
f3cad0ad 462 return 0;
ae299be0 463}
0454dd93
DR
464
465/*
466 * path = Canonical absolute path
467 * prefix_list = Colon-separated list of absolute paths
468 *
2860b57a 469 * Determines, for each path in prefix_list, whether the "prefix" really
0454dd93
DR
470 * is an ancestor directory of path. Returns the length of the longest
471 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
472 * is an ancestor. (Note that this means 0 is returned if prefix_list is
473 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
474 * are not considered to be their own ancestors. path must be in a
475 * canonical form: empty components, or "." or ".." components are not
476 * allowed. prefix_list may be null, which is like "".
477 */
478int longest_ancestor_length(const char *path, const char *prefix_list)
479{
480 char buf[PATH_MAX+1];
481 const char *ceil, *colon;
482 int len, max_len = -1;
483
484 if (prefix_list == NULL || !strcmp(path, "/"))
485 return -1;
486
487 for (colon = ceil = prefix_list; *colon; ceil = colon+1) {
43a7ddb5 488 for (colon = ceil; *colon && *colon != PATH_SEP; colon++);
0454dd93
DR
489 len = colon - ceil;
490 if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
491 continue;
492 strlcpy(buf, ceil, len+1);
43a7ddb5
RS
493 if (normalize_path_copy(buf, buf) < 0)
494 continue;
495 len = strlen(buf);
496 if (len > 0 && buf[len-1] == '/')
497 buf[--len] = '\0';
0454dd93
DR
498
499 if (!strncmp(path, buf, len) &&
500 path[len] == '/' &&
501 len > max_len) {
502 max_len = len;
503 }
504 }
505
506 return max_len;
507}
4fcc86b0
JS
508
509/* strip arbitrary amount of directory separators at end of path */
510static inline int chomp_trailing_dir_sep(const char *path, int len)
511{
512 while (len && is_dir_sep(path[len - 1]))
513 len--;
514 return len;
515}
516
517/*
518 * If path ends with suffix (complete path components), returns the
519 * part before suffix (sans trailing directory separators).
520 * Otherwise returns NULL.
521 */
522char *strip_path_suffix(const char *path, const char *suffix)
523{
524 int path_len = strlen(path), suffix_len = strlen(suffix);
525
526 while (suffix_len) {
527 if (!path_len)
528 return NULL;
529
530 if (is_dir_sep(path[path_len - 1])) {
531 if (!is_dir_sep(suffix[suffix_len - 1]))
532 return NULL;
533 path_len = chomp_trailing_dir_sep(path, path_len);
534 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
535 }
536 else if (path[--path_len] != suffix[--suffix_len])
537 return NULL;
538 }
539
540 if (path_len && !is_dir_sep(path[path_len - 1]))
541 return NULL;
542 return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
543}