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
;
23 static time_t index_file_timestamp
;
24 unsigned int active_nr
, active_alloc
, active_cache_changed
;
26 struct cache_tree
*active_cache_tree
;
28 static void *cache_mmap
;
29 static size_t cache_mmap_size
;
32 * This only updates the "non-critical" parts of the directory
33 * cache, ie the parts that aren't tracked by GIT, and only used
34 * to validate the cache.
36 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
38 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
39 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
41 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
42 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
44 ce
->ce_dev
= htonl(st
->st_dev
);
45 ce
->ce_ino
= htonl(st
->st_ino
);
46 ce
->ce_uid
= htonl(st
->st_uid
);
47 ce
->ce_gid
= htonl(st
->st_gid
);
48 ce
->ce_size
= htonl(st
->st_size
);
51 ce
->ce_flags
|= htons(CE_VALID
);
54 static int ce_compare_data(struct cache_entry
*ce
, struct stat
*st
)
57 int fd
= open(ce
->name
, O_RDONLY
);
60 unsigned char sha1
[20];
61 if (!index_fd(sha1
, fd
, st
, 0, OBJ_BLOB
, ce
->name
))
62 match
= hashcmp(sha1
, ce
->sha1
);
63 /* index_fd() closed the file descriptor already */
68 static int ce_compare_link(struct cache_entry
*ce
, size_t expected_size
)
74 enum object_type type
;
77 target
= xmalloc(expected_size
);
78 len
= readlink(ce
->name
, target
, expected_size
);
79 if (len
!= expected_size
) {
83 buffer
= read_sha1_file(ce
->sha1
, &type
, &size
);
88 if (size
== expected_size
)
89 match
= memcmp(buffer
, target
, size
);
95 static int ce_compare_gitlink(struct cache_entry
*ce
)
97 unsigned char sha1
[20];
100 * We don't actually require that the .git directory
101 * under DIRLNK directory be a valid git directory. It
102 * might even be missing (in case nobody populated that
105 * If so, we consider it always to match.
107 if (resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) < 0)
109 return hashcmp(sha1
, ce
->sha1
);
112 static int ce_modified_check_fs(struct cache_entry
*ce
, struct stat
*st
)
114 switch (st
->st_mode
& S_IFMT
) {
116 if (ce_compare_data(ce
, st
))
120 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
124 /* No need to do anything, we did the exact compare in "match_stat_basic" */
132 static int ce_match_stat_basic(struct cache_entry
*ce
, struct stat
*st
)
134 unsigned int changed
= 0;
136 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
138 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
139 /* We consider only the owner x bit to be relevant for
142 if (trust_executable_bit
&&
143 (0100 & (ntohl(ce
->ce_mode
) ^ st
->st_mode
)))
144 changed
|= MODE_CHANGED
;
147 if (!S_ISLNK(st
->st_mode
) &&
148 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
149 changed
|= TYPE_CHANGED
;
152 if (!S_ISDIR(st
->st_mode
))
153 changed
|= TYPE_CHANGED
;
154 else if (ce_compare_gitlink(ce
))
155 changed
|= DATA_CHANGED
;
158 die("internal error: ce_mode is %o", ntohl(ce
->ce_mode
));
160 if (ce
->ce_mtime
.sec
!= htonl(st
->st_mtime
))
161 changed
|= MTIME_CHANGED
;
162 if (ce
->ce_ctime
.sec
!= htonl(st
->st_ctime
))
163 changed
|= CTIME_CHANGED
;
167 * nsec seems unreliable - not all filesystems support it, so
168 * as long as it is in the inode cache you get right nsec
169 * but after it gets flushed, you get zero nsec.
171 if (ce
->ce_mtime
.nsec
!= htonl(st
->st_mtim
.tv_nsec
))
172 changed
|= MTIME_CHANGED
;
173 if (ce
->ce_ctime
.nsec
!= htonl(st
->st_ctim
.tv_nsec
))
174 changed
|= CTIME_CHANGED
;
177 if (ce
->ce_uid
!= htonl(st
->st_uid
) ||
178 ce
->ce_gid
!= htonl(st
->st_gid
))
179 changed
|= OWNER_CHANGED
;
180 if (ce
->ce_ino
!= htonl(st
->st_ino
))
181 changed
|= INODE_CHANGED
;
185 * st_dev breaks on network filesystems where different
186 * clients will have different views of what "device"
187 * the filesystem is on
189 if (ce
->ce_dev
!= htonl(st
->st_dev
))
190 changed
|= INODE_CHANGED
;
193 if (ce
->ce_size
!= htonl(st
->st_size
))
194 changed
|= DATA_CHANGED
;
199 int ce_match_stat(struct cache_entry
*ce
, struct stat
*st
, int options
)
201 unsigned int changed
;
202 int ignore_valid
= options
& 01;
203 int assume_racy_is_modified
= options
& 02;
206 * If it's marked as always valid in the index, it's
207 * valid whatever the checked-out copy says.
209 if (!ignore_valid
&& (ce
->ce_flags
& htons(CE_VALID
)))
212 changed
= ce_match_stat_basic(ce
, st
);
215 * Within 1 second of this sequence:
216 * echo xyzzy >file && git-update-index --add file
217 * running this command:
219 * would give a falsely clean cache entry. The mtime and
220 * length match the cache, and other stat fields do not change.
222 * We could detect this at update-index time (the cache entry
223 * being registered/updated records the same time as "now")
224 * and delay the return from git-update-index, but that would
225 * effectively mean we can make at most one commit per second,
226 * which is not acceptable. Instead, we check cache entries
227 * whose mtime are the same as the index file timestamp more
228 * carefully than others.
231 index_file_timestamp
&&
232 index_file_timestamp
<= ntohl(ce
->ce_mtime
.sec
)) {
233 if (assume_racy_is_modified
)
234 changed
|= DATA_CHANGED
;
236 changed
|= ce_modified_check_fs(ce
, st
);
242 int ce_modified(struct cache_entry
*ce
, struct stat
*st
, int really
)
244 int changed
, changed_fs
;
245 changed
= ce_match_stat(ce
, st
, really
);
249 * If the mode or type has changed, there's no point in trying
250 * to refresh the entry - it's not going to match
252 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
255 /* Immediately after read-tree or update-index --cacheinfo,
256 * the length field is zero. For other cases the ce_size
257 * should match the SHA1 recorded in the index entry.
259 if ((changed
& DATA_CHANGED
) && ce
->ce_size
!= htonl(0))
262 changed_fs
= ce_modified_check_fs(ce
, st
);
264 return changed
| changed_fs
;
268 int base_name_compare(const char *name1
, int len1
, int mode1
,
269 const char *name2
, int len2
, int mode2
)
271 unsigned char c1
, c2
;
272 int len
= len1
< len2
? len1
: len2
;
275 cmp
= memcmp(name1
, name2
, len
);
280 if (!c1
&& (S_ISDIR(mode1
) || S_ISDIRLNK(mode1
)))
282 if (!c2
&& (S_ISDIR(mode2
) || S_ISDIRLNK(mode2
)))
284 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
287 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
289 int len1
= flags1
& CE_NAMEMASK
;
290 int len2
= flags2
& CE_NAMEMASK
;
291 int len
= len1
< len2
? len1
: len2
;
294 cmp
= memcmp(name1
, name2
, len
);
303 flags1
&= CE_STAGEMASK
;
304 flags2
&= CE_STAGEMASK
;
313 int cache_name_pos(const char *name
, int namelen
)
319 while (last
> first
) {
320 int next
= (last
+ first
) >> 1;
321 struct cache_entry
*ce
= active_cache
[next
];
322 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ntohs(ce
->ce_flags
));
334 /* Remove entry, return true if there are more entries to go.. */
335 int remove_cache_entry_at(int pos
)
337 active_cache_changed
= 1;
339 if (pos
>= active_nr
)
341 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
) * sizeof(struct cache_entry
*));
345 int remove_file_from_cache(const char *path
)
347 int pos
= cache_name_pos(path
, strlen(path
));
350 while (pos
< active_nr
&& !strcmp(active_cache
[pos
]->name
, path
))
351 remove_cache_entry_at(pos
);
355 int add_file_to_cache(const char *path
, int verbose
)
359 struct cache_entry
*ce
;
361 if (lstat(path
, &st
))
362 die("%s: unable to stat (%s)", path
, strerror(errno
));
364 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
365 die("%s: can only add regular files, symbolic links or git-directories", path
);
367 namelen
= strlen(path
);
368 size
= cache_entry_size(namelen
);
369 ce
= xcalloc(1, size
);
370 memcpy(ce
->name
, path
, namelen
);
371 ce
->ce_flags
= htons(namelen
);
372 fill_stat_cache_info(ce
, &st
);
374 if (trust_executable_bit
&& has_symlinks
)
375 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
377 /* If there is an existing entry, pick the mode bits and type
378 * from it, otherwise assume unexecutable regular file.
380 struct cache_entry
*ent
;
381 int pos
= cache_name_pos(path
, namelen
);
383 ent
= (0 <= pos
) ? active_cache
[pos
] : NULL
;
384 ce
->ce_mode
= ce_mode_from_stat(ent
, st
.st_mode
);
387 if (index_path(ce
->sha1
, path
, &st
, 1))
388 die("unable to index file %s", path
);
389 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
))
390 die("unable to add %s to index",path
);
392 printf("add '%s'\n", path
);
393 cache_tree_invalidate_path(active_cache_tree
, path
);
397 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
399 int len
= ce_namelen(a
);
400 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
403 int ce_path_match(const struct cache_entry
*ce
, const char **pathspec
)
405 const char *match
, *name
;
411 len
= ce_namelen(ce
);
413 while ((match
= *pathspec
++) != NULL
) {
414 int matchlen
= strlen(match
);
417 if (memcmp(name
, match
, matchlen
))
419 if (matchlen
&& name
[matchlen
-1] == '/')
421 if (name
[matchlen
] == '/' || !name
[matchlen
])
430 * We fundamentally don't like some paths: we don't want
431 * dot or dot-dot anywhere, and for obvious reasons don't
432 * want to recurse into ".git" either.
434 * Also, we don't want double slashes or slashes at the
435 * end that can make pathnames ambiguous.
437 static int verify_dotfile(const char *rest
)
440 * The first character was '.', but that
441 * has already been discarded, we now test
445 /* "." is not allowed */
450 * ".git" followed by NUL or slash is bad. This
451 * shares the path end test with the ".." case.
461 if (rest
[1] == '\0' || rest
[1] == '/')
467 int verify_path(const char *path
)
484 if (verify_dotfile(path
))
494 * Do we have another file that has the beginning components being a
495 * proper superset of the name we're trying to add?
497 static int has_file_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
500 int len
= ce_namelen(ce
);
501 int stage
= ce_stage(ce
);
502 const char *name
= ce
->name
;
504 while (pos
< active_nr
) {
505 struct cache_entry
*p
= active_cache
[pos
++];
507 if (len
>= ce_namelen(p
))
509 if (memcmp(name
, p
->name
, len
))
511 if (ce_stage(p
) != stage
)
513 if (p
->name
[len
] != '/')
515 if (!ce_stage(p
) && !p
->ce_mode
)
520 remove_cache_entry_at(--pos
);
526 * Do we have another file with a pathname that is a proper
527 * subset of the name we're trying to add?
529 static int has_dir_name(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
532 int stage
= ce_stage(ce
);
533 const char *name
= ce
->name
;
534 const char *slash
= name
+ ce_namelen(ce
);
542 if (slash
<= ce
->name
)
547 pos
= cache_name_pos(name
, ntohs(create_ce_flags(len
, stage
)));
550 * Found one, but not so fast. This could
551 * be a marker that says "I was here, but
552 * I am being removed". Such an entry is
553 * not a part of the resulting tree, and
554 * it is Ok to have a directory at the same
557 if (stage
|| active_cache
[pos
]->ce_mode
) {
561 remove_cache_entry_at(pos
);
569 * Trivial optimization: if we find an entry that
570 * already matches the sub-directory, then we know
571 * we're ok, and we can exit.
573 while (pos
< active_nr
) {
574 struct cache_entry
*p
= active_cache
[pos
];
575 if ((ce_namelen(p
) <= len
) ||
576 (p
->name
[len
] != '/') ||
577 memcmp(p
->name
, name
, len
))
578 break; /* not our subdirectory */
579 if (ce_stage(p
) == stage
&& (stage
|| p
->ce_mode
))
580 /* p is at the same stage as our entry, and
581 * is a subdirectory of what we are looking
582 * at, so we cannot have conflicts at our
583 * level or anything shorter.
592 /* We may be in a situation where we already have path/file and path
593 * is being added, or we already have path and path/file is being
594 * added. Either one would result in a nonsense tree that has path
595 * twice when git-write-tree tries to write it out. Prevent it.
597 * If ok-to-replace is specified, we remove the conflicting entries
598 * from the cache so the caller should recompute the insert position.
599 * When this happens, we return non-zero.
601 static int check_file_directory_conflict(const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
606 * When ce is an "I am going away" entry, we allow it to be added
608 if (!ce_stage(ce
) && !ce
->ce_mode
)
612 * We check if the path is a sub-path of a subsequent pathname
613 * first, since removing those will not change the position
616 retval
= has_file_name(ce
, pos
, ok_to_replace
);
619 * Then check if the path might have a clashing sub-directory
622 return retval
+ has_dir_name(ce
, pos
, ok_to_replace
);
625 int add_cache_entry(struct cache_entry
*ce
, int option
)
628 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
629 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
630 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
632 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
634 /* existing match? Just replace it. */
636 active_cache_changed
= 1;
637 active_cache
[pos
] = ce
;
643 * Inserting a merged entry ("stage 0") into the index
644 * will always replace all non-merged entries..
646 if (pos
< active_nr
&& ce_stage(ce
) == 0) {
647 while (ce_same_name(active_cache
[pos
], ce
)) {
649 if (!remove_cache_entry_at(pos
))
656 if (!verify_path(ce
->name
))
659 if (!skip_df_check
&&
660 check_file_directory_conflict(ce
, pos
, ok_to_replace
)) {
662 return error("'%s' appears as both a file and as a directory", ce
->name
);
663 pos
= cache_name_pos(ce
->name
, ntohs(ce
->ce_flags
));
667 /* Make sure the array is big enough .. */
668 if (active_nr
== active_alloc
) {
669 active_alloc
= alloc_nr(active_alloc
);
670 active_cache
= xrealloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
676 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
677 active_cache
[pos
] = ce
;
678 active_cache_changed
= 1;
683 * "refresh" does not calculate a new sha1 file or bring the
684 * cache up-to-date for mode/content changes. But what it
685 * _does_ do is to "re-match" the stat information of a file
686 * with the cache, so that you can refresh the cache for a
687 * file that hasn't been changed but where the stat entry is
690 * For example, you'd want to do this after doing a "git-read-tree",
691 * to link up the stat cache details with the proper files.
693 static struct cache_entry
*refresh_cache_ent(struct cache_entry
*ce
, int really
, int *err
)
696 struct cache_entry
*updated
;
699 if (lstat(ce
->name
, &st
) < 0) {
705 changed
= ce_match_stat(ce
, &st
, really
);
707 if (really
&& assume_unchanged
&&
708 !(ce
->ce_flags
& htons(CE_VALID
)))
709 ; /* mark this one VALID again */
714 if (ce_modified(ce
, &st
, really
)) {
721 updated
= xmalloc(size
);
722 memcpy(updated
, ce
, size
);
723 fill_stat_cache_info(updated
, &st
);
725 /* In this case, if really is not set, we should leave
726 * CE_VALID bit alone. Otherwise, paths marked with
727 * --no-assume-unchanged (i.e. things to be edited) will
728 * reacquire CE_VALID bit automatically, which is not
729 * really what we want.
731 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
732 updated
->ce_flags
&= ~htons(CE_VALID
);
737 int refresh_cache(unsigned int flags
)
741 int really
= (flags
& REFRESH_REALLY
) != 0;
742 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
743 int quiet
= (flags
& REFRESH_QUIET
) != 0;
744 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
746 for (i
= 0; i
< active_nr
; i
++) {
747 struct cache_entry
*ce
, *new;
750 ce
= active_cache
[i
];
752 while ((i
< active_nr
) &&
753 ! strcmp(active_cache
[i
]->name
, ce
->name
))
758 printf("%s: needs merge\n", ce
->name
);
763 new = refresh_cache_ent(ce
, really
, &cache_errno
);
767 if (not_new
&& cache_errno
== ENOENT
)
769 if (really
&& cache_errno
== EINVAL
) {
770 /* If we are doing --really-refresh that
771 * means the index is not valid anymore.
773 ce
->ce_flags
&= ~htons(CE_VALID
);
774 active_cache_changed
= 1;
778 printf("%s: needs update\n", ce
->name
);
782 active_cache_changed
= 1;
783 /* You can NOT just free active_cache[i] here, since it
784 * might not be necessarily malloc()ed but can also come
786 active_cache
[i
] = new;
791 struct cache_entry
*refresh_cache_entry(struct cache_entry
*ce
, int really
)
793 return refresh_cache_ent(ce
, really
, NULL
);
796 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
799 unsigned char sha1
[20];
801 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
802 return error("bad signature");
803 if (hdr
->hdr_version
!= htonl(2))
804 return error("bad index version");
806 SHA1_Update(&c
, hdr
, size
- 20);
807 SHA1_Final(sha1
, &c
);
808 if (hashcmp(sha1
, (unsigned char *)hdr
+ size
- 20))
809 return error("bad index file sha1 signature");
813 static int read_index_extension(const char *ext
, void *data
, unsigned long sz
)
815 switch (CACHE_EXT(ext
)) {
817 active_cache_tree
= cache_tree_read(data
, sz
);
820 if (*ext
< 'A' || 'Z' < *ext
)
821 return error("index uses %.4s extension, which we do not understand",
823 fprintf(stderr
, "ignoring %.4s extension\n", ext
);
831 return read_cache_from(get_index_file());
834 /* remember to discard_cache() before reading a different cache! */
835 int read_cache_from(const char *path
)
839 unsigned long offset
;
840 struct cache_header
*hdr
;
847 index_file_timestamp
= 0;
848 fd
= open(path
, O_RDONLY
);
852 die("index file open failed (%s)", strerror(errno
));
855 if (!fstat(fd
, &st
)) {
856 cache_mmap_size
= xsize_t(st
.st_size
);
858 if (cache_mmap_size
>= sizeof(struct cache_header
) + 20)
859 cache_mmap
= xmmap(NULL
, cache_mmap_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
861 die("index file smaller than expected");
863 die("cannot stat the open index (%s)", strerror(errno
));
867 if (verify_hdr(hdr
, cache_mmap_size
) < 0)
870 active_nr
= ntohl(hdr
->hdr_entries
);
871 active_alloc
= alloc_nr(active_nr
);
872 active_cache
= xcalloc(active_alloc
, sizeof(struct cache_entry
*));
874 offset
= sizeof(*hdr
);
875 for (i
= 0; i
< active_nr
; i
++) {
876 struct cache_entry
*ce
= (struct cache_entry
*) ((char *) cache_mmap
+ offset
);
877 offset
= offset
+ ce_size(ce
);
878 active_cache
[i
] = ce
;
880 index_file_timestamp
= st
.st_mtime
;
881 while (offset
<= cache_mmap_size
- 20 - 8) {
882 /* After an array of active_nr index entries,
883 * there can be arbitrary number of extended
884 * sections, each of which is prefixed with
885 * extension name (4-byte) and section length
886 * in 4-byte network byte order.
888 unsigned long extsize
;
889 memcpy(&extsize
, (char *) cache_mmap
+ offset
+ 4, 4);
890 extsize
= ntohl(extsize
);
891 if (read_index_extension(((const char *) cache_mmap
) + offset
,
892 (char *) cache_mmap
+ offset
+ 8,
901 munmap(cache_mmap
, cache_mmap_size
);
903 die("index file corrupt");
906 int discard_cache(void)
910 active_nr
= active_cache_changed
= 0;
911 index_file_timestamp
= 0;
912 cache_tree_free(&active_cache_tree
);
913 if (cache_mmap
== NULL
)
915 ret
= munmap(cache_mmap
, cache_mmap_size
);
919 /* no need to throw away allocated active_cache */
923 #define WRITE_BUFFER_SIZE 8192
924 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
925 static unsigned long write_buffer_len
;
927 static int ce_write_flush(SHA_CTX
*context
, int fd
)
929 unsigned int buffered
= write_buffer_len
;
931 SHA1_Update(context
, write_buffer
, buffered
);
932 if (write_in_full(fd
, write_buffer
, buffered
) != buffered
)
934 write_buffer_len
= 0;
939 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
942 unsigned int buffered
= write_buffer_len
;
943 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
946 memcpy(write_buffer
+ buffered
, data
, partial
);
948 if (buffered
== WRITE_BUFFER_SIZE
) {
949 write_buffer_len
= buffered
;
950 if (ce_write_flush(context
, fd
))
954 write_buffer_len
= buffered
;
956 data
= (char *) data
+ partial
;
961 static int write_index_ext_header(SHA_CTX
*context
, int fd
,
962 unsigned int ext
, unsigned int sz
)
966 return ((ce_write(context
, fd
, &ext
, 4) < 0) ||
967 (ce_write(context
, fd
, &sz
, 4) < 0)) ? -1 : 0;
970 static int ce_flush(SHA_CTX
*context
, int fd
)
972 unsigned int left
= write_buffer_len
;
975 write_buffer_len
= 0;
976 SHA1_Update(context
, write_buffer
, left
);
979 /* Flush first if not enough space for SHA1 signature */
980 if (left
+ 20 > WRITE_BUFFER_SIZE
) {
981 if (write_in_full(fd
, write_buffer
, left
) != left
)
986 /* Append the SHA1 signature at the end */
987 SHA1_Final(write_buffer
+ left
, context
);
989 return (write_in_full(fd
, write_buffer
, left
) != left
) ? -1 : 0;
992 static void ce_smudge_racily_clean_entry(struct cache_entry
*ce
)
995 * The only thing we care about in this function is to smudge the
996 * falsely clean entry due to touch-update-touch race, so we leave
997 * everything else as they are. We are called for entries whose
998 * ce_mtime match the index file mtime.
1002 if (lstat(ce
->name
, &st
) < 0)
1004 if (ce_match_stat_basic(ce
, &st
))
1006 if (ce_modified_check_fs(ce
, &st
)) {
1007 /* This is "racily clean"; smudge it. Note that this
1008 * is a tricky code. At first glance, it may appear
1009 * that it can break with this sequence:
1011 * $ echo xyzzy >frotz
1012 * $ git-update-index --add frotz
1015 * $ echo filfre >nitfol
1016 * $ git-update-index --add nitfol
1018 * but it does not. When the second update-index runs,
1019 * it notices that the entry "frotz" has the same timestamp
1020 * as index, and if we were to smudge it by resetting its
1021 * size to zero here, then the object name recorded
1022 * in index is the 6-byte file but the cached stat information
1023 * becomes zero --- which would then match what we would
1024 * obtain from the filesystem next time we stat("frotz").
1026 * However, the second update-index, before calling
1027 * this function, notices that the cached size is 6
1028 * bytes and what is on the filesystem is an empty
1029 * file, and never calls us, so the cached size information
1030 * for "frotz" stays 6 which does not match the filesystem.
1032 ce
->ce_size
= htonl(0);
1036 int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
1039 struct cache_header hdr
;
1042 for (i
= removed
= 0; i
< entries
; i
++)
1043 if (!cache
[i
]->ce_mode
)
1046 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
1047 hdr
.hdr_version
= htonl(2);
1048 hdr
.hdr_entries
= htonl(entries
- removed
);
1051 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
1054 for (i
= 0; i
< entries
; i
++) {
1055 struct cache_entry
*ce
= cache
[i
];
1058 if (index_file_timestamp
&&
1059 index_file_timestamp
<= ntohl(ce
->ce_mtime
.sec
))
1060 ce_smudge_racily_clean_entry(ce
);
1061 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
1065 /* Write extension data here */
1066 if (active_cache_tree
) {
1068 void *data
= cache_tree_write(active_cache_tree
, &sz
);
1070 !write_index_ext_header(&c
, newfd
, CACHE_EXT_TREE
, sz
) &&
1071 !ce_write(&c
, newfd
, data
, sz
))
1078 return ce_flush(&c
, newfd
);