]>
Commit | Line | Data |
---|---|---|
26c8a533 LT |
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. | |
5 | * | |
6 | * It's obviously not thread-safe. Sue me. But it's quite | |
7 | * useful for doing things like | |
8 | * | |
9 | * f = open(mkpath("%s/%s.git", base, name), O_RDONLY); | |
10 | * | |
11 | * which is what it's designed for. | |
12 | */ | |
13 | #include "cache.h" | |
14 | ||
26c8a533 LT |
15 | static char bad_path[] = "/bad-path/"; |
16 | ||
e7676d2f LT |
17 | static char *get_pathname(void) |
18 | { | |
19 | static char pathname_array[4][PATH_MAX]; | |
20 | static int index; | |
21 | return pathname_array[3 & ++index]; | |
22 | } | |
23 | ||
26c8a533 LT |
24 | static char *cleanup_path(char *path) |
25 | { | |
26 | /* Clean it up */ | |
27 | if (!memcmp(path, "./", 2)) { | |
28 | path += 2; | |
29 | while (*path == '/') | |
30 | path++; | |
31 | } | |
32 | return path; | |
33 | } | |
34 | ||
35 | char *mkpath(const char *fmt, ...) | |
36 | { | |
37 | va_list args; | |
38 | unsigned len; | |
e7676d2f | 39 | char *pathname = get_pathname(); |
26c8a533 LT |
40 | |
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); | |
47 | } | |
48 | ||
49 | char *git_path(const char *fmt, ...) | |
50 | { | |
5da1606d | 51 | const char *git_dir = get_git_dir(); |
e7676d2f | 52 | char *pathname = get_pathname(); |
26c8a533 LT |
53 | va_list args; |
54 | unsigned len; | |
55 | ||
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); | |
68 | } | |
f2db68ed HE |
69 | |
70 | ||
71 | /* git_mkstemp() - create tmp file honoring TMPDIR variable */ | |
72 | int git_mkstemp(char *path, size_t len, const char *template) | |
73 | { | |
74 | char *env, *pch = path; | |
75 | ||
76 | if ((env = getenv("TMPDIR")) == NULL) { | |
77 | strcpy(pch, "/tmp/"); | |
78 | len -= 5; | |
35c3c629 HE |
79 | pch += 5; |
80 | } else { | |
81 | size_t n = snprintf(pch, len, "%s/", env); | |
82 | ||
83 | len -= n; | |
84 | pch += n; | |
85 | } | |
f2db68ed | 86 | |
817151e6 | 87 | strlcpy(pch, template, len); |
f2db68ed HE |
88 | |
89 | return mkstemp(path); | |
90 | } | |
91 | ||
92 | ||
c847f537 | 93 | int validate_headref(const char *path) |
0870ca7f JH |
94 | { |
95 | struct stat st; | |
96 | char *buf, buffer[256]; | |
c847f537 | 97 | unsigned char sha1[20]; |
0870ca7f JH |
98 | int len, fd; |
99 | ||
100 | if (lstat(path, &st) < 0) | |
101 | return -1; | |
102 | ||
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; | |
109 | } | |
110 | ||
111 | /* | |
112 | * Anything else, just open it and try to see if it is a symbolic ref. | |
113 | */ | |
114 | fd = open(path, O_RDONLY); | |
115 | if (fd < 0) | |
116 | return -1; | |
93d26e4c | 117 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
0870ca7f JH |
118 | close(fd); |
119 | ||
120 | /* | |
121 | * Is it a symbolic ref? | |
122 | */ | |
c847f537 | 123 | if (len < 4) |
0870ca7f | 124 | return -1; |
c847f537 JH |
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; | |
132 | } | |
133 | ||
134 | /* | |
135 | * Is this a detached HEAD? | |
136 | */ | |
137 | if (!get_sha1_hex(buffer, sha1)) | |
0870ca7f | 138 | return 0; |
c847f537 | 139 | |
0870ca7f JH |
140 | return -1; |
141 | } | |
142 | ||
d79374c7 | 143 | static char *user_path(char *buf, char *path, int sz) |
54f4b874 | 144 | { |
d79374c7 JH |
145 | struct passwd *pw; |
146 | char *slash; | |
147 | int len, baselen; | |
54f4b874 | 148 | |
d79374c7 JH |
149 | if (!path || path[0] != '~') |
150 | return NULL; | |
151 | path++; | |
152 | slash = strchr(path, '/'); | |
153 | if (path[0] == '/' || !path[0]) { | |
154 | pw = getpwuid(getuid()); | |
155 | } | |
156 | else { | |
157 | if (slash) { | |
158 | *slash = 0; | |
159 | pw = getpwnam(path); | |
160 | *slash = '/'; | |
54f4b874 | 161 | } |
d79374c7 JH |
162 | else |
163 | pw = getpwnam(path); | |
54f4b874 | 164 | } |
d79374c7 JH |
165 | if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir)) |
166 | return NULL; | |
167 | baselen = strlen(pw->pw_dir); | |
168 | memcpy(buf, pw->pw_dir, baselen); | |
169 | while ((1 < baselen) && (buf[baselen-1] == '/')) { | |
170 | buf[baselen-1] = 0; | |
171 | baselen--; | |
172 | } | |
173 | if (slash && slash[1]) { | |
174 | len = strlen(slash); | |
175 | if (sz <= baselen + len) | |
176 | return NULL; | |
177 | memcpy(buf + baselen, slash, len + 1); | |
178 | } | |
179 | return buf; | |
54f4b874 AE |
180 | } |
181 | ||
d79374c7 JH |
182 | /* |
183 | * First, one directory to try is determined by the following algorithm. | |
184 | * | |
185 | * (0) If "strict" is given, the path is used as given and no DWIM is | |
186 | * done. Otherwise: | |
187 | * (1) "~/path" to mean path under the running user's home directory; | |
188 | * (2) "~user/path" to mean path under named user's home directory; | |
189 | * (3) "relative/path" to mean cwd relative directory; or | |
190 | * (4) "/absolute/path" to mean absolute directory. | |
191 | * | |
192 | * Unless "strict" is given, we try access() for existence of "%s.git/.git", | |
193 | * "%s/.git", "%s.git", "%s" in this order. The first one that exists is | |
194 | * what we try. | |
195 | * | |
196 | * Second, we try chdir() to that. Upon failure, we return NULL. | |
197 | * | |
198 | * Then, we try if the current directory is a valid git repository. | |
199 | * Upon failure, we return NULL. | |
200 | * | |
201 | * If all goes well, we return the directory we used to chdir() (but | |
202 | * before ~user is expanded), avoiding getcwd() resolving symbolic | |
203 | * links. User relative paths are also returned as they are given, | |
204 | * except DWIM suffixing. | |
205 | */ | |
54f4b874 AE |
206 | char *enter_repo(char *path, int strict) |
207 | { | |
d79374c7 JH |
208 | static char used_path[PATH_MAX]; |
209 | static char validated_path[PATH_MAX]; | |
210 | ||
211 | if (!path) | |
54f4b874 AE |
212 | return NULL; |
213 | ||
d79374c7 JH |
214 | if (!strict) { |
215 | static const char *suffix[] = { | |
216 | ".git/.git", "/.git", ".git", "", NULL, | |
217 | }; | |
218 | int len = strlen(path); | |
219 | int i; | |
220 | while ((1 < len) && (path[len-1] == '/')) { | |
221 | path[len-1] = 0; | |
222 | len--; | |
223 | } | |
224 | if (PATH_MAX <= len) | |
54f4b874 | 225 | return NULL; |
d79374c7 JH |
226 | if (path[0] == '~') { |
227 | if (!user_path(used_path, path, PATH_MAX)) | |
228 | return NULL; | |
229 | strcpy(validated_path, path); | |
230 | path = used_path; | |
231 | } | |
232 | else if (PATH_MAX - 10 < len) | |
233 | return NULL; | |
234 | else { | |
235 | path = strcpy(used_path, path); | |
236 | strcpy(validated_path, path); | |
237 | } | |
238 | len = strlen(path); | |
239 | for (i = 0; suffix[i]; i++) { | |
240 | strcpy(path + len, suffix[i]); | |
241 | if (!access(path, F_OK)) { | |
242 | strcat(validated_path, suffix[i]); | |
243 | break; | |
244 | } | |
245 | } | |
246 | if (!suffix[i] || chdir(path)) | |
0870ca7f | 247 | return NULL; |
d79374c7 | 248 | path = validated_path; |
0870ca7f | 249 | } |
d79374c7 JH |
250 | else if (chdir(path)) |
251 | return NULL; | |
54f4b874 | 252 | |
d79374c7 | 253 | if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && |
c847f537 | 254 | validate_headref("HEAD") == 0) { |
3a55602e | 255 | setenv("GIT_DIR", ".", 1); |
1644162a | 256 | check_repository_format(); |
d79374c7 | 257 | return path; |
54f4b874 AE |
258 | } |
259 | ||
260 | return NULL; | |
261 | } | |
138086a7 JH |
262 | |
263 | int adjust_shared_perm(const char *path) | |
264 | { | |
265 | struct stat st; | |
266 | int mode; | |
267 | ||
268 | if (!shared_repository) | |
269 | return 0; | |
270 | if (lstat(path, &st) < 0) | |
271 | return -1; | |
272 | mode = st.st_mode; | |
273 | if (mode & S_IRUSR) | |
94df2506 JH |
274 | mode |= (shared_repository == PERM_GROUP |
275 | ? S_IRGRP | |
276 | : (shared_repository == PERM_EVERYBODY | |
277 | ? (S_IRGRP|S_IROTH) | |
278 | : 0)); | |
279 | ||
138086a7 JH |
280 | if (mode & S_IWUSR) |
281 | mode |= S_IWGRP; | |
94df2506 | 282 | |
138086a7 | 283 | if (mode & S_IXUSR) |
94df2506 JH |
284 | mode |= (shared_repository == PERM_GROUP |
285 | ? S_IXGRP | |
286 | : (shared_repository == PERM_EVERYBODY | |
287 | ? (S_IXGRP|S_IXOTH) | |
288 | : 0)); | |
138086a7 JH |
289 | if (S_ISDIR(mode)) |
290 | mode |= S_ISGID; | |
fe732ede | 291 | if ((mode & st.st_mode) != mode && chmod(path, mode) < 0) |
138086a7 JH |
292 | return -2; |
293 | return 0; | |
294 | } |