]> git.proxmox.com Git - libgit2.git/blob - tests/index/racy.c
Merge pull request #3525 from pks-t/pks/fix-nested-struct-warning
[libgit2.git] / tests / index / racy.c
1 #include "clar_libgit2.h"
2 #include "../checkout/checkout_helpers.h"
3
4 #include "buffer.h"
5 #include "index.h"
6 #include "repository.h"
7
8 static git_repository *g_repo;
9
10 void test_index_racy__initialize(void)
11 {
12 cl_git_pass(git_repository_init(&g_repo, "diff_racy", false));
13 }
14
15 void test_index_racy__cleanup(void)
16 {
17 git_repository_free(g_repo);
18 g_repo = NULL;
19
20 cl_fixture_cleanup("diff_racy");
21 }
22
23 void test_index_racy__diff(void)
24 {
25 git_index *index;
26 git_diff *diff;
27 git_buf path = GIT_BUF_INIT;
28
29 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
30 cl_git_mkfile(path.ptr, "A");
31
32 /* Put 'A' into the index */
33 cl_git_pass(git_repository_index(&index, g_repo));
34 cl_git_pass(git_index_add_bypath(index, "A"));
35 cl_git_pass(git_index_write(index));
36
37 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
38 cl_assert_equal_i(0, git_diff_num_deltas(diff));
39 git_diff_free(diff);
40
41 /* Change its contents quickly, so we get the same timestamp */
42 cl_git_mkfile(path.ptr, "B");
43
44 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
45 cl_assert_equal_i(1, git_diff_num_deltas(diff));
46
47 git_index_free(index);
48 git_diff_free(diff);
49 git_buf_free(&path);
50 }
51
52 void test_index_racy__write_index_just_after_file(void)
53 {
54 git_index *index;
55 git_diff *diff;
56 git_buf path = GIT_BUF_INIT;
57 struct timeval times[2];
58
59 /* Make sure we do have a timestamp */
60 cl_git_pass(git_repository_index(&index, g_repo));
61 cl_git_pass(git_index_write(index));
62
63 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
64 cl_git_mkfile(path.ptr, "A");
65 /* Force the file's timestamp to be a second after we wrote the index */
66 times[0].tv_sec = index->stamp.mtime.tv_sec + 1;
67 times[0].tv_usec = index->stamp.mtime.tv_nsec / 1000;
68 times[1].tv_sec = index->stamp.mtime.tv_sec + 1;
69 times[1].tv_usec = index->stamp.mtime.tv_nsec / 1000;
70 cl_git_pass(p_utimes(path.ptr, times));
71
72 /*
73 * Put 'A' into the index, the size field will be filled,
74 * because the index' on-disk timestamp does not match the
75 * file's timestamp.
76 */
77 cl_git_pass(git_index_add_bypath(index, "A"));
78 cl_git_pass(git_index_write(index));
79
80 cl_git_mkfile(path.ptr, "B");
81 /*
82 * Pretend this index' modification happend a second after the
83 * file update, and rewrite the file in that same second.
84 */
85 times[0].tv_sec = index->stamp.mtime.tv_sec + 2;
86 times[0].tv_usec = index->stamp.mtime.tv_nsec / 1000;
87 times[1].tv_sec = index->stamp.mtime.tv_sec + 2;
88 times[0].tv_usec = index->stamp.mtime.tv_nsec / 1000;
89
90 cl_git_pass(p_utimes(git_index_path(index), times));
91 cl_git_pass(p_utimes(path.ptr, times));
92
93 cl_git_pass(git_index_read(index, true));
94
95 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
96 cl_assert_equal_i(1, git_diff_num_deltas(diff));
97
98 git_buf_free(&path);
99 git_diff_free(diff);
100 git_index_free(index);
101 }
102
103
104 static void setup_race(void)
105 {
106 git_buf path = GIT_BUF_INIT;
107 git_index *index;
108 const git_index_entry *entry;
109 int i, found_race = 0;
110
111 /* Make sure we do have a timestamp */
112 cl_git_pass(git_repository_index__weakptr(&index, g_repo));
113 cl_git_pass(git_index_write(index));
114
115 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
116
117 /* Make sure writing the file, adding and rewriting happen in the same second */
118 for (i = 0; i < 10; i++) {
119 struct stat st;
120 cl_git_mkfile(path.ptr, "A");
121
122 cl_git_pass(git_index_add_bypath(index, "A"));
123 cl_git_mkfile(path.ptr, "B");
124 cl_git_pass(git_index_write(index));
125
126 cl_git_mkfile(path.ptr, "");
127
128 cl_git_pass(p_stat(path.ptr, &st));
129 cl_assert(entry = git_index_get_bypath(index, "A", 0));
130 if (entry->mtime.seconds == (int32_t) st.st_mtime) {
131 found_race = 1;
132 break;
133 }
134 }
135
136 if (!found_race)
137 cl_fail("failed to find race after 10 attempts");
138
139 git_buf_free(&path);
140 }
141
142 void test_index_racy__smudges_index_entry_on_save(void)
143 {
144 git_index *index;
145 const git_index_entry *entry;
146
147 setup_race();
148
149 /* write the index, which will smudge anything that had the same timestamp
150 * as the index when the index was loaded. that way future loads of the
151 * index (with the new timestamp) will know that these files were not
152 * clean.
153 */
154
155 cl_git_pass(git_repository_index__weakptr(&index, g_repo));
156 cl_git_pass(git_index_write(index));
157
158 cl_assert(entry = git_index_get_bypath(index, "A", 0));
159 cl_assert_equal_i(0, entry->file_size);
160 }
161
162 void test_index_racy__detects_diff_of_change_in_identical_timestamp(void)
163 {
164 git_index *index;
165 git_diff *diff;
166
167 cl_git_pass(git_repository_index__weakptr(&index, g_repo));
168
169 setup_race();
170
171 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
172 cl_assert_equal_i(1, git_diff_num_deltas(diff));
173
174 git_diff_free(diff);
175 }
176
177 static void setup_uptodate_files(void)
178 {
179 git_buf path = GIT_BUF_INIT;
180 git_index *index;
181 git_index_entry new_entry = {{0}};
182
183 cl_git_pass(git_repository_index(&index, g_repo));
184
185 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
186 cl_git_mkfile(path.ptr, "A");
187
188 /* Put 'A' into the index */
189 cl_git_pass(git_index_add_bypath(index, "A"));
190
191 /* Put 'B' into the index */
192 new_entry.path = "B";
193 new_entry.mode = GIT_FILEMODE_BLOB;
194 cl_git_pass(git_index_add(index, &new_entry));
195
196 /* Put 'C' into the index */
197 new_entry.path = "C";
198 new_entry.mode = GIT_FILEMODE_BLOB;
199 cl_git_pass(git_index_add_frombuffer(index, &new_entry, "hello!\n", 7));
200
201 git_index_free(index);
202 git_buf_free(&path);
203 }
204
205 void test_index_racy__adding_to_index_is_uptodate(void)
206 {
207 git_index *index;
208 const git_index_entry *entry;
209
210 setup_uptodate_files();
211
212 cl_git_pass(git_repository_index(&index, g_repo));
213
214 /* ensure that they're all uptodate */
215 cl_assert((entry = git_index_get_bypath(index, "A", 0)));
216 cl_assert_equal_i(GIT_IDXENTRY_UPTODATE, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
217
218 cl_assert((entry = git_index_get_bypath(index, "B", 0)));
219 cl_assert_equal_i(GIT_IDXENTRY_UPTODATE, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
220
221 cl_assert((entry = git_index_get_bypath(index, "C", 0)));
222 cl_assert_equal_i(GIT_IDXENTRY_UPTODATE, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
223
224 cl_git_pass(git_index_write(index));
225
226 git_index_free(index);
227 }
228
229 void test_index_racy__reading_clears_uptodate_bit(void)
230 {
231 git_index *index;
232 const git_index_entry *entry;
233
234 setup_uptodate_files();
235
236 cl_git_pass(git_repository_index(&index, g_repo));
237 cl_git_pass(git_index_write(index));
238
239 cl_git_pass(git_index_read(index, true));
240
241 /* ensure that no files are uptodate */
242 cl_assert((entry = git_index_get_bypath(index, "A", 0)));
243 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
244
245 cl_assert((entry = git_index_get_bypath(index, "B", 0)));
246 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
247
248 cl_assert((entry = git_index_get_bypath(index, "C", 0)));
249 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
250
251 git_index_free(index);
252 }
253
254 void test_index_racy__read_tree_clears_uptodate_bit(void)
255 {
256 git_index *index;
257 git_tree *tree;
258 const git_index_entry *entry;
259 git_oid id;
260
261 setup_uptodate_files();
262
263 cl_git_pass(git_repository_index(&index, g_repo));
264 cl_git_pass(git_index_write_tree_to(&id, index, g_repo));
265 cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
266 cl_git_pass(git_index_read_tree(index, tree));
267
268 /* ensure that no files are uptodate */
269 cl_assert((entry = git_index_get_bypath(index, "A", 0)));
270 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
271
272 cl_assert((entry = git_index_get_bypath(index, "B", 0)));
273 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
274
275 cl_assert((entry = git_index_get_bypath(index, "C", 0)));
276 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
277
278 git_tree_free(tree);
279 git_index_free(index);
280 }
281
282 void test_index_racy__read_index_smudges(void)
283 {
284 git_index *index, *newindex;
285 const git_index_entry *entry;
286
287 /* if we are reading an index into our new index, ensure that any
288 * racy entries in the index that we're reading are smudged so that
289 * we don't propagate their timestamps without further investigation.
290 */
291 setup_race();
292
293 cl_git_pass(git_repository_index(&index, g_repo));
294 cl_git_pass(git_index_new(&newindex));
295 cl_git_pass(git_index_read_index(newindex, index));
296
297 cl_assert(entry = git_index_get_bypath(newindex, "A", 0));
298 cl_assert_equal_i(0, entry->file_size);
299
300 git_index_free(index);
301 git_index_free(newindex);
302 }
303
304 void test_index_racy__read_index_clears_uptodate_bit(void)
305 {
306 git_index *index, *newindex;
307 const git_index_entry *entry;
308
309 setup_uptodate_files();
310
311 cl_git_pass(git_repository_index(&index, g_repo));
312 cl_git_pass(git_index_new(&newindex));
313 cl_git_pass(git_index_read_index(newindex, index));
314
315 /* ensure that files brought in from the other index are not uptodate */
316 cl_assert((entry = git_index_get_bypath(newindex, "A", 0)));
317 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
318
319 cl_assert((entry = git_index_get_bypath(newindex, "B", 0)));
320 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
321
322 cl_assert((entry = git_index_get_bypath(newindex, "C", 0)));
323 cl_assert_equal_i(0, (entry->flags_extended & GIT_IDXENTRY_UPTODATE));
324
325 git_index_free(index);
326 git_index_free(newindex);
327 }