2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #define NO_THE_INDEX_COMPATIBILITY_MACROS
8 #include "cache-tree.h"
14 * The first letter should be 'A'..'Z' for extensions that are not
15 * necessary for a correct operation (i.e. optimization data).
16 * When new extensions are added that _needs_ to be understood in
17 * order to correctly interpret the index file, pick character that
18 * is outside the range, to cause the reader to abort.
21 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
22 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
24 struct index_state the_index
;
27 * This only updates the "non-critical" parts of the directory
28 * cache, ie the parts that aren't tracked by GIT, and only used
29 * to validate the cache.
31 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
33 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
34 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
36 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
37 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
39 ce
->ce_dev
= htonl(st
->st_dev
);
40 ce
->ce_ino
= htonl(st
->st_ino
);
41 ce
->ce_uid
= htonl(st
->st_uid
);
42 ce
->ce_gid
= htonl(st
->st_gid
);
43 ce
->ce_size
= htonl(st
->st_size
);
46 ce
->ce_flags
|= htons(CE_VALID
);
49 static int ce_compare_data(struct cache_entry
*ce
, struct stat
*st
)
52 int fd
= open(ce
->name
, O_RDONLY
);
55 unsigned char sha1
[20];
56 if (!index_fd(sha1
, fd
, st
, 0, OBJ_BLOB
, ce
->name
))
57 match
= hashcmp(sha1
, ce
->sha1
);
58 /* index_fd() closed the file descriptor already */
63 static int ce_compare_link(struct cache_entry
*ce
, size_t expected_size
)
69 enum object_type type
;
72 target
= xmalloc(expected_size
);
73 len
= readlink(ce
->name
, target
, expected_size
);
74 if (len
!= expected_size
) {
78 buffer
= read_sha1_file(ce
->sha1
, &type
, &size
);
83 if (size
== expected_size
)
84 match
= memcmp(buffer
, target
, size
);
90 static int ce_compare_gitlink(struct cache_entry
*ce
)
92 unsigned char sha1
[20];
95 * We don't actually require that the .git directory
96 * under GITLINK directory be a valid git directory. It
97 * might even be missing (in case nobody populated that
100 * If so, we consider it always to match.
102 if (resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) < 0)
104 return hashcmp(sha1
, ce
->sha1
);
107 static int ce_modified_check_fs(struct cache_entry
*ce
, struct stat
*st
)
109 switch (st
->st_mode
& S_IFMT
) {
111 if (ce_compare_data(ce
, st
))
115 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
119 if (S_ISGITLINK(ntohl(ce
->ce_mode
)))
127 static int ce_match_stat_basic(struct cache_entry
*ce
, struct stat
*st
)
129 unsigned int changed
= 0;
131 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
133 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
134 /* We consider only the owner x bit to be relevant for
137 if (trust_executable_bit
&&
138 (0100 & (ntohl(ce
->ce_mode
) ^ st
->st_mode
)))
139 changed
|= MODE_CHANGED
;
142 if (!S_ISLNK(st
->st_mode
) &&
143 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
144 changed
|= TYPE_CHANGED
;
147 if (!S_ISDIR(st
->st_mode
))
148 changed
|= TYPE_CHANGED
;
149 else if (ce_compare_gitlink(ce
))
150 changed
|= DATA_CHANGED
;
153 die("internal error: ce_mode is %lo", ntohl(ce
->ce_mode
));
155 if (ce
->ce_mtime
.sec
!= htonl(st
->st_mtime
))
156 changed
|= MTIME_CHANGED
;
157 if (ce
->ce_ctime
.sec
!= htonl(st
->st_ctime
))
158 changed
|= CTIME_CHANGED
;
162 * nsec seems unreliable - not all filesystems support it, so
163 * as long as it is in the inode cache you get right nsec
164 * but after it gets flushed, you get zero nsec.
166 if (ce
->ce_mtime
.nsec
!= htonl(st
->st_mtim
.tv_nsec
))
167 changed
|= MTIME_CHANGED
;
168 if (ce
->ce_ctime
.nsec
!= htonl(st
->st_ctim
.tv_nsec
))
169 changed
|= CTIME_CHANGED
;
172 if (ce
->ce_uid
!= htonl(st
->st_uid
) ||
173 ce
->ce_gid
!= htonl(st
->st_gid
))
174 changed
|= OWNER_CHANGED
;
175 if (ce
->ce_ino
!= htonl(st
->st_ino
))
176 changed
|= INODE_CHANGED
;
180 * st_dev breaks on network filesystems where different
181 * clients will have different views of what "device"
182 * the filesystem is on
184 if (ce
->ce_dev
!= htonl(st
->st_dev
))
185 changed
|= INODE_CHANGED
;
188 if (ce
->ce_size
!= htonl(st
->st_size
))
189 changed
|= DATA_CHANGED
;
194 int ie_match_stat(struct index_state
*istate
,
195 struct cache_entry
*ce
, struct stat
*st
, int options
)
197 unsigned int changed
;
198 int ignore_valid
= options
& 01;
199 int assume_racy_is_modified
= options
& 02;
202 * If it's marked as always valid in the index, it's
203 * valid whatever the checked-out copy says.
205 if (!ignore_valid
&& (ce
->ce_flags
& htons(CE_VALID
)))
208 changed
= ce_match_stat_basic(ce
, st
);
211 * Within 1 second of this sequence:
212 * echo xyzzy >file && git-update-index --add file
213 * running this command:
215 * would give a falsely clean cache entry. The mtime and
216 * length match the cache, and other stat fields do not change.
218 * We could detect this at update-index time (the cache entry
219 * being registered/updated records the same time as "now")
220 * and delay the return from git-update-index, but that would
221 * effectively mean we can make at most one commit per second,
222 * which is not acceptable. Instead, we check cache entries
223 * whose mtime are the same as the index file timestamp more
224 * carefully than others.
228 istate
->timestamp
<= ntohl(ce
->ce_mtime
.sec
)) {
229 if (assume_racy_is_modified
)
230 changed
|= DATA_CHANGED
;
232 changed
|= ce_modified_check_fs(ce
, st
);
238 int ie_modified(struct index_state
*istate
,
239 struct cache_entry
*ce
, struct stat
*st
, int really
)
241 int changed
, changed_fs
;
242 changed
= ie_match_stat(istate
, ce
, st
, really
);
246 * If the mode or type has changed, there's no point in trying
247 * to refresh the entry - it's not going to match
249 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
252 /* Immediately after read-tree or update-index --cacheinfo,
253 * the length field is zero. For other cases the ce_size
254 * should match the SHA1 recorded in the index entry.
256 if ((changed
& DATA_CHANGED
) && ce
->ce_size
!= htonl(0))
259 changed_fs
= ce_modified_check_fs(ce
, st
);
261 return changed
| changed_fs
;
265 int base_name_compare(const char *name1
, int len1
, int mode1
,
266 const char *name2
, int len2
, int mode2
)
268 unsigned char c1
, c2
;
269 int len
= len1
< len2
? len1
: len2
;
272 cmp
= memcmp(name1
, name2
, len
);
277 if (!c1
&& S_ISDIR(mode1
))
279 if (!c2
&& S_ISDIR(mode2
))
281 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
284 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
286 int len1
= flags1
& CE_NAMEMASK
;
287 int len2
= flags2
& CE_NAMEMASK
;
288 int len
= len1
< len2
? len1
: len2
;
291 cmp
= memcmp(name1
, name2
, len
);
300 flags1
&= CE_STAGEMASK
;
301 flags2
&= CE_STAGEMASK
;
310 int index_name_pos(struct index_state
*istate
, const char *name
, int namelen
)
315 last
= istate
->cache_nr
;
316 while (last
> first
) {
317 int next
= (last
+ first
) >> 1;
318 struct cache_entry
*ce
= istate
->cache
[next
];
319 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ntohs(ce
->ce_flags
));
331 /* Remove entry, return true if there are more entries to go.. */
332 int remove_index_entry_at(struct index_state
*istate
, int pos
)
334 istate
->cache_changed
= 1;
336 if (pos
>= istate
->cache_nr
)
338 memmove(istate
->cache
+ pos
,
339 istate
->cache
+ pos
+ 1,
340 (istate
->cache_nr
- pos
) * sizeof(struct cache_entry
*));
344 int remove_file_from_index(struct index_state
*istate
, const char *path
)
346 int pos
= index_name_pos(istate
, path
, strlen(path
));
349 while (pos
< istate
->cache_nr
&& !strcmp(istate
->cache
[pos
]->name
, path
))
350 remove_index_entry_at(istate
, pos
);
354 static int compare_name(struct cache_entry
*ce
, const char *path
, int namelen
)
356 return namelen
!= ce_namelen(ce
) || memcmp(path
, ce
->name
, namelen
);
359 static int index_name_pos_also_unmerged(struct index_state
*istate
,
360 const char *path
, int namelen
)
362 int pos
= index_name_pos(istate
, path
, namelen
);
363 struct cache_entry
*ce
;
368 /* maybe unmerged? */
370 if (pos
>= istate
->cache_nr
||
371 compare_name((ce
= istate
->cache
[pos
]), path
, namelen
))
374 /* order of preference: stage 2, 1, 3 */
375 if (ce_stage(ce
) == 1 && pos
+ 1 < istate
->cache_nr
&&
376 ce_stage((ce
= istate
->cache
[pos
+ 1])) == 2 &&
377 !compare_name(ce
, path
, namelen
))
382 int add_file_to_index(struct index_state
*istate
, const char *path
, int verbose
)
384 int size
, namelen
, pos
;
386 struct cache_entry
*ce
;
388 if (lstat(path
, &st
))
389 die("%s: unable to stat (%s)", path
, strerror(errno
));
391 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
392 die("%s: can only add regular files, symbolic links or git-directories", path
);
394 namelen
= strlen(path
);
395 if (S_ISDIR(st
.st_mode
)) {
396 while (namelen
&& path
[namelen
-1] == '/')
399 size
= cache_entry_size(namelen
);
400 ce
= xcalloc(1, size
);
401 memcpy(ce
->name
, path
, namelen
);
402 ce
->ce_flags
= htons(namelen
);
403 fill_stat_cache_info(ce
, &st
);
405 if (trust_executable_bit
&& has_symlinks
)
406 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
408 /* If there is an existing entry, pick the mode bits and type
409 * from it, otherwise assume unexecutable regular file.
411 struct cache_entry
*ent
;
412 int pos
= index_name_pos_also_unmerged(istate
, path
, namelen
);
414 ent
= (0 <= pos
) ? istate
->cache
[pos
] : NULL
;
415 ce
->ce_mode
= ce_mode_from_stat(ent
, st
.st_mode
);
418 pos
= index_name_pos(istate
, ce
->name
, namelen
);
420 !ce_stage(istate
->cache
[pos
]) &&
421 !ie_modified(istate
, istate
->cache
[pos
], &st
, 1)) {
422 /* Nothing changed, really */
427 if (index_path(ce
->sha1
, path
, &st
, 1))
428 die("unable to index file %s", path
);
429 if (add_index_entry(istate
, ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
))
430 die("unable to add %s to index",path
);
432 printf("add '%s'\n", path
);
433 cache_tree_invalidate_path(istate
->cache_tree
, path
);
437 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
439 int len
= ce_namelen(a
);
440 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
443 int ce_path_match(const struct cache_entry
*ce
, const char **pathspec
)
445 const char *match
, *name
;
451 len
= ce_namelen(ce
);
453 while ((match
= *pathspec
++) != NULL
) {
454 int matchlen
= strlen(match
);
457 if (memcmp(name
, match
, matchlen
))
459 if (matchlen
&& name
[matchlen
-1] == '/')
461 if (name
[matchlen
] == '/' || !name
[matchlen
])
470 * We fundamentally don't like some paths: we don't want
471 * dot or dot-dot anywhere, and for obvious reasons don't
472 * want to recurse into ".git" either.
474 * Also, we don't want double slashes or slashes at the
475 * end that can make pathnames ambiguous.
477 static int verify_dotfile(const char *rest
)
480 * The first character was '.', but that
481 * has already been discarded, we now test
485 /* "." is not allowed */
490 * ".git" followed by NUL or slash is bad. This
491 * shares the path end test with the ".." case.
501 if (rest
[1] == '\0' || rest
[1] == '/')
507 int verify_path(const char *path
)
512 if (is_absolute_path(path
))
513 return error("Cannot handle absolute path: %s", path
);
529 if (verify_dotfile(path
))
539 * Do we have another file that has the beginning components being a
540 * proper superset of the name we're trying to add?
542 static int has_file_name(struct index_state
*istate
,
543 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
546 int len
= ce_namelen(ce
);
547 int stage
= ce_stage(ce
);
548 const char *name
= ce
->name
;
550 while (pos
< istate
->cache_nr
) {
551 struct cache_entry
*p
= istate
->cache
[pos
++];
553 if (len
>= ce_namelen(p
))
555 if (memcmp(name
, p
->name
, len
))
557 if (ce_stage(p
) != stage
)
559 if (p
->name
[len
] != '/')
561 if (!ce_stage(p
) && !p
->ce_mode
)
566 remove_index_entry_at(istate
, --pos
);
572 * Do we have another file with a pathname that is a proper
573 * subset of the name we're trying to add?
575 static int has_dir_name(struct index_state
*istate
,
576 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
579 int stage
= ce_stage(ce
);
580 const char *name
= ce
->name
;
581 const char *slash
= name
+ ce_namelen(ce
);
589 if (slash
<= ce
->name
)
594 pos
= index_name_pos(istate
, name
, ntohs(create_ce_flags(len
, stage
)));
597 * Found one, but not so fast. This could
598 * be a marker that says "I was here, but
599 * I am being removed". Such an entry is
600 * not a part of the resulting tree, and
601 * it is Ok to have a directory at the same
604 if (stage
|| istate
->cache
[pos
]->ce_mode
) {
608 remove_index_entry_at(istate
, pos
);
616 * Trivial optimization: if we find an entry that
617 * already matches the sub-directory, then we know
618 * we're ok, and we can exit.
620 while (pos
< istate
->cache_nr
) {
621 struct cache_entry
*p
= istate
->cache
[pos
];
622 if ((ce_namelen(p
) <= len
) ||
623 (p
->name
[len
] != '/') ||
624 memcmp(p
->name
, name
, len
))
625 break; /* not our subdirectory */
626 if (ce_stage(p
) == stage
&& (stage
|| p
->ce_mode
))
627 /* p is at the same stage as our entry, and
628 * is a subdirectory of what we are looking
629 * at, so we cannot have conflicts at our
630 * level or anything shorter.
639 /* We may be in a situation where we already have path/file and path
640 * is being added, or we already have path and path/file is being
641 * added. Either one would result in a nonsense tree that has path
642 * twice when git-write-tree tries to write it out. Prevent it.
644 * If ok-to-replace is specified, we remove the conflicting entries
645 * from the cache so the caller should recompute the insert position.
646 * When this happens, we return non-zero.
648 static int check_file_directory_conflict(struct index_state
*istate
,
649 const struct cache_entry
*ce
,
650 int pos
, int ok_to_replace
)
655 * When ce is an "I am going away" entry, we allow it to be added
657 if (!ce_stage(ce
) && !ce
->ce_mode
)
661 * We check if the path is a sub-path of a subsequent pathname
662 * first, since removing those will not change the position
665 retval
= has_file_name(istate
, ce
, pos
, ok_to_replace
);
668 * Then check if the path might have a clashing sub-directory
671 return retval
+ has_dir_name(istate
, ce
, pos
, ok_to_replace
);
674 static int add_index_entry_with_check(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
677 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
678 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
679 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
681 pos
= index_name_pos(istate
, ce
->name
, ntohs(ce
->ce_flags
));
683 /* existing match? Just replace it. */
685 istate
->cache_changed
= 1;
686 istate
->cache
[pos
] = ce
;
692 * Inserting a merged entry ("stage 0") into the index
693 * will always replace all non-merged entries..
695 if (pos
< istate
->cache_nr
&& ce_stage(ce
) == 0) {
696 while (ce_same_name(istate
->cache
[pos
], ce
)) {
698 if (!remove_index_entry_at(istate
, pos
))
705 if (!verify_path(ce
->name
))
708 if (!skip_df_check
&&
709 check_file_directory_conflict(istate
, ce
, pos
, ok_to_replace
)) {
711 return error("'%s' appears as both a file and as a directory",
713 pos
= index_name_pos(istate
, ce
->name
, ntohs(ce
->ce_flags
));
719 int add_index_entry(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
723 if (option
& ADD_CACHE_JUST_APPEND
)
724 pos
= istate
->cache_nr
;
727 ret
= add_index_entry_with_check(istate
, ce
, option
);
733 /* Make sure the array is big enough .. */
734 if (istate
->cache_nr
== istate
->cache_alloc
) {
735 istate
->cache_alloc
= alloc_nr(istate
->cache_alloc
);
736 istate
->cache
= xrealloc(istate
->cache
,
737 istate
->cache_alloc
* sizeof(struct cache_entry
*));
742 if (istate
->cache_nr
> pos
+ 1)
743 memmove(istate
->cache
+ pos
+ 1,
745 (istate
->cache_nr
- pos
- 1) * sizeof(ce
));
746 istate
->cache
[pos
] = ce
;
747 istate
->cache_changed
= 1;
752 * "refresh" does not calculate a new sha1 file or bring the
753 * cache up-to-date for mode/content changes. But what it
754 * _does_ do is to "re-match" the stat information of a file
755 * with the cache, so that you can refresh the cache for a
756 * file that hasn't been changed but where the stat entry is
759 * For example, you'd want to do this after doing a "git-read-tree",
760 * to link up the stat cache details with the proper files.
762 static struct cache_entry
*refresh_cache_ent(struct index_state
*istate
,
763 struct cache_entry
*ce
, int really
, int *err
)
766 struct cache_entry
*updated
;
769 if (lstat(ce
->name
, &st
) < 0) {
775 changed
= ie_match_stat(istate
, ce
, &st
, really
);
777 if (really
&& assume_unchanged
&&
778 !(ce
->ce_flags
& htons(CE_VALID
)))
779 ; /* mark this one VALID again */
784 if (ie_modified(istate
, ce
, &st
, really
)) {
791 updated
= xmalloc(size
);
792 memcpy(updated
, ce
, size
);
793 fill_stat_cache_info(updated
, &st
);
795 /* In this case, if really is not set, we should leave
796 * CE_VALID bit alone. Otherwise, paths marked with
797 * --no-assume-unchanged (i.e. things to be edited) will
798 * reacquire CE_VALID bit automatically, which is not
799 * really what we want.
801 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
802 updated
->ce_flags
&= ~htons(CE_VALID
);
807 int refresh_index(struct index_state
*istate
, unsigned int flags
, const char **pathspec
, char *seen
)
811 int really
= (flags
& REFRESH_REALLY
) != 0;
812 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
813 int quiet
= (flags
& REFRESH_QUIET
) != 0;
814 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
816 for (i
= 0; i
< istate
->cache_nr
; i
++) {
817 struct cache_entry
*ce
, *new;
820 ce
= istate
->cache
[i
];
822 while ((i
< istate
->cache_nr
) &&
823 ! strcmp(istate
->cache
[i
]->name
, ce
->name
))
828 printf("%s: needs merge\n", ce
->name
);
833 if (pathspec
&& !match_pathspec(pathspec
, ce
->name
, strlen(ce
->name
), 0, seen
))
836 new = refresh_cache_ent(istate
, ce
, really
, &cache_errno
);
840 if (not_new
&& cache_errno
== ENOENT
)
842 if (really
&& cache_errno
== EINVAL
) {
843 /* If we are doing --really-refresh that
844 * means the index is not valid anymore.
846 ce
->ce_flags
&= ~htons(CE_VALID
);
847 istate
->cache_changed
= 1;
851 printf("%s: needs update\n", ce
->name
);
855 istate
->cache_changed
= 1;
856 /* You can NOT just free istate->cache[i] here, since it
857 * might not be necessarily malloc()ed but can also come
859 istate
->cache
[i
] = new;
864 struct cache_entry
*refresh_cache_entry(struct cache_entry
*ce
, int really
)
866 return refresh_cache_ent(&the_index
, ce
, really
, NULL
);
869 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
872 unsigned char sha1
[20];
874 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
875 return error("bad signature");
876 if (hdr
->hdr_version
!= htonl(2))
877 return error("bad index version");
879 SHA1_Update(&c
, hdr
, size
- 20);
880 SHA1_Final(sha1
, &c
);
881 if (hashcmp(sha1
, (unsigned char *)hdr
+ size
- 20))
882 return error("bad index file sha1 signature");
886 static int read_index_extension(struct index_state
*istate
,
887 const char *ext
, void *data
, unsigned long sz
)
889 switch (CACHE_EXT(ext
)) {
891 istate
->cache_tree
= cache_tree_read(data
, sz
);
894 if (*ext
< 'A' || 'Z' < *ext
)
895 return error("index uses %.4s extension, which we do not understand",
897 fprintf(stderr
, "ignoring %.4s extension\n", ext
);
903 int read_index(struct index_state
*istate
)
905 return read_index_from(istate
, get_index_file());
908 /* remember to discard_cache() before reading a different cache! */
909 int read_index_from(struct index_state
*istate
, const char *path
)
913 unsigned long offset
;
914 struct cache_header
*hdr
;
918 return istate
->cache_nr
;
921 istate
->timestamp
= 0;
922 fd
= open(path
, O_RDONLY
);
926 die("index file open failed (%s)", strerror(errno
));
930 die("cannot stat the open index (%s)", strerror(errno
));
933 istate
->mmap_size
= xsize_t(st
.st_size
);
934 if (istate
->mmap_size
< sizeof(struct cache_header
) + 20)
935 die("index file smaller than expected");
937 istate
->mmap
= xmmap(NULL
, istate
->mmap_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
941 if (verify_hdr(hdr
, istate
->mmap_size
) < 0)
944 istate
->cache_nr
= ntohl(hdr
->hdr_entries
);
945 istate
->cache_alloc
= alloc_nr(istate
->cache_nr
);
946 istate
->cache
= xcalloc(istate
->cache_alloc
, sizeof(struct cache_entry
*));
948 offset
= sizeof(*hdr
);
949 for (i
= 0; i
< istate
->cache_nr
; i
++) {
950 struct cache_entry
*ce
;
952 ce
= (struct cache_entry
*)((char *)(istate
->mmap
) + offset
);
953 offset
= offset
+ ce_size(ce
);
954 istate
->cache
[i
] = ce
;
956 istate
->timestamp
= st
.st_mtime
;
957 while (offset
<= istate
->mmap_size
- 20 - 8) {
958 /* After an array of active_nr index entries,
959 * there can be arbitrary number of extended
960 * sections, each of which is prefixed with
961 * extension name (4-byte) and section length
962 * in 4-byte network byte order.
964 unsigned long extsize
;
965 memcpy(&extsize
, (char *)(istate
->mmap
) + offset
+ 4, 4);
966 extsize
= ntohl(extsize
);
967 if (read_index_extension(istate
,
968 ((const char *) (istate
->mmap
)) + offset
,
969 (char *) (istate
->mmap
) + offset
+ 8,
975 return istate
->cache_nr
;
978 munmap(istate
->mmap
, istate
->mmap_size
);
980 die("index file corrupt");
983 int discard_index(struct index_state
*istate
)
987 istate
->cache_nr
= 0;
988 istate
->cache_changed
= 0;
989 istate
->timestamp
= 0;
990 cache_tree_free(&(istate
->cache_tree
));
991 if (istate
->mmap
== NULL
)
993 ret
= munmap(istate
->mmap
, istate
->mmap_size
);
995 istate
->mmap_size
= 0;
997 /* no need to throw away allocated active_cache */
1001 #define WRITE_BUFFER_SIZE 8192
1002 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
1003 static unsigned long write_buffer_len
;
1005 static int ce_write_flush(SHA_CTX
*context
, int fd
)
1007 unsigned int buffered
= write_buffer_len
;
1009 SHA1_Update(context
, write_buffer
, buffered
);
1010 if (write_in_full(fd
, write_buffer
, buffered
) != buffered
)
1012 write_buffer_len
= 0;
1017 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
1020 unsigned int buffered
= write_buffer_len
;
1021 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
1024 memcpy(write_buffer
+ buffered
, data
, partial
);
1025 buffered
+= partial
;
1026 if (buffered
== WRITE_BUFFER_SIZE
) {
1027 write_buffer_len
= buffered
;
1028 if (ce_write_flush(context
, fd
))
1032 write_buffer_len
= buffered
;
1034 data
= (char *) data
+ partial
;
1039 static int write_index_ext_header(SHA_CTX
*context
, int fd
,
1040 unsigned int ext
, unsigned int sz
)
1044 return ((ce_write(context
, fd
, &ext
, 4) < 0) ||
1045 (ce_write(context
, fd
, &sz
, 4) < 0)) ? -1 : 0;
1048 static int ce_flush(SHA_CTX
*context
, int fd
)
1050 unsigned int left
= write_buffer_len
;
1053 write_buffer_len
= 0;
1054 SHA1_Update(context
, write_buffer
, left
);
1057 /* Flush first if not enough space for SHA1 signature */
1058 if (left
+ 20 > WRITE_BUFFER_SIZE
) {
1059 if (write_in_full(fd
, write_buffer
, left
) != left
)
1064 /* Append the SHA1 signature at the end */
1065 SHA1_Final(write_buffer
+ left
, context
);
1067 return (write_in_full(fd
, write_buffer
, left
) != left
) ? -1 : 0;
1070 static void ce_smudge_racily_clean_entry(struct cache_entry
*ce
)
1073 * The only thing we care about in this function is to smudge the
1074 * falsely clean entry due to touch-update-touch race, so we leave
1075 * everything else as they are. We are called for entries whose
1076 * ce_mtime match the index file mtime.
1080 if (lstat(ce
->name
, &st
) < 0)
1082 if (ce_match_stat_basic(ce
, &st
))
1084 if (ce_modified_check_fs(ce
, &st
)) {
1085 /* This is "racily clean"; smudge it. Note that this
1086 * is a tricky code. At first glance, it may appear
1087 * that it can break with this sequence:
1089 * $ echo xyzzy >frotz
1090 * $ git-update-index --add frotz
1093 * $ echo filfre >nitfol
1094 * $ git-update-index --add nitfol
1096 * but it does not. When the second update-index runs,
1097 * it notices that the entry "frotz" has the same timestamp
1098 * as index, and if we were to smudge it by resetting its
1099 * size to zero here, then the object name recorded
1100 * in index is the 6-byte file but the cached stat information
1101 * becomes zero --- which would then match what we would
1102 * obtain from the filesystem next time we stat("frotz").
1104 * However, the second update-index, before calling
1105 * this function, notices that the cached size is 6
1106 * bytes and what is on the filesystem is an empty
1107 * file, and never calls us, so the cached size information
1108 * for "frotz" stays 6 which does not match the filesystem.
1110 ce
->ce_size
= htonl(0);
1111 trace_printf("trace: index: Index object for '%s' smudged due to being racily clean\n", ce
->name
);
1115 int write_index(struct index_state
*istate
, int newfd
)
1118 struct cache_header hdr
;
1120 struct cache_entry
**cache
= istate
->cache
;
1121 int entries
= istate
->cache_nr
;
1123 for (i
= removed
= 0; i
< entries
; i
++)
1124 if (!cache
[i
]->ce_mode
)
1127 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
1128 hdr
.hdr_version
= htonl(2);
1129 hdr
.hdr_entries
= htonl(entries
- removed
);
1132 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
1135 for (i
= 0; i
< entries
; i
++) {
1136 struct cache_entry
*ce
= cache
[i
];
1139 if (istate
->timestamp
&&
1140 istate
->timestamp
<= ntohl(ce
->ce_mtime
.sec
))
1141 ce_smudge_racily_clean_entry(ce
);
1142 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
1146 /* Write extension data here */
1147 if (istate
->cache_tree
) {
1149 void *data
= cache_tree_write(istate
->cache_tree
, &sz
);
1151 !write_index_ext_header(&c
, newfd
, CACHE_EXT_TREE
, sz
) &&
1152 !ce_write(&c
, newfd
, data
, sz
))
1159 return ce_flush(&c
, newfd
);