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 snprintf(buf
, n
, bad_path
);
47 return cleanup_path(buf
);
50 char *mkpath(const char *fmt
, ...)
54 char *pathname
= get_pathname();
57 len
= vsnprintf(pathname
, PATH_MAX
, fmt
, args
);
61 return cleanup_path(pathname
);
64 char *git_path(const char *fmt
, ...)
66 const char *git_dir
= get_git_dir();
67 char *pathname
= get_pathname();
71 len
= strlen(git_dir
);
72 if (len
> PATH_MAX
-100)
74 memcpy(pathname
, git_dir
, len
);
75 if (len
&& git_dir
[len
-1] != '/')
76 pathname
[len
++] = '/';
78 len
+= vsnprintf(pathname
+ len
, PATH_MAX
- len
, fmt
, args
);
82 return cleanup_path(pathname
);
86 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
87 int git_mkstemp(char *path
, size_t len
, const char *template)
92 tmp
= getenv("TMPDIR");
95 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
100 return mkstemp(path
);
104 int validate_headref(const char *path
)
107 char *buf
, buffer
[256];
108 unsigned char sha1
[20];
112 if (lstat(path
, &st
) < 0)
115 /* Make sure it is a "refs/.." symlink */
116 if (S_ISLNK(st
.st_mode
)) {
117 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
118 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
124 * Anything else, just open it and try to see if it is a symbolic ref.
126 fd
= open(path
, O_RDONLY
);
129 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
133 * Is it a symbolic ref?
137 if (!memcmp("ref:", buffer
, 4)) {
140 while (len
&& isspace(*buf
))
142 if (len
>= 5 && !memcmp("refs/", buf
, 5))
147 * Is this a detached HEAD?
149 if (!get_sha1_hex(buffer
, sha1
))
155 static char *user_path(char *buf
, char *path
, int sz
)
161 if (!path
|| path
[0] != '~')
164 slash
= strchr(path
, '/');
165 if (path
[0] == '/' || !path
[0]) {
166 pw
= getpwuid(getuid());
177 if (!pw
|| !pw
->pw_dir
|| sz
<= strlen(pw
->pw_dir
))
179 baselen
= strlen(pw
->pw_dir
);
180 memcpy(buf
, pw
->pw_dir
, baselen
);
181 while ((1 < baselen
) && (buf
[baselen
-1] == '/')) {
185 if (slash
&& slash
[1]) {
187 if (sz
<= baselen
+ len
)
189 memcpy(buf
+ baselen
, slash
, len
+ 1);
195 * First, one directory to try is determined by the following algorithm.
197 * (0) If "strict" is given, the path is used as given and no DWIM is
199 * (1) "~/path" to mean path under the running user's home directory;
200 * (2) "~user/path" to mean path under named user's home directory;
201 * (3) "relative/path" to mean cwd relative directory; or
202 * (4) "/absolute/path" to mean absolute directory.
204 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
205 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
208 * Second, we try chdir() to that. Upon failure, we return NULL.
210 * Then, we try if the current directory is a valid git repository.
211 * Upon failure, we return NULL.
213 * If all goes well, we return the directory we used to chdir() (but
214 * before ~user is expanded), avoiding getcwd() resolving symbolic
215 * links. User relative paths are also returned as they are given,
216 * except DWIM suffixing.
218 char *enter_repo(char *path
, int strict
)
220 static char used_path
[PATH_MAX
];
221 static char validated_path
[PATH_MAX
];
227 static const char *suffix
[] = {
228 ".git/.git", "/.git", ".git", "", NULL
,
230 int len
= strlen(path
);
232 while ((1 < len
) && (path
[len
-1] == '/')) {
238 if (path
[0] == '~') {
239 if (!user_path(used_path
, path
, PATH_MAX
))
241 strcpy(validated_path
, path
);
244 else if (PATH_MAX
- 10 < len
)
247 path
= strcpy(used_path
, path
);
248 strcpy(validated_path
, path
);
251 for (i
= 0; suffix
[i
]; i
++) {
252 strcpy(path
+ len
, suffix
[i
]);
253 if (!access(path
, F_OK
)) {
254 strcat(validated_path
, suffix
[i
]);
258 if (!suffix
[i
] || chdir(path
))
260 path
= validated_path
;
262 else if (chdir(path
))
265 if (access("objects", X_OK
) == 0 && access("refs", X_OK
) == 0 &&
266 validate_headref("HEAD") == 0) {
267 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
268 check_repository_format();
275 int adjust_shared_perm(const char *path
)
280 if (!shared_repository
)
282 if (lstat(path
, &st
) < 0)
286 if (shared_repository
) {
287 int tweak
= shared_repository
;
288 if (!(mode
& S_IWUSR
))
292 /* Preserve old PERM_UMASK behaviour */
298 mode
|= FORCE_DIR_SET_GID
;
300 /* Copy read bits to execute bits */
301 mode
|= (shared_repository
& 0444) >> 2;
304 if ((mode
& st
.st_mode
) != mode
&& chmod(path
, mode
) < 0)
309 const char *make_relative_path(const char *abs
, const char *base
)
311 static char buf
[PATH_MAX
+ 1];
315 baselen
= strlen(base
);
316 if (prefixcmp(abs
, base
))
318 if (abs
[baselen
] == '/')
320 else if (base
[baselen
- 1] != '/')
322 strcpy(buf
, abs
+ baselen
);
327 * path = absolute path
328 * buf = buffer of at least max(2, strlen(path)+1) bytes
329 * It is okay if buf == path, but they should not overlap otherwise.
331 * Performs the following normalizations on path, storing the result in buf:
332 * - Removes trailing slashes.
333 * - Removes empty components.
334 * - Removes "." components.
335 * - Removes ".." components, and the components the precede them.
336 * "" and paths that contain only slashes are normalized to "/".
337 * Returns the length of the output.
339 * Note that this function is purely textual. It does not follow symlinks,
340 * verify the existence of the path, or make any system calls.
342 int normalize_absolute_path(char *buf
, const char *path
)
344 const char *comp_start
= path
, *comp_end
= path
;
350 while (*comp_start
) {
351 assert(*comp_start
== '/');
352 while (*++comp_end
&& *comp_end
!= '/')
354 comp_len
= comp_end
- comp_start
;
356 if (!strncmp("/", comp_start
, comp_len
) ||
357 !strncmp("/.", comp_start
, comp_len
))
360 if (!strncmp("/..", comp_start
, comp_len
)) {
361 while (dst
> buf
&& *--dst
!= '/')
366 memcpy(dst
, comp_start
, comp_len
);
369 comp_start
= comp_end
;
380 * path = Canonical absolute path
381 * prefix_list = Colon-separated list of absolute paths
383 * Determines, for each path in prefix_list, whether the "prefix" really
384 * is an ancestor directory of path. Returns the length of the longest
385 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
386 * is an ancestor. (Note that this means 0 is returned if prefix_list is
387 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
388 * are not considered to be their own ancestors. path must be in a
389 * canonical form: empty components, or "." or ".." components are not
390 * allowed. prefix_list may be null, which is like "".
392 int longest_ancestor_length(const char *path
, const char *prefix_list
)
394 char buf
[PATH_MAX
+1];
395 const char *ceil
, *colon
;
396 int len
, max_len
= -1;
398 if (prefix_list
== NULL
|| !strcmp(path
, "/"))
401 for (colon
= ceil
= prefix_list
; *colon
; ceil
= colon
+1) {
402 for (colon
= ceil
; *colon
&& *colon
!= ':'; colon
++);
404 if (len
== 0 || len
> PATH_MAX
|| !is_absolute_path(ceil
))
406 strlcpy(buf
, ceil
, len
+1);
407 len
= normalize_absolute_path(buf
, buf
);
408 /* Strip "trailing slashes" from "/". */
412 if (!strncmp(path
, buf
, len
) &&