6 static int read_ref(const char *path
, unsigned char *sha1
)
9 int fd
= open(path
, O_RDONLY
);
13 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
14 ret
= get_sha1_hex(buffer
, sha1
);
20 static int do_for_each_ref(const char *base
, int (*fn
)(const char *path
, const unsigned char *sha1
))
23 DIR *dir
= opendir(base
);
27 int baselen
= strlen(base
);
28 char *path
= xmalloc(baselen
+ 257);
30 if (!strncmp(base
, "./", 2)) {
34 memcpy(path
, base
, baselen
);
35 if (baselen
&& base
[baselen
-1] != '/')
36 path
[baselen
++] = '/';
38 while ((de
= readdir(dir
)) != NULL
) {
39 unsigned char sha1
[20];
43 if (de
->d_name
[0] == '.')
45 namelen
= strlen(de
->d_name
);
48 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
49 if (lstat(path
, &st
) < 0)
51 if (S_ISDIR(st
.st_mode
)) {
52 retval
= do_for_each_ref(path
, fn
);
57 if (read_ref(path
, sha1
) < 0)
59 if (!has_sha1_file(sha1
))
61 retval
= fn(path
, sha1
);
71 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
73 return do_for_each_ref(get_refs_directory(), fn
);
76 static char *ref_file_name(const char *ref
)
78 char *base
= get_refs_directory();
79 int baselen
= strlen(base
);
80 int reflen
= strlen(ref
);
81 char *ret
= xmalloc(baselen
+ 2 + reflen
);
82 sprintf(ret
, "%s/%s", base
, ref
);
86 static char *ref_lock_file_name(const char *ref
)
88 char *base
= get_refs_directory();
89 int baselen
= strlen(base
);
90 int reflen
= strlen(ref
);
91 char *ret
= xmalloc(baselen
+ 7 + reflen
);
92 sprintf(ret
, "%s/%s.lock", base
, ref
);
96 static int read_ref_file(const char *filename
, unsigned char *sha1
) {
97 int fd
= open(filename
, O_RDONLY
);
100 return error("Couldn't open %s\n", filename
);
102 if ((read(fd
, hex
, 41) < 41) ||
104 get_sha1_hex(hex
, sha1
)) {
105 error("Couldn't read a hash from %s\n", filename
);
113 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
117 if (check_ref_format(ref
))
119 filename
= ref_file_name(ref
);
120 retval
= read_ref_file(filename
, sha1
);
125 static int lock_ref_file(const char *filename
, const char *lock_filename
,
126 const unsigned char *old_sha1
)
128 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
129 unsigned char current_sha1
[20];
132 return error("Couldn't open lock file for %s: %s",
133 filename
, strerror(errno
));
135 retval
= read_ref_file(filename
, current_sha1
);
139 unlink(lock_filename
);
140 return error("Could not read the current value of %s",
143 if (memcmp(current_sha1
, old_sha1
, 20)) {
145 unlink(lock_filename
);
146 error("The current value of %s is %s",
147 filename
, sha1_to_hex(current_sha1
));
148 return error("Expected %s",
149 sha1_to_hex(old_sha1
));
154 unlink(lock_filename
);
155 return error("Unexpectedly found a value of %s for %s",
156 sha1_to_hex(current_sha1
), filename
);
162 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
167 if (check_ref_format(ref
))
169 filename
= ref_file_name(ref
);
170 lock_filename
= ref_lock_file_name(ref
);
171 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
177 static int write_ref_file(const char *filename
,
178 const char *lock_filename
, int fd
,
179 const unsigned char *sha1
)
181 char *hex
= sha1_to_hex(sha1
);
183 if (write(fd
, hex
, 40) < 40 ||
184 write(fd
, &term
, 1) < 1) {
185 error("Couldn't write %s\n", filename
);
190 rename(lock_filename
, filename
);
194 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
201 if (check_ref_format(ref
))
203 filename
= ref_file_name(ref
);
204 lock_filename
= ref_lock_file_name(ref
);
205 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
211 int check_ref_format(const char *ref
)
214 if (ref
[0] == '.' || ref
[0] == '/')
216 middle
= strchr(ref
, '/');
217 if (!middle
|| !middle
[1])
219 if (strchr(middle
+ 1, '/'))
224 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
230 if (check_ref_format(ref
))
232 filename
= ref_file_name(ref
);
233 lock_filename
= ref_lock_file_name(ref
);
234 fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
236 error("Writing %s", lock_filename
);
239 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);