skip t5512 because remote does not yet work
[git/platforms/storm.git] / path.c
blob2a7626970502e2a8f60b5b26358afe2e8cd0d519
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 /* on Windows it is TMP and TEMP */
79 if (!tmp)
80 tmp = getenv("TMP");
81 if (!tmp)
82 tmp = getenv("TEMP");
83 if (!tmp)
84 tmp = "/tmp";
85 n = snprintf(path, len, "%s/%s", tmp, template);
86 if (len <= n) {
87 errno = ENAMETOOLONG;
88 return -1;
90 return mkstemp(path);
94 int validate_headref(const char *path)
96 struct stat st;
97 char *buf, buffer[256];
98 unsigned char sha1[20];
99 int len, fd;
101 if (lstat(path, &st) < 0)
102 return -1;
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))
108 return 0;
109 return -1;
113 * Anything else, just open it and try to see if it is a symbolic ref.
115 fd = open(path, O_RDONLY);
116 if (fd < 0)
117 return -1;
118 len = read_in_full(fd, buffer, sizeof(buffer)-1);
119 close(fd);
122 * Is it a symbolic ref?
124 if (len < 4)
125 return -1;
126 if (!memcmp("ref:", buffer, 4)) {
127 buf = buffer + 4;
128 len -= 4;
129 while (len && isspace(*buf))
130 buf++, len--;
131 if (len >= 5 && !memcmp("refs/", buf, 5))
132 return 0;
136 * Is this a detached HEAD?
138 if (!get_sha1_hex(buffer, sha1))
139 return 0;
141 return -1;
144 static char *user_path(char *buf, char *path, int sz)
146 struct passwd *pw;
147 char *slash;
148 int len, baselen;
150 if (!path || path[0] != '~')
151 return NULL;
152 path++;
153 slash = strchr(path, '/');
154 if (path[0] == '/' || !path[0]) {
155 pw = getpwuid(getuid());
157 else {
158 if (slash) {
159 *slash = 0;
160 pw = getpwnam(path);
161 *slash = '/';
163 else
164 pw = getpwnam(path);
166 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
167 return NULL;
168 baselen = strlen(pw->pw_dir);
169 memcpy(buf, pw->pw_dir, baselen);
170 while ((1 < baselen) && (buf[baselen-1] == '/')) {
171 buf[baselen-1] = 0;
172 baselen--;
174 if (slash && slash[1]) {
175 len = strlen(slash);
176 if (sz <= baselen + len)
177 return NULL;
178 memcpy(buf + baselen, slash, len + 1);
180 return buf;
184 * First, one directory to try is determined by the following algorithm.
186 * (0) If "strict" is given, the path is used as given and no DWIM is
187 * done. Otherwise:
188 * (1) "~/path" to mean path under the running user's home directory;
189 * (2) "~user/path" to mean path under named user's home directory;
190 * (3) "relative/path" to mean cwd relative directory; or
191 * (4) "/absolute/path" to mean absolute directory.
193 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
194 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
195 * what we try.
197 * Second, we try chdir() to that. Upon failure, we return NULL.
199 * Then, we try if the current directory is a valid git repository.
200 * Upon failure, we return NULL.
202 * If all goes well, we return the directory we used to chdir() (but
203 * before ~user is expanded), avoiding getcwd() resolving symbolic
204 * links. User relative paths are also returned as they are given,
205 * except DWIM suffixing.
207 char *enter_repo(char *path, int strict)
209 static char used_path[PATH_MAX];
210 static char validated_path[PATH_MAX];
212 if (!path)
213 return NULL;
215 if (!strict) {
216 static const char *suffix[] = {
217 ".git/.git", "/.git", ".git", "", NULL,
219 int len = strlen(path);
220 int i;
221 while ((1 < len) && (path[len-1] == '/')) {
222 path[len-1] = 0;
223 len--;
225 if (PATH_MAX <= len)
226 return NULL;
227 if (path[0] == '~') {
228 if (!user_path(used_path, path, PATH_MAX))
229 return NULL;
230 strcpy(validated_path, path);
231 path = used_path;
233 else if (PATH_MAX - 10 < len)
234 return NULL;
235 else {
236 path = strcpy(used_path, path);
237 strcpy(validated_path, path);
239 len = strlen(path);
240 for (i = 0; suffix[i]; i++) {
241 strcpy(path + len, suffix[i]);
242 if (!access(path, F_OK)) {
243 strcat(validated_path, suffix[i]);
244 break;
247 if (!suffix[i] || chdir(path))
248 return NULL;
249 path = validated_path;
251 else if (chdir(path))
252 return NULL;
254 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
255 validate_headref("HEAD") == 0) {
256 set_git_dir(".");
257 check_repository_format();
258 return path;
261 return NULL;
264 int adjust_shared_perm(const char *path)
266 struct stat st;
267 int mode;
269 if (!shared_repository)
270 return 0;
271 if (lstat(path, &st) < 0)
272 return -1;
273 mode = st.st_mode;
274 if (mode & S_IRUSR)
275 mode |= (shared_repository == PERM_GROUP
276 ? S_IRGRP
277 : (shared_repository == PERM_EVERYBODY
278 ? (S_IRGRP|S_IROTH)
279 : 0));
281 if (mode & S_IWUSR)
282 mode |= S_IWGRP;
284 if (mode & S_IXUSR)
285 mode |= (shared_repository == PERM_GROUP
286 ? S_IXGRP
287 : (shared_repository == PERM_EVERYBODY
288 ? (S_IXGRP|S_IXOTH)
289 : 0));
290 if (S_ISDIR(mode))
291 mode |= S_ISGID;
292 if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
293 return -2;
294 return 0;
297 /* We allow "recursive" symbolic links. Only within reason, though. */
298 #define MAXDEPTH 5
300 const char *make_absolute_path(const char *path)
302 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
303 char cwd[1024] = "";
304 int buf_index = 1, len;
306 int depth = MAXDEPTH;
307 char *last_elem = NULL;
308 struct stat st;
310 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
311 die ("Too long path: %.*s", 60, path);
313 while (depth--) {
314 if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
315 char *last_slash = strrchr(buf, '/');
316 if (last_slash) {
317 *last_slash = '\0';
318 last_elem = xstrdup(last_slash + 1);
319 } else
320 last_elem = xstrdup(buf);
323 if (*buf) {
324 if (!*cwd && !getcwd(cwd, sizeof(cwd)))
325 die ("Could not get current working directory");
327 if (chdir(buf))
328 die ("Could not switch to '%s'", buf);
330 if (!getcwd(buf, PATH_MAX))
331 die ("Could not get current working directory");
333 if (last_elem) {
334 int len = strlen(buf);
335 if (len + strlen(last_elem) + 2 > PATH_MAX)
336 die ("Too long path name: '%s/%s'",
337 buf, last_elem);
338 buf[len] = '/';
339 strcpy(buf + len + 1, last_elem);
340 free(last_elem);
341 last_elem = NULL;
344 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
345 len = readlink(buf, next_buf, PATH_MAX);
346 if (len < 0)
347 die ("Invalid symlink: %s", buf);
348 next_buf[len] = '\0';
349 buf = next_buf;
350 buf_index = 1 - buf_index;
351 next_buf = bufs[buf_index];
352 } else
353 break;
356 if (*cwd && chdir(cwd))
357 die ("Could not change back to '%s'", cwd);
359 return buf;
362 char *make_native_separator(char* path) {
363 #ifdef __MINGW32__
364 char* c;
365 for (c = path; *c; c++) {
366 if (*c == '/')
367 *c = '\\';
369 return path;
370 #else
371 return path;
372 #endif