@matheusbsbs / matheus-bagliano / commits / 29e4d36357
Fork 0

Racy GIT

This fixes the longstanding "Racy GIT" problem, which was pretty much there from the beginning of time, but was first demonstrated by Pasky in this message on October 24, 2005: http://marc.theaimsgroup.com/?l=git&m=113014629716878 If you run the following sequence of commands: echo frotz >infocom git update-index --add infocom echo xyzzy >infocom so that the second update to file "infocom" does not change st_mtime, what is recorded as the stat information for the cache entry "infocom" exactly matches what is on the filesystem (owner, group, inum, mtime, ctime, mode, length). After this sequence, we incorrectly think "infocom" file still has string "frotz" in it, and get really confused. E.g. git-diff-files would say there is no change, git-update-index --refresh would not even look at the filesystem to correct the situation. Some ways of working around this issue were already suggested by Linus in the same thread on the same day, including waiting until the next second before returning from update-index if a cache entry written out has the current timestamp, but that means we can make at most one commit per second, and given that the e-mail patch workflow used by Linus needs to process at least 5 commits per second, it is not an acceptable solution. Linus notes that git-apply is primarily used to update the index while processing e-mailed patches, which is true, and git-apply's up-to-date check is fooled by the same problem but luckily in the other direction, so it is not really a big issue, but still it is disturbing. The function ce_match_stat() is called to bypass the comparison against filesystem data when the stat data recorded in the cache entry matches what stat() returns from the filesystem. This patch tackles the problem by changing it to actually go to the filesystem data for cache entries that have the same mtime as the index file itself. This works as long as the index file and working tree files are on the filesystems that share the same monotonic clock. Files on network mounted filesystems sometimes get skewed timestamps compared to "date" output, but as long as working tree files' timestamps are skewed the same way as the index file's, this approach still works. The only problematic files are the ones that have the same timestamp as the index file's, because two file updates that sandwitch the index file update must happen within the same second to trigger the problem. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 20, 2005 at 00:02 UTC 29e4d3635709778bcc808dbad0477efad82f8d7e
2 files changed +110 -54
read-cache.c
+86 -54
@@ -6,6 +6,7 @@
6 #include "cache.h"
7
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;
11
12 /*
@@ -28,6 +29,64 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
29 ce->ce_size = htonl(st->st_size);
30 }
31
32 +static int ce_compare_data(struct cache_entry *ce, struct stat *st)
33 +{
34 + int match = -1;
35 + int fd = open(ce->name, O_RDONLY);
36 +
37 + if (fd >= 0) {
38 + unsigned char sha1[20];
39 + if (!index_fd(sha1, fd, st, 0, NULL))
40 + match = memcmp(sha1, ce->sha1, 20);
41 + close(fd);
42 + }
43 + return match;
44 +}
45 +
46 +static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
47 +{
48 + int match = -1;
49 + char *target;
50 + void *buffer;
51 + unsigned long size;
52 + char type[10];
53 + int len;
54 +
55 + target = xmalloc(expected_size);
56 + len = readlink(ce->name, target, expected_size);
57 + if (len != expected_size) {
58 + free(target);
59 + return -1;
60 + }
61 + buffer = read_sha1_file(ce->sha1, type, &size);
62 + if (!buffer) {
63 + free(target);
64 + return -1;
65 + }
66 + if (size == expected_size)
67 + match = memcmp(buffer, target, size);
68 + free(buffer);
69 + free(target);
70 + return match;
71 +}
72 +
73 +static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
74 +{
75 + switch (st->st_mode & S_IFMT) {
76 + case S_IFREG:
77 + if (ce_compare_data(ce, st))
78 + return DATA_CHANGED;
79 + break;
80 + case S_IFLNK:
81 + if (ce_compare_link(ce, st->st_size))
82 + return DATA_CHANGED;
83 + break;
84 + default:
85 + return TYPE_CHANGED;
86 + }
87 + return 0;
88 +}
89 +
90 int ce_match_stat(struct cache_entry *ce, struct stat *st)
91 {
92 unsigned int changed = 0;
@@ -83,57 +142,37 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st)
142
143 if (ce->ce_size != htonl(st->st_size))
144 changed |= DATA_CHANGED;
86 - return changed;
87 -}
88 -
89 -static int ce_compare_data(struct cache_entry *ce, struct stat *st)
90 -{
91 - int match = -1;
92 - int fd = open(ce->name, O_RDONLY);
145
94 - if (fd >= 0) {
95 - unsigned char sha1[20];
96 - if (!index_fd(sha1, fd, st, 0, NULL))
97 - match = memcmp(sha1, ce->sha1, 20);
98 - close(fd);
99 - }
100 - return match;
101 -}
102 -
103 -static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
104 -{
105 - int match = -1;
106 - char *target;
107 - void *buffer;
108 - unsigned long size;
109 - char type[10];
110 - int len;
146 + /*
147 + * Within 1 second of this sequence:
148 + * echo xyzzy >file && git-update-index --add file
149 + * running this command:
150 + * echo frotz >file
151 + * would give a falsely clean cache entry. The mtime and
152 + * length match the cache, and other stat fields do not change.
153 + *
154 + * We could detect this at update-index time (the cache entry
155 + * being registered/updated records the same time as "now")
156 + * and delay the return from git-update-index, but that would
157 + * effectively mean we can make at most one commit per second,
158 + * which is not acceptable. Instead, we check cache entries
159 + * whose mtime are the same as the index file timestamp more
160 + * careful than others.
161 + */
162 + if (!changed &&
163 + index_file_timestamp &&
164 + index_file_timestamp <= ntohl(ce->ce_mtime.sec))
165 + changed |= ce_modified_check_fs(ce, st);
166
112 - target = xmalloc(expected_size);
113 - len = readlink(ce->name, target, expected_size);
114 - if (len != expected_size) {
115 - free(target);
116 - return -1;
117 - }
118 - buffer = read_sha1_file(ce->sha1, type, &size);
119 - if (!buffer) {
120 - free(target);
121 - return -1;
122 - }
123 - if (size == expected_size)
124 - match = memcmp(buffer, target, size);
125 - free(buffer);
126 - free(target);
127 - return match;
167 + return changed;
168 }
169
170 int ce_modified(struct cache_entry *ce, struct stat *st)
171 {
132 - int changed;
172 + int changed, changed_fs;
173 changed = ce_match_stat(ce, st);
174 if (!changed)
175 return 0;
136 -
176 /*
177 * If the mode or type has changed, there's no point in trying
178 * to refresh the entry - it's not going to match
@@ -148,18 +187,9 @@ int ce_modified(struct cache_entry *ce, struct stat *st)
187 if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
188 return changed;
189
151 - switch (st->st_mode & S_IFMT) {
152 - case S_IFREG:
153 - if (ce_compare_data(ce, st))
154 - return changed | DATA_CHANGED;
155 - break;
156 - case S_IFLNK:
157 - if (ce_compare_link(ce, st->st_size))
158 - return changed | DATA_CHANGED;
159 - break;
160 - default:
161 - return changed | TYPE_CHANGED;
162 - }
190 + changed_fs = ce_modified_check_fs(ce, st);
191 + if (changed_fs)
192 + return changed | changed_fs;
193 return 0;
194 }
195
@@ -471,6 +501,7 @@ int read_cache(void)
501 return active_nr;
502
503 errno = ENOENT;
504 + index_file_timestamp = 0;
505 fd = open(get_index_file(), O_RDONLY);
506 if (fd < 0) {
507 if (errno == ENOENT)
@@ -504,6 +535,7 @@ int read_cache(void)
535 offset = offset + ce_size(ce);
536 active_cache[i] = ce;
537 }
538 + index_file_timestamp = st.st_mtime;
539 return active_nr;
540
541 unmap:
t/t0010-racy-git.sh new
+24
@@ -0,0 +1,24 @@
1 +#!/bin/sh
2 +
3 +test_description='racy GIT'
4 +
5 +. ./test-lib.sh
6 +
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
11 +do
12 + rm -f .git/index
13 + echo frotz >infocom
14 + echo xyzzy >activision
15 + git update-index --add infocom activision
16 + echo xyzzy >infocom
17 +
18 + files=`git diff-files -p`
19 + test_expect_success \
20 + "Racy GIT trial #$trial" \
21 + 'test "" != "$files"'
22 +done
23 +
24 +test_done