Fix "do not DWIM" patch to enter_repo"
[git/jnareb-git.git] / path.c
blob4d889473a78a2c6fa38a0b54a48fe55bd83e9b02
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"
14 #include <pwd.h>
16 static char pathname[PATH_MAX];
17 static char bad_path[] = "/bad-path/";
19 static char *cleanup_path(char *path)
21 /* Clean it up */
22 if (!memcmp(path, "./", 2)) {
23 path += 2;
24 while (*path == '/')
25 path++;
27 return path;
30 char *mkpath(const char *fmt, ...)
32 va_list args;
33 unsigned len;
35 va_start(args, fmt);
36 len = vsnprintf(pathname, PATH_MAX, fmt, args);
37 va_end(args);
38 if (len >= PATH_MAX)
39 return bad_path;
40 return cleanup_path(pathname);
43 char *git_path(const char *fmt, ...)
45 const char *git_dir = get_git_dir();
46 va_list args;
47 unsigned len;
49 len = strlen(git_dir);
50 if (len > PATH_MAX-100)
51 return bad_path;
52 memcpy(pathname, git_dir, len);
53 if (len && git_dir[len-1] != '/')
54 pathname[len++] = '/';
55 va_start(args, fmt);
56 len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
57 va_end(args);
58 if (len >= PATH_MAX)
59 return bad_path;
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) {
70 strcpy(pch, "/tmp/");
71 len -= 5;
72 pch += 5;
73 } else {
74 size_t n = snprintf(pch, len, "%s/", env);
76 len -= n;
77 pch += n;
80 safe_strncpy(pch, template, len);
82 return mkstemp(path);
86 char *safe_strncpy(char *dest, const char *src, size_t n)
88 strncpy(dest, src, n);
89 dest[n - 1] = '\0';
91 return dest;
94 int validate_symref(const char *path)
96 struct stat st;
97 char *buf, buffer[256];
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(fd, buffer, sizeof(buffer)-1);
118 close(fd);
121 * Is it a symbolic ref?
123 if (len < 4 || memcmp("ref:", buffer, 4))
124 return -1;
125 buf = buffer + 4;
126 len -= 4;
127 while (len && isspace(*buf))
128 buf++, len--;
129 if (len >= 5 && !memcmp("refs/", buf, 5))
130 return 0;
131 return -1;
134 static char *current_dir(void)
136 return getcwd(pathname, sizeof(pathname));
139 static int user_chdir(char *path)
141 char *dir = path;
143 if(*dir == '~') { /* user-relative path */
144 struct passwd *pw;
145 char *slash = strchr(dir, '/');
147 dir++;
148 /* '~/' and '~' (no slash) means users own home-dir */
149 if(!*dir || *dir == '/')
150 pw = getpwuid(getuid());
151 else {
152 if (slash) {
153 *slash = '\0';
154 pw = getpwnam(dir);
155 *slash = '/';
157 else
158 pw = getpwnam(dir);
161 /* make sure we got something back that we can chdir() to */
162 if(!pw || chdir(pw->pw_dir) < 0)
163 return -1;
165 if(!slash || !slash[1]) /* no path following username */
166 return 0;
168 dir = slash + 1;
171 /* ~foo/path/to/repo is now path/to/repo and we're in foo's homedir */
172 if(chdir(dir) < 0)
173 return -1;
175 return 0;
178 char *enter_repo(char *path, int strict)
180 if(!path)
181 return NULL;
183 if (strict) {
184 if (chdir(path) < 0)
185 return NULL;
187 else {
188 if (!*path)
189 ; /* happy -- no chdir */
190 else if (!user_chdir(path))
191 ; /* happy -- as given */
192 else if (!user_chdir(mkpath("%s.git", path)))
193 ; /* happy -- uemacs --> uemacs.git */
194 else
195 return NULL;
196 (void)chdir(".git");
199 if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
200 validate_symref("HEAD") == 0) {
201 putenv("GIT_DIR=.");
202 return current_dir();
205 return NULL;