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