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"
13 * The first letter should be 'A'..'Z' for extensions that are not
14 * necessary for a correct operation (i.e. optimization data).
15 * When new extensions are added that _needs_ to be understood in
16 * order to correctly interpret the index file, pick character that
17 * is outside the range, to cause the reader to abort.
20 #define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
21 #define CACHE_EXT_TREE 0x54524545 /* "TREE" */
23 struct index_state the_index
;
26 * This only updates the "non-critical" parts of the directory
27 * cache, ie the parts that aren't tracked by GIT, and only used
28 * to validate the cache.
30 void fill_stat_cache_info(struct cache_entry
*ce
, struct stat
*st
)
32 ce
->ce_ctime
.sec
= htonl(st
->st_ctime
);
33 ce
->ce_mtime
.sec
= htonl(st
->st_mtime
);
35 ce
->ce_ctime
.nsec
= htonl(st
->st_ctim
.tv_nsec
);
36 ce
->ce_mtime
.nsec
= htonl(st
->st_mtim
.tv_nsec
);
38 ce
->ce_dev
= htonl(st
->st_dev
);
39 ce
->ce_ino
= htonl(st
->st_ino
);
40 ce
->ce_uid
= htonl(st
->st_uid
);
41 ce
->ce_gid
= htonl(st
->st_gid
);
42 ce
->ce_size
= htonl(st
->st_size
);
45 ce
->ce_flags
|= htons(CE_VALID
);
48 static int ce_compare_data(struct cache_entry
*ce
, struct stat
*st
)
51 int fd
= open(ce
->name
, O_RDONLY
);
54 unsigned char sha1
[20];
55 if (!index_fd(sha1
, fd
, st
, 0, OBJ_BLOB
, ce
->name
))
56 match
= hashcmp(sha1
, ce
->sha1
);
57 /* index_fd() closed the file descriptor already */
62 static int ce_compare_link(struct cache_entry
*ce
, size_t expected_size
)
68 enum object_type type
;
71 target
= xmalloc(expected_size
);
72 len
= readlink(ce
->name
, target
, expected_size
);
73 if (len
!= expected_size
) {
77 buffer
= read_sha1_file(ce
->sha1
, &type
, &size
);
82 if (size
== expected_size
)
83 match
= memcmp(buffer
, target
, size
);
89 static int ce_compare_gitlink(struct cache_entry
*ce
)
91 unsigned char sha1
[20];
94 * We don't actually require that the .git directory
95 * under DIRLNK directory be a valid git directory. It
96 * might even be missing (in case nobody populated that
99 * If so, we consider it always to match.
101 if (resolve_gitlink_ref(ce
->name
, "HEAD", sha1
) < 0)
103 return hashcmp(sha1
, ce
->sha1
);
106 static int ce_modified_check_fs(struct cache_entry
*ce
, struct stat
*st
)
108 switch (st
->st_mode
& S_IFMT
) {
110 if (ce_compare_data(ce
, st
))
114 if (ce_compare_link(ce
, xsize_t(st
->st_size
)))
118 if (S_ISDIRLNK(ntohl(ce
->ce_mode
)))
126 static int ce_match_stat_basic(struct cache_entry
*ce
, struct stat
*st
)
128 unsigned int changed
= 0;
130 switch (ntohl(ce
->ce_mode
) & S_IFMT
) {
132 changed
|= !S_ISREG(st
->st_mode
) ? TYPE_CHANGED
: 0;
133 /* We consider only the owner x bit to be relevant for
136 if (trust_executable_bit
&&
137 (0100 & (ntohl(ce
->ce_mode
) ^ st
->st_mode
)))
138 changed
|= MODE_CHANGED
;
141 if (!S_ISLNK(st
->st_mode
) &&
142 (has_symlinks
|| !S_ISREG(st
->st_mode
)))
143 changed
|= TYPE_CHANGED
;
146 if (!S_ISDIR(st
->st_mode
))
147 changed
|= TYPE_CHANGED
;
148 else if (ce_compare_gitlink(ce
))
149 changed
|= DATA_CHANGED
;
152 die("internal error: ce_mode is %o", ntohl(ce
->ce_mode
));
154 if (ce
->ce_mtime
.sec
!= htonl(st
->st_mtime
))
155 changed
|= MTIME_CHANGED
;
156 if (ce
->ce_ctime
.sec
!= htonl(st
->st_ctime
))
157 changed
|= CTIME_CHANGED
;
161 * nsec seems unreliable - not all filesystems support it, so
162 * as long as it is in the inode cache you get right nsec
163 * but after it gets flushed, you get zero nsec.
165 if (ce
->ce_mtime
.nsec
!= htonl(st
->st_mtim
.tv_nsec
))
166 changed
|= MTIME_CHANGED
;
167 if (ce
->ce_ctime
.nsec
!= htonl(st
->st_ctim
.tv_nsec
))
168 changed
|= CTIME_CHANGED
;
171 if (ce
->ce_uid
!= htonl(st
->st_uid
) ||
172 ce
->ce_gid
!= htonl(st
->st_gid
))
173 changed
|= OWNER_CHANGED
;
174 if (ce
->ce_ino
!= htonl(st
->st_ino
))
175 changed
|= INODE_CHANGED
;
179 * st_dev breaks on network filesystems where different
180 * clients will have different views of what "device"
181 * the filesystem is on
183 if (ce
->ce_dev
!= htonl(st
->st_dev
))
184 changed
|= INODE_CHANGED
;
187 if (ce
->ce_size
!= htonl(st
->st_size
))
188 changed
|= DATA_CHANGED
;
193 int ie_match_stat(struct index_state
*istate
,
194 struct cache_entry
*ce
, struct stat
*st
, int options
)
196 unsigned int changed
;
197 int ignore_valid
= options
& 01;
198 int assume_racy_is_modified
= options
& 02;
201 * If it's marked as always valid in the index, it's
202 * valid whatever the checked-out copy says.
204 if (!ignore_valid
&& (ce
->ce_flags
& htons(CE_VALID
)))
207 changed
= ce_match_stat_basic(ce
, st
);
210 * Within 1 second of this sequence:
211 * echo xyzzy >file && git-update-index --add file
212 * running this command:
214 * would give a falsely clean cache entry. The mtime and
215 * length match the cache, and other stat fields do not change.
217 * We could detect this at update-index time (the cache entry
218 * being registered/updated records the same time as "now")
219 * and delay the return from git-update-index, but that would
220 * effectively mean we can make at most one commit per second,
221 * which is not acceptable. Instead, we check cache entries
222 * whose mtime are the same as the index file timestamp more
223 * carefully than others.
227 istate
->timestamp
<= ntohl(ce
->ce_mtime
.sec
)) {
228 if (assume_racy_is_modified
)
229 changed
|= DATA_CHANGED
;
231 changed
|= ce_modified_check_fs(ce
, st
);
237 int ie_modified(struct index_state
*istate
,
238 struct cache_entry
*ce
, struct stat
*st
, int really
)
240 int changed
, changed_fs
;
241 changed
= ie_match_stat(istate
, ce
, st
, really
);
245 * If the mode or type has changed, there's no point in trying
246 * to refresh the entry - it's not going to match
248 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
251 /* Immediately after read-tree or update-index --cacheinfo,
252 * the length field is zero. For other cases the ce_size
253 * should match the SHA1 recorded in the index entry.
255 if ((changed
& DATA_CHANGED
) && ce
->ce_size
!= htonl(0))
258 changed_fs
= ce_modified_check_fs(ce
, st
);
260 return changed
| changed_fs
;
264 int base_name_compare(const char *name1
, int len1
, int mode1
,
265 const char *name2
, int len2
, int mode2
)
267 unsigned char c1
, c2
;
268 int len
= len1
< len2
? len1
: len2
;
271 cmp
= memcmp(name1
, name2
, len
);
276 if (!c1
&& S_ISDIR(mode1
))
278 if (!c2
&& S_ISDIR(mode2
))
280 return (c1
< c2
) ? -1 : (c1
> c2
) ? 1 : 0;
283 int cache_name_compare(const char *name1
, int flags1
, const char *name2
, int flags2
)
285 int len1
= flags1
& CE_NAMEMASK
;
286 int len2
= flags2
& CE_NAMEMASK
;
287 int len
= len1
< len2
? len1
: len2
;
290 cmp
= memcmp(name1
, name2
, len
);
299 flags1
&= CE_STAGEMASK
;
300 flags2
&= CE_STAGEMASK
;
309 int index_name_pos(struct index_state
*istate
, const char *name
, int namelen
)
314 last
= istate
->cache_nr
;
315 while (last
> first
) {
316 int next
= (last
+ first
) >> 1;
317 struct cache_entry
*ce
= istate
->cache
[next
];
318 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ntohs(ce
->ce_flags
));
330 /* Remove entry, return true if there are more entries to go.. */
331 int remove_index_entry_at(struct index_state
*istate
, int pos
)
333 istate
->cache_changed
= 1;
335 if (pos
>= istate
->cache_nr
)
337 memmove(istate
->cache
+ pos
,
338 istate
->cache
+ pos
+ 1,
339 (istate
->cache_nr
- pos
) * sizeof(struct cache_entry
*));
343 int remove_file_from_index(struct index_state
*istate
, const char *path
)
345 int pos
= index_name_pos(istate
, path
, strlen(path
));
348 while (pos
< istate
->cache_nr
&& !strcmp(istate
->cache
[pos
]->name
, path
))
349 remove_index_entry_at(istate
, pos
);
353 int add_file_to_index(struct index_state
*istate
, const char *path
, int verbose
)
357 struct cache_entry
*ce
;
359 if (lstat(path
, &st
))
360 die("%s: unable to stat (%s)", path
, strerror(errno
));
362 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
) && !S_ISDIR(st
.st_mode
))
363 die("%s: can only add regular files, symbolic links or git-directories", path
);
365 namelen
= strlen(path
);
366 if (S_ISDIR(st
.st_mode
)) {
367 while (namelen
&& path
[namelen
-1] == '/')
370 size
= cache_entry_size(namelen
);
371 ce
= xcalloc(1, size
);
372 memcpy(ce
->name
, path
, namelen
);
373 ce
->ce_flags
= htons(namelen
);
374 fill_stat_cache_info(ce
, &st
);
376 if (trust_executable_bit
&& has_symlinks
)
377 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
379 /* If there is an existing entry, pick the mode bits and type
380 * from it, otherwise assume unexecutable regular file.
382 struct cache_entry
*ent
;
383 int pos
= index_name_pos(istate
, path
, namelen
);
385 ent
= (0 <= pos
) ? istate
->cache
[pos
] : NULL
;
386 ce
->ce_mode
= ce_mode_from_stat(ent
, st
.st_mode
);
389 if (index_path(ce
->sha1
, path
, &st
, 1))
390 die("unable to index file %s", path
);
391 if (add_index_entry(istate
, ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
))
392 die("unable to add %s to index",path
);
394 printf("add '%s'\n", path
);
395 cache_tree_invalidate_path(istate
->cache_tree
, path
);
399 int ce_same_name(struct cache_entry
*a
, struct cache_entry
*b
)
401 int len
= ce_namelen(a
);
402 return ce_namelen(b
) == len
&& !memcmp(a
->name
, b
->name
, len
);
405 int ce_path_match(const struct cache_entry
*ce
, const char **pathspec
)
407 const char *match
, *name
;
413 len
= ce_namelen(ce
);
415 while ((match
= *pathspec
++) != NULL
) {
416 int matchlen
= strlen(match
);
419 if (memcmp(name
, match
, matchlen
))
421 if (matchlen
&& name
[matchlen
-1] == '/')
423 if (name
[matchlen
] == '/' || !name
[matchlen
])
432 * We fundamentally don't like some paths: we don't want
433 * dot or dot-dot anywhere, and for obvious reasons don't
434 * want to recurse into ".git" either.
436 * Also, we don't want double slashes or slashes at the
437 * end that can make pathnames ambiguous.
439 static int verify_dotfile(const char *rest
)
442 * The first character was '.', but that
443 * has already been discarded, we now test
447 /* "." is not allowed */
452 * ".git" followed by NUL or slash is bad. This
453 * shares the path end test with the ".." case.
463 if (rest
[1] == '\0' || rest
[1] == '/')
469 int verify_path(const char *path
)
486 if (verify_dotfile(path
))
496 * Do we have another file that has the beginning components being a
497 * proper superset of the name we're trying to add?
499 static int has_file_name(struct index_state
*istate
,
500 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
503 int len
= ce_namelen(ce
);
504 int stage
= ce_stage(ce
);
505 const char *name
= ce
->name
;
507 while (pos
< istate
->cache_nr
) {
508 struct cache_entry
*p
= istate
->cache
[pos
++];
510 if (len
>= ce_namelen(p
))
512 if (memcmp(name
, p
->name
, len
))
514 if (ce_stage(p
) != stage
)
516 if (p
->name
[len
] != '/')
518 if (!ce_stage(p
) && !p
->ce_mode
)
523 remove_index_entry_at(istate
, --pos
);
529 * Do we have another file with a pathname that is a proper
530 * subset of the name we're trying to add?
532 static int has_dir_name(struct index_state
*istate
,
533 const struct cache_entry
*ce
, int pos
, int ok_to_replace
)
536 int stage
= ce_stage(ce
);
537 const char *name
= ce
->name
;
538 const char *slash
= name
+ ce_namelen(ce
);
546 if (slash
<= ce
->name
)
551 pos
= index_name_pos(istate
, name
, ntohs(create_ce_flags(len
, stage
)));
554 * Found one, but not so fast. This could
555 * be a marker that says "I was here, but
556 * I am being removed". Such an entry is
557 * not a part of the resulting tree, and
558 * it is Ok to have a directory at the same
561 if (stage
|| istate
->cache
[pos
]->ce_mode
) {
565 remove_index_entry_at(istate
, pos
);
573 * Trivial optimization: if we find an entry that
574 * already matches the sub-directory, then we know
575 * we're ok, and we can exit.
577 while (pos
< istate
->cache_nr
) {
578 struct cache_entry
*p
= istate
->cache
[pos
];
579 if ((ce_namelen(p
) <= len
) ||
580 (p
->name
[len
] != '/') ||
581 memcmp(p
->name
, name
, len
))
582 break; /* not our subdirectory */
583 if (ce_stage(p
) == stage
&& (stage
|| p
->ce_mode
))
584 /* p is at the same stage as our entry, and
585 * is a subdirectory of what we are looking
586 * at, so we cannot have conflicts at our
587 * level or anything shorter.
596 /* We may be in a situation where we already have path/file and path
597 * is being added, or we already have path and path/file is being
598 * added. Either one would result in a nonsense tree that has path
599 * twice when git-write-tree tries to write it out. Prevent it.
601 * If ok-to-replace is specified, we remove the conflicting entries
602 * from the cache so the caller should recompute the insert position.
603 * When this happens, we return non-zero.
605 static int check_file_directory_conflict(struct index_state
*istate
,
606 const struct cache_entry
*ce
,
607 int pos
, int ok_to_replace
)
612 * When ce is an "I am going away" entry, we allow it to be added
614 if (!ce_stage(ce
) && !ce
->ce_mode
)
618 * We check if the path is a sub-path of a subsequent pathname
619 * first, since removing those will not change the position
622 retval
= has_file_name(istate
, ce
, pos
, ok_to_replace
);
625 * Then check if the path might have a clashing sub-directory
628 return retval
+ has_dir_name(istate
, ce
, pos
, ok_to_replace
);
631 int add_index_entry(struct index_state
*istate
, struct cache_entry
*ce
, int option
)
634 int ok_to_add
= option
& ADD_CACHE_OK_TO_ADD
;
635 int ok_to_replace
= option
& ADD_CACHE_OK_TO_REPLACE
;
636 int skip_df_check
= option
& ADD_CACHE_SKIP_DFCHECK
;
638 pos
= index_name_pos(istate
, ce
->name
, ntohs(ce
->ce_flags
));
640 /* existing match? Just replace it. */
642 istate
->cache_changed
= 1;
643 istate
->cache
[pos
] = ce
;
649 * Inserting a merged entry ("stage 0") into the index
650 * will always replace all non-merged entries..
652 if (pos
< istate
->cache_nr
&& ce_stage(ce
) == 0) {
653 while (ce_same_name(istate
->cache
[pos
], ce
)) {
655 if (!remove_index_entry_at(istate
, pos
))
662 if (!verify_path(ce
->name
))
665 if (!skip_df_check
&&
666 check_file_directory_conflict(istate
, ce
, pos
, ok_to_replace
)) {
668 return error("'%s' appears as both a file and as a directory",
670 pos
= index_name_pos(istate
, ce
->name
, ntohs(ce
->ce_flags
));
674 /* Make sure the array is big enough .. */
675 if (istate
->cache_nr
== istate
->cache_alloc
) {
676 istate
->cache_alloc
= alloc_nr(istate
->cache_alloc
);
677 istate
->cache
= xrealloc(istate
->cache
,
678 istate
->cache_alloc
* sizeof(struct cache_entry
*));
683 if (istate
->cache_nr
> pos
)
684 memmove(istate
->cache
+ pos
+ 1,
686 (istate
->cache_nr
- pos
- 1) * sizeof(ce
));
687 istate
->cache
[pos
] = ce
;
688 istate
->cache_changed
= 1;
693 * "refresh" does not calculate a new sha1 file or bring the
694 * cache up-to-date for mode/content changes. But what it
695 * _does_ do is to "re-match" the stat information of a file
696 * with the cache, so that you can refresh the cache for a
697 * file that hasn't been changed but where the stat entry is
700 * For example, you'd want to do this after doing a "git-read-tree",
701 * to link up the stat cache details with the proper files.
703 static struct cache_entry
*refresh_cache_ent(struct index_state
*istate
,
704 struct cache_entry
*ce
, int really
, int *err
)
707 struct cache_entry
*updated
;
710 if (lstat(ce
->name
, &st
) < 0) {
716 changed
= ie_match_stat(istate
, ce
, &st
, really
);
718 if (really
&& assume_unchanged
&&
719 !(ce
->ce_flags
& htons(CE_VALID
)))
720 ; /* mark this one VALID again */
725 if (ie_modified(istate
, ce
, &st
, really
)) {
732 updated
= xmalloc(size
);
733 memcpy(updated
, ce
, size
);
734 fill_stat_cache_info(updated
, &st
);
736 /* In this case, if really is not set, we should leave
737 * CE_VALID bit alone. Otherwise, paths marked with
738 * --no-assume-unchanged (i.e. things to be edited) will
739 * reacquire CE_VALID bit automatically, which is not
740 * really what we want.
742 if (!really
&& assume_unchanged
&& !(ce
->ce_flags
& htons(CE_VALID
)))
743 updated
->ce_flags
&= ~htons(CE_VALID
);
748 int refresh_index(struct index_state
*istate
, unsigned int flags
)
752 int really
= (flags
& REFRESH_REALLY
) != 0;
753 int allow_unmerged
= (flags
& REFRESH_UNMERGED
) != 0;
754 int quiet
= (flags
& REFRESH_QUIET
) != 0;
755 int not_new
= (flags
& REFRESH_IGNORE_MISSING
) != 0;
757 for (i
= 0; i
< istate
->cache_nr
; i
++) {
758 struct cache_entry
*ce
, *new;
761 ce
= istate
->cache
[i
];
763 while ((i
< istate
->cache_nr
) &&
764 ! strcmp(istate
->cache
[i
]->name
, ce
->name
))
769 printf("%s: needs merge\n", ce
->name
);
774 new = refresh_cache_ent(istate
, ce
, really
, &cache_errno
);
778 if (not_new
&& cache_errno
== ENOENT
)
780 if (really
&& cache_errno
== EINVAL
) {
781 /* If we are doing --really-refresh that
782 * means the index is not valid anymore.
784 ce
->ce_flags
&= ~htons(CE_VALID
);
785 istate
->cache_changed
= 1;
789 printf("%s: needs update\n", ce
->name
);
793 istate
->cache_changed
= 1;
794 /* You can NOT just free istate->cache[i] here, since it
795 * might not be necessarily malloc()ed but can also come
797 istate
->cache
[i
] = new;
802 struct cache_entry
*refresh_cache_entry(struct cache_entry
*ce
, int really
)
804 return refresh_cache_ent(&the_index
, ce
, really
, NULL
);
807 static int verify_hdr(struct cache_header
*hdr
, unsigned long size
)
810 unsigned char sha1
[20];
812 if (hdr
->hdr_signature
!= htonl(CACHE_SIGNATURE
))
813 return error("bad signature");
814 if (hdr
->hdr_version
!= htonl(2))
815 return error("bad index version");
817 SHA1_Update(&c
, hdr
, size
- 20);
818 SHA1_Final(sha1
, &c
);
819 if (hashcmp(sha1
, (unsigned char *)hdr
+ size
- 20))
820 return error("bad index file sha1 signature");
824 static int read_index_extension(struct index_state
*istate
,
825 const char *ext
, void *data
, unsigned long sz
)
827 switch (CACHE_EXT(ext
)) {
829 istate
->cache_tree
= cache_tree_read(data
, sz
);
832 if (*ext
< 'A' || 'Z' < *ext
)
833 return error("index uses %.4s extension, which we do not understand",
835 fprintf(stderr
, "ignoring %.4s extension\n", ext
);
841 int read_index(struct index_state
*istate
)
843 return read_index_from(istate
, get_index_file());
846 /* remember to discard_cache() before reading a different cache! */
847 int read_index_from(struct index_state
*istate
, const char *path
)
851 unsigned long offset
;
852 struct cache_header
*hdr
;
856 return istate
->cache_nr
;
859 istate
->timestamp
= 0;
860 fd
= open(path
, O_RDONLY
);
864 die("index file open failed (%s)", strerror(errno
));
867 if (!fstat(fd
, &st
)) {
868 istate
->mmap_size
= xsize_t(st
.st_size
);
870 if (istate
->mmap_size
>= sizeof(struct cache_header
) + 20)
871 istate
->mmap
= xmmap(NULL
, istate
->mmap_size
,
872 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
874 die("index file smaller than expected");
876 die("cannot stat the open index (%s)", strerror(errno
));
880 if (verify_hdr(hdr
, istate
->mmap_size
) < 0)
883 istate
->cache_nr
= ntohl(hdr
->hdr_entries
);
884 istate
->cache_alloc
= alloc_nr(istate
->cache_nr
);
885 istate
->cache
= xcalloc(istate
->cache_alloc
, sizeof(struct cache_entry
*));
887 offset
= sizeof(*hdr
);
888 for (i
= 0; i
< istate
->cache_nr
; i
++) {
889 struct cache_entry
*ce
;
891 ce
= (struct cache_entry
*)((char *)(istate
->mmap
) + offset
);
892 offset
= offset
+ ce_size(ce
);
893 istate
->cache
[i
] = ce
;
895 istate
->timestamp
= st
.st_mtime
;
896 while (offset
<= istate
->mmap_size
- 20 - 8) {
897 /* After an array of active_nr index entries,
898 * there can be arbitrary number of extended
899 * sections, each of which is prefixed with
900 * extension name (4-byte) and section length
901 * in 4-byte network byte order.
903 unsigned long extsize
;
904 memcpy(&extsize
, (char *)(istate
->mmap
) + offset
+ 4, 4);
905 extsize
= ntohl(extsize
);
906 if (read_index_extension(istate
,
907 ((const char *) (istate
->mmap
)) + offset
,
908 (char *) (istate
->mmap
) + offset
+ 8,
914 return istate
->cache_nr
;
917 munmap(istate
->mmap
, istate
->mmap_size
);
919 die("index file corrupt");
922 int discard_index(struct index_state
*istate
)
926 istate
->cache_nr
= 0;
927 istate
->cache_changed
= 0;
928 istate
->timestamp
= 0;
929 cache_tree_free(&(istate
->cache_tree
));
930 if (istate
->mmap
== NULL
)
932 ret
= munmap(istate
->mmap
, istate
->mmap_size
);
934 istate
->mmap_size
= 0;
936 /* no need to throw away allocated active_cache */
940 #define WRITE_BUFFER_SIZE 8192
941 static unsigned char write_buffer
[WRITE_BUFFER_SIZE
];
942 static unsigned long write_buffer_len
;
944 static int ce_write_flush(SHA_CTX
*context
, int fd
)
946 unsigned int buffered
= write_buffer_len
;
948 SHA1_Update(context
, write_buffer
, buffered
);
949 if (write_in_full(fd
, write_buffer
, buffered
) != buffered
)
951 write_buffer_len
= 0;
956 static int ce_write(SHA_CTX
*context
, int fd
, void *data
, unsigned int len
)
959 unsigned int buffered
= write_buffer_len
;
960 unsigned int partial
= WRITE_BUFFER_SIZE
- buffered
;
963 memcpy(write_buffer
+ buffered
, data
, partial
);
965 if (buffered
== WRITE_BUFFER_SIZE
) {
966 write_buffer_len
= buffered
;
967 if (ce_write_flush(context
, fd
))
971 write_buffer_len
= buffered
;
973 data
= (char *) data
+ partial
;
978 static int write_index_ext_header(SHA_CTX
*context
, int fd
,
979 unsigned int ext
, unsigned int sz
)
983 return ((ce_write(context
, fd
, &ext
, 4) < 0) ||
984 (ce_write(context
, fd
, &sz
, 4) < 0)) ? -1 : 0;
987 static int ce_flush(SHA_CTX
*context
, int fd
)
989 unsigned int left
= write_buffer_len
;
992 write_buffer_len
= 0;
993 SHA1_Update(context
, write_buffer
, left
);
996 /* Flush first if not enough space for SHA1 signature */
997 if (left
+ 20 > WRITE_BUFFER_SIZE
) {
998 if (write_in_full(fd
, write_buffer
, left
) != left
)
1003 /* Append the SHA1 signature at the end */
1004 SHA1_Final(write_buffer
+ left
, context
);
1006 return (write_in_full(fd
, write_buffer
, left
) != left
) ? -1 : 0;
1009 static void ce_smudge_racily_clean_entry(struct cache_entry
*ce
)
1012 * The only thing we care about in this function is to smudge the
1013 * falsely clean entry due to touch-update-touch race, so we leave
1014 * everything else as they are. We are called for entries whose
1015 * ce_mtime match the index file mtime.
1019 if (lstat(ce
->name
, &st
) < 0)
1021 if (ce_match_stat_basic(ce
, &st
))
1023 if (ce_modified_check_fs(ce
, &st
)) {
1024 /* This is "racily clean"; smudge it. Note that this
1025 * is a tricky code. At first glance, it may appear
1026 * that it can break with this sequence:
1028 * $ echo xyzzy >frotz
1029 * $ git-update-index --add frotz
1032 * $ echo filfre >nitfol
1033 * $ git-update-index --add nitfol
1035 * but it does not. When the second update-index runs,
1036 * it notices that the entry "frotz" has the same timestamp
1037 * as index, and if we were to smudge it by resetting its
1038 * size to zero here, then the object name recorded
1039 * in index is the 6-byte file but the cached stat information
1040 * becomes zero --- which would then match what we would
1041 * obtain from the filesystem next time we stat("frotz").
1043 * However, the second update-index, before calling
1044 * this function, notices that the cached size is 6
1045 * bytes and what is on the filesystem is an empty
1046 * file, and never calls us, so the cached size information
1047 * for "frotz" stays 6 which does not match the filesystem.
1049 ce
->ce_size
= htonl(0);
1053 int write_index(struct index_state
*istate
, int newfd
)
1056 struct cache_header hdr
;
1058 struct cache_entry
**cache
= istate
->cache
;
1059 int entries
= istate
->cache_nr
;
1061 for (i
= removed
= 0; i
< entries
; i
++)
1062 if (!cache
[i
]->ce_mode
)
1065 hdr
.hdr_signature
= htonl(CACHE_SIGNATURE
);
1066 hdr
.hdr_version
= htonl(2);
1067 hdr
.hdr_entries
= htonl(entries
- removed
);
1070 if (ce_write(&c
, newfd
, &hdr
, sizeof(hdr
)) < 0)
1073 for (i
= 0; i
< entries
; i
++) {
1074 struct cache_entry
*ce
= cache
[i
];
1077 if (istate
->timestamp
&&
1078 istate
->timestamp
<= ntohl(ce
->ce_mtime
.sec
))
1079 ce_smudge_racily_clean_entry(ce
);
1080 if (ce_write(&c
, newfd
, ce
, ce_size(ce
)) < 0)
1084 /* Write extension data here */
1085 if (istate
->cache_tree
) {
1087 void *data
= cache_tree_write(istate
->cache_tree
, &sz
);
1089 !write_index_ext_header(&c
, newfd
, CACHE_EXT_TREE
, sz
) &&
1090 !ce_write(&c
, newfd
, data
, sz
))
1097 return ce_flush(&c
, newfd
);