2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
7 #include "cache-tree.h"
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
;
22 static time_t index_file_timestamp
;
23 unsigned int active_nr
, active_alloc
, active_cache_changed
;
25 struct cache_tree
*active_cache_tree
;
27 static void *cache_mmap
;
28 static size_t cache_mmap_size
;
31 * This only updates the "non-critical" parts of the directory
32 * cache, ie the parts that aren't tracked by GIT, and only used
33 * to validate the cache.
35 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
37 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
38 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
40 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
41 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
43 ce
->ce_dev
= htonl(st
->st_dev
);
44 ce
->ce_ino
= htonl(st
->st_ino
);
45 ce
->ce_uid
= htonl(st
->st_uid
);
46 ce
->ce_gid
= htonl(st
->st_gid
);
47 ce
->ce_size
= htonl(st
->st_size
);
50 ce
->ce_flags
|= htons(CE_VALID
);
53 static int ce_compare_data(struct cache_entry
*ce
, struct stat
*st
)
56 int fd
= open(ce
->name
, O_RDONLY
);
59 unsigned char sha1
[20];
60 if (!index_fd(sha1
, fd
, st
, 0, OBJ_BLOB
, ce
->name
))
61 match
= hashcmp(sha1
, ce
->sha1
);
62 /* index_fd() closed the file descriptor already */
67 static int ce_compare_link(struct cache_entry
*ce
, size_t expected_size
)
73 enum object_type type
;
76 target
= xmalloc(expected_size
);
77 len
= readlink(ce
->name
, target
, expected_size
);
78 if (len
!= expected_size
) {
82 buffer
= read_sha1_file(ce
->sha1
, &type
, &size
);
87 if (size
== expected_size
)
88 match
= memcmp(buffer
, target
, size
);
94 static int ce_modified_check_fs(struct cache_entry
*ce
, struct stat
*st
)
96 switch (st
->st_mode
& S_IFMT
) {
98 if (ce_compare_data(ce
, st
))
102 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
111 static int ce_match_stat_basic(struct cache_entry
*ce
, struct stat
*st
)
113 unsigned int changed
= 0;
115 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
117 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
118 /* We consider only the owner x bit to be relevant for
121 if (trust_executable_bit
&&
122 (0100 & (ntohl(ce
->ce_mode
) ^ st
->st_mode
)))
123 changed
|= MODE_CHANGED
;
126 if (!S_ISLNK(st
->st_mode
) &&
127 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
128 changed
|= TYPE_CHANGED
;
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
;
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
;
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
;
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
;
166 if (ce
->ce_size
!= htonl(st
->st_size
))
167 changed
|= DATA_CHANGED
;
172 int ce_match_stat(struct cache_entry
*ce
, struct stat
*st
, int options
)
174 unsigned int changed
;
175 int ignore_valid
= options
& 01;
176 int assume_racy_is_modified
= options
& 02;
179 * If it's marked as always valid in the index, it's
180 * valid whatever the checked-out copy says.
182 if (!ignore_valid
&& (ce
->ce_flags
& htons(CE_VALID
)))
185 changed
= ce_match_stat_basic(ce
, st
);
188 * Within 1 second of this sequence:
189 * echo xyzzy >file && git-update-index --add file
190 * running this command:
192 * would give a falsely clean cache entry. The mtime and
193 * length match the cache, and other stat fields do not change.
195 * We could detect this at update-index time (the cache entry
196 * being registered/updated records the same time as "now")
197 * and delay the return from git-update-index, but that would
198 * effectively mean we can make at most one commit per second,
199 * which is not acceptable. Instead, we check cache entries
200 * whose mtime are the same as the index file timestamp more
201 * carefully than others.
204 index_file_timestamp
&&
205 index_file_timestamp
<= ntohl(ce
->ce_mtime
.sec
)) {
206 if (assume_racy_is_modified
)
207 changed
|= DATA_CHANGED
;
209 changed
|= ce_modified_check_fs(ce
, st
);
215 int ce_modified(struct cache_entry
*ce
, struct stat
*st
, int really
)
217 int changed
, changed_fs
;
218 changed
= ce_match_stat(ce
, st
, really
);
222 * If the mode or type has changed, there's no point in trying
223 * to refresh the entry - it's not going to match
225 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
228 /* Immediately after read-tree or update-index --cacheinfo,
229 * the length field is zero. For other cases the ce_size
230 * should match the SHA1 recorded in the index entry.
232 if ((changed
& DATA_CHANGED
) && ce
->ce_size
!= htonl(0))
235 changed_fs
= ce_modified_check_fs(ce
, st
);
237 return changed
| changed_fs
;
241 int base_name_compare(const char *name1
, int len1
, int mode1
,
242 const char *name2
, int len2
, int mode2
)
244 unsigned char c1
, c2
;
245 int len
= len1
< len2
? len1
: len2
;
248 cmp
= memcmp(name1
, name2
, len
);
253 if (!c1
&& S_ISDIR(mode1
))
255 if (!c2
&& S_ISDIR(mode2
))
257 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
260 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
262 int len1
= flags1
& CE_NAMEMASK
;
263 int len2
= flags2
& CE_NAMEMASK
;
264 int len
= len1
< len2
? len1
: len2
;
267 cmp
= memcmp(name1
, name2
, len
);
276 flags1
&= CE_STAGEMASK
;
277 flags2
&= CE_STAGEMASK
;
286 int cache_name_pos(const char *name
, int namelen
)
292 while (last
> first
) {
293 int next
= (last
+ first
) >> 1;
294 struct cache_entry
*ce
= active_cache
[next
];
295 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ntohs(ce
->ce_flags
));
307 /* Remove entry, return true if there are more entries to go.. */
308 int remove_cache_entry_at(int pos
)
310 active_cache_changed
= 1;
312 if (pos
>= active_nr
)
314 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
318 int remove_file_from_cache(const char *path
)
320 int pos
= cache_name_pos(path
, strlen(path
));
323 while (pos
< active_nr
&& !strcmp(active_cache
[pos
]->name
, path
))
324 remove_cache_entry_at(pos
);
328 int add_file_to_cache(const char *path
, int verbose
)
332 struct cache_entry
*ce
;
334 if (lstat(path
, &st
))
335 die("%s: unable to stat (%s)", path
, strerror(errno
));
337 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
))
338 die("%s: can only add regular files or symbolic links", path
);
340 namelen
= strlen(path
);
341 size
= cache_entry_size(namelen
);
342 ce
= xcalloc(1, size
);
343 memcpy(ce
->name
, path
, namelen
);
344 ce
->ce_flags
= htons(namelen
);
345 fill_stat_cache_info(ce
, &st
);
347 if (trust_executable_bit
&& has_symlinks
)
348 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
350 /* If there is an existing entry, pick the mode bits and type
351 * from it, otherwise assume unexecutable regular file.
353 struct cache_entry
*ent
;
354 int pos
= cache_name_pos(path
, namelen
);
356 ent
= (0 <= pos
) ? active_cache
[pos
] : NULL
;
357 ce
->ce_mode
= ce_mode_from_stat(ent
, st
.st_mode
);
360 if (index_path(ce
->sha1
, path
, &st
, 1))
361 die("unable to index file %s", path
);
362 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
))
363 die("unable to add %s to index",path
);
365 printf("add '%s'\n", path
);
366 cache_tree_invalidate_path(active_cache_tree
, path
);
370 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
372 int len
= ce_namelen(a
);
373 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
376 int ce_path_match(const struct cache_entry
*ce
, const char **pathspec
)
378 const char *match
, *name
;
384 len
= ce_namelen(ce
);
386 while ((match
= *pathspec
++) != NULL
) {
387 int matchlen
= strlen(match
);
390 if (memcmp(name
, match
, matchlen
))
392 if (matchlen
&& name
[matchlen
-1] == '/')
394 if (name
[matchlen
] == '/' || !name
[matchlen
])
403 * We fundamentally don't like some paths: we don't want
404 * dot or dot-dot anywhere, and for obvious reasons don't
405 * want to recurse into ".git" either.
407 * Also, we don't want double slashes or slashes at the
408 * end that can make pathnames ambiguous.
410 static int verify_dotfile(const char *rest
)
413 * The first character was '.', but that
414 * has already been discarded, we now test
418 /* "." is not allowed */
423 * ".git" followed by NUL or slash is bad. This
424 * shares the path end test with the ".." case.
434 if (rest
[1] == '\0' || rest
[1] == '/')
440 int verify_path(const char *path
)
457 if (verify_dotfile(path
))
467 * Do we have another file that has the beginning components being a
468 * proper superset of the name we're trying to add?
470 static int has_file_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
473 int len
= ce_namelen(ce
);
474 int stage
= ce_stage(ce
);
475 const char *name
= ce
->name
;
477 while (pos
< active_nr
) {
478 struct cache_entry
*p
= active_cache
[pos
++];
480 if (len
>= ce_namelen(p
))
482 if (memcmp(name
, p
->name
, len
))
484 if (ce_stage(p
) != stage
)
486 if (p
->name
[len
] != '/')
488 if (!ce_stage(p
) && !p
->ce_mode
)
493 remove_cache_entry_at(--pos
);
499 * Do we have another file with a pathname that is a proper
500 * subset of the name we're trying to add?
502 static int has_dir_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
505 int stage
= ce_stage(ce
);
506 const char *name
= ce
->name
;
507 const char *slash
= name
+ ce_namelen(ce
);
515 if (slash
<= ce
->name
)
520 pos
= cache_name_pos(name
, ntohs(create_ce_flags(len
, stage
)));
523 * Found one, but not so fast. This could
524 * be a marker that says "I was here, but
525 * I am being removed". Such an entry is
526 * not a part of the resulting tree, and
527 * it is Ok to have a directory at the same
530 if (stage
|| active_cache
[pos
]->ce_mode
) {
534 remove_cache_entry_at(pos
);
542 * Trivial optimization: if we find an entry that
543 * already matches the sub-directory, then we know
544 * we're ok, and we can exit.
546 while (pos
< active_nr
) {
547 struct cache_entry
*p
= active_cache
[pos
];
548 if ((ce_namelen(p
) <= len
) ||
549 (p
->name
[len
] != '/') ||
550 memcmp(p
->name
, name
, len
))
551 break; /* not our subdirectory */
552 if (ce_stage(p
) == stage
&& (stage
|| p
->ce_mode
))
553 /* p is at the same stage as our entry, and
554 * is a subdirectory of what we are looking
555 * at, so we cannot have conflicts at our
556 * level or anything shorter.
565 /* We may be in a situation where we already have path/file and path
566 * is being added, or we already have path and path/file is being
567 * added. Either one would result in a nonsense tree that has path
568 * twice when git-write-tree tries to write it out. Prevent it.
570 * If ok-to-replace is specified, we remove the conflicting entries
571 * from the cache so the caller should recompute the insert position.
572 * When this happens, we return non-zero.
574 static int check_file_directory_conflict(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
579 * When ce is an "I am going away" entry, we allow it to be added
581 if (!ce_stage(ce
) && !ce
->ce_mode
)
585 * We check if the path is a sub-path of a subsequent pathname
586 * first, since removing those will not change the position
589 retval
= has_file_name(ce
, pos
, ok_to_replace
);
592 * Then check if the path might have a clashing sub-directory
595 return retval
+ has_dir_name(ce
, pos
, ok_to_replace
);
598 int add_cache_entry(struct cache_entry
*ce
, int option
)
601 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
602 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
603 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
605 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
607 /* existing match? Just replace it. */
609 active_cache_changed
= 1;
610 active_cache
[pos
] = ce
;
616 * Inserting a merged entry ("stage 0") into the index
617 * will always replace all non-merged entries..
619 if (pos
< active_nr
&& ce_stage(ce
) == 0) {
620 while (ce_same_name(active_cache
[pos
], ce
)) {
622 if (!remove_cache_entry_at(pos
))
629 if (!verify_path(ce
->name
))
632 if (!skip_df_check
&&
633 check_file_directory_conflict(ce
, pos
, ok_to_replace
)) {
635 return error("'%s' appears as both a file and as a directory", ce
->name
);
636 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
640 /* Make sure the array is big enough .. */
641 if (active_nr
== active_alloc
) {
642 active_alloc
= alloc_nr(active_alloc
);
643 active_cache
= xrealloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
649 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
650 active_cache
[pos
] = ce
;
651 active_cache_changed
= 1;
656 * "refresh" does not calculate a new sha1 file or bring the
657 * cache up-to-date for mode/content changes. But what it
658 * _does_ do is to "re-match" the stat information of a file
659 * with the cache, so that you can refresh the cache for a
660 * file that hasn't been changed but where the stat entry is
663 * For example, you'd want to do this after doing a "git-read-tree",
664 * to link up the stat cache details with the proper files.
666 static struct cache_entry
*refresh_cache_ent(struct cache_entry
*ce
, int really
, int *err
)
669 struct cache_entry
*updated
;
672 if (lstat(ce
->name
, &st
) < 0) {
678 changed
= ce_match_stat(ce
, &st
, really
);
680 if (really
&& assume_unchanged
&&
681 !(ce
->ce_flags
& htons(CE_VALID
)))
682 ; /* mark this one VALID again */
687 if (ce_modified(ce
, &st
, really
)) {
694 updated
= xmalloc(size
);
695 memcpy(updated
, ce
, size
);
696 fill_stat_cache_info(updated
, &st
);
698 /* In this case, if really is not set, we should leave
699 * CE_VALID bit alone. Otherwise, paths marked with
700 * --no-assume-unchanged (i.e. things to be edited) will
701 * reacquire CE_VALID bit automatically, which is not
702 * really what we want.
704 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
705 updated
->ce_flags
&= ~htons(CE_VALID
);
710 int refresh_cache(unsigned int flags
)
714 int really
= (flags
& REFRESH_REALLY
) != 0;
715 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
716 int quiet
= (flags
& REFRESH_QUIET
) != 0;
717 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
719 for (i
= 0; i
< active_nr
; i
++) {
720 struct cache_entry
*ce
, *new;
723 ce
= active_cache
[i
];
725 while ((i
< active_nr
) &&
726 ! strcmp(active_cache
[i
]->name
, ce
->name
))
731 printf("%s: needs merge\n", ce
->name
);
736 new = refresh_cache_ent(ce
, really
, &cache_errno
);
740 if (not_new
&& cache_errno
== ENOENT
)
742 if (really
&& cache_errno
== EINVAL
) {
743 /* If we are doing --really-refresh that
744 * means the index is not valid anymore.
746 ce
->ce_flags
&= ~htons(CE_VALID
);
747 active_cache_changed
= 1;
751 printf("%s: needs update\n", ce
->name
);
755 active_cache_changed
= 1;
756 /* You can NOT just free active_cache[i] here, since it
757 * might not be necessarily malloc()ed but can also come
759 active_cache
[i
] = new;
764 struct cache_entry
*refresh_cache_entry(struct cache_entry
*ce
, int really
)
766 return refresh_cache_ent(ce
, really
, NULL
);
769 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
772 unsigned char sha1
[20];
774 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
775 return error("bad signature");
776 if (hdr
->hdr_version
!= htonl(2))
777 return error("bad index version");
779 SHA1_Update(&c
, hdr
, size
- 20);
780 SHA1_Final(sha1
, &c
);
781 if (hashcmp(sha1
, (unsigned char *)hdr
+ size
- 20))
782 return error("bad index file sha1 signature");
786 static int read_index_extension(const char *ext
, void *data
, unsigned long sz
)
788 switch (CACHE_EXT(ext
)) {
790 active_cache_tree
= cache_tree_read(data
, sz
);
793 if (*ext
< 'A' || 'Z' < *ext
)
794 return error("index uses %.4s extension, which we do not understand",
796 fprintf(stderr
, "ignoring %.4s extension\n", ext
);
804 return read_cache_from(get_index_file());
807 /* remember to discard_cache() before reading a different cache! */
808 int read_cache_from(const char *path
)
812 unsigned long offset
;
813 struct cache_header
*hdr
;
820 index_file_timestamp
= 0;
821 fd
= open(path
, O_RDONLY
);
825 die("index file open failed (%s)", strerror(errno
));
828 if (!fstat(fd
, &st
)) {
829 cache_mmap_size
= xsize_t(st
.st_size
);
831 if (cache_mmap_size
>= sizeof(struct cache_header
) + 20)
832 cache_mmap
= xmmap(NULL
, cache_mmap_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
834 die("index file smaller than expected");
836 die("cannot stat the open index (%s)", strerror(errno
));
840 if (verify_hdr(hdr
, cache_mmap_size
) < 0)
843 active_nr
= ntohl(hdr
->hdr_entries
);
844 active_alloc
= alloc_nr(active_nr
);
845 active_cache
= xcalloc(active_alloc
, sizeof(struct cache_entry
*));
847 offset
= sizeof(*hdr
);
848 for (i
= 0; i
< active_nr
; i
++) {
849 struct cache_entry
*ce
= (struct cache_entry
*) ((char *) cache_mmap
+ offset
);
850 offset
= offset
+ ce_size(ce
);
851 active_cache
[i
] = ce
;
853 index_file_timestamp
= st
.st_mtime
;
854 while (offset
<= cache_mmap_size
- 20 - 8) {
855 /* After an array of active_nr index entries,
856 * there can be arbitrary number of extended
857 * sections, each of which is prefixed with
858 * extension name (4-byte) and section length
859 * in 4-byte network byte order.
861 unsigned long extsize
;
862 memcpy(&extsize
, (char *) cache_mmap
+ offset
+ 4, 4);
863 extsize
= ntohl(extsize
);
864 if (read_index_extension(((const char *) cache_mmap
) + offset
,
865 (char *) cache_mmap
+ offset
+ 8,
874 munmap(cache_mmap
, cache_mmap_size
);
876 die("index file corrupt");
879 int discard_cache(void)
883 active_nr
= active_cache_changed
= 0;
884 index_file_timestamp
= 0;
885 cache_tree_free(&active_cache_tree
);
886 if (cache_mmap
== NULL
)
888 ret
= munmap(cache_mmap
, cache_mmap_size
);
892 /* no need to throw away allocated active_cache */
896 #define WRITE_BUFFER_SIZE 8192
897 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
898 static unsigned long write_buffer_len
;
900 static int ce_write_flush(SHA_CTX
*context
, int fd
)
902 unsigned int buffered
= write_buffer_len
;
904 SHA1_Update(context
, write_buffer
, buffered
);
905 if (write_in_full(fd
, write_buffer
, buffered
) != buffered
)
907 write_buffer_len
= 0;
912 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
915 unsigned int buffered
= write_buffer_len
;
916 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
919 memcpy(write_buffer
+ buffered
, data
, partial
);
921 if (buffered
== WRITE_BUFFER_SIZE
) {
922 write_buffer_len
= buffered
;
923 if (ce_write_flush(context
, fd
))
927 write_buffer_len
= buffered
;
929 data
= (char *) data
+ partial
;
934 static int write_index_ext_header(SHA_CTX
*context
, int fd
,
935 unsigned int ext
, unsigned int sz
)
939 return ((ce_write(context
, fd
, &ext
, 4) < 0) ||
940 (ce_write(context
, fd
, &sz
, 4) < 0)) ? -1 : 0;
943 static int ce_flush(SHA_CTX
*context
, int fd
)
945 unsigned int left
= write_buffer_len
;
948 write_buffer_len
= 0;
949 SHA1_Update(context
, write_buffer
, left
);
952 /* Flush first if not enough space for SHA1 signature */
953 if (left
+ 20 > WRITE_BUFFER_SIZE
) {
954 if (write_in_full(fd
, write_buffer
, left
) != left
)
959 /* Append the SHA1 signature at the end */
960 SHA1_Final(write_buffer
+ left
, context
);
962 return (write_in_full(fd
, write_buffer
, left
) != left
) ? -1 : 0;
965 static void ce_smudge_racily_clean_entry(struct cache_entry
*ce
)
968 * The only thing we care about in this function is to smudge the
969 * falsely clean entry due to touch-update-touch race, so we leave
970 * everything else as they are. We are called for entries whose
971 * ce_mtime match the index file mtime.
975 if (lstat(ce
->name
, &st
) < 0)
977 if (ce_match_stat_basic(ce
, &st
))
979 if (ce_modified_check_fs(ce
, &st
)) {
980 /* This is "racily clean"; smudge it. Note that this
981 * is a tricky code. At first glance, it may appear
982 * that it can break with this sequence:
984 * $ echo xyzzy >frotz
985 * $ git-update-index --add frotz
988 * $ echo filfre >nitfol
989 * $ git-update-index --add nitfol
991 * but it does not. When the second update-index runs,
992 * it notices that the entry "frotz" has the same timestamp
993 * as index, and if we were to smudge it by resetting its
994 * size to zero here, then the object name recorded
995 * in index is the 6-byte file but the cached stat information
996 * becomes zero --- which would then match what we would
997 * obtain from the filesystem next time we stat("frotz").
999 * However, the second update-index, before calling
1000 * this function, notices that the cached size is 6
1001 * bytes and what is on the filesystem is an empty
1002 * file, and never calls us, so the cached size information
1003 * for "frotz" stays 6 which does not match the filesystem.
1005 ce
->ce_size
= htonl(0);
1009 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
1012 struct cache_header hdr
;
1015 for (i
= removed
= 0; i
< entries
; i
++)
1016 if (!cache
[i
]->ce_mode
)
1019 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
1020 hdr
.hdr_version
= htonl(2);
1021 hdr
.hdr_entries
= htonl(entries
- removed
);
1024 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
1027 for (i
= 0; i
< entries
; i
++) {
1028 struct cache_entry
*ce
= cache
[i
];
1031 if (index_file_timestamp
&&
1032 index_file_timestamp
<= ntohl(ce
->ce_mtime
.sec
))
1033 ce_smudge_racily_clean_entry(ce
);
1034 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
1038 /* Write extension data here */
1039 if (active_cache_tree
) {
1041 void *data
= cache_tree_write(active_cache_tree
, &sz
);
1043 !write_index_ext_header(&c
, newfd
, CACHE_EXT_TREE
, sz
) &&
1044 !ce_write(&c
, newfd
, data
, sz
))
1051 return ce_flush(&c
, newfd
);