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 *mkpath(const char *fmt
, ...)
39 char *pathname
= get_pathname();
42 len
= vsnprintf(pathname
, PATH_MAX
, fmt
, args
);
46 return cleanup_path(pathname
);
49 char *git_path(const char *fmt
, ...)
51 const char *git_dir
= get_git_dir();
52 char *pathname
= get_pathname();
56 len
= strlen(git_dir
);
57 if (len
> PATH_MAX
-100)
59 memcpy(pathname
, git_dir
, len
);
60 if (len
&& git_dir
[len
-1] != '/')
61 pathname
[len
++] = '/';
63 len
+= vsnprintf(pathname
+ len
, PATH_MAX
- len
, fmt
, args
);
67 return cleanup_path(pathname
);
71 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
72 int git_mkstemp(char *path
, size_t len
, const char *template)
77 tmp
= getenv("TMPDIR");
80 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
89 int validate_headref(const char *path
)
92 char *buf
, buffer
[256];
93 unsigned char sha1
[20];
97 if (lstat(path
, &st
) < 0)
100 /* Make sure it is a "refs/.." symlink */
101 if (S_ISLNK(st
.st_mode
)) {
102 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
103 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
109 * Anything else, just open it and try to see if it is a symbolic ref.
111 fd
= open(path
, O_RDONLY
);
114 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
118 * Is it a symbolic ref?
122 if (!memcmp("ref:", buffer
, 4)) {
125 while (len
&& isspace(*buf
))
127 if (len
>= 5 && !memcmp("refs/", buf
, 5))
132 * Is this a detached HEAD?
134 if (!get_sha1_hex(buffer
, sha1
))
140 static char *user_path(char *buf
, char *path
, int sz
)
146 if (!path
|| path
[0] != '~')
149 slash
= strchr(path
, '/');
150 if (path
[0] == '/' || !path
[0]) {
151 pw
= getpwuid(getuid());
162 if (!pw
|| !pw
->pw_dir
|| sz
<= strlen(pw
->pw_dir
))
164 baselen
= strlen(pw
->pw_dir
);
165 memcpy(buf
, pw
->pw_dir
, baselen
);
166 while ((1 < baselen
) && (buf
[baselen
-1] == '/')) {
170 if (slash
&& slash
[1]) {
172 if (sz
<= baselen
+ len
)
174 memcpy(buf
+ baselen
, slash
, len
+ 1);
180 * First, one directory to try is determined by the following algorithm.
182 * (0) If "strict" is given, the path is used as given and no DWIM is
184 * (1) "~/path" to mean path under the running user's home directory;
185 * (2) "~user/path" to mean path under named user's home directory;
186 * (3) "relative/path" to mean cwd relative directory; or
187 * (4) "/absolute/path" to mean absolute directory.
189 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
190 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
193 * Second, we try chdir() to that. Upon failure, we return NULL.
195 * Then, we try if the current directory is a valid git repository.
196 * Upon failure, we return NULL.
198 * If all goes well, we return the directory we used to chdir() (but
199 * before ~user is expanded), avoiding getcwd() resolving symbolic
200 * links. User relative paths are also returned as they are given,
201 * except DWIM suffixing.
203 char *enter_repo(char *path
, int strict
)
205 static char used_path
[PATH_MAX
];
206 static char validated_path
[PATH_MAX
];
212 static const char *suffix
[] = {
213 ".git/.git", "/.git", ".git", "", NULL
,
215 int len
= strlen(path
);
217 while ((1 < len
) && (path
[len
-1] == '/')) {
223 if (path
[0] == '~') {
224 if (!user_path(used_path
, path
, PATH_MAX
))
226 strcpy(validated_path
, path
);
229 else if (PATH_MAX
- 10 < len
)
232 path
= strcpy(used_path
, path
);
233 strcpy(validated_path
, path
);
236 for (i
= 0; suffix
[i
]; i
++) {
237 strcpy(path
+ len
, suffix
[i
]);
238 if (!access(path
, F_OK
)) {
239 strcat(validated_path
, suffix
[i
]);
243 if (!suffix
[i
] || chdir(path
))
245 path
= validated_path
;
247 else if (chdir(path
))
250 if (access("objects", X_OK
) == 0 && access("refs", X_OK
) == 0 &&
251 validate_headref("HEAD") == 0) {
252 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
253 check_repository_format();
260 int adjust_shared_perm(const char *path
)
265 if (!shared_repository
)
267 if (lstat(path
, &st
) < 0)
271 if (shared_repository
) {
272 int tweak
= shared_repository
;
273 if (!(mode
& S_IWUSR
))
277 /* Preserve old PERM_UMASK behaviour */
283 mode
|= FORCE_DIR_SET_GID
;
285 /* Copy read bits to execute bits */
286 mode
|= (shared_repository
& 0444) >> 2;
289 if ((mode
& st
.st_mode
) != mode
&& chmod(path
, mode
) < 0)
294 const char *make_relative_path(const char *abs
, const char *base
)
296 static char buf
[PATH_MAX
+ 1];
300 baselen
= strlen(base
);
301 if (prefixcmp(abs
, base
))
303 if (abs
[baselen
] == '/')
305 else if (base
[baselen
- 1] != '/')
307 strcpy(buf
, abs
+ baselen
);
312 * path = absolute path
313 * buf = buffer of at least max(2, strlen(path)+1) bytes
314 * It is okay if buf == path, but they should not overlap otherwise.
316 * Performs the following normalizations on path, storing the result in buf:
317 * - Removes trailing slashes.
318 * - Removes empty components.
319 * - Removes "." components.
320 * - Removes ".." components, and the components the precede them.
321 * "" and paths that contain only slashes are normalized to "/".
322 * Returns the length of the output.
324 * Note that this function is purely textual. It does not follow symlinks,
325 * verify the existence of the path, or make any system calls.
327 int normalize_absolute_path(char *buf
, const char *path
)
329 const char *comp_start
= path
, *comp_end
= path
;
335 while (*comp_start
) {
336 assert(*comp_start
== '/');
337 while (*++comp_end
&& *comp_end
!= '/')
339 comp_len
= comp_end
- comp_start
;
341 if (!strncmp("/", comp_start
, comp_len
) ||
342 !strncmp("/.", comp_start
, comp_len
))
345 if (!strncmp("/..", comp_start
, comp_len
)) {
346 while (dst
> buf
&& *--dst
!= '/')
351 memmove(dst
, comp_start
, comp_len
);
354 comp_start
= comp_end
;
365 * path = Canonical absolute path
366 * prefix_list = Colon-separated list of absolute paths
368 * Determines, for each path in prefix_list, whether the "prefix" really
369 * is an ancestor directory of path. Returns the length of the longest
370 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
371 * is an ancestor. (Note that this means 0 is returned if prefix_list is
372 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
373 * are not considered to be their own ancestors. path must be in a
374 * canonical form: empty components, or "." or ".." components are not
375 * allowed. prefix_list may be null, which is like "".
377 int longest_ancestor_length(const char *path
, const char *prefix_list
)
379 char buf
[PATH_MAX
+1];
380 const char *ceil
, *colon
;
381 int len
, max_len
= -1;
383 if (prefix_list
== NULL
|| !strcmp(path
, "/"))
386 for (colon
= ceil
= prefix_list
; *colon
; ceil
= colon
+1) {
387 for (colon
= ceil
; *colon
&& *colon
!= ':'; colon
++);
389 if (len
== 0 || len
> PATH_MAX
|| !is_absolute_path(ceil
))
391 strlcpy(buf
, ceil
, len
+1);
392 len
= normalize_absolute_path(buf
, buf
);
393 /* Strip "trailing slashes" from "/". */
397 if (!strncmp(path
, buf
, len
) &&