2 * I'm tired of doing "vsnprintf()" etc just to open a
3 * file, so here's a "return static buffer with printf"
6 * It's obviously not thread-safe. Sue me. But it's quite
7 * useful for doing things like
9 * f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
11 * which is what it's designed for.
15 static char bad_path
[] = "/bad-path/";
17 static char *get_pathname(void)
19 static char pathname_array
[4][PATH_MAX
];
21 return pathname_array
[3 & ++index
];
24 static char *cleanup_path(char *path
)
27 if (!memcmp(path
, "./", 2)) {
35 char *mksnpath(char *buf
, size_t n
, const char *fmt
, ...)
41 len
= vsnprintf(buf
, n
, fmt
, args
);
44 strlcpy(buf
, bad_path
, n
);
47 return cleanup_path(buf
);
50 static char *git_vsnpath(char *buf
, size_t n
, const char *fmt
, va_list args
)
52 const char *git_dir
= get_git_dir();
55 len
= strlen(git_dir
);
58 memcpy(buf
, git_dir
, len
);
59 if (len
&& !is_dir_sep(git_dir
[len
-1]))
61 len
+= vsnprintf(buf
+ len
, n
- len
, fmt
, args
);
64 return cleanup_path(buf
);
66 strlcpy(buf
, bad_path
, n
);
70 char *git_snpath(char *buf
, size_t n
, const char *fmt
, ...)
74 (void)git_vsnpath(buf
, n
, fmt
, args
);
79 char *git_pathdup(const char *fmt
, ...)
84 (void)git_vsnpath(path
, sizeof(path
), fmt
, args
);
89 char *mkpath(const char *fmt
, ...)
93 char *pathname
= get_pathname();
96 len
= vsnprintf(pathname
, PATH_MAX
, fmt
, args
);
100 return cleanup_path(pathname
);
103 char *git_path(const char *fmt
, ...)
105 const char *git_dir
= get_git_dir();
106 char *pathname
= get_pathname();
110 len
= strlen(git_dir
);
111 if (len
> PATH_MAX
-100)
113 memcpy(pathname
, git_dir
, len
);
114 if (len
&& git_dir
[len
-1] != '/')
115 pathname
[len
++] = '/';
117 len
+= vsnprintf(pathname
+ len
, PATH_MAX
- len
, fmt
, args
);
121 return cleanup_path(pathname
);
125 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
126 int git_mkstemp(char *path
, size_t len
, const char *template)
131 tmp
= getenv("TMPDIR");
134 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
136 errno
= ENAMETOOLONG
;
139 return mkstemp(path
);
143 int validate_headref(const char *path
)
146 char *buf
, buffer
[256];
147 unsigned char sha1
[20];
151 if (lstat(path
, &st
) < 0)
154 /* Make sure it is a "refs/.." symlink */
155 if (S_ISLNK(st
.st_mode
)) {
156 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
157 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
163 * Anything else, just open it and try to see if it is a symbolic ref.
165 fd
= open(path
, O_RDONLY
);
168 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
172 * Is it a symbolic ref?
176 if (!memcmp("ref:", buffer
, 4)) {
179 while (len
&& isspace(*buf
))
181 if (len
>= 5 && !memcmp("refs/", buf
, 5))
186 * Is this a detached HEAD?
188 if (!get_sha1_hex(buffer
, sha1
))
194 static char *user_path(char *buf
, char *path
, int sz
)
200 if (!path
|| path
[0] != '~')
203 slash
= strchr(path
, '/');
204 if (path
[0] == '/' || !path
[0]) {
205 pw
= getpwuid(getuid());
216 if (!pw
|| !pw
->pw_dir
|| sz
<= strlen(pw
->pw_dir
))
218 baselen
= strlen(pw
->pw_dir
);
219 memcpy(buf
, pw
->pw_dir
, baselen
);
220 while ((1 < baselen
) && (buf
[baselen
-1] == '/')) {
224 if (slash
&& slash
[1]) {
226 if (sz
<= baselen
+ len
)
228 memcpy(buf
+ baselen
, slash
, len
+ 1);
234 * First, one directory to try is determined by the following algorithm.
236 * (0) If "strict" is given, the path is used as given and no DWIM is
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.
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
247 * Second, we try chdir() to that. Upon failure, we return NULL.
249 * Then, we try if the current directory is a valid git repository.
250 * Upon failure, we return NULL.
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.
257 char *enter_repo(char *path
, int strict
)
259 static char used_path
[PATH_MAX
];
260 static char validated_path
[PATH_MAX
];
266 static const char *suffix
[] = {
267 ".git/.git", "/.git", ".git", "", NULL
,
269 int len
= strlen(path
);
271 while ((1 < len
) && (path
[len
-1] == '/')) {
277 if (path
[0] == '~') {
278 if (!user_path(used_path
, path
, PATH_MAX
))
280 strcpy(validated_path
, path
);
283 else if (PATH_MAX
- 10 < len
)
286 path
= strcpy(used_path
, path
);
287 strcpy(validated_path
, 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
]);
297 if (!suffix
[i
] || chdir(path
))
299 path
= validated_path
;
301 else if (chdir(path
))
304 if (access("objects", X_OK
) == 0 && access("refs", X_OK
) == 0 &&
305 validate_headref("HEAD") == 0) {
306 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
307 check_repository_format();
314 int adjust_shared_perm(const char *path
)
319 if (!shared_repository
)
321 if (lstat(path
, &st
) < 0)
325 if (shared_repository
) {
326 int tweak
= shared_repository
;
327 if (!(mode
& S_IWUSR
))
331 /* Preserve old PERM_UMASK behaviour */
337 mode
|= FORCE_DIR_SET_GID
;
339 /* Copy read bits to execute bits */
340 mode
|= (shared_repository
& 0444) >> 2;
343 if ((mode
& st
.st_mode
) != mode
&& chmod(path
, mode
) < 0)
348 const char *make_relative_path(const char *abs
, const char *base
)
350 static char buf
[PATH_MAX
+ 1];
354 baselen
= strlen(base
);
355 if (prefixcmp(abs
, base
))
357 if (abs
[baselen
] == '/')
359 else if (base
[baselen
- 1] != '/')
361 strcpy(buf
, abs
+ baselen
);
366 * path = absolute path
367 * buf = buffer of at least max(2, strlen(path)+1) bytes
368 * It is okay if buf == path, but they should not overlap otherwise.
370 * Performs the following normalizations on path, storing the result in buf:
371 * - Removes trailing slashes.
372 * - Removes empty components.
373 * - Removes "." components.
374 * - Removes ".." components, and the components the precede them.
375 * "" and paths that contain only slashes are normalized to "/".
376 * Returns the length of the output.
378 * Note that this function is purely textual. It does not follow symlinks,
379 * verify the existence of the path, or make any system calls.
381 int normalize_absolute_path(char *buf
, const char *path
)
383 const char *comp_start
= path
, *comp_end
= path
;
389 while (*comp_start
) {
390 assert(*comp_start
== '/');
391 while (*++comp_end
&& *comp_end
!= '/')
393 comp_len
= comp_end
- comp_start
;
395 if (!strncmp("/", comp_start
, comp_len
) ||
396 !strncmp("/.", comp_start
, comp_len
))
399 if (!strncmp("/..", comp_start
, comp_len
)) {
400 while (dst
> buf
&& *--dst
!= '/')
405 memmove(dst
, comp_start
, comp_len
);
408 comp_start
= comp_end
;
419 * path = Canonical absolute path
420 * prefix_list = Colon-separated list of absolute paths
422 * Determines, for each path in prefix_list, whether the "prefix" really
423 * is an ancestor directory of path. Returns the length of the longest
424 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
425 * is an ancestor. (Note that this means 0 is returned if prefix_list is
426 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
427 * are not considered to be their own ancestors. path must be in a
428 * canonical form: empty components, or "." or ".." components are not
429 * allowed. prefix_list may be null, which is like "".
431 int longest_ancestor_length(const char *path
, const char *prefix_list
)
433 char buf
[PATH_MAX
+1];
434 const char *ceil
, *colon
;
435 int len
, max_len
= -1;
437 if (prefix_list
== NULL
|| !strcmp(path
, "/"))
440 for (colon
= ceil
= prefix_list
; *colon
; ceil
= colon
+1) {
441 for (colon
= ceil
; *colon
&& *colon
!= ':'; colon
++);
443 if (len
== 0 || len
> PATH_MAX
|| !is_absolute_path(ceil
))
445 strlcpy(buf
, ceil
, len
+1);
446 len
= normalize_absolute_path(buf
, buf
);
447 /* Strip "trailing slashes" from "/". */
451 if (!strncmp(path
, buf
, len
) &&