t5300-pack-object: Set up the objects for --strict tests only once.
[git/mingw.git] / path.c
blob84af5229d0fe98c4b8b080ff0a74f1981576c492
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 #ifdef __MINGW32__
79 /* on Windows it is TMP and TEMP */
80 if (!tmp)
81 tmp = getenv("TMP");
82 if (!tmp)
83 tmp = getenv("TEMP");
84 #endif
85 if (!tmp)
86 tmp = "/tmp";
87 n = snprintf(path, len, "%s/%s", tmp, template);
88 if (len <= n) {
89 errno = ENAMETOOLONG;
90 return -1;
92 return mkstemp(path);
96 int validate_headref(const char *path)
98 struct stat st;
99 char *buf, buffer[256];
100 unsigned char sha1[20];
101 int len, fd;
103 if (lstat(path, &st) < 0)
104 return -1;
106 /* Make sure it is a "refs/.." symlink */
107 if (S_ISLNK(st.st_mode)) {
108 len = readlink(path, buffer, sizeof(buffer)-1);
109 if (len >= 5 && !memcmp("refs/", buffer, 5))
110 return 0;
111 return -1;
115 * Anything else, just open it and try to see if it is a symbolic ref.
117 fd = open(path, O_RDONLY);
118 if (fd < 0)
119 return -1;
120 len = read_in_full(fd, buffer, sizeof(buffer)-1);
121 close(fd);
124 * Is it a symbolic ref?
126 if (len < 4)
127 return -1;
128 if (!memcmp("ref:", buffer, 4)) {
129 buf = buffer + 4;
130 len -= 4;
131 while (len && isspace(*buf))
132 buf++, len--;
133 if (len >= 5 && !memcmp("refs/", buf, 5))
134 return 0;
138 * Is this a detached HEAD?
140 if (!get_sha1_hex(buffer, sha1))
141 return 0;
143 return -1;
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;
186 * First, one directory to try is determined by the following algorithm.
188 * (0) If "strict" is given, the path is used as given and no DWIM is
189 * done. Otherwise:
190 * (1) "~/path" to mean path under the running user's home directory;
191 * (2) "~user/path" to mean path under named user's home directory;
192 * (3) "relative/path" to mean cwd relative directory; or
193 * (4) "/absolute/path" to mean absolute directory.
195 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
196 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
197 * what we try.
199 * Second, we try chdir() to that. Upon failure, we return NULL.
201 * Then, we try if the current directory is a valid git repository.
202 * Upon failure, we return NULL.
204 * If all goes well, we return the directory we used to chdir() (but
205 * before ~user is expanded), avoiding getcwd() resolving symbolic
206 * links. User relative paths are also returned as they are given,
207 * except DWIM suffixing.
209 char *enter_repo(char *path, int strict)
211 static char used_path[PATH_MAX];
212 static char validated_path[PATH_MAX];
214 if (!path)
215 return NULL;
217 if (!strict) {
218 static const char *suffix[] = {
219 ".git/.git", "/.git", ".git", "", NULL,
221 int len = strlen(path);
222 int i;
223 while ((1 < len) && (path[len-1] == '/')) {
224 path[len-1] = 0;
225 len--;
227 if (PATH_MAX <= len)
228 return NULL;
229 if (path[0] == '~') {
230 if (!user_path(used_path, path, PATH_MAX))
231 return NULL;
232 strcpy(validated_path, path);
233 path = used_path;
235 else if (PATH_MAX - 10 < len)
236 return NULL;
237 else {
238 path = strcpy(used_path, path);
239 strcpy(validated_path, path);
241 len = strlen(path);
242 for (i = 0; suffix[i]; i++) {
243 strcpy(path + len, suffix[i]);
244 if (!access(path, F_OK)) {
245 strcat(validated_path, suffix[i]);
246 break;
249 if (!suffix[i] || chdir(path))
250 return NULL;
251 path = validated_path;
253 else if (chdir(path))
254 return NULL;
256 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
257 validate_headref("HEAD") == 0) {
258 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
259 check_repository_format();
260 return path;
263 return NULL;
266 int adjust_shared_perm(const char *path)
268 struct stat st;
269 int mode;
271 if (!shared_repository)
272 return 0;
273 if (lstat(path, &st) < 0)
274 return -1;
275 mode = st.st_mode;
276 if (mode & S_IRUSR)
277 mode |= (shared_repository == PERM_GROUP
278 ? S_IRGRP
279 : (shared_repository == PERM_EVERYBODY
280 ? (S_IRGRP|S_IROTH)
281 : 0));
283 if (mode & S_IWUSR)
284 mode |= S_IWGRP;
286 if (mode & S_IXUSR)
287 mode |= (shared_repository == PERM_GROUP
288 ? S_IXGRP
289 : (shared_repository == PERM_EVERYBODY
290 ? (S_IXGRP|S_IXOTH)
291 : 0));
292 if (S_ISDIR(mode))
293 mode |= FORCE_DIR_SET_GID;
294 if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
295 return -2;
296 return 0;
299 /* We allow "recursive" symbolic links. Only within reason, though. */
300 #define MAXDEPTH 5
302 const char *make_absolute_path(const char *path)
304 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
305 char cwd[1024] = "";
306 int buf_index = 1, len;
308 int depth = MAXDEPTH;
309 char *last_elem = NULL;
310 struct stat st;
312 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
313 die ("Too long path: %.*s", 60, path);
315 while (depth--) {
316 if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
317 char *last_slash = strrchr(buf, '/');
318 if (last_slash) {
319 *last_slash = '\0';
320 last_elem = xstrdup(last_slash + 1);
321 } else {
322 last_elem = xstrdup(buf);
323 *buf = '\0';
327 if (*buf) {
328 if (!*cwd && !getcwd(cwd, sizeof(cwd)))
329 die ("Could not get current working directory");
331 if (chdir(buf))
332 die ("Could not switch to '%s'", buf);
334 if (!getcwd(buf, PATH_MAX))
335 die ("Could not get current working directory");
337 if (last_elem) {
338 int len = strlen(buf);
339 if (len + strlen(last_elem) + 2 > PATH_MAX)
340 die ("Too long path name: '%s/%s'",
341 buf, last_elem);
342 buf[len] = '/';
343 strcpy(buf + len + 1, last_elem);
344 free(last_elem);
345 last_elem = NULL;
348 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
349 len = readlink(buf, next_buf, PATH_MAX);
350 if (len < 0)
351 die ("Invalid symlink: %s", buf);
352 next_buf[len] = '\0';
353 buf = next_buf;
354 buf_index = 1 - buf_index;
355 next_buf = bufs[buf_index];
356 } else
357 break;
360 if (*cwd && chdir(cwd))
361 die ("Could not change back to '%s'", cwd);
363 return buf;