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);
29 memcpy(path
, base
, baselen
);
30 if (baselen
&& base
[baselen
-1] != '/')
31 path
[baselen
++] = '/';
33 while ((de
= readdir(dir
)) != NULL
) {
34 unsigned char sha1
[20];
38 if (de
->d_name
[0] == '.')
40 namelen
= strlen(de
->d_name
);
43 memcpy(path
+ baselen
, de
->d_name
, namelen
+1);
44 if (lstat(path
, &st
) < 0)
46 if (S_ISDIR(st
.st_mode
)) {
47 retval
= do_for_each_ref(path
, fn
);
52 if (read_ref(path
, sha1
) < 0)
54 if (!has_sha1_file(sha1
))
56 retval
= fn(path
, sha1
);
66 int for_each_ref(int (*fn
)(const char *path
, const unsigned char *sha1
))
68 return do_for_each_ref(get_refs_directory(), fn
);
71 static char *ref_file_name(const char *ref
)
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
);
81 static char *ref_lock_file_name(const char *ref
)
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
);
91 static int read_ref_file(const char *filename
, unsigned char *sha1
) {
92 int fd
= open(filename
, O_RDONLY
);
95 return error("Couldn't open %s\n", filename
);
97 if ((read(fd
, hex
, 41) < 41) ||
99 get_sha1_hex(hex
, sha1
)) {
100 error("Couldn't read a hash from %s\n", filename
);
108 int get_ref_sha1(const char *ref
, unsigned char *sha1
)
112 if (check_ref_format(ref
))
114 filename
= ref_file_name(ref
);
115 retval
= read_ref_file(filename
, sha1
);
120 static int lock_ref_file(const char *filename
, const char *lock_filename
,
121 const unsigned char *old_sha1
)
123 int fd
= open(lock_filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
124 unsigned char current_sha1
[20];
127 return error("Couldn't open lock file for %s: %s",
128 filename
, strerror(errno
));
130 retval
= read_ref_file(filename
, current_sha1
);
134 unlink(lock_filename
);
135 return error("Could not read the current value of %s",
138 if (memcmp(current_sha1
, old_sha1
, 20)) {
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
));
149 unlink(lock_filename
);
150 return error("Unexpectedly found a value of %s for %s",
151 sha1_to_hex(current_sha1
), filename
);
157 int lock_ref_sha1(const char *ref
, const unsigned char *old_sha1
)
162 if (check_ref_format(ref
))
164 filename
= ref_file_name(ref
);
165 lock_filename
= ref_lock_file_name(ref
);
166 retval
= lock_ref_file(filename
, lock_filename
, old_sha1
);
172 static int write_ref_file(const char *filename
,
173 const char *lock_filename
, int fd
,
174 const unsigned char *sha1
)
176 char *hex
= sha1_to_hex(sha1
);
178 if (write(fd
, hex
, 40) < 40 ||
179 write(fd
, &term
, 1) < 1) {
180 error("Couldn't write %s\n", filename
);
185 rename(lock_filename
, filename
);
189 int write_ref_sha1(const char *ref
, int fd
, const unsigned char *sha1
)
196 if (check_ref_format(ref
))
198 filename
= ref_file_name(ref
);
199 lock_filename
= ref_lock_file_name(ref
);
200 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);
206 int check_ref_format(const char *ref
)
209 if (ref
[0] == '.' || ref
[0] == '/')
211 middle
= strchr(ref
, '/');
212 if (!middle
|| !middle
[1])
214 if (strchr(middle
+ 1, '/'))
219 int write_ref_sha1_unlocked(const char *ref
, const unsigned char *sha1
)
225 if (check_ref_format(ref
))
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);
231 error("Writing %s", lock_filename
);
234 retval
= write_ref_file(filename
, lock_filename
, fd
, sha1
);