2 * I'm tired of doing "vsnprintf()" etc just to open a
3 * file, so here's a "return static buffer with printf"
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.
16 static char pathname[PATH_MAX];
17 static char bad_path[] = "/bad-path/";
19 static char *cleanup_path(char *path)
22 if (!memcmp(path, "./", 2)) {
30 char *mkpath(const char *fmt, ...)
36 len = vsnprintf(pathname, PATH_MAX, fmt, args);
40 return cleanup_path(pathname);
43 char *git_path(const char *fmt, ...)
45 const char *git_dir = get_git_dir();
49 len = strlen(git_dir);
50 if (len > PATH_MAX-100)
52 memcpy(pathname, git_dir, len);
53 if (len && git_dir[len-1] != '/')
54 pathname[len++] = '/';
56 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
60 return cleanup_path(pathname);
64 /* git_mkstemp() - create tmp file honoring TMPDIR variable */
65 int git_mkstemp(char *path, size_t len, const char *template)
67 char *env, *pch = path;
69 if ((env = getenv("TMPDIR")) == NULL) {
74 size_t n = snprintf(pch, len, "%s/", env);
80 safe_strncpy(pch, template, len);
86 size_t safe_strncpy(char *dest, const char *src, size_t size)
88 size_t ret = strlen(src);
91 size_t len = (ret >= size) ? size - 1 : ret;
92 memcpy(dest, src, len);
99 int validate_symref(const char *path)
102 char *buf, buffer[256];
105 if (lstat(path, &st) < 0)
108 /* Make sure it is a "refs/.." symlink */
109 if (S_ISLNK(st.st_mode)) {
110 len = readlink(path, buffer, sizeof(buffer)-1);
111 if (len >= 5 && !memcmp("refs/", buffer, 5))
117 * Anything else, just open it and try to see if it is a symbolic ref.
119 fd = open(path, O_RDONLY);
122 len = read(fd, buffer, sizeof(buffer)-1);
126 * Is it a symbolic ref?
128 if (len < 4 || memcmp("ref:", buffer, 4))
132 while (len && isspace(*buf))
134 if (len >= 5 && !memcmp("refs/", buf, 5))
139 static char *user_path(char *buf, char *path, int sz)
145 if (!path || path[0] != '~')
148 slash = strchr(path, '/');
149 if (path[0] == '/' || !path[0]) {
150 pw = getpwuid(getuid());
161 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
163 baselen = strlen(pw->pw_dir);
164 memcpy(buf, pw->pw_dir, baselen);
165 while ((1 < baselen) && (buf[baselen-1] == '/')) {
169 if (slash && slash[1]) {
171 if (sz <= baselen + len)
173 memcpy(buf + baselen, slash, len + 1);
179 * First, one directory to try is determined by the following algorithm.
181 * (0) If "strict" is given, the path is used as given and no DWIM is
183 * (1) "~/path" to mean path under the running user's home directory;
184 * (2) "~user/path" to mean path under named user's home directory;
185 * (3) "relative/path" to mean cwd relative directory; or
186 * (4) "/absolute/path" to mean absolute directory.
188 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
189 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
192 * Second, we try chdir() to that. Upon failure, we return NULL.
194 * Then, we try if the current directory is a valid git repository.
195 * Upon failure, we return NULL.
197 * If all goes well, we return the directory we used to chdir() (but
198 * before ~user is expanded), avoiding getcwd() resolving symbolic
199 * links. User relative paths are also returned as they are given,
200 * except DWIM suffixing.
202 char *enter_repo(char *path, int strict)
204 static char used_path[PATH_MAX];
205 static char validated_path[PATH_MAX];
211 static const char *suffix[] = {
212 ".git/.git", "/.git", ".git", "", NULL,
214 int len = strlen(path);
216 while ((1 < len) && (path[len-1] == '/')) {
222 if (path[0] == '~') {
223 if (!user_path(used_path, path, PATH_MAX))
225 strcpy(validated_path, path);
228 else if (PATH_MAX - 10 < len)
231 path = strcpy(used_path, path);
232 strcpy(validated_path, path);
235 for (i = 0; suffix[i]; i++) {
236 strcpy(path + len, suffix[i]);
237 if (!access(path, F_OK)) {
238 strcat(validated_path, suffix[i]);
242 if (!suffix[i] || chdir(path))
244 path = validated_path;
246 else if (chdir(path))
249 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
250 validate_symref("HEAD") == 0) {
252 check_repository_format();
259 int adjust_shared_perm(const char *path)
264 if (!shared_repository)
266 if (lstat(path, &st) < 0)
270 mode |= (shared_repository == PERM_GROUP
272 : (shared_repository == PERM_EVERYBODY
280 mode |= (shared_repository == PERM_GROUP
282 : (shared_repository == PERM_EVERYBODY
287 if (chmod(path, mode) < 0)