2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #include "cache-tree.h"
12 * The first letter should be 'A'..'Z' for extensions that are not
13 * necessary for a correct operation (i.e. optimization data).
14 * When new extensions are added that _needs_ to be understood in
15 * order to correctly interpret the index file, pick character that
16 * is outside the range, to cause the reader to abort.
19 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
20 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
22 struct cache_entry
**active_cache
= NULL
;
23 static time_t index_file_timestamp
;
24 unsigned int active_nr
= 0, active_alloc
= 0, active_cache_changed
= 0;
26 struct cache_tree
*active_cache_tree
= NULL
;
30 static void *cache_mmap
= NULL
;
31 static size_t cache_mmap_size
= 0;
34 * This only updates the "non-critical" parts of the directory
35 * cache, ie the parts that aren't tracked by GIT, and only used
36 * to validate the cache.
38 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
40 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
41 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
43 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
44 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
46 ce
->ce_dev
= htonl(st
->st_dev
);
47 ce
->ce_ino
= htonl(st
->st_ino
);
48 ce
->ce_uid
= htonl(st
->st_uid
);
49 ce
->ce_gid
= htonl(st
->st_gid
);
50 ce
->ce_size
= htonl(st
->st_size
);
53 ce
->ce_flags
|= htons(CE_VALID
);
56 static int ce_compare_data(struct cache_entry
*ce
, struct stat
*st
)
59 int fd
= open(ce
->name
, O_RDONLY
);
62 unsigned char sha1
[20];
63 if (!index_fd(sha1
, fd
, st
, 0, NULL
))
64 match
= memcmp(sha1
, ce
->sha1
, 20);
65 /* index_fd() closed the file descriptor already */
70 static int ce_compare_link(struct cache_entry
*ce
, unsigned long expected_size
)
79 target
= xmalloc(expected_size
);
80 len
= readlink(ce
->name
, target
, expected_size
);
81 if (len
!= expected_size
) {
85 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
90 if (size
== expected_size
)
91 match
= memcmp(buffer
, target
, size
);
97 static int ce_modified_check_fs(struct cache_entry
*ce
, struct stat
*st
)
99 switch (st
->st_mode
& S_IFMT
) {
101 if (ce_compare_data(ce
, st
))
105 if (ce_compare_link(ce
, st
->st_size
))
114 static int ce_match_stat_basic(struct cache_entry
*ce
, struct stat
*st
)
116 unsigned int changed
= 0;
118 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
120 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
121 /* We consider only the owner x bit to be relevant for
124 if (trust_executable_bit
&&
125 (0100 & (ntohl(ce
->ce_mode
) ^ st
->st_mode
)))
126 changed
|= MODE_CHANGED
;
129 changed
|= !S_ISLNK(st
->st_mode
) ? TYPE_CHANGED
: 0;
132 die("internal error: ce_mode is %o", ntohl(ce
->ce_mode
));
134 if (ce
->ce_mtime
.sec
!= htonl(st
->st_mtime
))
135 changed
|= MTIME_CHANGED
;
136 if (ce
->ce_ctime
.sec
!= htonl(st
->st_ctime
))
137 changed
|= CTIME_CHANGED
;
141 * nsec seems unreliable - not all filesystems support it, so
142 * as long as it is in the inode cache you get right nsec
143 * but after it gets flushed, you get zero nsec.
145 if (ce
->ce_mtime
.nsec
!= htonl(st
->st_mtim
.tv_nsec
))
146 changed
|= MTIME_CHANGED
;
147 if (ce
->ce_ctime
.nsec
!= htonl(st
->st_ctim
.tv_nsec
))
148 changed
|= CTIME_CHANGED
;
151 if (ce
->ce_uid
!= htonl(st
->st_uid
) ||
152 ce
->ce_gid
!= htonl(st
->st_gid
))
153 changed
|= OWNER_CHANGED
;
154 if (ce
->ce_ino
!= htonl(st
->st_ino
))
155 changed
|= INODE_CHANGED
;
159 * st_dev breaks on network filesystems where different
160 * clients will have different views of what "device"
161 * the filesystem is on
163 if (ce
->ce_dev
!= htonl(st
->st_dev
))
164 changed
|= INODE_CHANGED
;
167 if (ce
->ce_size
!= htonl(st
->st_size
))
168 changed
|= DATA_CHANGED
;
173 int ce_match_stat(struct cache_entry
*ce
, struct stat
*st
, int ignore_valid
)
175 unsigned int changed
;
178 * If it's marked as always valid in the index, it's
179 * valid whatever the checked-out copy says.
181 if (!ignore_valid
&& (ce
->ce_flags
& htons(CE_VALID
)))
184 changed
= ce_match_stat_basic(ce
, st
);
187 * Within 1 second of this sequence:
188 * echo xyzzy >file && git-update-index --add file
189 * running this command:
191 * would give a falsely clean cache entry. The mtime and
192 * length match the cache, and other stat fields do not change.
194 * We could detect this at update-index time (the cache entry
195 * being registered/updated records the same time as "now")
196 * and delay the return from git-update-index, but that would
197 * effectively mean we can make at most one commit per second,
198 * which is not acceptable. Instead, we check cache entries
199 * whose mtime are the same as the index file timestamp more
200 * carefully than others.
203 index_file_timestamp
&&
204 index_file_timestamp
<= ntohl(ce
->ce_mtime
.sec
))
205 changed
|= ce_modified_check_fs(ce
, st
);
210 int ce_modified(struct cache_entry
*ce
, struct stat
*st
, int really
)
212 int changed
, changed_fs
;
213 changed
= ce_match_stat(ce
, st
, really
);
217 * If the mode or type has changed, there's no point in trying
218 * to refresh the entry - it's not going to match
220 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
223 /* Immediately after read-tree or update-index --cacheinfo,
224 * the length field is zero. For other cases the ce_size
225 * should match the SHA1 recorded in the index entry.
227 if ((changed
& DATA_CHANGED
) && ce
->ce_size
!= htonl(0))
230 changed_fs
= ce_modified_check_fs(ce
, st
);
232 return changed
| changed_fs
;
236 int base_name_compare(const char *name1
, int len1
, int mode1
,
237 const char *name2
, int len2
, int mode2
)
239 unsigned char c1
, c2
;
240 int len
= len1
< len2
? len1
: len2
;
243 cmp
= memcmp(name1
, name2
, len
);
248 if (!c1
&& S_ISDIR(mode1
))
250 if (!c2
&& S_ISDIR(mode2
))
252 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
255 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
257 int len1
= flags1
& CE_NAMEMASK
;
258 int len2
= flags2
& CE_NAMEMASK
;
259 int len
= len1
< len2
? len1
: len2
;
262 cmp
= memcmp(name1
, name2
, len
);
271 flags1
&= CE_STAGEMASK
;
272 flags2
&= CE_STAGEMASK
;
281 int cache_name_pos(const char *name
, int namelen
)
287 while (last
> first
) {
288 int next
= (last
+ first
) >> 1;
289 struct cache_entry
*ce
= active_cache
[next
];
290 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ntohs(ce
->ce_flags
));
302 /* Remove entry, return true if there are more entries to go.. */
303 int remove_cache_entry_at(int pos
)
305 active_cache_changed
= 1;
307 if (pos
>= active_nr
)
309 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
313 int remove_file_from_cache(const char *path
)
315 int pos
= cache_name_pos(path
, strlen(path
));
318 while (pos
< active_nr
&& !strcmp(active_cache
[pos
]->name
, path
))
319 remove_cache_entry_at(pos
);
323 int add_file_to_index(const char *path
, int verbose
)
327 struct cache_entry
*ce
;
329 if (lstat(path
, &st
))
330 die("%s: unable to stat (%s)", path
, strerror(errno
));
332 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
))
333 die("%s: can only add regular files or symbolic links", path
);
335 namelen
= strlen(path
);
336 size
= cache_entry_size(namelen
);
337 ce
= xcalloc(1, size
);
338 memcpy(ce
->name
, path
, namelen
);
339 ce
->ce_flags
= htons(namelen
);
340 fill_stat_cache_info(ce
, &st
);
342 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
343 if (!trust_executable_bit
) {
344 /* If there is an existing entry, pick the mode bits
347 int pos
= cache_name_pos(path
, namelen
);
349 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
352 if (index_path(ce
->sha1
, path
, &st
, 1))
353 die("unable to index file %s", path
);
354 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
))
355 die("unable to add %s to index",path
);
357 printf("add '%s'\n", path
);
358 cache_tree_invalidate_path(active_cache_tree
, path
);
362 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
364 int len
= ce_namelen(a
);
365 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
368 int ce_path_match(const struct cache_entry
*ce
, const char **pathspec
)
370 const char *match
, *name
;
376 len
= ce_namelen(ce
);
378 while ((match
= *pathspec
++) != NULL
) {
379 int matchlen
= strlen(match
);
382 if (memcmp(name
, match
, matchlen
))
384 if (matchlen
&& name
[matchlen
-1] == '/')
386 if (name
[matchlen
] == '/' || !name
[matchlen
])
395 * We fundamentally don't like some paths: we don't want
396 * dot or dot-dot anywhere, and for obvious reasons don't
397 * want to recurse into ".git" either.
399 * Also, we don't want double slashes or slashes at the
400 * end that can make pathnames ambiguous.
402 static int verify_dotfile(const char *rest
)
405 * The first character was '.', but that
406 * has already been discarded, we now test
410 /* "." is not allowed */
415 * ".git" followed by NUL or slash is bad. This
416 * shares the path end test with the ".." case.
426 if (rest
[1] == '\0' || rest
[1] == '/')
432 int verify_path(const char *path
)
449 if (verify_dotfile(path
))
459 * Do we have another file that has the beginning components being a
460 * proper superset of the name we're trying to add?
462 static int has_file_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
465 int len
= ce_namelen(ce
);
466 int stage
= ce_stage(ce
);
467 const char *name
= ce
->name
;
469 while (pos
< active_nr
) {
470 struct cache_entry
*p
= active_cache
[pos
++];
472 if (len
>= ce_namelen(p
))
474 if (memcmp(name
, p
->name
, len
))
476 if (ce_stage(p
) != stage
)
478 if (p
->name
[len
] != '/')
483 remove_cache_entry_at(--pos
);
489 * Do we have another file with a pathname that is a proper
490 * subset of the name we're trying to add?
492 static int has_dir_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
495 int stage
= ce_stage(ce
);
496 const char *name
= ce
->name
;
497 const char *slash
= name
+ ce_namelen(ce
);
505 if (slash
<= ce
->name
)
510 pos
= cache_name_pos(name
, ntohs(create_ce_flags(len
, stage
)));
515 remove_cache_entry_at(pos
);
520 * Trivial optimization: if we find an entry that
521 * already matches the sub-directory, then we know
522 * we're ok, and we can exit.
525 while (pos
< active_nr
) {
526 struct cache_entry
*p
= active_cache
[pos
];
527 if ((ce_namelen(p
) <= len
) ||
528 (p
->name
[len
] != '/') ||
529 memcmp(p
->name
, name
, len
))
530 break; /* not our subdirectory */
531 if (ce_stage(p
) == stage
)
532 /* p is at the same stage as our entry, and
533 * is a subdirectory of what we are looking
534 * at, so we cannot have conflicts at our
535 * level or anything shorter.
544 /* We may be in a situation where we already have path/file and path
545 * is being added, or we already have path and path/file is being
546 * added. Either one would result in a nonsense tree that has path
547 * twice when git-write-tree tries to write it out. Prevent it.
549 * If ok-to-replace is specified, we remove the conflicting entries
550 * from the cache so the caller should recompute the insert position.
551 * When this happens, we return non-zero.
553 static int check_file_directory_conflict(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
556 * We check if the path is a sub-path of a subsequent pathname
557 * first, since removing those will not change the position
560 int retval
= has_file_name(ce
, pos
, ok_to_replace
);
562 * Then check if the path might have a clashing sub-directory
565 return retval
+ has_dir_name(ce
, pos
, ok_to_replace
);
568 int add_cache_entry(struct cache_entry
*ce
, int option
)
571 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
572 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
573 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
575 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
577 /* existing match? Just replace it. */
579 active_cache_changed
= 1;
580 active_cache
[pos
] = ce
;
586 * Inserting a merged entry ("stage 0") into the index
587 * will always replace all non-merged entries..
589 if (pos
< active_nr
&& ce_stage(ce
) == 0) {
590 while (ce_same_name(active_cache
[pos
], ce
)) {
592 if (!remove_cache_entry_at(pos
))
599 if (!verify_path(ce
->name
))
602 if (!skip_df_check
&&
603 check_file_directory_conflict(ce
, pos
, ok_to_replace
)) {
606 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
610 /* Make sure the array is big enough .. */
611 if (active_nr
== active_alloc
) {
612 active_alloc
= alloc_nr(active_alloc
);
613 active_cache
= xrealloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
619 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
620 active_cache
[pos
] = ce
;
621 active_cache_changed
= 1;
626 * "refresh" does not calculate a new sha1 file or bring the
627 * cache up-to-date for mode/content changes. But what it
628 * _does_ do is to "re-match" the stat information of a file
629 * with the cache, so that you can refresh the cache for a
630 * file that hasn't been changed but where the stat entry is
633 * For example, you'd want to do this after doing a "git-read-tree",
634 * to link up the stat cache details with the proper files.
636 struct cache_entry
*refresh_cache_entry(struct cache_entry
*ce
, int really
)
639 struct cache_entry
*updated
;
642 if (lstat(ce
->name
, &st
) < 0) {
647 changed
= ce_match_stat(ce
, &st
, really
);
649 if (really
&& assume_unchanged
&&
650 !(ce
->ce_flags
& htons(CE_VALID
)))
651 ; /* mark this one VALID again */
656 if (ce_modified(ce
, &st
, really
)) {
657 cache_errno
= EINVAL
;
662 updated
= xmalloc(size
);
663 memcpy(updated
, ce
, size
);
664 fill_stat_cache_info(updated
, &st
);
666 /* In this case, if really is not set, we should leave
667 * CE_VALID bit alone. Otherwise, paths marked with
668 * --no-assume-unchanged (i.e. things to be edited) will
669 * reacquire CE_VALID bit automatically, which is not
670 * really what we want.
672 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
673 updated
->ce_flags
&= ~htons(CE_VALID
);
678 int refresh_cache(unsigned int flags
)
682 int really
= (flags
& REFRESH_REALLY
) != 0;
683 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
684 int quiet
= (flags
& REFRESH_QUIET
) != 0;
685 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
687 for (i
= 0; i
< active_nr
; i
++) {
688 struct cache_entry
*ce
, *new;
689 ce
= active_cache
[i
];
691 while ((i
< active_nr
) &&
692 ! strcmp(active_cache
[i
]->name
, ce
->name
))
697 printf("%s: needs merge\n", ce
->name
);
702 new = refresh_cache_entry(ce
, really
);
706 if (not_new
&& cache_errno
== ENOENT
)
708 if (really
&& cache_errno
== EINVAL
) {
709 /* If we are doing --really-refresh that
710 * means the index is not valid anymore.
712 ce
->ce_flags
&= ~htons(CE_VALID
);
713 active_cache_changed
= 1;
717 printf("%s: needs update\n", ce
->name
);
721 active_cache_changed
= 1;
722 /* You can NOT just free active_cache[i] here, since it
723 * might not be necessarily malloc()ed but can also come
725 active_cache
[i
] = new;
730 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
733 unsigned char sha1
[20];
735 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
736 return error("bad signature");
737 if (hdr
->hdr_version
!= htonl(2))
738 return error("bad index version");
740 SHA1_Update(&c
, hdr
, size
- 20);
741 SHA1_Final(sha1
, &c
);
742 if (memcmp(sha1
, (char *) hdr
+ size
- 20, 20))
743 return error("bad index file sha1 signature");
747 static int read_index_extension(const char *ext
, void *data
, unsigned long sz
)
749 switch (CACHE_EXT(ext
)) {
751 active_cache_tree
= cache_tree_read(data
, sz
);
754 if (*ext
< 'A' || 'Z' < *ext
)
755 return error("index uses %.4s extension, which we do not understand",
757 fprintf(stderr
, "ignoring %.4s extension\n", ext
);
765 return read_cache_from(get_index_file());
768 /* remember to discard_cache() before reading a different cache! */
769 int read_cache_from(const char *path
)
773 unsigned long offset
;
774 struct cache_header
*hdr
;
781 index_file_timestamp
= 0;
782 fd
= open(path
, O_RDONLY
);
786 die("index file open failed (%s)", strerror(errno
));
789 cache_mmap
= MAP_FAILED
;
790 if (!fstat(fd
, &st
)) {
791 cache_mmap_size
= st
.st_size
;
793 if (cache_mmap_size
>= sizeof(struct cache_header
) + 20)
794 cache_mmap
= mmap(NULL
, cache_mmap_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
797 if (cache_mmap
== MAP_FAILED
)
798 die("index file mmap failed (%s)", strerror(errno
));
801 if (verify_hdr(hdr
, cache_mmap_size
) < 0)
804 active_nr
= ntohl(hdr
->hdr_entries
);
805 active_alloc
= alloc_nr(active_nr
);
806 active_cache
= xcalloc(active_alloc
, sizeof(struct cache_entry
*));
808 offset
= sizeof(*hdr
);
809 for (i
= 0; i
< active_nr
; i
++) {
810 struct cache_entry
*ce
= (struct cache_entry
*) ((char *) cache_mmap
+ offset
);
811 offset
= offset
+ ce_size(ce
);
812 active_cache
[i
] = ce
;
814 index_file_timestamp
= st
.st_mtime
;
815 while (offset
<= cache_mmap_size
- 20 - 8) {
816 /* After an array of active_nr index entries,
817 * there can be arbitrary number of extended
818 * sections, each of which is prefixed with
819 * extension name (4-byte) and section length
820 * in 4-byte network byte order.
822 unsigned long extsize
;
823 memcpy(&extsize
, (char *) cache_mmap
+ offset
+ 4, 4);
824 extsize
= ntohl(extsize
);
825 if (read_index_extension(((const char *) cache_mmap
) + offset
,
826 (char *) cache_mmap
+ offset
+ 8,
835 munmap(cache_mmap
, cache_mmap_size
);
837 die("index file corrupt");
840 #define WRITE_BUFFER_SIZE 8192
841 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
842 static unsigned long write_buffer_len
;
844 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
847 unsigned int buffered
= write_buffer_len
;
848 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
851 memcpy(write_buffer
+ buffered
, data
, partial
);
853 if (buffered
== WRITE_BUFFER_SIZE
) {
854 SHA1_Update(context
, write_buffer
, WRITE_BUFFER_SIZE
);
855 if (write(fd
, write_buffer
, WRITE_BUFFER_SIZE
) != WRITE_BUFFER_SIZE
)
859 write_buffer_len
= buffered
;
861 data
= (char *) data
+ partial
;
866 static int write_index_ext_header(SHA_CTX
*context
, int fd
,
867 unsigned int ext
, unsigned int sz
)
871 if ((ce_write(context
, fd
, &ext
, 4) < 0) ||
872 (ce_write(context
, fd
, &sz
, 4) < 0))
877 static int ce_flush(SHA_CTX
*context
, int fd
)
879 unsigned int left
= write_buffer_len
;
882 write_buffer_len
= 0;
883 SHA1_Update(context
, write_buffer
, left
);
886 /* Flush first if not enough space for SHA1 signature */
887 if (left
+ 20 > WRITE_BUFFER_SIZE
) {
888 if (write(fd
, write_buffer
, left
) != left
)
893 /* Append the SHA1 signature at the end */
894 SHA1_Final(write_buffer
+ left
, context
);
896 if (write(fd
, write_buffer
, left
) != left
)
901 static void ce_smudge_racily_clean_entry(struct cache_entry
*ce
)
904 * The only thing we care about in this function is to smudge the
905 * falsely clean entry due to touch-update-touch race, so we leave
906 * everything else as they are. We are called for entries whose
907 * ce_mtime match the index file mtime.
911 if (lstat(ce
->name
, &st
) < 0)
913 if (ce_match_stat_basic(ce
, &st
))
915 if (ce_modified_check_fs(ce
, &st
)) {
916 /* This is "racily clean"; smudge it. Note that this
917 * is a tricky code. At first glance, it may appear
918 * that it can break with this sequence:
920 * $ echo xyzzy >frotz
921 * $ git-update-index --add frotz
924 * $ echo filfre >nitfol
925 * $ git-update-index --add nitfol
927 * but it does not. When the second update-index runs,
928 * it notices that the entry "frotz" has the same timestamp
929 * as index, and if we were to smudge it by resetting its
930 * size to zero here, then the object name recorded
931 * in index is the 6-byte file but the cached stat information
932 * becomes zero --- which would then match what we would
933 * obtain from the filesystem next time we stat("frotz").
935 * However, the second update-index, before calling
936 * this function, notices that the cached size is 6
937 * bytes and what is on the filesystem is an empty
938 * file, and never calls us, so the cached size information
939 * for "frotz" stays 6 which does not match the filesystem.
941 ce
->ce_size
= htonl(0);
945 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
948 struct cache_header hdr
;
949 int i
, removed
, recent
;
953 for (i
= removed
= 0; i
< entries
; i
++)
954 if (!cache
[i
]->ce_mode
)
957 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
958 hdr
.hdr_version
= htonl(2);
959 hdr
.hdr_entries
= htonl(entries
- removed
);
962 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
965 now
= fstat(newfd
, &st
) ? 0 : st
.st_mtime
;
967 for (i
= 0; i
< entries
; i
++) {
968 struct cache_entry
*ce
= cache
[i
];
969 time_t entry_time
= (time_t) ntohl(ce
->ce_mtime
.sec
);
972 if (index_file_timestamp
&& index_file_timestamp
<= entry_time
)
973 ce_smudge_racily_clean_entry(ce
);
974 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
976 if (now
&& now
<= entry_time
)
980 /* Write extension data here */
981 if (active_cache_tree
) {
983 void *data
= cache_tree_write(active_cache_tree
, &sz
);
985 !write_index_ext_header(&c
, newfd
, CACHE_EXT_TREE
, sz
) &&
986 !ce_write(&c
, newfd
, data
, sz
))
995 * To prevent later ce_match_stat() from always falling into
996 * check_fs(), if we have too many entries that can trigger
997 * racily clean check, we are better off delaying the return.
998 * We arbitrarily say if more than 20 paths or 25% of total
999 * paths are very new, we delay the return until the index
1000 * file gets a new timestamp.
1004 * This assumes that nobody is touching the working tree while
1005 * we are updating the index.
1007 if (20 < recent
|| entries
<= recent
* 4) {
1008 now
= fstat(newfd
, &st
) ? 0 : st
.st_mtime
;
1009 while (now
&& !fstat(newfd
, &st
) && st
.st_mtime
<= now
) {
1010 struct timespec rq
, rm
;
1011 off_t where
= lseek(newfd
, 0, SEEK_CUR
);
1013 rq
.tv_nsec
= 250000000;
1014 nanosleep(&rq
, &rm
);
1015 if ((where
== (off_t
) -1) ||
1016 (write(newfd
, "", 1) != 1) ||
1017 (lseek(newfd
, -1, SEEK_CUR
) != where
) ||
1018 ftruncate(newfd
, where
))
1022 return ce_flush(&c
, newfd
);