1#include "refs.h"
2#include "cache.h"
34
#include <errno.h>
56
static int read_ref(const char *path, unsigned char *sha1)
7{
8int ret = -1;
9int fd = open(path, O_RDONLY);
1011
if (fd >= 0) {
12char buffer[60];
13if (read(fd, buffer, sizeof(buffer)) >= 40)
14ret = get_sha1_hex(buffer, sha1);
15close(fd);
16}
17return ret;
18}
1920
static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
21{
22int retval = 0;
23DIR *dir = opendir(base);
2425
if (dir) {
26struct dirent *de;
27int baselen = strlen(base);
28char *path = xmalloc(baselen + 257);
2930
if (!strncmp(base, "./", 2)) {
31base += 2;
32baselen -= 2;
33}
34memcpy(path, base, baselen);
35if (baselen && base[baselen-1] != '/')
36path[baselen++] = '/';
3738
while ((de = readdir(dir)) != NULL) {
39unsigned char sha1[20];
40struct stat st;
41int namelen;
4243
if (de->d_name[0] == '.')
44continue;
45namelen = strlen(de->d_name);
46if (namelen > 255)
47continue;
48memcpy(path + baselen, de->d_name, namelen+1);
49if (lstat(path, &st) < 0)
50continue;
51if (S_ISDIR(st.st_mode)) {
52retval = do_for_each_ref(path, fn);
53if (retval)
54break;
55continue;
56}
57if (read_ref(path, sha1) < 0)
58continue;
59if (!has_sha1_file(sha1))
60continue;
61retval = fn(path, sha1);
62if (retval)
63break;
64}
65free(path);
66closedir(dir);
67}
68return retval;
69}
7071
int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
72{
73return do_for_each_ref(get_refs_directory(), fn);
74}
7576
static char *ref_file_name(const char *ref)
77{
78char *base = get_refs_directory();
79int baselen = strlen(base);
80int reflen = strlen(ref);
81char *ret = xmalloc(baselen + 2 + reflen);
82sprintf(ret, "%s/%s", base, ref);
83return ret;
84}
8586
static char *ref_lock_file_name(const char *ref)
87{
88char *base = get_refs_directory();
89int baselen = strlen(base);
90int reflen = strlen(ref);
91char *ret = xmalloc(baselen + 7 + reflen);
92sprintf(ret, "%s/%s.lock", base, ref);
93return ret;
94}
9596
static int read_ref_file(const char *filename, unsigned char *sha1) {
97int fd = open(filename, O_RDONLY);
98char hex[41];
99if (fd < 0) {
100return error("Couldn't open %s\n", filename);
101}
102if ((read(fd, hex, 41) < 41) ||
103(hex[40] != '\n') ||
104get_sha1_hex(hex, sha1)) {
105error("Couldn't read a hash from %s\n", filename);
106close(fd);
107return -1;
108}
109close(fd);
110return 0;
111}
112113
int get_ref_sha1(const char *ref, unsigned char *sha1)
114{
115char *filename;
116int retval;
117if (check_ref_format(ref))
118return -1;
119filename = ref_file_name(ref);
120retval = read_ref_file(filename, sha1);
121free(filename);
122return retval;
123}
124125
static int lock_ref_file(const char *filename, const char *lock_filename,
126const unsigned char *old_sha1)
127{
128int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
129unsigned char current_sha1[20];
130int retval;
131if (fd < 0) {
132return error("Couldn't open lock file for %s: %s",
133filename, strerror(errno));
134}
135retval = read_ref_file(filename, current_sha1);
136if (old_sha1) {
137if (retval) {
138close(fd);
139unlink(lock_filename);
140return error("Could not read the current value of %s",
141filename);
142}
143if (memcmp(current_sha1, old_sha1, 20)) {
144close(fd);
145unlink(lock_filename);
146error("The current value of %s is %s",
147filename, sha1_to_hex(current_sha1));
148return error("Expected %s",
149sha1_to_hex(old_sha1));
150}
151} else {
152if (!retval) {
153close(fd);
154unlink(lock_filename);
155return error("Unexpectedly found a value of %s for %s",
156sha1_to_hex(current_sha1), filename);
157}
158}
159return fd;
160}
161162
int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
163{
164char *filename;
165char *lock_filename;
166int retval;
167if (check_ref_format(ref))
168return -1;
169filename = ref_file_name(ref);
170lock_filename = ref_lock_file_name(ref);
171retval = lock_ref_file(filename, lock_filename, old_sha1);
172free(filename);
173free(lock_filename);
174return retval;
175}
176177
static int write_ref_file(const char *filename,
178const char *lock_filename, int fd,
179const unsigned char *sha1)
180{
181char *hex = sha1_to_hex(sha1);
182char term = '\n';
183if (write(fd, hex, 40) < 40 ||
184write(fd, &term, 1) < 1) {
185error("Couldn't write %s\n", filename);
186close(fd);
187return -1;
188}
189close(fd);
190rename(lock_filename, filename);
191return 0;
192}
193194
int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
195{
196char *filename;
197char *lock_filename;
198int retval;
199if (fd < 0)
200return -1;
201if (check_ref_format(ref))
202return -1;
203filename = ref_file_name(ref);
204lock_filename = ref_lock_file_name(ref);
205retval = write_ref_file(filename, lock_filename, fd, sha1);
206free(filename);
207free(lock_filename);
208return retval;
209}
210211
int check_ref_format(const char *ref)
212{
213char *middle;
214if (ref[0] == '.' || ref[0] == '/')
215return -1;
216middle = strchr(ref, '/');
217if (!middle || !middle[1])
218return -1;
219if (strchr(middle + 1, '/'))
220return -1;
221return 0;
222}
223224
int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
225{
226char *filename;
227char *lock_filename;
228int fd;
229int retval;
230if (check_ref_format(ref))
231return -1;
232filename = ref_file_name(ref);
233lock_filename = ref_lock_file_name(ref);
234fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
235if (fd < 0) {
236error("Writing %s", lock_filename);
237perror("Open");
238}
239retval = write_ref_file(filename, lock_filename, fd, sha1);
240free(filename);
241free(lock_filename);
242return retval;
243}