Merge commit '079f7cb27b6636f9153c24a82f3d5d74a7b0b6b1'
[git/mingw.git] / path.c
blobced3a7c5861ad2db30ef51f0f1d050321716f23e
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 char *env, *pch = path;
76 if ((env = getenv("TMPDIR")) == NULL) {
77 strcpy(pch, "/tmp/");
78 len -= 5;
79 pch += 5;
80 } else {
81 size_t n = snprintf(pch, len, "%s/", env);
83 len -= n;
84 pch += n;
87 strlcpy(pch, template, len);
89 return mkstemp(path);
93 int validate_headref(const char *path)
95 struct stat st;
96 char *buf, buffer[256];
97 unsigned char sha1[20];
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_in_full(fd, buffer, sizeof(buffer)-1);
118 close(fd);
121 * Is it a symbolic ref?
123 if (len < 4)
124 return -1;
125 if (!memcmp("ref:", buffer, 4)) {
126 buf = buffer + 4;
127 len -= 4;
128 while (len && isspace(*buf))
129 buf++, len--;
130 if (len >= 5 && !memcmp("refs/", buf, 5))
131 return 0;
135 * Is this a detached HEAD?
137 if (!get_sha1_hex(buffer, sha1))
138 return 0;
140 return -1;
143 #ifndef NO_ETC_PASSWD
145 static char *user_path(char *buf, char *path, int sz)
147 struct passwd *pw;
148 char *slash;
149 int len, baselen;
151 if (!path || path[0] != '~')
152 return NULL;
153 path++;
154 slash = strchr(path, '/');
155 if (path[0] == '/' || !path[0]) {
156 pw = getpwuid(getuid());
158 else {
159 if (slash) {
160 *slash = 0;
161 pw = getpwnam(path);
162 *slash = '/';
164 else
165 pw = getpwnam(path);
167 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
168 return NULL;
169 baselen = strlen(pw->pw_dir);
170 memcpy(buf, pw->pw_dir, baselen);
171 while ((1 < baselen) && (buf[baselen-1] == '/')) {
172 buf[baselen-1] = 0;
173 baselen--;
175 if (slash && slash[1]) {
176 len = strlen(slash);
177 if (sz <= baselen + len)
178 return NULL;
179 memcpy(buf + baselen, slash, len + 1);
181 return buf;
184 #else
186 static char *user_path(char *buf, char *path, int sz)
188 return getenv("HOME");
191 #endif
194 * First, one directory to try is determined by the following algorithm.
196 * (0) If "strict" is given, the path is used as given and no DWIM is
197 * done. Otherwise:
198 * (1) "~/path" to mean path under the running user's home directory;
199 * (2) "~user/path" to mean path under named user's home directory;
200 * (3) "relative/path" to mean cwd relative directory; or
201 * (4) "/absolute/path" to mean absolute directory.
203 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
204 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
205 * what we try.
207 * Second, we try chdir() to that. Upon failure, we return NULL.
209 * Then, we try if the current directory is a valid git repository.
210 * Upon failure, we return NULL.
212 * If all goes well, we return the directory we used to chdir() (but
213 * before ~user is expanded), avoiding getcwd() resolving symbolic
214 * links. User relative paths are also returned as they are given,
215 * except DWIM suffixing.
217 char *enter_repo(char *path, int strict)
219 static char used_path[PATH_MAX];
220 static char validated_path[PATH_MAX];
222 if (!path)
223 return NULL;
225 if (!strict) {
226 static const char *suffix[] = {
227 ".git/.git", "/.git", ".git", "", NULL,
229 int len = strlen(path);
230 int i;
231 while ((1 < len) && (path[len-1] == '/')) {
232 path[len-1] = 0;
233 len--;
235 if (PATH_MAX <= len)
236 return NULL;
237 if (path[0] == '~') {
238 if (!user_path(used_path, path, PATH_MAX))
239 return NULL;
240 strcpy(validated_path, path);
241 path = used_path;
243 else if (PATH_MAX - 10 < len)
244 return NULL;
245 else {
246 path = strcpy(used_path, path);
247 strcpy(validated_path, path);
249 len = strlen(path);
250 for (i = 0; suffix[i]; i++) {
251 strcpy(path + len, suffix[i]);
252 if (!access(path, F_OK)) {
253 strcat(validated_path, suffix[i]);
254 break;
257 if (!suffix[i] || chdir(path))
258 return NULL;
259 path = validated_path;
261 else if (chdir(path))
262 return NULL;
264 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
265 validate_headref("HEAD") == 0) {
266 setenv("GIT_DIR", ".", 1);
267 check_repository_format();
268 return path;
271 return NULL;
274 int adjust_shared_perm(const char *path)
276 struct stat st;
277 int mode;
279 if (!shared_repository)
280 return 0;
281 if (lstat(path, &st) < 0)
282 return -1;
283 mode = st.st_mode;
284 if (mode & S_IRUSR)
285 mode |= (shared_repository == PERM_GROUP
286 ? S_IRGRP
287 : (shared_repository == PERM_EVERYBODY
288 ? (S_IRGRP|S_IROTH)
289 : 0));
291 if (mode & S_IWUSR)
292 mode |= S_IWGRP;
294 if (mode & S_IXUSR)
295 mode |= (shared_repository == PERM_GROUP
296 ? S_IXGRP
297 : (shared_repository == PERM_EVERYBODY
298 ? (S_IXGRP|S_IXOTH)
299 : 0));
300 if (S_ISDIR(mode))
301 mode |= S_ISGID;
302 if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
303 return -2;
304 return 0;