cache-tree: protect against "git prune".
[git/git.git] / read-cache.c
CommitLineData
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 8struct cache_entry **active_cache = NULL;
29e4d363 9static time_t index_file_timestamp;
ee267527 10unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
e83c5163 11
415e96c8
JH
12/*
13 * This only updates the "non-critical" parts of the directory
14 * cache, ie the parts that aren't tracked by GIT, and only used
15 * to validate the cache.
16 */
17void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
18{
19 ce->ce_ctime.sec = htonl(st->st_ctime);
20 ce->ce_mtime.sec = htonl(st->st_mtime);
2cb45e95 21#ifdef USE_NSEC
415e96c8
JH
22 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
23 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
24#endif
25 ce->ce_dev = htonl(st->st_dev);
26 ce->ce_ino = htonl(st->st_ino);
27 ce->ce_uid = htonl(st->st_uid);
28 ce->ce_gid = htonl(st->st_gid);
29 ce->ce_size = htonl(st->st_size);
5f73076c
JH
30
31 if (assume_unchanged)
32 ce->ce_flags |= htons(CE_VALID);
415e96c8
JH
33}
34
29e4d363
JH
35static int ce_compare_data(struct cache_entry *ce, struct stat *st)
36{
37 int match = -1;
38 int fd = open(ce->name, O_RDONLY);
39
40 if (fd >= 0) {
41 unsigned char sha1[20];
42 if (!index_fd(sha1, fd, st, 0, NULL))
43 match = memcmp(sha1, ce->sha1, 20);
44 close(fd);
45 }
46 return match;
47}
48
49static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
50{
51 int match = -1;
52 char *target;
53 void *buffer;
54 unsigned long size;
55 char type[10];
56 int len;
57
58 target = xmalloc(expected_size);
59 len = readlink(ce->name, target, expected_size);
60 if (len != expected_size) {
61 free(target);
62 return -1;
63 }
64 buffer = read_sha1_file(ce->sha1, type, &size);
65 if (!buffer) {
66 free(target);
67 return -1;
68 }
69 if (size == expected_size)
70 match = memcmp(buffer, target, size);
71 free(buffer);
72 free(target);
73 return match;
74}
75
76static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
77{
78 switch (st->st_mode & S_IFMT) {
79 case S_IFREG:
80 if (ce_compare_data(ce, st))
81 return DATA_CHANGED;
82 break;
83 case S_IFLNK:
84 if (ce_compare_link(ce, st->st_size))
85 return DATA_CHANGED;
86 break;
87 default:
88 return TYPE_CHANGED;
89 }
90 return 0;
91}
92
407c8eb0 93static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
734aab75
LT
94{
95 unsigned int changed = 0;
96
8ae0a8c5
KS
97 switch (ntohl(ce->ce_mode) & S_IFMT) {
98 case S_IFREG:
99 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
3e09cdfd
JH
100 /* We consider only the owner x bit to be relevant for
101 * "mode changes"
102 */
103 if (trust_executable_bit &&
104 (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
ffbe1add 105 changed |= MODE_CHANGED;
8ae0a8c5
KS
106 break;
107 case S_IFLNK:
108 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
109 break;
110 default:
111 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
112 }
ccc4feb5 113 if (ce->ce_mtime.sec != htonl(st->st_mtime))
734aab75 114 changed |= MTIME_CHANGED;
ccc4feb5
LT
115 if (ce->ce_ctime.sec != htonl(st->st_ctime))
116 changed |= CTIME_CHANGED;
117
2cb45e95 118#ifdef USE_NSEC
ccc4feb5
LT
119 /*
120 * nsec seems unreliable - not all filesystems support it, so
121 * as long as it is in the inode cache you get right nsec
122 * but after it gets flushed, you get zero nsec.
123 */
94dfb7f2 124 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
ccc4feb5 125 changed |= MTIME_CHANGED;
94dfb7f2 126 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
734aab75 127 changed |= CTIME_CHANGED;
ccc4feb5
LT
128#endif
129
130 if (ce->ce_uid != htonl(st->st_uid) ||
131 ce->ce_gid != htonl(st->st_gid))
734aab75 132 changed |= OWNER_CHANGED;
2cb45e95 133 if (ce->ce_ino != htonl(st->st_ino))
734aab75 134 changed |= INODE_CHANGED;
2cb45e95
LT
135
136#ifdef USE_STDEV
137 /*
138 * st_dev breaks on network filesystems where different
139 * clients will have different views of what "device"
140 * the filesystem is on
141 */
142 if (ce->ce_dev != htonl(st->st_dev))
143 changed |= INODE_CHANGED;
144#endif
145
ccc4feb5 146 if (ce->ce_size != htonl(st->st_size))
734aab75 147 changed |= DATA_CHANGED;
b0391890 148
407c8eb0
JH
149 return changed;
150}
151
5f73076c 152int ce_match_stat(struct cache_entry *ce, struct stat *st, int ignore_valid)
407c8eb0 153{
5f73076c
JH
154 unsigned int changed;
155
156 /*
157 * If it's marked as always valid in the index, it's
158 * valid whatever the checked-out copy says.
159 */
160 if (!ignore_valid && (ce->ce_flags & htons(CE_VALID)))
161 return 0;
162
163 changed = ce_match_stat_basic(ce, st);
407c8eb0 164
29e4d363
JH
165 /*
166 * Within 1 second of this sequence:
167 * echo xyzzy >file && git-update-index --add file
168 * running this command:
169 * echo frotz >file
170 * would give a falsely clean cache entry. The mtime and
171 * length match the cache, and other stat fields do not change.
172 *
173 * We could detect this at update-index time (the cache entry
174 * being registered/updated records the same time as "now")
175 * and delay the return from git-update-index, but that would
176 * effectively mean we can make at most one commit per second,
177 * which is not acceptable. Instead, we check cache entries
178 * whose mtime are the same as the index file timestamp more
5f73076c 179 * carefully than others.
29e4d363
JH
180 */
181 if (!changed &&
182 index_file_timestamp &&
183 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
184 changed |= ce_modified_check_fs(ce, st);
b0391890 185
29e4d363 186 return changed;
b0391890
JH
187}
188
5f73076c 189int ce_modified(struct cache_entry *ce, struct stat *st, int really)
b0391890 190{
29e4d363 191 int changed, changed_fs;
5f73076c 192 changed = ce_match_stat(ce, st, really);
b0391890
JH
193 if (!changed)
194 return 0;
b0391890
JH
195 /*
196 * If the mode or type has changed, there's no point in trying
197 * to refresh the entry - it's not going to match
198 */
199 if (changed & (MODE_CHANGED | TYPE_CHANGED))
200 return changed;
201
202 /* Immediately after read-tree or update-index --cacheinfo,
203 * the length field is zero. For other cases the ce_size
204 * should match the SHA1 recorded in the index entry.
205 */
206 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
207 return changed;
208
29e4d363
JH
209 changed_fs = ce_modified_check_fs(ce, st);
210 if (changed_fs)
211 return changed | changed_fs;
b0391890
JH
212 return 0;
213}
214
958ba6c9
LT
215int base_name_compare(const char *name1, int len1, int mode1,
216 const char *name2, int len2, int mode2)
217{
218 unsigned char c1, c2;
219 int len = len1 < len2 ? len1 : len2;
220 int cmp;
221
222 cmp = memcmp(name1, name2, len);
223 if (cmp)
224 return cmp;
225 c1 = name1[len];
226 c2 = name2[len];
227 if (!c1 && S_ISDIR(mode1))
228 c1 = '/';
229 if (!c2 && S_ISDIR(mode2))
230 c2 = '/';
231 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
232}
233
95fd5bf8 234int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
eb38c22f 235{
95fd5bf8
LT
236 int len1 = flags1 & CE_NAMEMASK;
237 int len2 = flags2 & CE_NAMEMASK;
eb38c22f
LT
238 int len = len1 < len2 ? len1 : len2;
239 int cmp;
240
241 cmp = memcmp(name1, name2, len);
242 if (cmp)
243 return cmp;
244 if (len1 < len2)
245 return -1;
246 if (len1 > len2)
247 return 1;
5f73076c 248
7b80be15
JH
249 /* Compare stages */
250 flags1 &= CE_STAGEMASK;
251 flags2 &= CE_STAGEMASK;
5f73076c 252
95fd5bf8
LT
253 if (flags1 < flags2)
254 return -1;
255 if (flags1 > flags2)
256 return 1;
eb38c22f
LT
257 return 0;
258}
259
260int cache_name_pos(const char *name, int namelen)
261{
262 int first, last;
263
264 first = 0;
265 last = active_nr;
266 while (last > first) {
267 int next = (last + first) >> 1;
268 struct cache_entry *ce = active_cache[next];
972d1bb0 269 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
eb38c22f 270 if (!cmp)
76e7f4ec 271 return next;
eb38c22f
LT
272 if (cmp < 0) {
273 last = next;
274 continue;
275 }
276 first = next+1;
277 }
76e7f4ec 278 return -first-1;
eb38c22f
LT
279}
280
7b937ca3 281/* Remove entry, return true if there are more entries to go.. */
dbbce55b 282int remove_cache_entry_at(int pos)
7b937ca3 283{
ee267527 284 active_cache_changed = 1;
7b937ca3
LT
285 active_nr--;
286 if (pos >= active_nr)
287 return 0;
288 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
289 return 1;
290}
291
6b5ee137 292int remove_file_from_cache(const char *path)
197ee8c9
LT
293{
294 int pos = cache_name_pos(path, strlen(path));
c4e3cca1
JH
295 if (pos < 0)
296 pos = -pos-1;
297 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
dbbce55b 298 remove_cache_entry_at(pos);
197ee8c9
LT
299 return 0;
300}
301
dbbce55b 302int ce_same_name(struct cache_entry *a, struct cache_entry *b)
7b937ca3
LT
303{
304 int len = ce_namelen(a);
305 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
306}
307
c0fd1f51
LT
308int ce_path_match(const struct cache_entry *ce, const char **pathspec)
309{
310 const char *match, *name;
311 int len;
312
313 if (!pathspec)
314 return 1;
315
316 len = ce_namelen(ce);
317 name = ce->name;
318 while ((match = *pathspec++) != NULL) {
319 int matchlen = strlen(match);
320 if (matchlen > len)
321 continue;
322 if (memcmp(name, match, matchlen))
323 continue;
324 if (matchlen && name[matchlen-1] == '/')
325 return 1;
326 if (name[matchlen] == '/' || !name[matchlen])
327 return 1;
f332726e
LT
328 if (!matchlen)
329 return 1;
c0fd1f51
LT
330 }
331 return 0;
332}
333
12676608
LT
334/*
335 * Do we have another file that has the beginning components being a
336 * proper superset of the name we're trying to add?
0f1e4f04 337 */
12676608 338static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
0f1e4f04 339{
12676608
LT
340 int retval = 0;
341 int len = ce_namelen(ce);
b155725d 342 int stage = ce_stage(ce);
12676608 343 const char *name = ce->name;
0f1e4f04 344
12676608
LT
345 while (pos < active_nr) {
346 struct cache_entry *p = active_cache[pos++];
0f1e4f04 347
12676608 348 if (len >= ce_namelen(p))
0f1e4f04 349 break;
12676608
LT
350 if (memcmp(name, p->name, len))
351 break;
b155725d
JH
352 if (ce_stage(p) != stage)
353 continue;
12676608
LT
354 if (p->name[len] != '/')
355 continue;
12676608
LT
356 retval = -1;
357 if (!ok_to_replace)
358 break;
359 remove_cache_entry_at(--pos);
0f1e4f04 360 }
12676608
LT
361 return retval;
362}
0f1e4f04 363
12676608
LT
364/*
365 * Do we have another file with a pathname that is a proper
366 * subset of the name we're trying to add?
367 */
368static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
369{
370 int retval = 0;
b155725d 371 int stage = ce_stage(ce);
12676608
LT
372 const char *name = ce->name;
373 const char *slash = name + ce_namelen(ce);
0f1e4f04 374
12676608
LT
375 for (;;) {
376 int len;
0f1e4f04 377
12676608
LT
378 for (;;) {
379 if (*--slash == '/')
380 break;
381 if (slash <= ce->name)
382 return retval;
383 }
384 len = slash - name;
0f1e4f04 385
b155725d 386 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
12676608
LT
387 if (pos >= 0) {
388 retval = -1;
389 if (ok_to_replace)
390 break;
dbbce55b 391 remove_cache_entry_at(pos);
12676608
LT
392 continue;
393 }
394
395 /*
396 * Trivial optimization: if we find an entry that
397 * already matches the sub-directory, then we know
b155725d 398 * we're ok, and we can exit.
12676608
LT
399 */
400 pos = -pos-1;
b155725d 401 while (pos < active_nr) {
12676608 402 struct cache_entry *p = active_cache[pos];
b155725d
JH
403 if ((ce_namelen(p) <= len) ||
404 (p->name[len] != '/') ||
405 memcmp(p->name, name, len))
406 break; /* not our subdirectory */
407 if (ce_stage(p) == stage)
408 /* p is at the same stage as our entry, and
409 * is a subdirectory of what we are looking
410 * at, so we cannot have conflicts at our
411 * level or anything shorter.
412 */
413 return retval;
414 pos++;
192268c1 415 }
0f1e4f04 416 }
12676608
LT
417 return retval;
418}
419
420/* We may be in a situation where we already have path/file and path
421 * is being added, or we already have path and path/file is being
422 * added. Either one would result in a nonsense tree that has path
423 * twice when git-write-tree tries to write it out. Prevent it.
424 *
425 * If ok-to-replace is specified, we remove the conflicting entries
426 * from the cache so the caller should recompute the insert position.
427 * When this happens, we return non-zero.
428 */
429static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
430{
431 /*
432 * We check if the path is a sub-path of a subsequent pathname
433 * first, since removing those will not change the position
434 * in the array
435 */
436 int retval = has_file_name(ce, pos, ok_to_replace);
437 /*
438 * Then check if the path might have a clashing sub-directory
439 * before it.
440 */
441 return retval + has_dir_name(ce, pos, ok_to_replace);
0f1e4f04
JH
442}
443
192268c1 444int add_cache_entry(struct cache_entry *ce, int option)
197ee8c9
LT
445{
446 int pos;
192268c1
JH
447 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
448 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
b155725d 449 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
5f73076c 450
972d1bb0 451 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
197ee8c9 452
3e09cdfd 453 /* existing match? Just replace it. */
76e7f4ec 454 if (pos >= 0) {
ee267527 455 active_cache_changed = 1;
76e7f4ec 456 active_cache[pos] = ce;
197ee8c9
LT
457 return 0;
458 }
76e7f4ec 459 pos = -pos-1;
197ee8c9 460
7b937ca3
LT
461 /*
462 * Inserting a merged entry ("stage 0") into the index
463 * will always replace all non-merged entries..
464 */
465 if (pos < active_nr && ce_stage(ce) == 0) {
dbbce55b 466 while (ce_same_name(active_cache[pos], ce)) {
7b937ca3 467 ok_to_add = 1;
dbbce55b 468 if (!remove_cache_entry_at(pos))
7b937ca3
LT
469 break;
470 }
471 }
472
121481ab
LT
473 if (!ok_to_add)
474 return -1;
475
3e09cdfd
JH
476 if (!skip_df_check &&
477 check_file_directory_conflict(ce, pos, ok_to_replace)) {
192268c1
JH
478 if (!ok_to_replace)
479 return -1;
972d1bb0 480 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
192268c1
JH
481 pos = -pos-1;
482 }
0f1e4f04 483
197ee8c9
LT
484 /* Make sure the array is big enough .. */
485 if (active_nr == active_alloc) {
486 active_alloc = alloc_nr(active_alloc);
812666c8 487 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
197ee8c9
LT
488 }
489
490 /* Add it in.. */
491 active_nr++;
492 if (active_nr > pos)
493 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
494 active_cache[pos] = ce;
ee267527 495 active_cache_changed = 1;
197ee8c9
LT
496 return 0;
497}
498
1af1c2b6 499static int verify_hdr(struct cache_header *hdr, unsigned long size, unsigned char *sha1)
e83c5163
LT
500{
501 SHA_CTX c;
1af1c2b6
JH
502 unsigned char sha1_buf[20];
503 if (!sha1)
504 sha1 = sha1_buf;
e83c5163 505
ccc4feb5 506 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
e83c5163 507 return error("bad signature");
ca9be054
LT
508 if (hdr->hdr_version != htonl(2))
509 return error("bad index version");
e83c5163 510 SHA1_Init(&c);
ca9be054 511 SHA1_Update(&c, hdr, size - 20);
e83c5163 512 SHA1_Final(sha1, &c);
ca9be054
LT
513 if (memcmp(sha1, (void *)hdr + size - 20, 20))
514 return error("bad index file sha1 signature");
e83c5163
LT
515 return 0;
516}
517
1af1c2b6 518int read_cache_1(unsigned char *cache_sha1)
e83c5163
LT
519{
520 int fd, i;
521 struct stat st;
522 unsigned long size, offset;
523 void *map;
524 struct cache_header *hdr;
525
526 errno = EBUSY;
527 if (active_cache)
5d1a5c02
LT
528 return active_nr;
529
e83c5163 530 errno = ENOENT;
29e4d363 531 index_file_timestamp = 0;
bb233d69 532 fd = open(get_index_file(), O_RDONLY);
5d1a5c02
LT
533 if (fd < 0) {
534 if (errno == ENOENT)
535 return 0;
536 die("index file open failed (%s)", strerror(errno));
537 }
e83c5163 538
19b2860c 539 size = 0; // avoid gcc warning
e35f9824 540 map = MAP_FAILED;
e83c5163 541 if (!fstat(fd, &st)) {
e83c5163
LT
542 size = st.st_size;
543 errno = EINVAL;
ca9be054 544 if (size >= sizeof(struct cache_header) + 20)
520fc241 545 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
e83c5163
LT
546 }
547 close(fd);
e35f9824 548 if (map == MAP_FAILED)
5d1a5c02 549 die("index file mmap failed (%s)", strerror(errno));
e83c5163
LT
550
551 hdr = map;
1af1c2b6 552 if (verify_hdr(hdr, size, cache_sha1) < 0)
e83c5163
LT
553 goto unmap;
554
ccc4feb5 555 active_nr = ntohl(hdr->hdr_entries);
e83c5163
LT
556 active_alloc = alloc_nr(active_nr);
557 active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
558
559 offset = sizeof(*hdr);
ccc4feb5 560 for (i = 0; i < active_nr; i++) {
e83c5163
LT
561 struct cache_entry *ce = map + offset;
562 offset = offset + ce_size(ce);
563 active_cache[i] = ce;
564 }
29e4d363 565 index_file_timestamp = st.st_mtime;
e83c5163
LT
566 return active_nr;
567
568unmap:
569 munmap(map, size);
570 errno = EINVAL;
5d1a5c02 571 die("index file corrupt");
e83c5163
LT
572}
573
4990aadc 574#define WRITE_BUFFER_SIZE 8192
bf0f910d 575static unsigned char write_buffer[WRITE_BUFFER_SIZE];
4990aadc
LT
576static unsigned long write_buffer_len;
577
ca9be054 578static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
4990aadc
LT
579{
580 while (len) {
581 unsigned int buffered = write_buffer_len;
582 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
583 if (partial > len)
584 partial = len;
585 memcpy(write_buffer + buffered, data, partial);
586 buffered += partial;
587 if (buffered == WRITE_BUFFER_SIZE) {
ca9be054 588 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
4990aadc
LT
589 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
590 return -1;
591 buffered = 0;
592 }
593 write_buffer_len = buffered;
594 len -= partial;
595 data += partial;
596 }
597 return 0;
598}
599
1af1c2b6 600static int ce_flush(SHA_CTX *context, int fd, unsigned char *sha1)
4990aadc
LT
601{
602 unsigned int left = write_buffer_len;
ca9be054 603
4990aadc
LT
604 if (left) {
605 write_buffer_len = 0;
ca9be054 606 SHA1_Update(context, write_buffer, left);
4990aadc 607 }
ca9be054 608
2c865d9a
QH
609 /* Flush first if not enough space for SHA1 signature */
610 if (left + 20 > WRITE_BUFFER_SIZE) {
611 if (write(fd, write_buffer, left) != left)
612 return -1;
613 left = 0;
614 }
615
ca9be054 616 /* Append the SHA1 signature at the end */
1af1c2b6
JH
617 SHA1_Final(sha1, context);
618 memcpy(write_buffer + left, sha1, 20);
ca9be054
LT
619 left += 20;
620 if (write(fd, write_buffer, left) != left)
621 return -1;
4990aadc
LT
622 return 0;
623}
624
407c8eb0
JH
625static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
626{
627 /*
628 * The only thing we care about in this function is to smudge the
629 * falsely clean entry due to touch-update-touch race, so we leave
630 * everything else as they are. We are called for entries whose
631 * ce_mtime match the index file mtime.
632 */
633 struct stat st;
634
635 if (lstat(ce->name, &st) < 0)
636 return;
637 if (ce_match_stat_basic(ce, &st))
638 return;
639 if (ce_modified_check_fs(ce, &st)) {
4b3511b0
JH
640 /* This is "racily clean"; smudge it. Note that this
641 * is a tricky code. At first glance, it may appear
642 * that it can break with this sequence:
643 *
644 * $ echo xyzzy >frotz
645 * $ git-update-index --add frotz
646 * $ : >frotz
647 * $ sleep 3
648 * $ echo filfre >nitfol
649 * $ git-update-index --add nitfol
650 *
651 * but it does not. Whe the second update-index runs,
652 * it notices that the entry "frotz" has the same timestamp
653 * as index, and if we were to smudge it by resetting its
654 * size to zero here, then the object name recorded
655 * in index is the 6-byte file but the cached stat information
656 * becomes zero --- which would then match what we would
657 * obtain from the filesystem next time we stat("frotz").
658 *
659 * However, the second update-index, before calling
660 * this function, notices that the cached size is 6
661 * bytes and what is on the filesystem is an empty
662 * file, and never calls us, so the cached size information
663 * for "frotz" stays 6 which does not match the filesystem.
664 */
407c8eb0
JH
665 ce->ce_size = htonl(0);
666 }
667}
668
1af1c2b6
JH
669int write_cache_1(int newfd, struct cache_entry **cache, int entries,
670 unsigned char *cache_sha1)
197ee8c9
LT
671{
672 SHA_CTX c;
673 struct cache_header hdr;
025a0709 674 int i, removed;
1af1c2b6
JH
675 int status;
676 unsigned char sha1[20];
025a0709
JH
677
678 for (i = removed = 0; i < entries; i++)
679 if (!cache[i]->ce_mode)
680 removed++;
197ee8c9 681
ccc4feb5 682 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
ca9be054 683 hdr.hdr_version = htonl(2);
025a0709 684 hdr.hdr_entries = htonl(entries - removed);
197ee8c9
LT
685
686 SHA1_Init(&c);
ca9be054 687 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
197ee8c9
LT
688 return -1;
689
690 for (i = 0; i < entries; i++) {
691 struct cache_entry *ce = cache[i];
aa16021e
LT
692 if (!ce->ce_mode)
693 continue;
407c8eb0
JH
694 if (index_file_timestamp &&
695 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
696 ce_smudge_racily_clean_entry(ce);
ca9be054 697 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
197ee8c9
LT
698 return -1;
699 }
1af1c2b6
JH
700 status = ce_flush(&c, newfd, sha1);
701 if (cache_sha1)
702 memcpy(cache_sha1, sha1, 20);
703 return status;
704}
705
706int read_cache(void)
707{
708 return read_cache_1(NULL);
709}
710
711int write_cache(int newfd, struct cache_entry **cache, int entries)
712{
713 return write_cache_1(newfd, cache, entries, NULL);
197ee8c9 714}