@matheusbsbs / matheus-bagliano / commits / 407c8eb0d0
Fork 0

Racy GIT (part #2)

The previous round caught the most trivial case well, but broke down once index file is updated again. Smudge problematic entries (they should be very few if any under normal interactive workflow) before writing a new index file out. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 20, 2005 at 12:12 UTC 407c8eb0d09d4b84bbfda9e04895a35c8fd6fef6
2 files changed +44 -5
read-cache.c
+31 -1
@@ -87,7 +87,7 @@ static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
87 return 0;
88 }
89
90 -int ce_match_stat(struct cache_entry *ce, struct stat *st)
90 +static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
91 {
92 unsigned int changed = 0;
93
@@ -143,6 +143,13 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st)
143 if (ce->ce_size != htonl(st->st_size))
144 changed |= DATA_CHANGED;
145
146 + return changed;
147 +}
148 +
149 +int ce_match_stat(struct cache_entry *ce, struct stat *st)
150 +{
151 + unsigned int changed = ce_match_stat_basic(ce, st);
152 +
153 /*
154 * Within 1 second of this sequence:
155 * echo xyzzy >file && git-update-index --add file
@@ -594,6 +601,26 @@ static int ce_flush(SHA_CTX *context, int fd)
601 return 0;
602 }
603
604 +static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
605 +{
606 + /*
607 + * The only thing we care about in this function is to smudge the
608 + * falsely clean entry due to touch-update-touch race, so we leave
609 + * everything else as they are. We are called for entries whose
610 + * ce_mtime match the index file mtime.
611 + */
612 + struct stat st;
613 +
614 + if (lstat(ce->name, &st) < 0)
615 + return;
616 + if (ce_match_stat_basic(ce, &st))
617 + return;
618 + if (ce_modified_check_fs(ce, &st)) {
619 + /* This is "racily clean"; smudge it */
620 + ce->ce_size = htonl(0);
621 + }
622 +}
623 +
624 int write_cache(int newfd, struct cache_entry **cache, int entries)
625 {
626 SHA_CTX c;
@@ -616,6 +643,9 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
643 struct cache_entry *ce = cache[i];
644 if (!ce->ce_mode)
645 continue;
646 + if (index_file_timestamp &&
647 + index_file_timestamp <= ntohl(ce->ce_mtime.sec))
648 + ce_smudge_racily_clean_entry(ce);
649 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
650 return -1;
651 }
t/t0010-racy-git.sh
+13 -4
@@ -7,18 +7,27 @@ test_description='racy GIT'
7 # This test can give false success if your machine is sufficiently
8 # slow or your trial happened to happen on second boundary.
9
10 -for trial in 0 1 2 3 4 5 6 7 8 9
10 +for trial in 0 1 2 3 4
11 do
12 rm -f .git/index
13 echo frotz >infocom
14 - echo xyzzy >activision
15 - git update-index --add infocom activision
14 + git update-index --add infocom
15 echo xyzzy >infocom
16
17 files=`git diff-files -p`
18 test_expect_success \
20 - "Racy GIT trial #$trial" \
19 + "Racy GIT trial #$trial part A" \
20 'test "" != "$files"'
21 +
22 + sleep 1
23 + echo xyzzy >cornerstone
24 + git update-index --add cornerstone
25 +
26 + files=`git diff-files -p`
27 + test_expect_success \
28 + "Racy GIT trial #$trial part B" \
29 + 'test "" != "$files"'
30 +
31 done
32
33 test_done