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