Remove pseudo SIG* definitions
[git/pclouds.git] / path.c
blob8a06cf76187ec682917b8feb48f744222affc714
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 /* on Windows it is TMP and TEMP */
78 (env = getenv("TMP")) == NULL &&
79 (env = getenv("TEMP")) == NULL) {
80 strcpy(pch, "/tmp/");
81 len -= 5;
82 pch += 5;
83 } else {
84 size_t n = snprintf(pch, len, "%s/", env);
86 len -= n;
87 pch += n;
90 strlcpy(pch, template, len);
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 #ifndef NO_ETC_PASSWD
148 static char *user_path(char *buf, char *path, int sz)
150 struct passwd *pw;
151 char *slash;
152 int len, baselen;
154 if (!path || path[0] != '~')
155 return NULL;
156 path++;
157 slash = strchr(path, '/');
158 if (path[0] == '/' || !path[0]) {
159 pw = getpwuid(getuid());
161 else {
162 if (slash) {
163 *slash = 0;
164 pw = getpwnam(path);
165 *slash = '/';
167 else
168 pw = getpwnam(path);
170 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
171 return NULL;
172 baselen = strlen(pw->pw_dir);
173 memcpy(buf, pw->pw_dir, baselen);
174 while ((1 < baselen) && (buf[baselen-1] == '/')) {
175 buf[baselen-1] = 0;
176 baselen--;
178 if (slash && slash[1]) {
179 len = strlen(slash);
180 if (sz <= baselen + len)
181 return NULL;
182 memcpy(buf + baselen, slash, len + 1);
184 return buf;
187 #else
189 static char *user_path(char *buf, char *path, int sz)
191 return getenv("HOME");
194 #endif
197 * First, one directory to try is determined by the following algorithm.
199 * (0) If "strict" is given, the path is used as given and no DWIM is
200 * done. Otherwise:
201 * (1) "~/path" to mean path under the running user's home directory;
202 * (2) "~user/path" to mean path under named user's home directory;
203 * (3) "relative/path" to mean cwd relative directory; or
204 * (4) "/absolute/path" to mean absolute directory.
206 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
207 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
208 * what we try.
210 * Second, we try chdir() to that. Upon failure, we return NULL.
212 * Then, we try if the current directory is a valid git repository.
213 * Upon failure, we return NULL.
215 * If all goes well, we return the directory we used to chdir() (but
216 * before ~user is expanded), avoiding getcwd() resolving symbolic
217 * links. User relative paths are also returned as they are given,
218 * except DWIM suffixing.
220 char *enter_repo(char *path, int strict)
222 static char used_path[PATH_MAX];
223 static char validated_path[PATH_MAX];
225 if (!path)
226 return NULL;
228 if (!strict) {
229 static const char *suffix[] = {
230 ".git/.git", "/.git", ".git", "", NULL,
232 int len = strlen(path);
233 int i;
234 while ((1 < len) && (path[len-1] == '/')) {
235 path[len-1] = 0;
236 len--;
238 if (PATH_MAX <= len)
239 return NULL;
240 if (path[0] == '~') {
241 if (!user_path(used_path, path, PATH_MAX))
242 return NULL;
243 strcpy(validated_path, path);
244 path = used_path;
246 else if (PATH_MAX - 10 < len)
247 return NULL;
248 else {
249 path = strcpy(used_path, path);
250 strcpy(validated_path, path);
252 len = strlen(path);
253 for (i = 0; suffix[i]; i++) {
254 strcpy(path + len, suffix[i]);
255 if (!access(path, F_OK)) {
256 strcat(validated_path, suffix[i]);
257 break;
260 if (!suffix[i] || chdir(path))
261 return NULL;
262 path = validated_path;
264 else if (chdir(path))
265 return NULL;
267 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
268 validate_headref("HEAD") == 0) {
269 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
270 check_repository_format();
271 return path;
274 return NULL;
277 int adjust_shared_perm(const char *path)
279 struct stat st;
280 int mode;
282 if (!shared_repository)
283 return 0;
284 if (lstat(path, &st) < 0)
285 return -1;
286 mode = st.st_mode;
287 if (mode & S_IRUSR)
288 mode |= (shared_repository == PERM_GROUP
289 ? S_IRGRP
290 : (shared_repository == PERM_EVERYBODY
291 ? (S_IRGRP|S_IROTH)
292 : 0));
294 if (mode & S_IWUSR)
295 mode |= S_IWGRP;
297 if (mode & S_IXUSR)
298 mode |= (shared_repository == PERM_GROUP
299 ? S_IXGRP
300 : (shared_repository == PERM_EVERYBODY
301 ? (S_IXGRP|S_IXOTH)
302 : 0));
303 if (S_ISDIR(mode))
304 mode |= S_ISGID;
305 if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
306 return -2;
307 return 0;