]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 LT |
6 | #include "cache.h" |
7 | ||
e83c5163 | 8 | struct cache_entry **active_cache = NULL; |
ee267527 | 9 | unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0; |
e83c5163 | 10 | |
415e96c8 JH |
11 | /* |
12 | * This only updates the "non-critical" parts of the directory | |
13 | * cache, ie the parts that aren't tracked by GIT, and only used | |
14 | * to validate the cache. | |
15 | */ | |
16 | void fill_stat_cache_info(struct cache_entry *ce, struct stat *st) | |
17 | { | |
18 | ce->ce_ctime.sec = htonl(st->st_ctime); | |
19 | ce->ce_mtime.sec = htonl(st->st_mtime); | |
2cb45e95 | 20 | #ifdef USE_NSEC |
415e96c8 JH |
21 | ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec); |
22 | ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec); | |
23 | #endif | |
24 | ce->ce_dev = htonl(st->st_dev); | |
25 | ce->ce_ino = htonl(st->st_ino); | |
26 | ce->ce_uid = htonl(st->st_uid); | |
27 | ce->ce_gid = htonl(st->st_gid); | |
28 | ce->ce_size = htonl(st->st_size); | |
29 | } | |
30 | ||
5d728c84 | 31 | int ce_match_stat(struct cache_entry *ce, struct stat *st) |
734aab75 LT |
32 | { |
33 | unsigned int changed = 0; | |
34 | ||
8ae0a8c5 KS |
35 | switch (ntohl(ce->ce_mode) & S_IFMT) { |
36 | case S_IFREG: | |
37 | changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; | |
ffbe1add KS |
38 | /* We consider only the owner x bit to be relevant for "mode changes" */ |
39 | if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)) | |
40 | changed |= MODE_CHANGED; | |
8ae0a8c5 KS |
41 | break; |
42 | case S_IFLNK: | |
43 | changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0; | |
44 | break; | |
45 | default: | |
46 | die("internal error: ce_mode is %o", ntohl(ce->ce_mode)); | |
47 | } | |
ccc4feb5 | 48 | if (ce->ce_mtime.sec != htonl(st->st_mtime)) |
734aab75 | 49 | changed |= MTIME_CHANGED; |
ccc4feb5 LT |
50 | if (ce->ce_ctime.sec != htonl(st->st_ctime)) |
51 | changed |= CTIME_CHANGED; | |
52 | ||
2cb45e95 | 53 | #ifdef USE_NSEC |
ccc4feb5 LT |
54 | /* |
55 | * nsec seems unreliable - not all filesystems support it, so | |
56 | * as long as it is in the inode cache you get right nsec | |
57 | * but after it gets flushed, you get zero nsec. | |
58 | */ | |
94dfb7f2 | 59 | if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec)) |
ccc4feb5 | 60 | changed |= MTIME_CHANGED; |
94dfb7f2 | 61 | if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec)) |
734aab75 | 62 | changed |= CTIME_CHANGED; |
ccc4feb5 LT |
63 | #endif |
64 | ||
65 | if (ce->ce_uid != htonl(st->st_uid) || | |
66 | ce->ce_gid != htonl(st->st_gid)) | |
734aab75 | 67 | changed |= OWNER_CHANGED; |
2cb45e95 | 68 | if (ce->ce_ino != htonl(st->st_ino)) |
734aab75 | 69 | changed |= INODE_CHANGED; |
2cb45e95 LT |
70 | |
71 | #ifdef USE_STDEV | |
72 | /* | |
73 | * st_dev breaks on network filesystems where different | |
74 | * clients will have different views of what "device" | |
75 | * the filesystem is on | |
76 | */ | |
77 | if (ce->ce_dev != htonl(st->st_dev)) | |
78 | changed |= INODE_CHANGED; | |
79 | #endif | |
80 | ||
ccc4feb5 | 81 | if (ce->ce_size != htonl(st->st_size)) |
734aab75 LT |
82 | changed |= DATA_CHANGED; |
83 | return changed; | |
84 | } | |
85 | ||
b0391890 JH |
86 | static int ce_compare_data(struct cache_entry *ce, struct stat *st) |
87 | { | |
88 | int match = -1; | |
89 | int fd = open(ce->name, O_RDONLY); | |
90 | ||
91 | if (fd >= 0) { | |
92 | unsigned char sha1[20]; | |
93 | if (!index_fd(sha1, fd, st, 0, NULL)) | |
94 | match = memcmp(sha1, ce->sha1, 20); | |
95 | close(fd); | |
96 | } | |
97 | return match; | |
98 | } | |
99 | ||
100 | static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size) | |
101 | { | |
102 | int match = -1; | |
103 | char *target; | |
104 | void *buffer; | |
105 | unsigned long size; | |
106 | char type[10]; | |
107 | int len; | |
108 | ||
109 | target = xmalloc(expected_size); | |
110 | len = readlink(ce->name, target, expected_size); | |
111 | if (len != expected_size) { | |
112 | free(target); | |
113 | return -1; | |
114 | } | |
115 | buffer = read_sha1_file(ce->sha1, type, &size); | |
116 | if (!buffer) { | |
117 | free(target); | |
118 | return -1; | |
119 | } | |
120 | if (size == expected_size) | |
121 | match = memcmp(buffer, target, size); | |
122 | free(buffer); | |
123 | free(target); | |
124 | return match; | |
125 | } | |
126 | ||
127 | int ce_modified(struct cache_entry *ce, struct stat *st) | |
128 | { | |
129 | int changed; | |
130 | changed = ce_match_stat(ce, st); | |
131 | if (!changed) | |
132 | return 0; | |
133 | ||
134 | /* | |
135 | * If the mode or type has changed, there's no point in trying | |
136 | * to refresh the entry - it's not going to match | |
137 | */ | |
138 | if (changed & (MODE_CHANGED | TYPE_CHANGED)) | |
139 | return changed; | |
140 | ||
141 | /* Immediately after read-tree or update-index --cacheinfo, | |
142 | * the length field is zero. For other cases the ce_size | |
143 | * should match the SHA1 recorded in the index entry. | |
144 | */ | |
145 | if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0)) | |
146 | return changed; | |
147 | ||
148 | switch (st->st_mode & S_IFMT) { | |
149 | case S_IFREG: | |
150 | if (ce_compare_data(ce, st)) | |
151 | return changed | DATA_CHANGED; | |
152 | break; | |
153 | case S_IFLNK: | |
154 | if (ce_compare_link(ce, st->st_size)) | |
155 | return changed | DATA_CHANGED; | |
156 | break; | |
157 | default: | |
158 | return changed | TYPE_CHANGED; | |
159 | } | |
160 | return 0; | |
161 | } | |
162 | ||
958ba6c9 LT |
163 | int base_name_compare(const char *name1, int len1, int mode1, |
164 | const char *name2, int len2, int mode2) | |
165 | { | |
166 | unsigned char c1, c2; | |
167 | int len = len1 < len2 ? len1 : len2; | |
168 | int cmp; | |
169 | ||
170 | cmp = memcmp(name1, name2, len); | |
171 | if (cmp) | |
172 | return cmp; | |
173 | c1 = name1[len]; | |
174 | c2 = name2[len]; | |
175 | if (!c1 && S_ISDIR(mode1)) | |
176 | c1 = '/'; | |
177 | if (!c2 && S_ISDIR(mode2)) | |
178 | c2 = '/'; | |
179 | return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
180 | } | |
181 | ||
95fd5bf8 | 182 | int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2) |
eb38c22f | 183 | { |
95fd5bf8 LT |
184 | int len1 = flags1 & CE_NAMEMASK; |
185 | int len2 = flags2 & CE_NAMEMASK; | |
eb38c22f LT |
186 | int len = len1 < len2 ? len1 : len2; |
187 | int cmp; | |
188 | ||
189 | cmp = memcmp(name1, name2, len); | |
190 | if (cmp) | |
191 | return cmp; | |
192 | if (len1 < len2) | |
193 | return -1; | |
194 | if (len1 > len2) | |
195 | return 1; | |
95fd5bf8 LT |
196 | if (flags1 < flags2) |
197 | return -1; | |
198 | if (flags1 > flags2) | |
199 | return 1; | |
eb38c22f LT |
200 | return 0; |
201 | } | |
202 | ||
203 | int cache_name_pos(const char *name, int namelen) | |
204 | { | |
205 | int first, last; | |
206 | ||
207 | first = 0; | |
208 | last = active_nr; | |
209 | while (last > first) { | |
210 | int next = (last + first) >> 1; | |
211 | struct cache_entry *ce = active_cache[next]; | |
972d1bb0 | 212 | int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags)); |
eb38c22f | 213 | if (!cmp) |
76e7f4ec | 214 | return next; |
eb38c22f LT |
215 | if (cmp < 0) { |
216 | last = next; | |
217 | continue; | |
218 | } | |
219 | first = next+1; | |
220 | } | |
76e7f4ec | 221 | return -first-1; |
eb38c22f LT |
222 | } |
223 | ||
7b937ca3 | 224 | /* Remove entry, return true if there are more entries to go.. */ |
dbbce55b | 225 | int remove_cache_entry_at(int pos) |
7b937ca3 | 226 | { |
ee267527 | 227 | active_cache_changed = 1; |
7b937ca3 LT |
228 | active_nr--; |
229 | if (pos >= active_nr) | |
230 | return 0; | |
231 | memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *)); | |
232 | return 1; | |
233 | } | |
234 | ||
6b5ee137 | 235 | int remove_file_from_cache(const char *path) |
197ee8c9 LT |
236 | { |
237 | int pos = cache_name_pos(path, strlen(path)); | |
c4e3cca1 JH |
238 | if (pos < 0) |
239 | pos = -pos-1; | |
240 | while (pos < active_nr && !strcmp(active_cache[pos]->name, path)) | |
dbbce55b | 241 | remove_cache_entry_at(pos); |
197ee8c9 LT |
242 | return 0; |
243 | } | |
244 | ||
dbbce55b | 245 | int ce_same_name(struct cache_entry *a, struct cache_entry *b) |
7b937ca3 LT |
246 | { |
247 | int len = ce_namelen(a); | |
248 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); | |
249 | } | |
250 | ||
c0fd1f51 LT |
251 | int ce_path_match(const struct cache_entry *ce, const char **pathspec) |
252 | { | |
253 | const char *match, *name; | |
254 | int len; | |
255 | ||
256 | if (!pathspec) | |
257 | return 1; | |
258 | ||
259 | len = ce_namelen(ce); | |
260 | name = ce->name; | |
261 | while ((match = *pathspec++) != NULL) { | |
262 | int matchlen = strlen(match); | |
263 | if (matchlen > len) | |
264 | continue; | |
265 | if (memcmp(name, match, matchlen)) | |
266 | continue; | |
267 | if (matchlen && name[matchlen-1] == '/') | |
268 | return 1; | |
269 | if (name[matchlen] == '/' || !name[matchlen]) | |
270 | return 1; | |
f332726e LT |
271 | if (!matchlen) |
272 | return 1; | |
c0fd1f51 LT |
273 | } |
274 | return 0; | |
275 | } | |
276 | ||
12676608 LT |
277 | /* |
278 | * Do we have another file that has the beginning components being a | |
279 | * proper superset of the name we're trying to add? | |
0f1e4f04 | 280 | */ |
12676608 | 281 | static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace) |
0f1e4f04 | 282 | { |
12676608 LT |
283 | int retval = 0; |
284 | int len = ce_namelen(ce); | |
b155725d | 285 | int stage = ce_stage(ce); |
12676608 | 286 | const char *name = ce->name; |
0f1e4f04 | 287 | |
12676608 LT |
288 | while (pos < active_nr) { |
289 | struct cache_entry *p = active_cache[pos++]; | |
0f1e4f04 | 290 | |
12676608 | 291 | if (len >= ce_namelen(p)) |
0f1e4f04 | 292 | break; |
12676608 LT |
293 | if (memcmp(name, p->name, len)) |
294 | break; | |
b155725d JH |
295 | if (ce_stage(p) != stage) |
296 | continue; | |
12676608 LT |
297 | if (p->name[len] != '/') |
298 | continue; | |
12676608 LT |
299 | retval = -1; |
300 | if (!ok_to_replace) | |
301 | break; | |
302 | remove_cache_entry_at(--pos); | |
0f1e4f04 | 303 | } |
12676608 LT |
304 | return retval; |
305 | } | |
0f1e4f04 | 306 | |
12676608 LT |
307 | /* |
308 | * Do we have another file with a pathname that is a proper | |
309 | * subset of the name we're trying to add? | |
310 | */ | |
311 | static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace) | |
312 | { | |
313 | int retval = 0; | |
b155725d | 314 | int stage = ce_stage(ce); |
12676608 LT |
315 | const char *name = ce->name; |
316 | const char *slash = name + ce_namelen(ce); | |
0f1e4f04 | 317 | |
12676608 LT |
318 | for (;;) { |
319 | int len; | |
0f1e4f04 | 320 | |
12676608 LT |
321 | for (;;) { |
322 | if (*--slash == '/') | |
323 | break; | |
324 | if (slash <= ce->name) | |
325 | return retval; | |
326 | } | |
327 | len = slash - name; | |
0f1e4f04 | 328 | |
b155725d | 329 | pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage))); |
12676608 LT |
330 | if (pos >= 0) { |
331 | retval = -1; | |
332 | if (ok_to_replace) | |
333 | break; | |
dbbce55b | 334 | remove_cache_entry_at(pos); |
12676608 LT |
335 | continue; |
336 | } | |
337 | ||
338 | /* | |
339 | * Trivial optimization: if we find an entry that | |
340 | * already matches the sub-directory, then we know | |
b155725d | 341 | * we're ok, and we can exit. |
12676608 LT |
342 | */ |
343 | pos = -pos-1; | |
b155725d | 344 | while (pos < active_nr) { |
12676608 | 345 | struct cache_entry *p = active_cache[pos]; |
b155725d JH |
346 | if ((ce_namelen(p) <= len) || |
347 | (p->name[len] != '/') || | |
348 | memcmp(p->name, name, len)) | |
349 | break; /* not our subdirectory */ | |
350 | if (ce_stage(p) == stage) | |
351 | /* p is at the same stage as our entry, and | |
352 | * is a subdirectory of what we are looking | |
353 | * at, so we cannot have conflicts at our | |
354 | * level or anything shorter. | |
355 | */ | |
356 | return retval; | |
357 | pos++; | |
192268c1 | 358 | } |
0f1e4f04 | 359 | } |
12676608 LT |
360 | return retval; |
361 | } | |
362 | ||
363 | /* We may be in a situation where we already have path/file and path | |
364 | * is being added, or we already have path and path/file is being | |
365 | * added. Either one would result in a nonsense tree that has path | |
366 | * twice when git-write-tree tries to write it out. Prevent it. | |
367 | * | |
368 | * If ok-to-replace is specified, we remove the conflicting entries | |
369 | * from the cache so the caller should recompute the insert position. | |
370 | * When this happens, we return non-zero. | |
371 | */ | |
372 | static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace) | |
373 | { | |
374 | /* | |
375 | * We check if the path is a sub-path of a subsequent pathname | |
376 | * first, since removing those will not change the position | |
377 | * in the array | |
378 | */ | |
379 | int retval = has_file_name(ce, pos, ok_to_replace); | |
380 | /* | |
381 | * Then check if the path might have a clashing sub-directory | |
382 | * before it. | |
383 | */ | |
384 | return retval + has_dir_name(ce, pos, ok_to_replace); | |
0f1e4f04 JH |
385 | } |
386 | ||
192268c1 | 387 | int add_cache_entry(struct cache_entry *ce, int option) |
197ee8c9 LT |
388 | { |
389 | int pos; | |
192268c1 JH |
390 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
391 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; | |
b155725d | 392 | int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; |
972d1bb0 | 393 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
197ee8c9 LT |
394 | |
395 | /* existing match? Just replace it */ | |
76e7f4ec | 396 | if (pos >= 0) { |
ee267527 | 397 | active_cache_changed = 1; |
76e7f4ec | 398 | active_cache[pos] = ce; |
197ee8c9 LT |
399 | return 0; |
400 | } | |
76e7f4ec | 401 | pos = -pos-1; |
197ee8c9 | 402 | |
7b937ca3 LT |
403 | /* |
404 | * Inserting a merged entry ("stage 0") into the index | |
405 | * will always replace all non-merged entries.. | |
406 | */ | |
407 | if (pos < active_nr && ce_stage(ce) == 0) { | |
dbbce55b | 408 | while (ce_same_name(active_cache[pos], ce)) { |
7b937ca3 | 409 | ok_to_add = 1; |
dbbce55b | 410 | if (!remove_cache_entry_at(pos)) |
7b937ca3 LT |
411 | break; |
412 | } | |
413 | } | |
414 | ||
121481ab LT |
415 | if (!ok_to_add) |
416 | return -1; | |
417 | ||
b155725d | 418 | if (!skip_df_check && check_file_directory_conflict(ce, pos, ok_to_replace)) { |
192268c1 JH |
419 | if (!ok_to_replace) |
420 | return -1; | |
972d1bb0 | 421 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
192268c1 JH |
422 | pos = -pos-1; |
423 | } | |
0f1e4f04 | 424 | |
197ee8c9 LT |
425 | /* Make sure the array is big enough .. */ |
426 | if (active_nr == active_alloc) { | |
427 | active_alloc = alloc_nr(active_alloc); | |
812666c8 | 428 | active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *)); |
197ee8c9 LT |
429 | } |
430 | ||
431 | /* Add it in.. */ | |
432 | active_nr++; | |
433 | if (active_nr > pos) | |
434 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); | |
435 | active_cache[pos] = ce; | |
ee267527 | 436 | active_cache_changed = 1; |
197ee8c9 LT |
437 | return 0; |
438 | } | |
439 | ||
e83c5163 LT |
440 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
441 | { | |
442 | SHA_CTX c; | |
443 | unsigned char sha1[20]; | |
444 | ||
ccc4feb5 | 445 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
e83c5163 | 446 | return error("bad signature"); |
ca9be054 LT |
447 | if (hdr->hdr_version != htonl(2)) |
448 | return error("bad index version"); | |
e83c5163 | 449 | SHA1_Init(&c); |
ca9be054 | 450 | SHA1_Update(&c, hdr, size - 20); |
e83c5163 | 451 | SHA1_Final(sha1, &c); |
ca9be054 LT |
452 | if (memcmp(sha1, (void *)hdr + size - 20, 20)) |
453 | return error("bad index file sha1 signature"); | |
e83c5163 LT |
454 | return 0; |
455 | } | |
456 | ||
457 | int read_cache(void) | |
458 | { | |
459 | int fd, i; | |
460 | struct stat st; | |
461 | unsigned long size, offset; | |
462 | void *map; | |
463 | struct cache_header *hdr; | |
464 | ||
465 | errno = EBUSY; | |
466 | if (active_cache) | |
467 | return error("more than one cachefile"); | |
468 | errno = ENOENT; | |
bb233d69 | 469 | fd = open(get_index_file(), O_RDONLY); |
e83c5163 LT |
470 | if (fd < 0) |
471 | return (errno == ENOENT) ? 0 : error("open failed"); | |
472 | ||
19b2860c | 473 | size = 0; // avoid gcc warning |
e35f9824 | 474 | map = MAP_FAILED; |
e83c5163 | 475 | if (!fstat(fd, &st)) { |
e83c5163 LT |
476 | size = st.st_size; |
477 | errno = EINVAL; | |
ca9be054 | 478 | if (size >= sizeof(struct cache_header) + 20) |
520fc241 | 479 | map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
e83c5163 LT |
480 | } |
481 | close(fd); | |
e35f9824 | 482 | if (map == MAP_FAILED) |
e83c5163 LT |
483 | return error("mmap failed"); |
484 | ||
485 | hdr = map; | |
486 | if (verify_hdr(hdr, size) < 0) | |
487 | goto unmap; | |
488 | ||
ccc4feb5 | 489 | active_nr = ntohl(hdr->hdr_entries); |
e83c5163 LT |
490 | active_alloc = alloc_nr(active_nr); |
491 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); | |
492 | ||
493 | offset = sizeof(*hdr); | |
ccc4feb5 | 494 | for (i = 0; i < active_nr; i++) { |
e83c5163 LT |
495 | struct cache_entry *ce = map + offset; |
496 | offset = offset + ce_size(ce); | |
497 | active_cache[i] = ce; | |
498 | } | |
499 | return active_nr; | |
500 | ||
501 | unmap: | |
502 | munmap(map, size); | |
503 | errno = EINVAL; | |
504 | return error("verify header failed"); | |
505 | } | |
506 | ||
4990aadc | 507 | #define WRITE_BUFFER_SIZE 8192 |
bf0f910d | 508 | static unsigned char write_buffer[WRITE_BUFFER_SIZE]; |
4990aadc LT |
509 | static unsigned long write_buffer_len; |
510 | ||
ca9be054 | 511 | static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) |
4990aadc LT |
512 | { |
513 | while (len) { | |
514 | unsigned int buffered = write_buffer_len; | |
515 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; | |
516 | if (partial > len) | |
517 | partial = len; | |
518 | memcpy(write_buffer + buffered, data, partial); | |
519 | buffered += partial; | |
520 | if (buffered == WRITE_BUFFER_SIZE) { | |
ca9be054 | 521 | SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE); |
4990aadc LT |
522 | if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE) |
523 | return -1; | |
524 | buffered = 0; | |
525 | } | |
526 | write_buffer_len = buffered; | |
527 | len -= partial; | |
528 | data += partial; | |
529 | } | |
530 | return 0; | |
531 | } | |
532 | ||
ca9be054 | 533 | static int ce_flush(SHA_CTX *context, int fd) |
4990aadc LT |
534 | { |
535 | unsigned int left = write_buffer_len; | |
ca9be054 | 536 | |
4990aadc LT |
537 | if (left) { |
538 | write_buffer_len = 0; | |
ca9be054 | 539 | SHA1_Update(context, write_buffer, left); |
4990aadc | 540 | } |
ca9be054 | 541 | |
2c865d9a QH |
542 | /* Flush first if not enough space for SHA1 signature */ |
543 | if (left + 20 > WRITE_BUFFER_SIZE) { | |
544 | if (write(fd, write_buffer, left) != left) | |
545 | return -1; | |
546 | left = 0; | |
547 | } | |
548 | ||
ca9be054 LT |
549 | /* Append the SHA1 signature at the end */ |
550 | SHA1_Final(write_buffer + left, context); | |
551 | left += 20; | |
552 | if (write(fd, write_buffer, left) != left) | |
553 | return -1; | |
4990aadc LT |
554 | return 0; |
555 | } | |
556 | ||
197ee8c9 LT |
557 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
558 | { | |
559 | SHA_CTX c; | |
560 | struct cache_header hdr; | |
025a0709 JH |
561 | int i, removed; |
562 | ||
563 | for (i = removed = 0; i < entries; i++) | |
564 | if (!cache[i]->ce_mode) | |
565 | removed++; | |
197ee8c9 | 566 | |
ccc4feb5 | 567 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
ca9be054 | 568 | hdr.hdr_version = htonl(2); |
025a0709 | 569 | hdr.hdr_entries = htonl(entries - removed); |
197ee8c9 LT |
570 | |
571 | SHA1_Init(&c); | |
ca9be054 | 572 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
197ee8c9 LT |
573 | return -1; |
574 | ||
575 | for (i = 0; i < entries; i++) { | |
576 | struct cache_entry *ce = cache[i]; | |
aa16021e LT |
577 | if (!ce->ce_mode) |
578 | continue; | |
ca9be054 | 579 | if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) |
197ee8c9 LT |
580 | return -1; |
581 | } | |
ca9be054 | 582 | return ce_flush(&c, newfd); |
197ee8c9 | 583 | } |