Do not drop data from '\0' until eol in patch output
[git/jnareb-git.git] / path.c
blobdb8905f3c352592124b2f0bda18fc0ccd048528e
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 strlcpy(pch, template, len);
82 return mkstemp(path);
86 int validate_symref(const char *path)
88 struct stat st;
89 char *buf, buffer[256];
90 int len, fd;
92 if (lstat(path, &st) < 0)
93 return -1;
95 /* Make sure it is a "refs/.." symlink */
96 if (S_ISLNK(st.st_mode)) {
97 len = readlink(path, buffer, sizeof(buffer)-1);
98 if (len >= 5 && !memcmp("refs/", buffer, 5))
99 return 0;
100 return -1;
104 * Anything else, just open it and try to see if it is a symbolic ref.
106 fd = open(path, O_RDONLY);
107 if (fd < 0)
108 return -1;
109 len = read(fd, buffer, sizeof(buffer)-1);
110 close(fd);
113 * Is it a symbolic ref?
115 if (len < 4 || memcmp("ref:", buffer, 4))
116 return -1;
117 buf = buffer + 4;
118 len -= 4;
119 while (len && isspace(*buf))
120 buf++, len--;
121 if (len >= 5 && !memcmp("refs/", buf, 5))
122 return 0;
123 return -1;
126 static char *user_path(char *buf, char *path, int sz)
128 struct passwd *pw;
129 char *slash;
130 int len, baselen;
132 if (!path || path[0] != '~')
133 return NULL;
134 path++;
135 slash = strchr(path, '/');
136 if (path[0] == '/' || !path[0]) {
137 pw = getpwuid(getuid());
139 else {
140 if (slash) {
141 *slash = 0;
142 pw = getpwnam(path);
143 *slash = '/';
145 else
146 pw = getpwnam(path);
148 if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir))
149 return NULL;
150 baselen = strlen(pw->pw_dir);
151 memcpy(buf, pw->pw_dir, baselen);
152 while ((1 < baselen) && (buf[baselen-1] == '/')) {
153 buf[baselen-1] = 0;
154 baselen--;
156 if (slash && slash[1]) {
157 len = strlen(slash);
158 if (sz <= baselen + len)
159 return NULL;
160 memcpy(buf + baselen, slash, len + 1);
162 return buf;
166 * First, one directory to try is determined by the following algorithm.
168 * (0) If "strict" is given, the path is used as given and no DWIM is
169 * done. Otherwise:
170 * (1) "~/path" to mean path under the running user's home directory;
171 * (2) "~user/path" to mean path under named user's home directory;
172 * (3) "relative/path" to mean cwd relative directory; or
173 * (4) "/absolute/path" to mean absolute directory.
175 * Unless "strict" is given, we try access() for existence of "%s.git/.git",
176 * "%s/.git", "%s.git", "%s" in this order. The first one that exists is
177 * what we try.
179 * Second, we try chdir() to that. Upon failure, we return NULL.
181 * Then, we try if the current directory is a valid git repository.
182 * Upon failure, we return NULL.
184 * If all goes well, we return the directory we used to chdir() (but
185 * before ~user is expanded), avoiding getcwd() resolving symbolic
186 * links. User relative paths are also returned as they are given,
187 * except DWIM suffixing.
189 char *enter_repo(char *path, int strict)
191 static char used_path[PATH_MAX];
192 static char validated_path[PATH_MAX];
194 if (!path)
195 return NULL;
197 if (!strict) {
198 static const char *suffix[] = {
199 ".git/.git", "/.git", ".git", "", NULL,
201 int len = strlen(path);
202 int i;
203 while ((1 < len) && (path[len-1] == '/')) {
204 path[len-1] = 0;
205 len--;
207 if (PATH_MAX <= len)
208 return NULL;
209 if (path[0] == '~') {
210 if (!user_path(used_path, path, PATH_MAX))
211 return NULL;
212 strcpy(validated_path, path);
213 path = used_path;
215 else if (PATH_MAX - 10 < len)
216 return NULL;
217 else {
218 path = strcpy(used_path, path);
219 strcpy(validated_path, path);
221 len = strlen(path);
222 for (i = 0; suffix[i]; i++) {
223 strcpy(path + len, suffix[i]);
224 if (!access(path, F_OK)) {
225 strcat(validated_path, suffix[i]);
226 break;
229 if (!suffix[i] || chdir(path))
230 return NULL;
231 path = validated_path;
233 else if (chdir(path))
234 return NULL;
236 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
237 validate_symref("HEAD") == 0) {
238 putenv("GIT_DIR=.");
239 check_repository_format();
240 return path;
243 return NULL;
246 int adjust_shared_perm(const char *path)
248 struct stat st;
249 int mode;
251 if (!shared_repository)
252 return 0;
253 if (lstat(path, &st) < 0)
254 return -1;
255 mode = st.st_mode;
256 if (mode & S_IRUSR)
257 mode |= (shared_repository == PERM_GROUP
258 ? S_IRGRP
259 : (shared_repository == PERM_EVERYBODY
260 ? (S_IRGRP|S_IROTH)
261 : 0));
263 if (mode & S_IWUSR)
264 mode |= S_IWGRP;
266 if (mode & S_IXUSR)
267 mode |= (shared_repository == PERM_GROUP
268 ? S_IXGRP
269 : (shared_repository == PERM_EVERYBODY
270 ? (S_IXGRP|S_IXOTH)
271 : 0));
272 if (S_ISDIR(mode))
273 mode |= S_ISGID;
274 if (chmod(path, mode) < 0)
275 return -2;
276 return 0;