cvsimport: set up commit environment in perl instead of using env
[git/dscho.git] / read-cache.c
blobb95edcc14c2ae4094372f4ec307cdcfa1b8f24fe
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 struct cache_entry **active_cache = NULL;
9 static time_t index_file_timestamp;
10 unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
13 * This only updates the "non-critical" parts of the directory
14 * cache, ie the parts that aren't tracked by GIT, and only used
15 * to validate the cache.
17 void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
19 ce->ce_ctime.sec = htonl(st->st_ctime);
20 ce->ce_mtime.sec = htonl(st->st_mtime);
21 #ifdef USE_NSEC
22 ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
23 ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
24 #endif
25 ce->ce_dev = htonl(st->st_dev);
26 ce->ce_ino = htonl(st->st_ino);
27 ce->ce_uid = htonl(st->st_uid);
28 ce->ce_gid = htonl(st->st_gid);
29 ce->ce_size = htonl(st->st_size);
31 if (assume_unchanged)
32 ce->ce_flags |= htons(CE_VALID);
35 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
37 int match = -1;
38 int fd = open(ce->name, O_RDONLY);
40 if (fd >= 0) {
41 unsigned char sha1[20];
42 if (!index_fd(sha1, fd, st, 0, NULL))
43 match = memcmp(sha1, ce->sha1, 20);
44 close(fd);
46 return match;
49 static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
51 int match = -1;
52 char *target;
53 void *buffer;
54 unsigned long size;
55 char type[10];
56 int len;
58 target = xmalloc(expected_size);
59 len = readlink(ce->name, target, expected_size);
60 if (len != expected_size) {
61 free(target);
62 return -1;
64 buffer = read_sha1_file(ce->sha1, type, &size);
65 if (!buffer) {
66 free(target);
67 return -1;
69 if (size == expected_size)
70 match = memcmp(buffer, target, size);
71 free(buffer);
72 free(target);
73 return match;
76 static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
78 switch (st->st_mode & S_IFMT) {
79 case S_IFREG:
80 if (ce_compare_data(ce, st))
81 return DATA_CHANGED;
82 break;
83 case S_IFLNK:
84 if (ce_compare_link(ce, st->st_size))
85 return DATA_CHANGED;
86 break;
87 default:
88 return TYPE_CHANGED;
90 return 0;
93 static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
95 unsigned int changed = 0;
97 switch (ntohl(ce->ce_mode) & S_IFMT) {
98 case S_IFREG:
99 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
100 /* We consider only the owner x bit to be relevant for
101 * "mode changes"
103 if (trust_executable_bit &&
104 (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
105 changed |= MODE_CHANGED;
106 break;
107 case S_IFLNK:
108 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
109 break;
110 default:
111 die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
113 if (ce->ce_mtime.sec != htonl(st->st_mtime))
114 changed |= MTIME_CHANGED;
115 if (ce->ce_ctime.sec != htonl(st->st_ctime))
116 changed |= CTIME_CHANGED;
118 #ifdef USE_NSEC
120 * nsec seems unreliable - not all filesystems support it, so
121 * as long as it is in the inode cache you get right nsec
122 * but after it gets flushed, you get zero nsec.
124 if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
125 changed |= MTIME_CHANGED;
126 if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
127 changed |= CTIME_CHANGED;
128 #endif
130 if (ce->ce_uid != htonl(st->st_uid) ||
131 ce->ce_gid != htonl(st->st_gid))
132 changed |= OWNER_CHANGED;
133 if (ce->ce_ino != htonl(st->st_ino))
134 changed |= INODE_CHANGED;
136 #ifdef USE_STDEV
138 * st_dev breaks on network filesystems where different
139 * clients will have different views of what "device"
140 * the filesystem is on
142 if (ce->ce_dev != htonl(st->st_dev))
143 changed |= INODE_CHANGED;
144 #endif
146 if (ce->ce_size != htonl(st->st_size))
147 changed |= DATA_CHANGED;
149 return changed;
152 int ce_match_stat(struct cache_entry *ce, struct stat *st, int ignore_valid)
154 unsigned int changed;
157 * If it's marked as always valid in the index, it's
158 * valid whatever the checked-out copy says.
160 if (!ignore_valid && (ce->ce_flags & htons(CE_VALID)))
161 return 0;
163 changed = ce_match_stat_basic(ce, st);
166 * Within 1 second of this sequence:
167 * echo xyzzy >file && git-update-index --add file
168 * running this command:
169 * echo frotz >file
170 * would give a falsely clean cache entry. The mtime and
171 * length match the cache, and other stat fields do not change.
173 * We could detect this at update-index time (the cache entry
174 * being registered/updated records the same time as "now")
175 * and delay the return from git-update-index, but that would
176 * effectively mean we can make at most one commit per second,
177 * which is not acceptable. Instead, we check cache entries
178 * whose mtime are the same as the index file timestamp more
179 * carefully than others.
181 if (!changed &&
182 index_file_timestamp &&
183 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
184 changed |= ce_modified_check_fs(ce, st);
186 return changed;
189 int ce_modified(struct cache_entry *ce, struct stat *st, int really)
191 int changed, changed_fs;
192 changed = ce_match_stat(ce, st, really);
193 if (!changed)
194 return 0;
196 * If the mode or type has changed, there's no point in trying
197 * to refresh the entry - it's not going to match
199 if (changed & (MODE_CHANGED | TYPE_CHANGED))
200 return changed;
202 /* Immediately after read-tree or update-index --cacheinfo,
203 * the length field is zero. For other cases the ce_size
204 * should match the SHA1 recorded in the index entry.
206 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
207 return changed;
209 changed_fs = ce_modified_check_fs(ce, st);
210 if (changed_fs)
211 return changed | changed_fs;
212 return 0;
215 int base_name_compare(const char *name1, int len1, int mode1,
216 const char *name2, int len2, int mode2)
218 unsigned char c1, c2;
219 int len = len1 < len2 ? len1 : len2;
220 int cmp;
222 cmp = memcmp(name1, name2, len);
223 if (cmp)
224 return cmp;
225 c1 = name1[len];
226 c2 = name2[len];
227 if (!c1 && S_ISDIR(mode1))
228 c1 = '/';
229 if (!c2 && S_ISDIR(mode2))
230 c2 = '/';
231 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
234 int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
236 int len1 = flags1 & CE_NAMEMASK;
237 int len2 = flags2 & CE_NAMEMASK;
238 int len = len1 < len2 ? len1 : len2;
239 int cmp;
241 cmp = memcmp(name1, name2, len);
242 if (cmp)
243 return cmp;
244 if (len1 < len2)
245 return -1;
246 if (len1 > len2)
247 return 1;
249 /* Compare stages */
250 flags1 &= CE_STAGEMASK;
251 flags2 &= CE_STAGEMASK;
253 if (flags1 < flags2)
254 return -1;
255 if (flags1 > flags2)
256 return 1;
257 return 0;
260 int cache_name_pos(const char *name, int namelen)
262 int first, last;
264 first = 0;
265 last = active_nr;
266 while (last > first) {
267 int next = (last + first) >> 1;
268 struct cache_entry *ce = active_cache[next];
269 int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
270 if (!cmp)
271 return next;
272 if (cmp < 0) {
273 last = next;
274 continue;
276 first = next+1;
278 return -first-1;
281 /* Remove entry, return true if there are more entries to go.. */
282 int remove_cache_entry_at(int pos)
284 active_cache_changed = 1;
285 active_nr--;
286 if (pos >= active_nr)
287 return 0;
288 memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
289 return 1;
292 int remove_file_from_cache(const char *path)
294 int pos = cache_name_pos(path, strlen(path));
295 if (pos < 0)
296 pos = -pos-1;
297 while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
298 remove_cache_entry_at(pos);
299 return 0;
302 int ce_same_name(struct cache_entry *a, struct cache_entry *b)
304 int len = ce_namelen(a);
305 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
308 int ce_path_match(const struct cache_entry *ce, const char **pathspec)
310 const char *match, *name;
311 int len;
313 if (!pathspec)
314 return 1;
316 len = ce_namelen(ce);
317 name = ce->name;
318 while ((match = *pathspec++) != NULL) {
319 int matchlen = strlen(match);
320 if (matchlen > len)
321 continue;
322 if (memcmp(name, match, matchlen))
323 continue;
324 if (matchlen && name[matchlen-1] == '/')
325 return 1;
326 if (name[matchlen] == '/' || !name[matchlen])
327 return 1;
328 if (!matchlen)
329 return 1;
331 return 0;
335 * Do we have another file that has the beginning components being a
336 * proper superset of the name we're trying to add?
338 static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
340 int retval = 0;
341 int len = ce_namelen(ce);
342 int stage = ce_stage(ce);
343 const char *name = ce->name;
345 while (pos < active_nr) {
346 struct cache_entry *p = active_cache[pos++];
348 if (len >= ce_namelen(p))
349 break;
350 if (memcmp(name, p->name, len))
351 break;
352 if (ce_stage(p) != stage)
353 continue;
354 if (p->name[len] != '/')
355 continue;
356 retval = -1;
357 if (!ok_to_replace)
358 break;
359 remove_cache_entry_at(--pos);
361 return retval;
365 * Do we have another file with a pathname that is a proper
366 * subset of the name we're trying to add?
368 static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
370 int retval = 0;
371 int stage = ce_stage(ce);
372 const char *name = ce->name;
373 const char *slash = name + ce_namelen(ce);
375 for (;;) {
376 int len;
378 for (;;) {
379 if (*--slash == '/')
380 break;
381 if (slash <= ce->name)
382 return retval;
384 len = slash - name;
386 pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
387 if (pos >= 0) {
388 retval = -1;
389 if (ok_to_replace)
390 break;
391 remove_cache_entry_at(pos);
392 continue;
396 * Trivial optimization: if we find an entry that
397 * already matches the sub-directory, then we know
398 * we're ok, and we can exit.
400 pos = -pos-1;
401 while (pos < active_nr) {
402 struct cache_entry *p = active_cache[pos];
403 if ((ce_namelen(p) <= len) ||
404 (p->name[len] != '/') ||
405 memcmp(p->name, name, len))
406 break; /* not our subdirectory */
407 if (ce_stage(p) == stage)
408 /* p is at the same stage as our entry, and
409 * is a subdirectory of what we are looking
410 * at, so we cannot have conflicts at our
411 * level or anything shorter.
413 return retval;
414 pos++;
417 return retval;
420 /* We may be in a situation where we already have path/file and path
421 * is being added, or we already have path and path/file is being
422 * added. Either one would result in a nonsense tree that has path
423 * twice when git-write-tree tries to write it out. Prevent it.
425 * If ok-to-replace is specified, we remove the conflicting entries
426 * from the cache so the caller should recompute the insert position.
427 * When this happens, we return non-zero.
429 static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
432 * We check if the path is a sub-path of a subsequent pathname
433 * first, since removing those will not change the position
434 * in the array
436 int retval = has_file_name(ce, pos, ok_to_replace);
438 * Then check if the path might have a clashing sub-directory
439 * before it.
441 return retval + has_dir_name(ce, pos, ok_to_replace);
444 int add_cache_entry(struct cache_entry *ce, int option)
446 int pos;
447 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
448 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
449 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
451 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
453 /* existing match? Just replace it. */
454 if (pos >= 0) {
455 active_cache_changed = 1;
456 active_cache[pos] = ce;
457 return 0;
459 pos = -pos-1;
462 * Inserting a merged entry ("stage 0") into the index
463 * will always replace all non-merged entries..
465 if (pos < active_nr && ce_stage(ce) == 0) {
466 while (ce_same_name(active_cache[pos], ce)) {
467 ok_to_add = 1;
468 if (!remove_cache_entry_at(pos))
469 break;
473 if (!ok_to_add)
474 return -1;
476 if (!skip_df_check &&
477 check_file_directory_conflict(ce, pos, ok_to_replace)) {
478 if (!ok_to_replace)
479 return -1;
480 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
481 pos = -pos-1;
484 /* Make sure the array is big enough .. */
485 if (active_nr == active_alloc) {
486 active_alloc = alloc_nr(active_alloc);
487 active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
490 /* Add it in.. */
491 active_nr++;
492 if (active_nr > pos)
493 memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
494 active_cache[pos] = ce;
495 active_cache_changed = 1;
496 return 0;
499 /* Three functions to allow overloaded pointer return; see linux/err.h */
500 static inline void *ERR_PTR(long error)
502 return (void *) error;
505 static inline long PTR_ERR(const void *ptr)
507 return (long) ptr;
510 static inline long IS_ERR(const void *ptr)
512 return (unsigned long)ptr > (unsigned long)-1000L;
516 * "refresh" does not calculate a new sha1 file or bring the
517 * cache up-to-date for mode/content changes. But what it
518 * _does_ do is to "re-match" the stat information of a file
519 * with the cache, so that you can refresh the cache for a
520 * file that hasn't been changed but where the stat entry is
521 * out of date.
523 * For example, you'd want to do this after doing a "git-read-tree",
524 * to link up the stat cache details with the proper files.
526 static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
528 struct stat st;
529 struct cache_entry *updated;
530 int changed, size;
532 if (lstat(ce->name, &st) < 0)
533 return ERR_PTR(-errno);
535 changed = ce_match_stat(ce, &st, really);
536 if (!changed) {
537 if (really && assume_unchanged &&
538 !(ce->ce_flags & htons(CE_VALID)))
539 ; /* mark this one VALID again */
540 else
541 return NULL;
544 if (ce_modified(ce, &st, really))
545 return ERR_PTR(-EINVAL);
547 size = ce_size(ce);
548 updated = xmalloc(size);
549 memcpy(updated, ce, size);
550 fill_stat_cache_info(updated, &st);
552 /* In this case, if really is not set, we should leave
553 * CE_VALID bit alone. Otherwise, paths marked with
554 * --no-assume-unchanged (i.e. things to be edited) will
555 * reacquire CE_VALID bit automatically, which is not
556 * really what we want.
558 if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
559 updated->ce_flags &= ~htons(CE_VALID);
561 return updated;
564 int refresh_cache(unsigned int flags)
566 int i;
567 int has_errors = 0;
568 int really = (flags & REFRESH_REALLY) != 0;
569 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
570 int quiet = (flags & REFRESH_QUIET) != 0;
571 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
573 for (i = 0; i < active_nr; i++) {
574 struct cache_entry *ce, *new;
575 ce = active_cache[i];
576 if (ce_stage(ce)) {
577 while ((i < active_nr) &&
578 ! strcmp(active_cache[i]->name, ce->name))
579 i++;
580 i--;
581 if (allow_unmerged)
582 continue;
583 printf("%s: needs merge\n", ce->name);
584 has_errors = 1;
585 continue;
588 new = refresh_entry(ce, really);
589 if (!new)
590 continue;
591 if (IS_ERR(new)) {
592 if (not_new && PTR_ERR(new) == -ENOENT)
593 continue;
594 if (really && PTR_ERR(new) == -EINVAL) {
595 /* If we are doing --really-refresh that
596 * means the index is not valid anymore.
598 ce->ce_flags &= ~htons(CE_VALID);
599 active_cache_changed = 1;
601 if (quiet)
602 continue;
603 printf("%s: needs update\n", ce->name);
604 has_errors = 1;
605 continue;
607 active_cache_changed = 1;
608 /* You can NOT just free active_cache[i] here, since it
609 * might not be necessarily malloc()ed but can also come
610 * from mmap(). */
611 active_cache[i] = new;
613 return has_errors;
616 static int verify_hdr(struct cache_header *hdr, unsigned long size)
618 SHA_CTX c;
619 unsigned char sha1[20];
621 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
622 return error("bad signature");
623 if (hdr->hdr_version != htonl(2))
624 return error("bad index version");
625 SHA1_Init(&c);
626 SHA1_Update(&c, hdr, size - 20);
627 SHA1_Final(sha1, &c);
628 if (memcmp(sha1, (void *)hdr + size - 20, 20))
629 return error("bad index file sha1 signature");
630 return 0;
633 int read_cache(void)
635 int fd, i;
636 struct stat st;
637 unsigned long size, offset;
638 void *map;
639 struct cache_header *hdr;
641 errno = EBUSY;
642 if (active_cache)
643 return active_nr;
645 errno = ENOENT;
646 index_file_timestamp = 0;
647 fd = open(get_index_file(), O_RDONLY);
648 if (fd < 0) {
649 if (errno == ENOENT)
650 return 0;
651 die("index file open failed (%s)", strerror(errno));
654 size = 0; // avoid gcc warning
655 map = MAP_FAILED;
656 if (!fstat(fd, &st)) {
657 size = st.st_size;
658 errno = EINVAL;
659 if (size >= sizeof(struct cache_header) + 20)
660 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
662 close(fd);
663 if (map == MAP_FAILED)
664 die("index file mmap failed (%s)", strerror(errno));
666 hdr = map;
667 if (verify_hdr(hdr, size) < 0)
668 goto unmap;
670 active_nr = ntohl(hdr->hdr_entries);
671 active_alloc = alloc_nr(active_nr);
672 active_cache = xcalloc(active_alloc, sizeof(struct cache_entry *));
674 offset = sizeof(*hdr);
675 for (i = 0; i < active_nr; i++) {
676 struct cache_entry *ce = map + offset;
677 offset = offset + ce_size(ce);
678 active_cache[i] = ce;
680 index_file_timestamp = st.st_mtime;
681 return active_nr;
683 unmap:
684 munmap(map, size);
685 errno = EINVAL;
686 die("index file corrupt");
689 #define WRITE_BUFFER_SIZE 8192
690 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
691 static unsigned long write_buffer_len;
693 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
695 while (len) {
696 unsigned int buffered = write_buffer_len;
697 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
698 if (partial > len)
699 partial = len;
700 memcpy(write_buffer + buffered, data, partial);
701 buffered += partial;
702 if (buffered == WRITE_BUFFER_SIZE) {
703 SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
704 if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
705 return -1;
706 buffered = 0;
708 write_buffer_len = buffered;
709 len -= partial;
710 data += partial;
712 return 0;
715 static int ce_flush(SHA_CTX *context, int fd)
717 unsigned int left = write_buffer_len;
719 if (left) {
720 write_buffer_len = 0;
721 SHA1_Update(context, write_buffer, left);
724 /* Flush first if not enough space for SHA1 signature */
725 if (left + 20 > WRITE_BUFFER_SIZE) {
726 if (write(fd, write_buffer, left) != left)
727 return -1;
728 left = 0;
731 /* Append the SHA1 signature at the end */
732 SHA1_Final(write_buffer + left, context);
733 left += 20;
734 if (write(fd, write_buffer, left) != left)
735 return -1;
736 return 0;
739 static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
742 * The only thing we care about in this function is to smudge the
743 * falsely clean entry due to touch-update-touch race, so we leave
744 * everything else as they are. We are called for entries whose
745 * ce_mtime match the index file mtime.
747 struct stat st;
749 if (lstat(ce->name, &st) < 0)
750 return;
751 if (ce_match_stat_basic(ce, &st))
752 return;
753 if (ce_modified_check_fs(ce, &st)) {
754 /* This is "racily clean"; smudge it. Note that this
755 * is a tricky code. At first glance, it may appear
756 * that it can break with this sequence:
758 * $ echo xyzzy >frotz
759 * $ git-update-index --add frotz
760 * $ : >frotz
761 * $ sleep 3
762 * $ echo filfre >nitfol
763 * $ git-update-index --add nitfol
765 * but it does not. Whe the second update-index runs,
766 * it notices that the entry "frotz" has the same timestamp
767 * as index, and if we were to smudge it by resetting its
768 * size to zero here, then the object name recorded
769 * in index is the 6-byte file but the cached stat information
770 * becomes zero --- which would then match what we would
771 * obtain from the filesystem next time we stat("frotz").
773 * However, the second update-index, before calling
774 * this function, notices that the cached size is 6
775 * bytes and what is on the filesystem is an empty
776 * file, and never calls us, so the cached size information
777 * for "frotz" stays 6 which does not match the filesystem.
779 ce->ce_size = htonl(0);
783 int write_cache(int newfd, struct cache_entry **cache, int entries)
785 SHA_CTX c;
786 struct cache_header hdr;
787 int i, removed;
789 for (i = removed = 0; i < entries; i++)
790 if (!cache[i]->ce_mode)
791 removed++;
793 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
794 hdr.hdr_version = htonl(2);
795 hdr.hdr_entries = htonl(entries - removed);
797 SHA1_Init(&c);
798 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
799 return -1;
801 for (i = 0; i < entries; i++) {
802 struct cache_entry *ce = cache[i];
803 if (!ce->ce_mode)
804 continue;
805 if (index_file_timestamp &&
806 index_file_timestamp <= ntohl(ce->ce_mtime.sec))
807 ce_smudge_racily_clean_entry(ce);
808 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
809 return -1;
811 return ce_flush(&c, newfd);