1#include "refs.h"
2#include "cache.h"
3
4#include <errno.h>
5
6/* We allow "recursive" symbolic refs. Only within reason, though */
7#define MAXDEPTH 5
8
9#ifndef USE_SYMLINK_HEAD
10#define USE_SYMLINK_HEAD 1
11#endif
12
13int validate_symref(const char *path)
14{
15 struct stat st;
16 char *buf, buffer[256];
17 int len, fd;
18
19 if (lstat(path, &st) < 0)
20 return -1;
21
22 /* Make sure it is a "refs/.." symlink */
23 if (S_ISLNK(st.st_mode)) {
24 len = readlink(path, buffer, sizeof(buffer)-1);
25 if (len >= 5 && !memcmp("refs/", buffer, 5))
26 return 0;
27 return -1;
28 }
29
30 /*
31 * Anything else, just open it and try to see if it is a symbolic ref.
32 */
33 fd = open(path, O_RDONLY);
34 if (fd < 0)
35 return -1;
36 len = read(fd, buffer, sizeof(buffer)-1);
37 close(fd);
38
39 /*
40 * Is it a symbolic ref?
41 */
42 if (len < 4 || memcmp("ref:", buffer, 4))
43 return -1;
44 buf = buffer + 4;
45 len -= 4;
46 while (len && isspace(*buf))
47 buf++, len--;
48 if (len >= 5 && !memcmp("refs/", buf, 5))
49 return 0;
50 return -1;
51}
52
53const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
54{
55 int depth = MAXDEPTH, len;
56 char buffer[256];
57
58 for (;;) {
59 struct stat st;
60 char *buf;
61 int fd;
62
63 if (--depth < 0)
64 return NULL;
65
66 /* Special case: non-existing file.
67 * Not having the refs/heads/new-branch is OK
68 * if we are writing into it, so is .git/HEAD
69 * that points at refs/heads/master still to be
70 * born. It is NOT OK if we are resolving for
71 * reading.
72 */
73 if (lstat(path, &st) < 0) {
74 if (reading || errno != ENOENT)
75 return NULL;
76 memset(sha1, 0, 20);
77 return path;
78 }
79
80 /* Follow "normalized" - ie "refs/.." symlinks by hand */
81 if (S_ISLNK(st.st_mode)) {
82 len = readlink(path, buffer, sizeof(buffer)-1);
83 if (len >= 5 && !memcmp("refs/", buffer, 5)) {
84 path = git_path("%.*s", len, buffer);
85 continue;
86 }
87 }
88
89 /*
90 * Anything else, just open it and try to use it as
91 * a ref
92 */
93 fd = open(path, O_RDONLY);
94 if (fd < 0)
95 return NULL;
96 len = read(fd, buffer, sizeof(buffer)-1);
97 close(fd);
98
99 /*
100 * Is it a symbolic ref?
101 */
102 if (len < 4 || memcmp("ref:", buffer, 4))
103 break;
104 buf = buffer + 4;
105 len -= 4;
106 while (len && isspace(*buf))
107 buf++, len--;
108 while (len && isspace(buf[len-1]))
109 buf[--len] = 0;
110 path = git_path("%.*s", len, buf);
111 }
112 if (len < 40 || get_sha1_hex(buffer, sha1))
113 return NULL;
114 return path;
115}
116
117int create_symref(const char *git_HEAD, const char *refs_heads_master)
118{
119 const char *lockpath;
120 char ref[1000];
121 int fd, len, written;
122
123#if USE_SYMLINK_HEAD
124 if (!only_use_symrefs) {
125 unlink(git_HEAD);
126 if (!symlink(refs_heads_master, git_HEAD))
127 return 0;
128 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
129 }
130#endif
131
132 len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
133 if (sizeof(ref) <= len) {
134 error("refname too long: %s", refs_heads_master);
135 return -1;
136 }
137 lockpath = mkpath("%s.lock", git_HEAD);
138 fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
139 written = write(fd, ref, len);
140 close(fd);
141 if (written != len) {
142 unlink(lockpath);
143 error("Unable to write to %s", lockpath);
144 return -2;
145 }
146 if (rename(lockpath, git_HEAD) < 0) {
147 unlink(lockpath);
148 error("Unable to create %s", git_HEAD);
149 return -3;
150 }
151 return 0;
152}
153
154int read_ref(const char *filename, unsigned char *sha1)
155{
156 if (resolve_ref(filename, sha1, 1))
157 return 0;
158 return -1;
159}
160
161static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
162{
163 int retval = 0;
164 DIR *dir = opendir(git_path("%s", base));
165
166 if (dir) {
167 struct dirent *de;
168 int baselen = strlen(base);
169 char *path = xmalloc(baselen + 257);
170
171 if (!strncmp(base, "./", 2)) {
172 base += 2;
173 baselen -= 2;
174 }
175 memcpy(path, base, baselen);
176 if (baselen && base[baselen-1] != '/')
177 path[baselen++] = '/';
178
179 while ((de = readdir(dir)) != NULL) {
180 unsigned char sha1[20];
181 struct stat st;
182 int namelen;
183
184 if (de->d_name[0] == '.')
185 continue;
186 namelen = strlen(de->d_name);
187 if (namelen > 255)
188 continue;
189 memcpy(path + baselen, de->d_name, namelen+1);
190 if (stat(git_path("%s", path), &st) < 0)
191 continue;
192 if (S_ISDIR(st.st_mode)) {
193 retval = do_for_each_ref(path, fn);
194 if (retval)
195 break;
196 continue;
197 }
198 if (read_ref(git_path("%s", path), sha1) < 0)
199 continue;
200 if (!has_sha1_file(sha1))
201 continue;
202 retval = fn(path, sha1);
203 if (retval)
204 break;
205 }
206 free(path);
207 closedir(dir);
208 }
209 return retval;
210}
211
212int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
213{
214 unsigned char sha1[20];
215 if (!read_ref(git_path("HEAD"), sha1))
216 return fn("HEAD", sha1);
217 return 0;
218}
219
220int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
221{
222 return do_for_each_ref("refs", fn);
223}
224
225static char *ref_file_name(const char *ref)
226{
227 char *base = get_refs_directory();
228 int baselen = strlen(base);
229 int reflen = strlen(ref);
230 char *ret = xmalloc(baselen + 2 + reflen);
231 sprintf(ret, "%s/%s", base, ref);
232 return ret;
233}
234
235static char *ref_lock_file_name(const char *ref)
236{
237 char *base = get_refs_directory();
238 int baselen = strlen(base);
239 int reflen = strlen(ref);
240 char *ret = xmalloc(baselen + 7 + reflen);
241 sprintf(ret, "%s/%s.lock", base, ref);
242 return ret;
243}
244
245int get_ref_sha1(const char *ref, unsigned char *sha1)
246{
247 const char *filename;
248
249 if (check_ref_format(ref))
250 return -1;
251 filename = git_path("refs/%s", ref);
252 return read_ref(filename, sha1);
253}
254
255static int lock_ref_file(const char *filename, const char *lock_filename,
256 const unsigned char *old_sha1)
257{
258 int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
259 unsigned char current_sha1[20];
260 int retval;
261 if (fd < 0) {
262 return error("Couldn't open lock file for %s: %s",
263 filename, strerror(errno));
264 }
265 retval = read_ref(filename, current_sha1);
266 if (old_sha1) {
267 if (retval) {
268 close(fd);
269 unlink(lock_filename);
270 return error("Could not read the current value of %s",
271 filename);
272 }
273 if (memcmp(current_sha1, old_sha1, 20)) {
274 close(fd);
275 unlink(lock_filename);
276 error("The current value of %s is %s",
277 filename, sha1_to_hex(current_sha1));
278 return error("Expected %s",
279 sha1_to_hex(old_sha1));
280 }
281 } else {
282 if (!retval) {
283 close(fd);
284 unlink(lock_filename);
285 return error("Unexpectedly found a value of %s for %s",
286 sha1_to_hex(current_sha1), filename);
287 }
288 }
289 return fd;
290}
291
292int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
293{
294 char *filename;
295 char *lock_filename;
296 int retval;
297 if (check_ref_format(ref))
298 return -1;
299 filename = ref_file_name(ref);
300 lock_filename = ref_lock_file_name(ref);
301 retval = lock_ref_file(filename, lock_filename, old_sha1);
302 free(filename);
303 free(lock_filename);
304 return retval;
305}
306
307static int write_ref_file(const char *filename,
308 const char *lock_filename, int fd,
309 const unsigned char *sha1)
310{
311 char *hex = sha1_to_hex(sha1);
312 char term = '\n';
313 if (write(fd, hex, 40) < 40 ||
314 write(fd, &term, 1) < 1) {
315 error("Couldn't write %s\n", filename);
316 close(fd);
317 return -1;
318 }
319 close(fd);
320 rename(lock_filename, filename);
321 return 0;
322}
323
324int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
325{
326 char *filename;
327 char *lock_filename;
328 int retval;
329 if (fd < 0)
330 return -1;
331 if (check_ref_format(ref))
332 return -1;
333 filename = ref_file_name(ref);
334 lock_filename = ref_lock_file_name(ref);
335 retval = write_ref_file(filename, lock_filename, fd, sha1);
336 free(filename);
337 free(lock_filename);
338 return retval;
339}
340
341/*
342 * Make sure "ref" is something reasonable to have under ".git/refs/";
343 * We do not like it if:
344 *
345 * - any path component of it begins with ".", or
346 * - it has double dots "..", or
347 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
348 * - it ends with a "/".
349 */
350
351static inline int bad_ref_char(int ch)
352{
353 return (((unsigned) ch) <= ' ' ||
354 ch == '~' || ch == '^' || ch == ':');
355}
356
357int check_ref_format(const char *ref)
358{
359 int ch, level;
360 const char *cp = ref;
361
362 level = 0;
363 while (1) {
364 while ((ch = *cp++) == '/')
365 ; /* tolerate duplicated slashes */
366 if (!ch)
367 return -1; /* should not end with slashes */
368
369 /* we are at the beginning of the path component */
370 if (ch == '.' || bad_ref_char(ch))
371 return -1;
372
373 /* scan the rest of the path component */
374 while ((ch = *cp++) != 0) {
375 if (bad_ref_char(ch))
376 return -1;
377 if (ch == '/')
378 break;
379 if (ch == '.' && *cp == '.')
380 return -1;
381 }
382 level++;
383 if (!ch) {
384 if (level < 2)
385 return -1; /* at least of form "heads/blah" */
386 return 0;
387 }
388 }
389}
390
391int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
392{
393 char *filename;
394 char *lock_filename;
395 int fd;
396 int retval;
397 if (check_ref_format(ref))
398 return -1;
399 filename = ref_file_name(ref);
400 lock_filename = ref_lock_file_name(ref);
401 fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
402 if (fd < 0) {
403 error("Writing %s", lock_filename);
404 perror("Open");
405 }
406 retval = write_ref_file(filename, lock_filename, fd, sha1);
407 free(filename);
408 free(lock_filename);
409 return retval;
410}