merge-recur: Cleanup last mixedCase variables...
[git/jrn.git] / read-cache.c
blob9c0a9fc2a1d8cba09e6f695883bf74b40243e99c
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "cache-tree.h"
9 /* Index extensions.
11 * The first letter should be 'A'..'Z' for extensions that are not
12 * necessary for a correct operation (i.e. optimization data).
13 * When new extensions are added that _needs_ to be understood in
14 * order to correctly interpret the index file, pick character that
15 * is outside the range, to cause the reader to abort.
18 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
19 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
21 struct cache_entry **active_cache = NULL;
22 static time_t index_file_timestamp;
23 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
25 struct cache_tree *active_cache_tree = NULL;
27 int cache_errno = 0;
29 static void *cache_mmap = NULL;
30 static size_t cache_mmap_size = 0;
33 * This only updates the "non-critical" parts of the directory
34 * cache, ie the parts that aren't tracked by GIT, and only used
35 * to validate the cache.
37 void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
39 ce->ce_ctime.sec = htonl(st->st_ctime);
40 ce->ce_mtime.sec = htonl(st->st_mtime);
41 #ifdef USE_NSEC
42 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
43 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
44 #endif
45 ce->ce_dev = htonl(st->st_dev);
46 ce->ce_ino = htonl(st->st_ino);
47 ce->ce_uid = htonl(st->st_uid);
48 ce->ce_gid = htonl(st->st_gid);
49 ce->ce_size = htonl(st->st_size);
51 if (assume_unchanged)
52 ce->ce_flags |= htons(CE_VALID);
55 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
57 int match = -1;
58 int fd = open(ce->name, O_RDONLY);
60 if (fd >= 0) {
61 unsigned char sha1[20];
62 if (!index_fd(sha1, fd, st, 0, NULL))
63 match = memcmp(sha1, ce->sha1, 20);
64 close(fd);
66 return match;
69 static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
71 int match = -1;
72 char *target;
73 void *buffer;
74 unsigned long size;
75 char type[10];
76 int len;
78 target = xmalloc(expected_size);
79 len = readlink(ce->name, target, expected_size);
80 if (len != expected_size) {
81 free(target);
82 return -1;
84 buffer = read_sha1_file(ce->sha1, type, &size);
85 if (!buffer) {
86 free(target);
87 return -1;
89 if (size == expected_size)
90 match = memcmp(buffer, target, size);
91 free(buffer);
92 free(target);
93 return match;
96 static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
98 switch (st->st_mode & S_IFMT) {
99 case S_IFREG:
100 if (ce_compare_data(ce, st))
101 return DATA_CHANGED;
102 break;
103 case S_IFLNK:
104 if (ce_compare_link(ce, st->st_size))
105 return DATA_CHANGED;
106 break;
107 default:
108 return TYPE_CHANGED;
110 return 0;
113 static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
115 unsigned int changed = 0;
117 switch (ntohl(ce->ce_mode) & S_IFMT) {
118 case S_IFREG:
119 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
120 /* We consider only the owner x bit to be relevant for
121 * "mode changes"
123 if (trust_executable_bit &&
124 (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
125 changed |= MODE_CHANGED;
126 break;
127 case S_IFLNK:
128 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
129 break;
130 default:
131 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
133 if (ce->ce_mtime.sec != htonl(st->st_mtime))
134 changed |= MTIME_CHANGED;
135 if (ce->ce_ctime.sec != htonl(st->st_ctime))
136 changed |= CTIME_CHANGED;
138 #ifdef USE_NSEC
140 * nsec seems unreliable - not all filesystems support it, so
141 * as long as it is in the inode cache you get right nsec
142 * but after it gets flushed, you get zero nsec.
144 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
145 changed |= MTIME_CHANGED;
146 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
147 changed |= CTIME_CHANGED;
148 #endif
150 if (ce->ce_uid != htonl(st->st_uid) ||
151 ce->ce_gid != htonl(st->st_gid))
152 changed |= OWNER_CHANGED;
153 if (ce->ce_ino != htonl(st->st_ino))
154 changed |= INODE_CHANGED;
156 #ifdef USE_STDEV
158 * st_dev breaks on network filesystems where different
159 * clients will have different views of what "device"
160 * the filesystem is on
162 if (ce->ce_dev != htonl(st->st_dev))
163 changed |= INODE_CHANGED;
164 #endif
166 if (ce->ce_size != htonl(st->st_size))
167 changed |= DATA_CHANGED;
169 return changed;
172 int ce_match_stat(struct cache_entry *ce, struct stat *st, int ignore_valid)
174 unsigned int changed;
177 * If it's marked as always valid in the index, it's
178 * valid whatever the checked-out copy says.
180 if (!ignore_valid && (ce->ce_flags & htons(CE_VALID)))
181 return 0;
183 changed = ce_match_stat_basic(ce, st);
186 * Within 1 second of this sequence:
187 * echo xyzzy >file && git-update-index --add file
188 * running this command:
189 * echo frotz >file
190 * would give a falsely clean cache entry. The mtime and
191 * length match the cache, and other stat fields do not change.
193 * We could detect this at update-index time (the cache entry
194 * being registered/updated records the same time as "now")
195 * and delay the return from git-update-index, but that would
196 * effectively mean we can make at most one commit per second,
197 * which is not acceptable. Instead, we check cache entries
198 * whose mtime are the same as the index file timestamp more
199 * carefully than others.
201 if (!changed &&
202 index_file_timestamp &&
203 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
204 changed |= ce_modified_check_fs(ce, st);
206 return changed;
209 int ce_modified(struct cache_entry *ce, struct stat *st, int really)
211 int changed, changed_fs;
212 changed = ce_match_stat(ce, st, really);
213 if (!changed)
214 return 0;
216 * If the mode or type has changed, there's no point in trying
217 * to refresh the entry - it's not going to match
219 if (changed & (MODE_CHANGED | TYPE_CHANGED))
220 return changed;
222 /* Immediately after read-tree or update-index --cacheinfo,
223 * the length field is zero. For other cases the ce_size
224 * should match the SHA1 recorded in the index entry.
226 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
227 return changed;
229 changed_fs = ce_modified_check_fs(ce, st);
230 if (changed_fs)
231 return changed | changed_fs;
232 return 0;
235 int base_name_compare(const char *name1, int len1, int mode1,
236 const char *name2, int len2, int mode2)
238 unsigned char c1, c2;
239 int len = len1 < len2 ? len1 : len2;
240 int cmp;
242 cmp = memcmp(name1, name2, len);
243 if (cmp)
244 return cmp;
245 c1 = name1[len];
246 c2 = name2[len];
247 if (!c1 && S_ISDIR(mode1))
248 c1 = '/';
249 if (!c2 && S_ISDIR(mode2))
250 c2 = '/';
251 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
254 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
256 int len1 = flags1 & CE_NAMEMASK;
257 int len2 = flags2 & CE_NAMEMASK;
258 int len = len1 < len2 ? len1 : len2;
259 int cmp;
261 cmp = memcmp(name1, name2, len);
262 if (cmp)
263 return cmp;
264 if (len1 < len2)
265 return -1;
266 if (len1 > len2)
267 return 1;
269 /* Compare stages */
270 flags1 &= CE_STAGEMASK;
271 flags2 &= CE_STAGEMASK;
273 if (flags1 < flags2)
274 return -1;
275 if (flags1 > flags2)
276 return 1;
277 return 0;
280 int cache_name_pos(const char *name, int namelen)
282 int first, last;
284 first = 0;
285 last = active_nr;
286 while (last > first) {
287 int next = (last + first) >> 1;
288 struct cache_entry *ce = active_cache[next];
289 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
290 if (!cmp)
291 return next;
292 if (cmp < 0) {
293 last = next;
294 continue;
296 first = next+1;
298 return -first-1;
301 /* Remove entry, return true if there are more entries to go.. */
302 int remove_cache_entry_at(int pos)
304 active_cache_changed = 1;
305 active_nr--;
306 if (pos >= active_nr)
307 return 0;
308 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
309 return 1;
312 int remove_file_from_cache(const char *path)
314 int pos = cache_name_pos(path, strlen(path));
315 if (pos < 0)
316 pos = -pos-1;
317 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
318 remove_cache_entry_at(pos);
319 return 0;
322 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
324 int len = ce_namelen(a);
325 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
328 int ce_path_match(const struct cache_entry *ce, const char **pathspec)
330 const char *match, *name;
331 int len;
333 if (!pathspec)
334 return 1;
336 len = ce_namelen(ce);
337 name = ce->name;
338 while ((match = *pathspec++) != NULL) {
339 int matchlen = strlen(match);
340 if (matchlen > len)
341 continue;
342 if (memcmp(name, match, matchlen))
343 continue;
344 if (matchlen && name[matchlen-1] == '/')
345 return 1;
346 if (name[matchlen] == '/' || !name[matchlen])
347 return 1;
348 if (!matchlen)
349 return 1;
351 return 0;
355 * We fundamentally don't like some paths: we don't want
356 * dot or dot-dot anywhere, and for obvious reasons don't
357 * want to recurse into ".git" either.
359 * Also, we don't want double slashes or slashes at the
360 * end that can make pathnames ambiguous.
362 static int verify_dotfile(const char *rest)
365 * The first character was '.', but that
366 * has already been discarded, we now test
367 * the rest.
369 switch (*rest) {
370 /* "." is not allowed */
371 case '\0': case '/':
372 return 0;
375 * ".git" followed by NUL or slash is bad. This
376 * shares the path end test with the ".." case.
378 case 'g':
379 if (rest[1] != 'i')
380 break;
381 if (rest[2] != 't')
382 break;
383 rest += 2;
384 /* fallthrough */
385 case '.':
386 if (rest[1] == '\0' || rest[1] == '/')
387 return 0;
389 return 1;
392 int verify_path(const char *path)
394 char c;
396 goto inside;
397 for (;;) {
398 if (!c)
399 return 1;
400 if (c == '/') {
401 inside:
402 c = *path++;
403 switch (c) {
404 default:
405 continue;
406 case '/': case '\0':
407 break;
408 case '.':
409 if (verify_dotfile(path))
410 continue;
412 return 0;
414 c = *path++;
419 * Do we have another file that has the beginning components being a
420 * proper superset of the name we're trying to add?
422 static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
424 int retval = 0;
425 int len = ce_namelen(ce);
426 int stage = ce_stage(ce);
427 const char *name = ce->name;
429 while (pos < active_nr) {
430 struct cache_entry *p = active_cache[pos++];
432 if (len >= ce_namelen(p))
433 break;
434 if (memcmp(name, p->name, len))
435 break;
436 if (ce_stage(p) != stage)
437 continue;
438 if (p->name[len] != '/')
439 continue;
440 retval = -1;
441 if (!ok_to_replace)
442 break;
443 remove_cache_entry_at(--pos);
445 return retval;
449 * Do we have another file with a pathname that is a proper
450 * subset of the name we're trying to add?
452 static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
454 int retval = 0;
455 int stage = ce_stage(ce);
456 const char *name = ce->name;
457 const char *slash = name + ce_namelen(ce);
459 for (;;) {
460 int len;
462 for (;;) {
463 if (*--slash == '/')
464 break;
465 if (slash <= ce->name)
466 return retval;
468 len = slash - name;
470 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
471 if (pos >= 0) {
472 retval = -1;
473 if (ok_to_replace)
474 break;
475 remove_cache_entry_at(pos);
476 continue;
480 * Trivial optimization: if we find an entry that
481 * already matches the sub-directory, then we know
482 * we're ok, and we can exit.
484 pos = -pos-1;
485 while (pos < active_nr) {
486 struct cache_entry *p = active_cache[pos];
487 if ((ce_namelen(p) <= len) ||
488 (p->name[len] != '/') ||
489 memcmp(p->name, name, len))
490 break; /* not our subdirectory */
491 if (ce_stage(p) == stage)
492 /* p is at the same stage as our entry, and
493 * is a subdirectory of what we are looking
494 * at, so we cannot have conflicts at our
495 * level or anything shorter.
497 return retval;
498 pos++;
501 return retval;
504 /* We may be in a situation where we already have path/file and path
505 * is being added, or we already have path and path/file is being
506 * added. Either one would result in a nonsense tree that has path
507 * twice when git-write-tree tries to write it out. Prevent it.
509 * If ok-to-replace is specified, we remove the conflicting entries
510 * from the cache so the caller should recompute the insert position.
511 * When this happens, we return non-zero.
513 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
516 * We check if the path is a sub-path of a subsequent pathname
517 * first, since removing those will not change the position
518 * in the array
520 int retval = has_file_name(ce, pos, ok_to_replace);
522 * Then check if the path might have a clashing sub-directory
523 * before it.
525 return retval + has_dir_name(ce, pos, ok_to_replace);
528 int add_cache_entry(struct cache_entry *ce, int option)
530 int pos;
531 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
532 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
533 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
535 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
537 /* existing match? Just replace it. */
538 if (pos >= 0) {
539 active_cache_changed = 1;
540 active_cache[pos] = ce;
541 return 0;
543 pos = -pos-1;
546 * Inserting a merged entry ("stage 0") into the index
547 * will always replace all non-merged entries..
549 if (pos < active_nr && ce_stage(ce) == 0) {
550 while (ce_same_name(active_cache[pos], ce)) {
551 ok_to_add = 1;
552 if (!remove_cache_entry_at(pos))
553 break;
557 if (!ok_to_add)
558 return -1;
559 if (!verify_path(ce->name))
560 return -1;
562 if (!skip_df_check &&
563 check_file_directory_conflict(ce, pos, ok_to_replace)) {
564 if (!ok_to_replace)
565 return -1;
566 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
567 pos = -pos-1;
570 /* Make sure the array is big enough .. */
571 if (active_nr == active_alloc) {
572 active_alloc = alloc_nr(active_alloc);
573 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
576 /* Add it in.. */
577 active_nr++;
578 if (active_nr > pos)
579 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
580 active_cache[pos] = ce;
581 active_cache_changed = 1;
582 return 0;
586 * "refresh" does not calculate a new sha1 file or bring the
587 * cache up-to-date for mode/content changes. But what it
588 * _does_ do is to "re-match" the stat information of a file
589 * with the cache, so that you can refresh the cache for a
590 * file that hasn't been changed but where the stat entry is
591 * out of date.
593 * For example, you'd want to do this after doing a "git-read-tree",
594 * to link up the stat cache details with the proper files.
596 struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
598 struct stat st;
599 struct cache_entry *updated;
600 int changed, size;
602 if (lstat(ce->name, &st) < 0) {
603 cache_errno = errno;
604 return NULL;
607 changed = ce_match_stat(ce, &st, really);
608 if (!changed) {
609 if (really && assume_unchanged &&
610 !(ce->ce_flags & htons(CE_VALID)))
611 ; /* mark this one VALID again */
612 else
613 return ce;
616 if (ce_modified(ce, &st, really)) {
617 cache_errno = EINVAL;
618 return NULL;
621 size = ce_size(ce);
622 updated = xmalloc(size);
623 memcpy(updated, ce, size);
624 fill_stat_cache_info(updated, &st);
626 /* In this case, if really is not set, we should leave
627 * CE_VALID bit alone. Otherwise, paths marked with
628 * --no-assume-unchanged (i.e. things to be edited) will
629 * reacquire CE_VALID bit automatically, which is not
630 * really what we want.
632 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
633 updated->ce_flags &= ~htons(CE_VALID);
635 return updated;
638 int refresh_cache(unsigned int flags)
640 int i;
641 int has_errors = 0;
642 int really = (flags & REFRESH_REALLY) != 0;
643 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
644 int quiet = (flags & REFRESH_QUIET) != 0;
645 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
647 for (i = 0; i < active_nr; i++) {
648 struct cache_entry *ce, *new;
649 ce = active_cache[i];
650 if (ce_stage(ce)) {
651 while ((i < active_nr) &&
652 ! strcmp(active_cache[i]->name, ce->name))
653 i++;
654 i--;
655 if (allow_unmerged)
656 continue;
657 printf("%s: needs merge\n", ce->name);
658 has_errors = 1;
659 continue;
662 new = refresh_cache_entry(ce, really);
663 if (new == ce)
664 continue;
665 if (!new) {
666 if (not_new && cache_errno == ENOENT)
667 continue;
668 if (really && cache_errno == EINVAL) {
669 /* If we are doing --really-refresh that
670 * means the index is not valid anymore.
672 ce->ce_flags &= ~htons(CE_VALID);
673 active_cache_changed = 1;
675 if (quiet)
676 continue;
677 printf("%s: needs update\n", ce->name);
678 has_errors = 1;
679 continue;
681 active_cache_changed = 1;
682 /* You can NOT just free active_cache[i] here, since it
683 * might not be necessarily malloc()ed but can also come
684 * from mmap(). */
685 active_cache[i] = new;
687 return has_errors;
690 static int verify_hdr(struct cache_header *hdr, unsigned long size)
692 SHA_CTX c;
693 unsigned char sha1[20];
695 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
696 return error("bad signature");
697 if (hdr->hdr_version != htonl(2))
698 return error("bad index version");
699 SHA1_Init(&c);
700 SHA1_Update(&c, hdr, size - 20);
701 SHA1_Final(sha1, &c);
702 if (memcmp(sha1, (char *) hdr + size - 20, 20))
703 return error("bad index file sha1 signature");
704 return 0;
707 static int read_index_extension(const char *ext, void *data, unsigned long sz)
709 switch (CACHE_EXT(ext)) {
710 case CACHE_EXT_TREE:
711 active_cache_tree = cache_tree_read(data, sz);
712 break;
713 default:
714 if (*ext < 'A' || 'Z' < *ext)
715 return error("index uses %.4s extension, which we do not understand",
716 ext);
717 fprintf(stderr, "ignoring %.4s extension\n", ext);
718 break;
720 return 0;
723 int read_cache(void)
725 return read_cache_from(get_index_file());
728 /* remember to discard_cache() before reading a different cache! */
729 int read_cache_from(const char *path)
731 int fd, i;
732 struct stat st;
733 unsigned long offset;
734 struct cache_header *hdr;
736 errno = EBUSY;
737 if (cache_mmap)
738 return active_nr;
740 errno = ENOENT;
741 index_file_timestamp = 0;
742 fd = open(path, O_RDONLY);
743 if (fd < 0) {
744 if (errno == ENOENT)
745 return 0;
746 die("index file open failed (%s)", strerror(errno));
749 cache_mmap = MAP_FAILED;
750 if (!fstat(fd, &st)) {
751 cache_mmap_size = st.st_size;
752 errno = EINVAL;
753 if (cache_mmap_size >= sizeof(struct cache_header) + 20)
754 cache_mmap = mmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
756 close(fd);
757 if (cache_mmap == MAP_FAILED)
758 die("index file mmap failed (%s)", strerror(errno));
760 hdr = cache_mmap;
761 if (verify_hdr(hdr, cache_mmap_size) < 0)
762 goto unmap;
764 active_nr = ntohl(hdr->hdr_entries);
765 active_alloc = alloc_nr(active_nr);
766 active_cache = xcalloc(active_alloc, sizeof(struct cache_entry *));
768 offset = sizeof(*hdr);
769 for (i = 0; i < active_nr; i++) {
770 struct cache_entry *ce = (struct cache_entry *) ((char *) cache_mmap + offset);
771 offset = offset + ce_size(ce);
772 active_cache[i] = ce;
774 index_file_timestamp = st.st_mtime;
775 while (offset <= cache_mmap_size - 20 - 8) {
776 /* After an array of active_nr index entries,
777 * there can be arbitrary number of extended
778 * sections, each of which is prefixed with
779 * extension name (4-byte) and section length
780 * in 4-byte network byte order.
782 unsigned long extsize;
783 memcpy(&extsize, (char *) cache_mmap + offset + 4, 4);
784 extsize = ntohl(extsize);
785 if (read_index_extension(((const char *) cache_mmap) + offset,
786 (char *) cache_mmap + offset + 8,
787 extsize) < 0)
788 goto unmap;
789 offset += 8;
790 offset += extsize;
792 return active_nr;
794 unmap:
795 munmap(cache_mmap, cache_mmap_size);
796 errno = EINVAL;
797 die("index file corrupt");
800 int discard_cache()
802 int ret;
804 if (cache_mmap == NULL)
805 return 0;
806 ret = munmap(cache_mmap, cache_mmap_size);
807 cache_mmap = NULL;
808 cache_mmap_size = 0;
809 active_nr = active_cache_changed = 0;
810 index_file_timestamp = 0;
811 cache_tree_free(&active_cache_tree);
813 /* no need to throw away allocated active_cache */
814 return ret;
817 #define WRITE_BUFFER_SIZE 8192
818 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
819 static unsigned long write_buffer_len;
821 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
823 while (len) {
824 unsigned int buffered = write_buffer_len;
825 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
826 if (partial > len)
827 partial = len;
828 memcpy(write_buffer + buffered, data, partial);
829 buffered += partial;
830 if (buffered == WRITE_BUFFER_SIZE) {
831 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
832 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
833 return -1;
834 buffered = 0;
836 write_buffer_len = buffered;
837 len -= partial;
838 data = (char *) data + partial;
840 return 0;
843 static int write_index_ext_header(SHA_CTX *context, int fd,
844 unsigned int ext, unsigned int sz)
846 ext = htonl(ext);
847 sz = htonl(sz);
848 if ((ce_write(context, fd, &ext, 4) < 0) ||
849 (ce_write(context, fd, &sz, 4) < 0))
850 return -1;
851 return 0;
854 static int ce_flush(SHA_CTX *context, int fd)
856 unsigned int left = write_buffer_len;
858 if (left) {
859 write_buffer_len = 0;
860 SHA1_Update(context, write_buffer, left);
863 /* Flush first if not enough space for SHA1 signature */
864 if (left + 20 > WRITE_BUFFER_SIZE) {
865 if (write(fd, write_buffer, left) != left)
866 return -1;
867 left = 0;
870 /* Append the SHA1 signature at the end */
871 SHA1_Final(write_buffer + left, context);
872 left += 20;
873 if (write(fd, write_buffer, left) != left)
874 return -1;
875 return 0;
878 static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
881 * The only thing we care about in this function is to smudge the
882 * falsely clean entry due to touch-update-touch race, so we leave
883 * everything else as they are. We are called for entries whose
884 * ce_mtime match the index file mtime.
886 struct stat st;
888 if (lstat(ce->name, &st) < 0)
889 return;
890 if (ce_match_stat_basic(ce, &st))
891 return;
892 if (ce_modified_check_fs(ce, &st)) {
893 /* This is "racily clean"; smudge it. Note that this
894 * is a tricky code. At first glance, it may appear
895 * that it can break with this sequence:
897 * $ echo xyzzy >frotz
898 * $ git-update-index --add frotz
899 * $ : >frotz
900 * $ sleep 3
901 * $ echo filfre >nitfol
902 * $ git-update-index --add nitfol
904 * but it does not. Whe the second update-index runs,
905 * it notices that the entry "frotz" has the same timestamp
906 * as index, and if we were to smudge it by resetting its
907 * size to zero here, then the object name recorded
908 * in index is the 6-byte file but the cached stat information
909 * becomes zero --- which would then match what we would
910 * obtain from the filesystem next time we stat("frotz").
912 * However, the second update-index, before calling
913 * this function, notices that the cached size is 6
914 * bytes and what is on the filesystem is an empty
915 * file, and never calls us, so the cached size information
916 * for "frotz" stays 6 which does not match the filesystem.
918 ce->ce_size = htonl(0);
922 int write_cache(int newfd, struct cache_entry **cache, int entries)
924 SHA_CTX c;
925 struct cache_header hdr;
926 int i, removed;
928 for (i = removed = 0; i < entries; i++)
929 if (!cache[i]->ce_mode)
930 removed++;
932 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
933 hdr.hdr_version = htonl(2);
934 hdr.hdr_entries = htonl(entries - removed);
936 SHA1_Init(&c);
937 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
938 return -1;
940 for (i = 0; i < entries; i++) {
941 struct cache_entry *ce = cache[i];
942 if (!ce->ce_mode)
943 continue;
944 if (index_file_timestamp &&
945 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
946 ce_smudge_racily_clean_entry(ce);
947 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
948 return -1;
951 /* Write extension data here */
952 if (active_cache_tree) {
953 unsigned long sz;
954 void *data = cache_tree_write(active_cache_tree, &sz);
955 if (data &&
956 !write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) &&
957 !ce_write(&c, newfd, data, sz))
959 else {
960 free(data);
961 return -1;
964 return ce_flush(&c, newfd);