Simplify and rename find_sha1_file()
[git.git] / path.c
blob7a35a26a1697480f0f43244132d8267deb301651
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 fd;
95 ssize_t len;
97 if (lstat(path, &st) < 0)
98 return -1;
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))
104 return 0;
105 return -1;
109 * Anything else, just open it and try to see if it is a symbolic ref.
111 fd = open(path, O_RDONLY);
112 if (fd < 0)
113 return -1;
114 len = read_in_full(fd, buffer, sizeof(buffer)-1);
115 close(fd);
118 * Is it a symbolic ref?
120 if (len < 4)
121 return -1;
122 if (!memcmp("ref:", buffer, 4)) {
123 buf = buffer + 4;
124 len -= 4;
125 while (len && isspace(*buf))
126 buf++, len--;
127 if (len >= 5 && !memcmp("refs/", buf, 5))
128 return 0;
132 * Is this a detached HEAD?
134 if (!get_sha1_hex(buffer, sha1))
135 return 0;
137 return -1;
140 static char *user_path(char *buf, char *path, int sz)
142 struct passwd *pw;
143 char *slash;
144 int len, baselen;
146 if (!path || path[0] != '~')
147 return NULL;
148 path++;
149 slash = strchr(path, '/');
150 if (path[0] == '/' || !path[0]) {
151 pw = getpwuid(getuid());
153 else {
154 if (slash) {
155 *slash = 0;
156 pw = getpwnam(path);
157 *slash = '/';
159 else
160 pw = getpwnam(path);
162 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
163 return NULL;
164 baselen = strlen(pw->pw_dir);
165 memcpy(buf, pw->pw_dir, baselen);
166 while ((1 < baselen) && (buf[baselen-1] == '/')) {
167 buf[baselen-1] = 0;
168 baselen--;
170 if (slash && slash[1]) {
171 len = strlen(slash);
172 if (sz <= baselen + len)
173 return NULL;
174 memcpy(buf + baselen, slash, len + 1);
176 return buf;
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
183 * done. Otherwise:
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
191 * what we try.
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];
208 if (!path)
209 return NULL;
211 if (!strict) {
212 static const char *suffix[] = {
213 ".git/.git", "/.git", ".git", "", NULL,
215 int len = strlen(path);
216 int i;
217 while ((1 < len) && (path[len-1] == '/')) {
218 path[len-1] = 0;
219 len--;
221 if (PATH_MAX <= len)
222 return NULL;
223 if (path[0] == '~') {
224 if (!user_path(used_path, path, PATH_MAX))
225 return NULL;
226 strcpy(validated_path, path);
227 path = used_path;
229 else if (PATH_MAX - 10 < len)
230 return NULL;
231 else {
232 path = strcpy(used_path, path);
233 strcpy(validated_path, path);
235 len = strlen(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]);
240 break;
243 if (!suffix[i] || chdir(path))
244 return NULL;
245 path = validated_path;
247 else if (chdir(path))
248 return NULL;
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();
254 return path;
257 return NULL;
260 int adjust_shared_perm(const char *path)
262 struct stat st;
263 int mode;
265 if (!shared_repository)
266 return 0;
267 if (lstat(path, &st) < 0)
268 return -1;
269 mode = st.st_mode;
271 if (shared_repository) {
272 int tweak = shared_repository;
273 if (!(mode & S_IWUSR))
274 tweak &= ~0222;
275 mode = (mode & ~0777) | tweak;
276 } else {
277 /* Preserve old PERM_UMASK behaviour */
278 if (mode & S_IWUSR)
279 mode |= S_IWGRP;
282 if (S_ISDIR(mode)) {
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)
290 return -2;
291 return 0;
294 static const char *get_pwd_cwd(void)
296 static char cwd[PATH_MAX + 1];
297 char *pwd;
298 struct stat cwd_stat, pwd_stat;
299 if (getcwd(cwd, PATH_MAX) == NULL)
300 return NULL;
301 pwd = getenv("PWD");
302 if (pwd && strcmp(pwd, cwd)) {
303 stat(cwd, &cwd_stat);
304 if (!stat(pwd, &pwd_stat) &&
305 pwd_stat.st_dev == cwd_stat.st_dev &&
306 pwd_stat.st_ino == cwd_stat.st_ino) {
307 strlcpy(cwd, pwd, PATH_MAX);
310 return cwd;
313 const char *make_nonrelative_path(const char *path)
315 static char buf[PATH_MAX + 1];
317 if (is_absolute_path(path)) {
318 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
319 die ("Too long path: %.*s", 60, path);
320 } else {
321 const char *cwd = get_pwd_cwd();
322 if (!cwd)
323 die("Cannot determine the current working directory");
324 if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
325 die ("Too long path: %.*s", 60, path);
327 return buf;
330 /* We allow "recursive" symbolic links. Only within reason, though. */
331 #define MAXDEPTH 5
333 const char *make_absolute_path(const char *path)
335 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
336 char cwd[1024] = "";
337 int buf_index = 1, len;
339 int depth = MAXDEPTH;
340 char *last_elem = NULL;
341 struct stat st;
343 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
344 die ("Too long path: %.*s", 60, path);
346 while (depth--) {
347 if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
348 char *last_slash = strrchr(buf, '/');
349 if (last_slash) {
350 *last_slash = '\0';
351 last_elem = xstrdup(last_slash + 1);
352 } else {
353 last_elem = xstrdup(buf);
354 *buf = '\0';
358 if (*buf) {
359 if (!*cwd && !getcwd(cwd, sizeof(cwd)))
360 die ("Could not get current working directory");
362 if (chdir(buf))
363 die ("Could not switch to '%s'", buf);
365 if (!getcwd(buf, PATH_MAX))
366 die ("Could not get current working directory");
368 if (last_elem) {
369 int len = strlen(buf);
370 if (len + strlen(last_elem) + 2 > PATH_MAX)
371 die ("Too long path name: '%s/%s'",
372 buf, last_elem);
373 buf[len] = '/';
374 strcpy(buf + len + 1, last_elem);
375 free(last_elem);
376 last_elem = NULL;
379 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
380 len = readlink(buf, next_buf, PATH_MAX);
381 if (len < 0)
382 die ("Invalid symlink: %s", buf);
383 next_buf[len] = '\0';
384 buf = next_buf;
385 buf_index = 1 - buf_index;
386 next_buf = bufs[buf_index];
387 } else
388 break;
391 if (*cwd && chdir(cwd))
392 die ("Could not change back to '%s'", cwd);
394 return buf;