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");
78 /* on Windows it is TMP and TEMP */
85 n
= snprintf(path
, len
, "%s/%s", tmp
, template);
94 int validate_headref(const char *path
)
97 char *buf
, buffer
[256];
98 unsigned char sha1
[20];
101 if (lstat(path
, &st
) < 0)
104 /* Make sure it is a "refs/.." symlink */
105 if (S_ISLNK(st
.st_mode
)) {
106 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
107 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
113 * Anything else, just open it and try to see if it is a symbolic ref.
115 fd
= open(path
, O_RDONLY
);
118 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
122 * Is it a symbolic ref?
126 if (!memcmp("ref:", buffer
, 4)) {
129 while (len
&& isspace(*buf
))
131 if (len
>= 5 && !memcmp("refs/", buf
, 5))
136 * Is this a detached HEAD?
138 if (!get_sha1_hex(buffer
, sha1
))
144 #ifndef NO_ETC_PASSWD
146 static char *user_path(char *buf
, char *path
, int sz
)
152 if (!path
|| path
[0] != '~')
155 slash
= strchr(path
, '/');
156 if (path
[0] == '/' || !path
[0]) {
157 pw
= getpwuid(getuid());
168 if (!pw
|| !pw
->pw_dir
|| sz
<= strlen(pw
->pw_dir
))
170 baselen
= strlen(pw
->pw_dir
);
171 memcpy(buf
, pw
->pw_dir
, baselen
);
172 while ((1 < baselen
) && (buf
[baselen
-1] == '/')) {
176 if (slash
&& slash
[1]) {
178 if (sz
<= baselen
+ len
)
180 memcpy(buf
+ baselen
, slash
, len
+ 1);
187 static char *user_path(char *buf
, char *path
, int sz
)
189 return getenv("HOME");
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 mode
|= (shared_repository
== PERM_GROUP
288 : (shared_repository
== PERM_EVERYBODY
296 mode
|= (shared_repository
== PERM_GROUP
298 : (shared_repository
== PERM_EVERYBODY
303 if ((mode
& st
.st_mode
) != mode
&& chmod(path
, mode
) < 0)
308 /* We allow "recursive" symbolic links. Only within reason, though. */
311 const char *make_absolute_path(const char *path
)
313 static char bufs
[2][PATH_MAX
+ 1], *buf
= bufs
[0], *next_buf
= bufs
[1];
315 int buf_index
= 1, len
;
317 int depth
= MAXDEPTH
;
318 char *last_elem
= NULL
;
321 if (strlcpy(buf
, path
, PATH_MAX
) >= PATH_MAX
)
322 die ("Too long path: %.*s", 60, path
);
325 if (stat(buf
, &st
) || !S_ISDIR(st
.st_mode
)) {
326 char *last_slash
= strrchr(buf
, '/');
329 last_elem
= xstrdup(last_slash
+ 1);
331 last_elem
= xstrdup(buf
);
335 if (!*cwd
&& !getcwd(cwd
, sizeof(cwd
)))
336 die ("Could not get current working directory");
339 die ("Could not switch to '%s'", buf
);
341 if (!getcwd(buf
, PATH_MAX
))
342 die ("Could not get current working directory");
345 int len
= strlen(buf
);
346 if (len
+ strlen(last_elem
) + 2 > PATH_MAX
)
347 die ("Too long path name: '%s/%s'",
350 strcpy(buf
+ len
+ 1, last_elem
);
355 if (!lstat(buf
, &st
) && S_ISLNK(st
.st_mode
)) {
356 len
= readlink(buf
, next_buf
, PATH_MAX
);
358 die ("Invalid symlink: %s", buf
);
359 next_buf
[len
] = '\0';
361 buf_index
= 1 - buf_index
;
362 next_buf
= bufs
[buf_index
];
367 if (*cwd
&& chdir(cwd
))
368 die ("Could not change back to '%s'", cwd
);