skip t5512 because remote does not yet work
[git/platforms/storm.git] / read-cache.c
blob7748c3ece79d23527387e1809e809ace6a61e84f
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define NO_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 #include "cache-tree.h"
9 #include "refs.h"
10 #include "dir.h"
12 /* Index extensions.
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);
35 #ifdef USE_NSEC
36 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
37 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
38 #endif
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);
45 if (assume_unchanged)
46 ce->ce_flags |= htons(CE_VALID);
49 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
51 int match = -1;
52 int fd = open(ce->name, O_RDONLY);
54 if (fd >= 0) {
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 */
60 return match;
63 static int ce_compare_link(struct cache_entry *ce, size_t expected_size)
65 int match = -1;
66 char *target;
67 void *buffer;
68 unsigned long size;
69 enum object_type type;
70 int len;
72 target = xmalloc(expected_size);
73 len = readlink(ce->name, target, expected_size);
74 if (len != expected_size) {
75 free(target);
76 return -1;
78 buffer = read_sha1_file(ce->sha1, &type, &size);
79 if (!buffer) {
80 free(target);
81 return -1;
83 if (size == expected_size)
84 match = memcmp(buffer, target, size);
85 free(buffer);
86 free(target);
87 return match;
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
98 * sub-project).
100 * If so, we consider it always to match.
102 if (resolve_gitlink_ref(ce->name, "HEAD", sha1) < 0)
103 return 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) {
110 case S_IFREG:
111 if (ce_compare_data(ce, st))
112 return DATA_CHANGED;
113 break;
114 case S_IFLNK:
115 if (ce_compare_link(ce, xsize_t(st->st_size)))
116 return DATA_CHANGED;
117 break;
118 case S_IFDIR:
119 if (S_ISGITLINK(ntohl(ce->ce_mode)))
120 return 0;
121 default:
122 return TYPE_CHANGED;
124 return 0;
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) {
132 case S_IFREG:
133 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
134 /* We consider only the owner x bit to be relevant for
135 * "mode changes"
137 if (trust_executable_bit &&
138 (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
139 changed |= MODE_CHANGED;
140 break;
141 case S_IFLNK:
142 if (!S_ISLNK(st->st_mode) &&
143 (has_symlinks || !S_ISREG(st->st_mode)))
144 changed |= TYPE_CHANGED;
145 break;
146 case S_IFGITLINK:
147 if (!S_ISDIR(st->st_mode))
148 changed |= TYPE_CHANGED;
149 else if (ce_compare_gitlink(ce))
150 changed |= DATA_CHANGED;
151 return changed;
152 case 0: /* Special case: unmerged file in index */
153 return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
154 default:
155 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
157 if (ce->ce_mtime.sec != htonl(st->st_mtime))
158 changed |= MTIME_CHANGED;
159 if (ce->ce_ctime.sec != htonl(st->st_ctime))
160 changed |= CTIME_CHANGED;
162 #ifdef USE_NSEC
164 * nsec seems unreliable - not all filesystems support it, so
165 * as long as it is in the inode cache you get right nsec
166 * but after it gets flushed, you get zero nsec.
168 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
169 changed |= MTIME_CHANGED;
170 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
171 changed |= CTIME_CHANGED;
172 #endif
174 if (ce->ce_uid != htonl(st->st_uid) ||
175 ce->ce_gid != htonl(st->st_gid))
176 changed |= OWNER_CHANGED;
177 if (ce->ce_ino != htonl(st->st_ino))
178 changed |= INODE_CHANGED;
180 #ifdef USE_STDEV
182 * st_dev breaks on network filesystems where different
183 * clients will have different views of what "device"
184 * the filesystem is on
186 if (ce->ce_dev != htonl(st->st_dev))
187 changed |= INODE_CHANGED;
188 #endif
190 if (ce->ce_size != htonl(st->st_size))
191 changed |= DATA_CHANGED;
193 return changed;
196 int ie_match_stat(struct index_state *istate,
197 struct cache_entry *ce, struct stat *st,
198 unsigned int options)
200 unsigned int changed;
201 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
202 int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;
205 * If it's marked as always valid in the index, it's
206 * valid whatever the checked-out copy says.
208 if (!ignore_valid && (ce->ce_flags & htons(CE_VALID)))
209 return 0;
211 changed = ce_match_stat_basic(ce, st);
214 * Within 1 second of this sequence:
215 * echo xyzzy >file && git-update-index --add file
216 * running this command:
217 * echo frotz >file
218 * would give a falsely clean cache entry. The mtime and
219 * length match the cache, and other stat fields do not change.
221 * We could detect this at update-index time (the cache entry
222 * being registered/updated records the same time as "now")
223 * and delay the return from git-update-index, but that would
224 * effectively mean we can make at most one commit per second,
225 * which is not acceptable. Instead, we check cache entries
226 * whose mtime are the same as the index file timestamp more
227 * carefully than others.
229 if (!changed &&
230 istate->timestamp &&
231 istate->timestamp <= ntohl(ce->ce_mtime.sec)) {
232 if (assume_racy_is_modified)
233 changed |= DATA_CHANGED;
234 else
235 changed |= ce_modified_check_fs(ce, st);
238 return changed;
241 int ie_modified(struct index_state *istate,
242 struct cache_entry *ce, struct stat *st, unsigned int options)
244 int changed, changed_fs;
246 changed = ie_match_stat(istate, ce, st, options);
247 if (!changed)
248 return 0;
250 * If the mode or type has changed, there's no point in trying
251 * to refresh the entry - it's not going to match
253 if (changed & (MODE_CHANGED | TYPE_CHANGED))
254 return changed;
256 /* Immediately after read-tree or update-index --cacheinfo,
257 * the length field is zero. For other cases the ce_size
258 * should match the SHA1 recorded in the index entry.
260 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
261 return changed;
263 changed_fs = ce_modified_check_fs(ce, st);
264 if (changed_fs)
265 return changed | changed_fs;
266 return 0;
269 int base_name_compare(const char *name1, int len1, int mode1,
270 const char *name2, int len2, int mode2)
272 unsigned char c1, c2;
273 int len = len1 < len2 ? len1 : len2;
274 int cmp;
276 cmp = memcmp(name1, name2, len);
277 if (cmp)
278 return cmp;
279 c1 = name1[len];
280 c2 = name2[len];
281 if (!c1 && S_ISDIR(mode1))
282 c1 = '/';
283 if (!c2 && S_ISDIR(mode2))
284 c2 = '/';
285 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
288 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
290 int len1 = flags1 & CE_NAMEMASK;
291 int len2 = flags2 & CE_NAMEMASK;
292 int len = len1 < len2 ? len1 : len2;
293 int cmp;
295 cmp = memcmp(name1, name2, len);
296 if (cmp)
297 return cmp;
298 if (len1 < len2)
299 return -1;
300 if (len1 > len2)
301 return 1;
303 /* Compare stages */
304 flags1 &= CE_STAGEMASK;
305 flags2 &= CE_STAGEMASK;
307 if (flags1 < flags2)
308 return -1;
309 if (flags1 > flags2)
310 return 1;
311 return 0;
314 int index_name_pos(struct index_state *istate, const char *name, int namelen)
316 int first, last;
318 first = 0;
319 last = istate->cache_nr;
320 while (last > first) {
321 int next = (last + first) >> 1;
322 struct cache_entry *ce = istate->cache[next];
323 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
324 if (!cmp)
325 return next;
326 if (cmp < 0) {
327 last = next;
328 continue;
330 first = next+1;
332 return -first-1;
335 /* Remove entry, return true if there are more entries to go.. */
336 int remove_index_entry_at(struct index_state *istate, int pos)
338 istate->cache_changed = 1;
339 istate->cache_nr--;
340 if (pos >= istate->cache_nr)
341 return 0;
342 memmove(istate->cache + pos,
343 istate->cache + pos + 1,
344 (istate->cache_nr - pos) * sizeof(struct cache_entry *));
345 return 1;
348 int remove_file_from_index(struct index_state *istate, const char *path)
350 int pos = index_name_pos(istate, path, strlen(path));
351 if (pos < 0)
352 pos = -pos-1;
353 cache_tree_invalidate_path(istate->cache_tree, path);
354 while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
355 remove_index_entry_at(istate, pos);
356 return 0;
359 static int compare_name(struct cache_entry *ce, const char *path, int namelen)
361 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
364 static int index_name_pos_also_unmerged(struct index_state *istate,
365 const char *path, int namelen)
367 int pos = index_name_pos(istate, path, namelen);
368 struct cache_entry *ce;
370 if (pos >= 0)
371 return pos;
373 /* maybe unmerged? */
374 pos = -1 - pos;
375 if (pos >= istate->cache_nr ||
376 compare_name((ce = istate->cache[pos]), path, namelen))
377 return -1;
379 /* order of preference: stage 2, 1, 3 */
380 if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr &&
381 ce_stage((ce = istate->cache[pos + 1])) == 2 &&
382 !compare_name(ce, path, namelen))
383 pos++;
384 return pos;
387 int add_file_to_index(struct index_state *istate, const char *path, int verbose)
389 int size, namelen, pos;
390 struct stat st;
391 struct cache_entry *ce;
392 unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY;
394 if (lstat(path, &st))
395 die("%s: unable to stat (%s)", path, strerror(errno));
397 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
398 die("%s: can only add regular files, symbolic links or git-directories", path);
400 namelen = strlen(path);
401 if (S_ISDIR(st.st_mode)) {
402 while (namelen && path[namelen-1] == '/')
403 namelen--;
405 size = cache_entry_size(namelen);
406 ce = xcalloc(1, size);
407 memcpy(ce->name, path, namelen);
408 ce->ce_flags = htons(namelen);
409 fill_stat_cache_info(ce, &st);
411 if (trust_executable_bit && has_symlinks)
412 ce->ce_mode = create_ce_mode(st.st_mode);
413 else {
414 /* If there is an existing entry, pick the mode bits and type
415 * from it, otherwise assume unexecutable regular file.
417 struct cache_entry *ent;
418 int pos = index_name_pos_also_unmerged(istate, path, namelen);
420 ent = (0 <= pos) ? istate->cache[pos] : NULL;
421 ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
424 pos = index_name_pos(istate, ce->name, namelen);
425 if (0 <= pos &&
426 !ce_stage(istate->cache[pos]) &&
427 !ie_match_stat(istate, istate->cache[pos], &st, ce_option)) {
428 /* Nothing changed, really */
429 free(ce);
430 return 0;
433 if (index_path(ce->sha1, path, &st, 1))
434 die("unable to index file %s", path);
435 if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
436 die("unable to add %s to index",path);
437 if (verbose)
438 printf("add '%s'\n", path);
439 return 0;
442 struct cache_entry *make_cache_entry(unsigned int mode,
443 const unsigned char *sha1, const char *path, int stage,
444 int refresh)
446 int size, len;
447 struct cache_entry *ce;
449 if (!verify_path(path))
450 return NULL;
452 len = strlen(path);
453 size = cache_entry_size(len);
454 ce = xcalloc(1, size);
456 hashcpy(ce->sha1, sha1);
457 memcpy(ce->name, path, len);
458 ce->ce_flags = create_ce_flags(len, stage);
459 ce->ce_mode = create_ce_mode(mode);
461 if (refresh)
462 return refresh_cache_entry(ce, 0);
464 return ce;
467 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
469 int len = ce_namelen(a);
470 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
473 int ce_path_match(const struct cache_entry *ce, const char **pathspec)
475 const char *match, *name;
476 int len;
478 if (!pathspec)
479 return 1;
481 len = ce_namelen(ce);
482 name = ce->name;
483 while ((match = *pathspec++) != NULL) {
484 int matchlen = strlen(match);
485 if (matchlen > len)
486 continue;
487 if (memcmp(name, match, matchlen))
488 continue;
489 if (matchlen && name[matchlen-1] == '/')
490 return 1;
491 if (name[matchlen] == '/' || !name[matchlen])
492 return 1;
493 if (!matchlen)
494 return 1;
496 return 0;
500 * We fundamentally don't like some paths: we don't want
501 * dot or dot-dot anywhere, and for obvious reasons don't
502 * want to recurse into ".git" either.
504 * Also, we don't want double slashes or slashes at the
505 * end that can make pathnames ambiguous.
507 static int verify_dotfile(const char *rest)
510 * The first character was '.', but that
511 * has already been discarded, we now test
512 * the rest.
514 switch (*rest) {
515 /* "." is not allowed */
516 case '\0': case '/':
517 return 0;
520 * ".git" followed by NUL or slash is bad. This
521 * shares the path end test with the ".." case.
523 case 'g':
524 if (rest[1] != 'i')
525 break;
526 if (rest[2] != 't')
527 break;
528 rest += 2;
529 /* fallthrough */
530 case '.':
531 if (rest[1] == '\0' || rest[1] == '/')
532 return 0;
534 return 1;
537 int verify_path(const char *path)
539 char c;
541 #ifdef __MINGW32__
542 if (is_absolute_path(path))
543 return error("Cannot handle absolute path: %s", path);
544 #endif
546 goto inside;
547 for (;;) {
548 if (!c)
549 return 1;
550 if (c == '/') {
551 inside:
552 c = *path++;
553 switch (c) {
554 default:
555 continue;
556 case '/': case '\0':
557 break;
558 case '.':
559 if (verify_dotfile(path))
560 continue;
562 return 0;
564 c = *path++;
569 * Do we have another file that has the beginning components being a
570 * proper superset of the name we're trying to add?
572 static int has_file_name(struct index_state *istate,
573 const struct cache_entry *ce, int pos, int ok_to_replace)
575 int retval = 0;
576 int len = ce_namelen(ce);
577 int stage = ce_stage(ce);
578 const char *name = ce->name;
580 while (pos < istate->cache_nr) {
581 struct cache_entry *p = istate->cache[pos++];
583 if (len >= ce_namelen(p))
584 break;
585 if (memcmp(name, p->name, len))
586 break;
587 if (ce_stage(p) != stage)
588 continue;
589 if (p->name[len] != '/')
590 continue;
591 if (!ce_stage(p) && !p->ce_mode)
592 continue;
593 retval = -1;
594 if (!ok_to_replace)
595 break;
596 remove_index_entry_at(istate, --pos);
598 return retval;
602 * Do we have another file with a pathname that is a proper
603 * subset of the name we're trying to add?
605 static int has_dir_name(struct index_state *istate,
606 const struct cache_entry *ce, int pos, int ok_to_replace)
608 int retval = 0;
609 int stage = ce_stage(ce);
610 const char *name = ce->name;
611 const char *slash = name + ce_namelen(ce);
613 for (;;) {
614 int len;
616 for (;;) {
617 if (*--slash == '/')
618 break;
619 if (slash <= ce->name)
620 return retval;
622 len = slash - name;
624 pos = index_name_pos(istate, name, ntohs(create_ce_flags(len, stage)));
625 if (pos >= 0) {
627 * Found one, but not so fast. This could
628 * be a marker that says "I was here, but
629 * I am being removed". Such an entry is
630 * not a part of the resulting tree, and
631 * it is Ok to have a directory at the same
632 * path.
634 if (stage || istate->cache[pos]->ce_mode) {
635 retval = -1;
636 if (!ok_to_replace)
637 break;
638 remove_index_entry_at(istate, pos);
639 continue;
642 else
643 pos = -pos-1;
646 * Trivial optimization: if we find an entry that
647 * already matches the sub-directory, then we know
648 * we're ok, and we can exit.
650 while (pos < istate->cache_nr) {
651 struct cache_entry *p = istate->cache[pos];
652 if ((ce_namelen(p) <= len) ||
653 (p->name[len] != '/') ||
654 memcmp(p->name, name, len))
655 break; /* not our subdirectory */
656 if (ce_stage(p) == stage && (stage || p->ce_mode))
657 /* p is at the same stage as our entry, and
658 * is a subdirectory of what we are looking
659 * at, so we cannot have conflicts at our
660 * level or anything shorter.
662 return retval;
663 pos++;
666 return retval;
669 /* We may be in a situation where we already have path/file and path
670 * is being added, or we already have path and path/file is being
671 * added. Either one would result in a nonsense tree that has path
672 * twice when git-write-tree tries to write it out. Prevent it.
674 * If ok-to-replace is specified, we remove the conflicting entries
675 * from the cache so the caller should recompute the insert position.
676 * When this happens, we return non-zero.
678 static int check_file_directory_conflict(struct index_state *istate,
679 const struct cache_entry *ce,
680 int pos, int ok_to_replace)
682 int retval;
685 * When ce is an "I am going away" entry, we allow it to be added
687 if (!ce_stage(ce) && !ce->ce_mode)
688 return 0;
691 * We check if the path is a sub-path of a subsequent pathname
692 * first, since removing those will not change the position
693 * in the array.
695 retval = has_file_name(istate, ce, pos, ok_to_replace);
698 * Then check if the path might have a clashing sub-directory
699 * before it.
701 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
704 static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
706 int pos;
707 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
708 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
709 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
711 cache_tree_invalidate_path(istate->cache_tree, ce->name);
712 pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags));
714 /* existing match? Just replace it. */
715 if (pos >= 0) {
716 istate->cache_changed = 1;
717 istate->cache[pos] = ce;
718 return 0;
720 pos = -pos-1;
723 * Inserting a merged entry ("stage 0") into the index
724 * will always replace all non-merged entries..
726 if (pos < istate->cache_nr && ce_stage(ce) == 0) {
727 while (ce_same_name(istate->cache[pos], ce)) {
728 ok_to_add = 1;
729 if (!remove_index_entry_at(istate, pos))
730 break;
734 if (!ok_to_add)
735 return -1;
736 if (!verify_path(ce->name))
737 return -1;
739 if (!skip_df_check &&
740 check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
741 if (!ok_to_replace)
742 return error("'%s' appears as both a file and as a directory",
743 ce->name);
744 pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags));
745 pos = -pos-1;
747 return pos + 1;
750 int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
752 int pos;
754 if (option & ADD_CACHE_JUST_APPEND)
755 pos = istate->cache_nr;
756 else {
757 int ret;
758 ret = add_index_entry_with_check(istate, ce, option);
759 if (ret <= 0)
760 return ret;
761 pos = ret - 1;
764 /* Make sure the array is big enough .. */
765 if (istate->cache_nr == istate->cache_alloc) {
766 istate->cache_alloc = alloc_nr(istate->cache_alloc);
767 istate->cache = xrealloc(istate->cache,
768 istate->cache_alloc * sizeof(struct cache_entry *));
771 /* Add it in.. */
772 istate->cache_nr++;
773 if (istate->cache_nr > pos + 1)
774 memmove(istate->cache + pos + 1,
775 istate->cache + pos,
776 (istate->cache_nr - pos - 1) * sizeof(ce));
777 istate->cache[pos] = ce;
778 istate->cache_changed = 1;
779 return 0;
783 * "refresh" does not calculate a new sha1 file or bring the
784 * cache up-to-date for mode/content changes. But what it
785 * _does_ do is to "re-match" the stat information of a file
786 * with the cache, so that you can refresh the cache for a
787 * file that hasn't been changed but where the stat entry is
788 * out of date.
790 * For example, you'd want to do this after doing a "git-read-tree",
791 * to link up the stat cache details with the proper files.
793 static struct cache_entry *refresh_cache_ent(struct index_state *istate,
794 struct cache_entry *ce,
795 unsigned int options, int *err)
797 struct stat st;
798 struct cache_entry *updated;
799 int changed, size;
800 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
802 if (lstat(ce->name, &st) < 0) {
803 if (err)
804 *err = errno;
805 return NULL;
808 changed = ie_match_stat(istate, ce, &st, options);
809 if (!changed) {
811 * The path is unchanged. If we were told to ignore
812 * valid bit, then we did the actual stat check and
813 * found that the entry is unmodified. If the entry
814 * is not marked VALID, this is the place to mark it
815 * valid again, under "assume unchanged" mode.
817 if (ignore_valid && assume_unchanged &&
818 !(ce->ce_flags & htons(CE_VALID)))
819 ; /* mark this one VALID again */
820 else
821 return ce;
824 if (ie_modified(istate, ce, &st, options)) {
825 if (err)
826 *err = EINVAL;
827 return NULL;
830 size = ce_size(ce);
831 updated = xmalloc(size);
832 memcpy(updated, ce, size);
833 fill_stat_cache_info(updated, &st);
836 * If ignore_valid is not set, we should leave CE_VALID bit
837 * alone. Otherwise, paths marked with --no-assume-unchanged
838 * (i.e. things to be edited) will reacquire CE_VALID bit
839 * automatically, which is not really what we want.
841 if (!ignore_valid && assume_unchanged &&
842 !(ce->ce_flags & htons(CE_VALID)))
843 updated->ce_flags &= ~htons(CE_VALID);
845 return updated;
848 int refresh_index(struct index_state *istate, unsigned int flags, const char **pathspec, char *seen)
850 int i;
851 int has_errors = 0;
852 int really = (flags & REFRESH_REALLY) != 0;
853 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
854 int quiet = (flags & REFRESH_QUIET) != 0;
855 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
856 unsigned int options = really ? CE_MATCH_IGNORE_VALID : 0;
858 for (i = 0; i < istate->cache_nr; i++) {
859 struct cache_entry *ce, *new;
860 int cache_errno = 0;
862 ce = istate->cache[i];
863 if (ce_stage(ce)) {
864 while ((i < istate->cache_nr) &&
865 ! strcmp(istate->cache[i]->name, ce->name))
866 i++;
867 i--;
868 if (allow_unmerged)
869 continue;
870 printf("%s: needs merge\n", ce->name);
871 has_errors = 1;
872 continue;
875 if (pathspec && !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen))
876 continue;
878 new = refresh_cache_ent(istate, ce, options, &cache_errno);
879 if (new == ce)
880 continue;
881 if (!new) {
882 if (not_new && cache_errno == ENOENT)
883 continue;
884 if (really && cache_errno == EINVAL) {
885 /* If we are doing --really-refresh that
886 * means the index is not valid anymore.
888 ce->ce_flags &= ~htons(CE_VALID);
889 istate->cache_changed = 1;
891 if (quiet)
892 continue;
893 printf("%s: needs update\n", ce->name);
894 has_errors = 1;
895 continue;
897 istate->cache_changed = 1;
898 /* You can NOT just free istate->cache[i] here, since it
899 * might not be necessarily malloc()ed but can also come
900 * from mmap(). */
901 istate->cache[i] = new;
903 return has_errors;
906 struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really)
908 return refresh_cache_ent(&the_index, ce, really, NULL);
911 static int verify_hdr(struct cache_header *hdr, unsigned long size)
913 SHA_CTX c;
914 unsigned char sha1[20];
916 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
917 return error("bad signature");
918 if (hdr->hdr_version != htonl(2))
919 return error("bad index version");
920 SHA1_Init(&c);
921 SHA1_Update(&c, hdr, size - 20);
922 SHA1_Final(sha1, &c);
923 if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
924 return error("bad index file sha1 signature");
925 return 0;
928 static int read_index_extension(struct index_state *istate,
929 const char *ext, void *data, unsigned long sz)
931 switch (CACHE_EXT(ext)) {
932 case CACHE_EXT_TREE:
933 istate->cache_tree = cache_tree_read(data, sz);
934 break;
935 default:
936 if (*ext < 'A' || 'Z' < *ext)
937 return error("index uses %.4s extension, which we do not understand",
938 ext);
939 fprintf(stderr, "ignoring %.4s extension\n", ext);
940 break;
942 return 0;
945 int read_index(struct index_state *istate)
947 return read_index_from(istate, get_index_file());
950 /* remember to discard_cache() before reading a different cache! */
951 int read_index_from(struct index_state *istate, const char *path)
953 int fd, i;
954 struct stat st;
955 unsigned long offset;
956 struct cache_header *hdr;
958 errno = EBUSY;
959 if (istate->mmap)
960 return istate->cache_nr;
962 errno = ENOENT;
963 istate->timestamp = 0;
964 fd = open(path, O_RDONLY);
965 if (fd < 0) {
966 if (errno == ENOENT)
967 return 0;
968 die("index file open failed (%s)", strerror(errno));
971 if (fstat(fd, &st))
972 die("cannot stat the open index (%s)", strerror(errno));
974 errno = EINVAL;
975 istate->mmap_size = xsize_t(st.st_size);
976 if (istate->mmap_size < sizeof(struct cache_header) + 20)
977 die("index file smaller than expected");
979 istate->mmap = xmmap(NULL, istate->mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
980 close(fd);
982 hdr = istate->mmap;
983 if (verify_hdr(hdr, istate->mmap_size) < 0)
984 goto unmap;
986 istate->cache_nr = ntohl(hdr->hdr_entries);
987 istate->cache_alloc = alloc_nr(istate->cache_nr);
988 istate->cache = xcalloc(istate->cache_alloc, sizeof(struct cache_entry *));
990 offset = sizeof(*hdr);
991 for (i = 0; i < istate->cache_nr; i++) {
992 struct cache_entry *ce;
994 ce = (struct cache_entry *)((char *)(istate->mmap) + offset);
995 offset = offset + ce_size(ce);
996 istate->cache[i] = ce;
998 istate->timestamp = st.st_mtime;
999 while (offset <= istate->mmap_size - 20 - 8) {
1000 /* After an array of active_nr index entries,
1001 * there can be arbitrary number of extended
1002 * sections, each of which is prefixed with
1003 * extension name (4-byte) and section length
1004 * in 4-byte network byte order.
1006 unsigned long extsize;
1007 memcpy(&extsize, (char *)(istate->mmap) + offset + 4, 4);
1008 extsize = ntohl(extsize);
1009 if (read_index_extension(istate,
1010 ((const char *) (istate->mmap)) + offset,
1011 (char *) (istate->mmap) + offset + 8,
1012 extsize) < 0)
1013 goto unmap;
1014 offset += 8;
1015 offset += extsize;
1017 return istate->cache_nr;
1019 unmap:
1020 munmap(istate->mmap, istate->mmap_size);
1021 errno = EINVAL;
1022 die("index file corrupt");
1025 int discard_index(struct index_state *istate)
1027 int ret;
1029 istate->cache_nr = 0;
1030 istate->cache_changed = 0;
1031 istate->timestamp = 0;
1032 cache_tree_free(&(istate->cache_tree));
1033 if (istate->mmap == NULL)
1034 return 0;
1035 ret = munmap(istate->mmap, istate->mmap_size);
1036 istate->mmap = NULL;
1037 istate->mmap_size = 0;
1039 /* no need to throw away allocated active_cache */
1040 return ret;
1043 #define WRITE_BUFFER_SIZE 8192
1044 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
1045 static unsigned long write_buffer_len;
1047 static int ce_write_flush(SHA_CTX *context, int fd)
1049 unsigned int buffered = write_buffer_len;
1050 if (buffered) {
1051 SHA1_Update(context, write_buffer, buffered);
1052 if (write_in_full(fd, write_buffer, buffered) != buffered)
1053 return -1;
1054 write_buffer_len = 0;
1056 return 0;
1059 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
1061 while (len) {
1062 unsigned int buffered = write_buffer_len;
1063 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
1064 if (partial > len)
1065 partial = len;
1066 memcpy(write_buffer + buffered, data, partial);
1067 buffered += partial;
1068 if (buffered == WRITE_BUFFER_SIZE) {
1069 write_buffer_len = buffered;
1070 if (ce_write_flush(context, fd))
1071 return -1;
1072 buffered = 0;
1074 write_buffer_len = buffered;
1075 len -= partial;
1076 data = (char *) data + partial;
1078 return 0;
1081 static int write_index_ext_header(SHA_CTX *context, int fd,
1082 unsigned int ext, unsigned int sz)
1084 ext = htonl(ext);
1085 sz = htonl(sz);
1086 return ((ce_write(context, fd, &ext, 4) < 0) ||
1087 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
1090 static int ce_flush(SHA_CTX *context, int fd)
1092 unsigned int left = write_buffer_len;
1094 if (left) {
1095 write_buffer_len = 0;
1096 SHA1_Update(context, write_buffer, left);
1099 /* Flush first if not enough space for SHA1 signature */
1100 if (left + 20 > WRITE_BUFFER_SIZE) {
1101 if (write_in_full(fd, write_buffer, left) != left)
1102 return -1;
1103 left = 0;
1106 /* Append the SHA1 signature at the end */
1107 SHA1_Final(write_buffer + left, context);
1108 left += 20;
1109 return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
1112 static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
1115 * The only thing we care about in this function is to smudge the
1116 * falsely clean entry due to touch-update-touch race, so we leave
1117 * everything else as they are. We are called for entries whose
1118 * ce_mtime match the index file mtime.
1120 struct stat st;
1122 if (lstat(ce->name, &st) < 0)
1123 return;
1124 if (ce_match_stat_basic(ce, &st))
1125 return;
1126 if (ce_modified_check_fs(ce, &st)) {
1127 /* This is "racily clean"; smudge it. Note that this
1128 * is a tricky code. At first glance, it may appear
1129 * that it can break with this sequence:
1131 * $ echo xyzzy >frotz
1132 * $ git-update-index --add frotz
1133 * $ : >frotz
1134 * $ sleep 3
1135 * $ echo filfre >nitfol
1136 * $ git-update-index --add nitfol
1138 * but it does not. When the second update-index runs,
1139 * it notices that the entry "frotz" has the same timestamp
1140 * as index, and if we were to smudge it by resetting its
1141 * size to zero here, then the object name recorded
1142 * in index is the 6-byte file but the cached stat information
1143 * becomes zero --- which would then match what we would
1144 * obtain from the filesystem next time we stat("frotz").
1146 * However, the second update-index, before calling
1147 * this function, notices that the cached size is 6
1148 * bytes and what is on the filesystem is an empty
1149 * file, and never calls us, so the cached size information
1150 * for "frotz" stays 6 which does not match the filesystem.
1152 ce->ce_size = htonl(0);
1153 trace_printf("trace: index: Index object for '%s' smudged due to being racily clean\n", ce->name);
1157 int write_index(struct index_state *istate, int newfd)
1159 SHA_CTX c;
1160 struct cache_header hdr;
1161 int i, err, removed;
1162 struct cache_entry **cache = istate->cache;
1163 int entries = istate->cache_nr;
1165 for (i = removed = 0; i < entries; i++)
1166 if (!cache[i]->ce_mode)
1167 removed++;
1169 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
1170 hdr.hdr_version = htonl(2);
1171 hdr.hdr_entries = htonl(entries - removed);
1173 SHA1_Init(&c);
1174 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
1175 return -1;
1177 for (i = 0; i < entries; i++) {
1178 struct cache_entry *ce = cache[i];
1179 if (!ce->ce_mode)
1180 continue;
1181 if (istate->timestamp &&
1182 istate->timestamp <= ntohl(ce->ce_mtime.sec))
1183 ce_smudge_racily_clean_entry(ce);
1184 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
1185 return -1;
1188 /* Write extension data here */
1189 if (istate->cache_tree) {
1190 struct strbuf sb;
1192 strbuf_init(&sb, 0);
1193 cache_tree_write(&sb, istate->cache_tree);
1194 err = write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) < 0
1195 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
1196 strbuf_release(&sb);
1197 if (err)
1198 return -1;
1200 return ce_flush(&c, newfd);