Merge branch 'maint'
[git/trast.git] / path.c
blob334b2bd195f56ac6aaf9f9b8a36fe59115cd758c
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"
14 #include <pwd.h>
16 static char pathname[PATH_MAX];
17 static char bad_path[] = "/bad-path/";
19 static char *cleanup_path(char *path)
21 /* Clean it up */
22 if (!memcmp(path, "./", 2)) {
23 path += 2;
24 while (*path == '/')
25 path++;
27 return path;
30 char *mkpath(const char *fmt, ...)
32 va_list args;
33 unsigned len;
35 va_start(args, fmt);
36 len = vsnprintf(pathname, PATH_MAX, fmt, args);
37 va_end(args);
38 if (len >= PATH_MAX)
39 return bad_path;
40 return cleanup_path(pathname);
43 char *git_path(const char *fmt, ...)
45 const char *git_dir = get_git_dir();
46 va_list args;
47 unsigned len;
49 len = strlen(git_dir);
50 if (len > PATH_MAX-100)
51 return bad_path;
52 memcpy(pathname, git_dir, len);
53 if (len && git_dir[len-1] != '/')
54 pathname[len++] = '/';
55 va_start(args, fmt);
56 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
57 va_end(args);
58 if (len >= PATH_MAX)
59 return bad_path;
60 return cleanup_path(pathname);
64 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
65 int git_mkstemp(char *path, size_t len, const char *template)
67 char *env, *pch = path;
69 if ((env = getenv("TMPDIR")) == NULL) {
70 strcpy(pch, "/tmp/");
71 len -= 5;
72 pch += 5;
73 } else {
74 size_t n = snprintf(pch, len, "%s/", env);
76 len -= n;
77 pch += n;
80 safe_strncpy(pch, template, len);
82 return mkstemp(path);
86 char *safe_strncpy(char *dest, const char *src, size_t n)
88 strncpy(dest, src, n);
89 dest[n - 1] = '\0';
91 return dest;
94 int validate_symref(const char *path)
96 struct stat st;
97 char *buf, buffer[256];
98 int len, fd;
100 if (lstat(path, &st) < 0)
101 return -1;
103 /* Make sure it is a "refs/.." symlink */
104 if (S_ISLNK(st.st_mode)) {
105 len = readlink(path, buffer, sizeof(buffer)-1);
106 if (len >= 5 && !memcmp("refs/", buffer, 5))
107 return 0;
108 return -1;
112 * Anything else, just open it and try to see if it is a symbolic ref.
114 fd = open(path, O_RDONLY);
115 if (fd < 0)
116 return -1;
117 len = read(fd, buffer, sizeof(buffer)-1);
118 close(fd);
121 * Is it a symbolic ref?
123 if (len < 4 || memcmp("ref:", buffer, 4))
124 return -1;
125 buf = buffer + 4;
126 len -= 4;
127 while (len && isspace(*buf))
128 buf++, len--;
129 if (len >= 5 && !memcmp("refs/", buf, 5))
130 return 0;
131 return -1;
134 static char *user_path(char *buf, char *path, int sz)
136 struct passwd *pw;
137 char *slash;
138 int len, baselen;
140 if (!path || path[0] != '~')
141 return NULL;
142 path++;
143 slash = strchr(path, '/');
144 if (path[0] == '/' || !path[0]) {
145 pw = getpwuid(getuid());
147 else {
148 if (slash) {
149 *slash = 0;
150 pw = getpwnam(path);
151 *slash = '/';
153 else
154 pw = getpwnam(path);
156 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
157 return NULL;
158 baselen = strlen(pw->pw_dir);
159 memcpy(buf, pw->pw_dir, baselen);
160 while ((1 < baselen) && (buf[baselen-1] == '/')) {
161 buf[baselen-1] = 0;
162 baselen--;
164 if (slash && slash[1]) {
165 len = strlen(slash);
166 if (sz <= baselen + len)
167 return NULL;
168 memcpy(buf + baselen, slash, len + 1);
170 return buf;
174 * First, one directory to try is determined by the following algorithm.
176 * (0) If "strict" is given, the path is used as given and no DWIM is
177 * done. Otherwise:
178 * (1) "~/path" to mean path under the running user's home directory;
179 * (2) "~user/path" to mean path under named user's home directory;
180 * (3) "relative/path" to mean cwd relative directory; or
181 * (4) "/absolute/path" to mean absolute directory.
183 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
184 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
185 * what we try.
187 * Second, we try chdir() to that. Upon failure, we return NULL.
189 * Then, we try if the current directory is a valid git repository.
190 * Upon failure, we return NULL.
192 * If all goes well, we return the directory we used to chdir() (but
193 * before ~user is expanded), avoiding getcwd() resolving symbolic
194 * links. User relative paths are also returned as they are given,
195 * except DWIM suffixing.
197 char *enter_repo(char *path, int strict)
199 static char used_path[PATH_MAX];
200 static char validated_path[PATH_MAX];
202 if (!path)
203 return NULL;
205 if (!strict) {
206 static const char *suffix[] = {
207 ".git/.git", "/.git", ".git", "", NULL,
209 int len = strlen(path);
210 int i;
211 while ((1 < len) && (path[len-1] == '/')) {
212 path[len-1] = 0;
213 len--;
215 if (PATH_MAX <= len)
216 return NULL;
217 if (path[0] == '~') {
218 if (!user_path(used_path, path, PATH_MAX))
219 return NULL;
220 strcpy(validated_path, path);
221 path = used_path;
223 else if (PATH_MAX - 10 < len)
224 return NULL;
225 else {
226 path = strcpy(used_path, path);
227 strcpy(validated_path, path);
229 len = strlen(path);
230 for (i = 0; suffix[i]; i++) {
231 strcpy(path + len, suffix[i]);
232 if (!access(path, F_OK)) {
233 strcat(validated_path, suffix[i]);
234 break;
237 if (!suffix[i] || chdir(path))
238 return NULL;
239 path = validated_path;
241 else if (chdir(path))
242 return NULL;
244 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
245 validate_symref("HEAD") == 0) {
246 putenv("GIT_DIR=.");
247 check_repository_format();
248 return path;
251 return NULL;