Fix fstat() implementation for pipes and sockets again.
[git/mingw.git] / path.c
blob2c5ebfb92204b6d0b84c8f65d38acb8873fec43d
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 #ifndef NO_ETC_PASSWD
146 static char *user_path(char *buf, char *path, int sz)
148 struct passwd *pw;
149 char *slash;
150 int len, baselen;
152 if (!path || path[0] != '~')
153 return NULL;
154 path++;
155 slash = strchr(path, '/');
156 if (path[0] == '/' || !path[0]) {
157 pw = getpwuid(getuid());
159 else {
160 if (slash) {
161 *slash = 0;
162 pw = getpwnam(path);
163 *slash = '/';
165 else
166 pw = getpwnam(path);
168 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
169 return NULL;
170 baselen = strlen(pw->pw_dir);
171 memcpy(buf, pw->pw_dir, baselen);
172 while ((1 < baselen) && (buf[baselen-1] == '/')) {
173 buf[baselen-1] = 0;
174 baselen--;
176 if (slash && slash[1]) {
177 len = strlen(slash);
178 if (sz <= baselen + len)
179 return NULL;
180 memcpy(buf + baselen, slash, len + 1);
182 return buf;
185 #else
187 static char *user_path(char *buf, char *path, int sz)
189 return getenv("HOME");
192 #endif
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
198 * done. Otherwise:
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
206 * what we try.
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];
223 if (!path)
224 return NULL;
226 if (!strict) {
227 static const char *suffix[] = {
228 ".git/.git", "/.git", ".git", "", NULL,
230 int len = strlen(path);
231 int i;
232 while ((1 < len) && (path[len-1] == '/')) {
233 path[len-1] = 0;
234 len--;
236 if (PATH_MAX <= len)
237 return NULL;
238 if (path[0] == '~') {
239 if (!user_path(used_path, path, PATH_MAX))
240 return NULL;
241 strcpy(validated_path, path);
242 path = used_path;
244 else if (PATH_MAX - 10 < len)
245 return NULL;
246 else {
247 path = strcpy(used_path, path);
248 strcpy(validated_path, path);
250 len = strlen(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]);
255 break;
258 if (!suffix[i] || chdir(path))
259 return NULL;
260 path = validated_path;
262 else if (chdir(path))
263 return NULL;
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();
269 return path;
272 return NULL;
275 int adjust_shared_perm(const char *path)
277 struct stat st;
278 int mode;
280 if (!shared_repository)
281 return 0;
282 if (lstat(path, &st) < 0)
283 return -1;
284 mode = st.st_mode;
285 if (mode & S_IRUSR)
286 mode |= (shared_repository == PERM_GROUP
287 ? S_IRGRP
288 : (shared_repository == PERM_EVERYBODY
289 ? (S_IRGRP|S_IROTH)
290 : 0));
292 if (mode & S_IWUSR)
293 mode |= S_IWGRP;
295 if (mode & S_IXUSR)
296 mode |= (shared_repository == PERM_GROUP
297 ? S_IXGRP
298 : (shared_repository == PERM_EVERYBODY
299 ? (S_IXGRP|S_IXOTH)
300 : 0));
301 if (S_ISDIR(mode))
302 mode |= S_ISGID;
303 if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
304 return -2;
305 return 0;
308 /* We allow "recursive" symbolic links. Only within reason, though. */
309 #define MAXDEPTH 5
311 const char *make_absolute_path(const char *path)
313 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
314 char cwd[1024] = "";
315 int buf_index = 1, len;
317 int depth = MAXDEPTH;
318 char *last_elem = NULL;
319 struct stat st;
321 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
322 die ("Too long path: %.*s", 60, path);
324 while (depth--) {
325 if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
326 char *last_slash = strrchr(buf, '/');
327 if (last_slash) {
328 *last_slash = '\0';
329 last_elem = xstrdup(last_slash + 1);
330 } else
331 last_elem = xstrdup(buf);
334 if (*buf) {
335 if (!*cwd && !getcwd(cwd, sizeof(cwd)))
336 die ("Could not get current working directory");
338 if (chdir(buf))
339 die ("Could not switch to '%s'", buf);
341 if (!getcwd(buf, PATH_MAX))
342 die ("Could not get current working directory");
344 if (last_elem) {
345 int len = strlen(buf);
346 if (len + strlen(last_elem) + 2 > PATH_MAX)
347 die ("Too long path name: '%s/%s'",
348 buf, last_elem);
349 buf[len] = '/';
350 strcpy(buf + len + 1, last_elem);
351 free(last_elem);
352 last_elem = NULL;
355 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
356 len = readlink(buf, next_buf, PATH_MAX);
357 if (len < 0)
358 die ("Invalid symlink: %s", buf);
359 next_buf[len] = '\0';
360 buf = next_buf;
361 buf_index = 1 - buf_index;
362 next_buf = bufs[buf_index];
363 } else
364 break;
367 if (*cwd && chdir(cwd))
368 die ("Could not change back to '%s'", cwd);
370 return buf;