Make ls-remote http://... list HEAD, like for git://...
[git/mjg.git] / path.c
blob2ae7cd997555883ecbe73b12fb53c1e6c650b62c
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.
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.
13 #include "cache.h"
15 static char bad_path[] = "/bad-path/";
17 static char *get_pathname(void)
19 static char pathname_array[4][PATH_MAX];
20 static int index;
21 return pathname_array[3 & ++index];
24 static char *cleanup_path(char *path)
26 /* Clean it up */
27 if (!memcmp(path, "./", 2)) {
28 path += 2;
29 while (*path == '/')
30 path++;
32 return path;
35 char *mkpath(const char *fmt, ...)
37 va_list args;
38 unsigned len;
39 char *pathname = get_pathname();
41 va_start(args, fmt);
42 len = vsnprintf(pathname, PATH_MAX, fmt, args);
43 va_end(args);
44 if (len >= PATH_MAX)
45 return bad_path;
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();
53 va_list args;
54 unsigned len;
56 len = strlen(git_dir);
57 if (len > PATH_MAX-100)
58 return bad_path;
59 memcpy(pathname, git_dir, len);
60 if (len && git_dir[len-1] != '/')
61 pathname[len++] = '/';
62 va_start(args, fmt);
63 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
64 va_end(args);
65 if (len >= PATH_MAX)
66 return bad_path;
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)
74 const char *tmp;
75 size_t n;
77 tmp = getenv("TMPDIR");
78 if (!tmp)
79 tmp = "/tmp";
80 n = snprintf(path, len, "%s/%s", tmp, template);
81 if (len <= n) {
82 errno = ENAMETOOLONG;
83 return -1;
85 return mkstemp(path);
89 int validate_headref(const char *path)
91 struct stat st;
92 char *buf, buffer[256];
93 unsigned char sha1[20];
94 int len, fd;
96 if (lstat(path, &st) < 0)
97 return -1;
99 /* Make sure it is a "refs/.." symlink */
100 if (S_ISLNK(st.st_mode)) {
101 len = readlink(path, buffer, sizeof(buffer)-1);
102 if (len >= 5 && !memcmp("refs/", buffer, 5))
103 return 0;
104 return -1;
108 * Anything else, just open it and try to see if it is a symbolic ref.
110 fd = open(path, O_RDONLY);
111 if (fd < 0)
112 return -1;
113 len = read_in_full(fd, buffer, sizeof(buffer)-1);
114 close(fd);
117 * Is it a symbolic ref?
119 if (len < 4)
120 return -1;
121 if (!memcmp("ref:", buffer, 4)) {
122 buf = buffer + 4;
123 len -= 4;
124 while (len && isspace(*buf))
125 buf++, len--;
126 if (len >= 5 && !memcmp("refs/", buf, 5))
127 return 0;
131 * Is this a detached HEAD?
133 if (!get_sha1_hex(buffer, sha1))
134 return 0;
136 return -1;
139 static char *user_path(char *buf, char *path, int sz)
141 struct passwd *pw;
142 char *slash;
143 int len, baselen;
145 if (!path || path[0] != '~')
146 return NULL;
147 path++;
148 slash = strchr(path, '/');
149 if (path[0] == '/' || !path[0]) {
150 pw = getpwuid(getuid());
152 else {
153 if (slash) {
154 *slash = 0;
155 pw = getpwnam(path);
156 *slash = '/';
158 else
159 pw = getpwnam(path);
161 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
162 return NULL;
163 baselen = strlen(pw->pw_dir);
164 memcpy(buf, pw->pw_dir, baselen);
165 while ((1 < baselen) && (buf[baselen-1] == '/')) {
166 buf[baselen-1] = 0;
167 baselen--;
169 if (slash && slash[1]) {
170 len = strlen(slash);
171 if (sz <= baselen + len)
172 return NULL;
173 memcpy(buf + baselen, slash, len + 1);
175 return buf;
179 * First, one directory to try is determined by the following algorithm.
181 * (0) If "strict" is given, the path is used as given and no DWIM is
182 * done. Otherwise:
183 * (1) "~/path" to mean path under the running user's home directory;
184 * (2) "~user/path" to mean path under named user's home directory;
185 * (3) "relative/path" to mean cwd relative directory; or
186 * (4) "/absolute/path" to mean absolute directory.
188 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
189 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
190 * what we try.
192 * Second, we try chdir() to that. Upon failure, we return NULL.
194 * Then, we try if the current directory is a valid git repository.
195 * Upon failure, we return NULL.
197 * If all goes well, we return the directory we used to chdir() (but
198 * before ~user is expanded), avoiding getcwd() resolving symbolic
199 * links. User relative paths are also returned as they are given,
200 * except DWIM suffixing.
202 char *enter_repo(char *path, int strict)
204 static char used_path[PATH_MAX];
205 static char validated_path[PATH_MAX];
207 if (!path)
208 return NULL;
210 if (!strict) {
211 static const char *suffix[] = {
212 ".git/.git", "/.git", ".git", "", NULL,
214 int len = strlen(path);
215 int i;
216 while ((1 < len) && (path[len-1] == '/')) {
217 path[len-1] = 0;
218 len--;
220 if (PATH_MAX <= len)
221 return NULL;
222 if (path[0] == '~') {
223 if (!user_path(used_path, path, PATH_MAX))
224 return NULL;
225 strcpy(validated_path, path);
226 path = used_path;
228 else if (PATH_MAX - 10 < len)
229 return NULL;
230 else {
231 path = strcpy(used_path, path);
232 strcpy(validated_path, path);
234 len = strlen(path);
235 for (i = 0; suffix[i]; i++) {
236 strcpy(path + len, suffix[i]);
237 if (!access(path, F_OK)) {
238 strcat(validated_path, suffix[i]);
239 break;
242 if (!suffix[i] || chdir(path))
243 return NULL;
244 path = validated_path;
246 else if (chdir(path))
247 return NULL;
249 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
250 validate_headref("HEAD") == 0) {
251 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
252 check_repository_format();
253 return path;
256 return NULL;
259 int adjust_shared_perm(const char *path)
261 struct stat st;
262 int mode;
264 if (!shared_repository)
265 return 0;
266 if (lstat(path, &st) < 0)
267 return -1;
268 mode = st.st_mode;
270 if (shared_repository) {
271 int tweak = shared_repository;
272 if (!(mode & S_IWUSR))
273 tweak &= ~0222;
274 mode = (mode & ~0777) | tweak;
275 } else {
276 /* Preserve old PERM_UMASK behaviour */
277 if (mode & S_IWUSR)
278 mode |= S_IWGRP;
281 if (S_ISDIR(mode)) {
282 mode |= FORCE_DIR_SET_GID;
284 /* Copy read bits to execute bits */
285 mode |= (shared_repository & 0444) >> 2;
288 if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
289 return -2;
290 return 0;
293 /* We allow "recursive" symbolic links. Only within reason, though. */
294 #define MAXDEPTH 5
296 const char *make_absolute_path(const char *path)
298 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
299 char cwd[1024] = "";
300 int buf_index = 1, len;
302 int depth = MAXDEPTH;
303 char *last_elem = NULL;
304 struct stat st;
306 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
307 die ("Too long path: %.*s", 60, path);
309 while (depth--) {
310 if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
311 char *last_slash = strrchr(buf, '/');
312 if (last_slash) {
313 *last_slash = '\0';
314 last_elem = xstrdup(last_slash + 1);
315 } else {
316 last_elem = xstrdup(buf);
317 *buf = '\0';
321 if (*buf) {
322 if (!*cwd && !getcwd(cwd, sizeof(cwd)))
323 die ("Could not get current working directory");
325 if (chdir(buf))
326 die ("Could not switch to '%s'", buf);
328 if (!getcwd(buf, PATH_MAX))
329 die ("Could not get current working directory");
331 if (last_elem) {
332 int len = strlen(buf);
333 if (len + strlen(last_elem) + 2 > PATH_MAX)
334 die ("Too long path name: '%s/%s'",
335 buf, last_elem);
336 buf[len] = '/';
337 strcpy(buf + len + 1, last_elem);
338 free(last_elem);
339 last_elem = NULL;
342 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
343 len = readlink(buf, next_buf, PATH_MAX);
344 if (len < 0)
345 die ("Invalid symlink: %s", buf);
346 next_buf[len] = '\0';
347 buf = next_buf;
348 buf_index = 1 - buf_index;
349 next_buf = bufs[buf_index];
350 } else
351 break;
354 if (*cwd && chdir(cwd))
355 die ("Could not change back to '%s'", cwd);
357 return buf;