]> git.proxmox.com Git - libgit2.git/blob - tests/index/racy.c
Merge pull request #3265 from libgit2/leaks
[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
7 static git_repository *g_repo;
8
9 void test_index_racy__initialize(void)
10 {
11 cl_git_pass(git_repository_init(&g_repo, "diff_racy", false));
12 }
13
14 void test_index_racy__cleanup(void)
15 {
16 git_repository_free(g_repo);
17 g_repo = NULL;
18
19 cl_fixture_cleanup("diff_racy");
20 }
21
22 void test_index_racy__diff(void)
23 {
24 git_index *index;
25 git_diff *diff;
26 git_buf path = GIT_BUF_INIT;
27
28 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
29 cl_git_mkfile(path.ptr, "A");
30
31 /* Put 'A' into the index */
32 cl_git_pass(git_repository_index(&index, g_repo));
33 cl_git_pass(git_index_add_bypath(index, "A"));
34 cl_git_pass(git_index_write(index));
35
36 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
37 cl_assert_equal_i(0, git_diff_num_deltas(diff));
38 git_diff_free(diff);
39
40 /* Change its contents quickly, so we get the same timestamp */
41 cl_git_mkfile(path.ptr, "B");
42
43 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
44 cl_assert_equal_i(1, git_diff_num_deltas(diff));
45
46 git_index_free(index);
47 git_diff_free(diff);
48 git_buf_free(&path);
49 }
50
51 void test_index_racy__write_index_just_after_file(void)
52 {
53 git_index *index;
54 git_diff *diff;
55 git_buf path = GIT_BUF_INIT;
56 struct timeval times[2];
57
58 /* Make sure we do have a timestamp */
59 cl_git_pass(git_repository_index(&index, g_repo));
60 cl_git_pass(git_index_write(index));
61
62 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
63 cl_git_mkfile(path.ptr, "A");
64 /* Force the file's timestamp to be a second after we wrote the index */
65 times[0].tv_sec = index->stamp.mtime + 1;
66 times[0].tv_usec = 0;
67 times[1].tv_sec = index->stamp.mtime + 1;
68 times[1].tv_usec = 0;
69 cl_git_pass(p_utimes(path.ptr, times));
70
71 /*
72 * Put 'A' into the index, the size field will be filled,
73 * because the index' on-disk timestamp does not match the
74 * file's timestamp.
75 */
76 cl_git_pass(git_index_add_bypath(index, "A"));
77 cl_git_pass(git_index_write(index));
78
79 cl_git_mkfile(path.ptr, "B");
80 /*
81 * Pretend this index' modification happend a second after the
82 * file update, and rewrite the file in that same second.
83 */
84 times[0].tv_sec = index->stamp.mtime + 2;
85 times[0].tv_usec = 0;
86 times[1].tv_sec = index->stamp.mtime + 2;
87 times[0].tv_usec = 0;
88
89 cl_git_pass(p_utimes(git_index_path(index), times));
90 cl_git_pass(p_utimes(path.ptr, times));
91
92 cl_git_pass(git_index_read(index, true));
93
94 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
95 cl_assert_equal_i(1, git_diff_num_deltas(diff));
96
97 git_buf_free(&path);
98 git_diff_free(diff);
99 git_index_free(index);
100 }
101
102 void test_index_racy__empty_file_after_smudge(void)
103 {
104 git_index *index;
105 git_diff *diff;
106 git_buf path = GIT_BUF_INIT;
107 int i, found_race = 0;
108 const git_index_entry *entry;
109
110 /* Make sure we do have a timestamp */
111 cl_git_pass(git_repository_index__weakptr(&index, g_repo));
112 cl_git_pass(git_index_write(index));
113
114 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
115
116 /* Make sure writing the file, adding and rewriting happen in the same second */
117 for (i = 0; i < 10; i++) {
118 struct stat st;
119 cl_git_mkfile(path.ptr, "A");
120
121 cl_git_pass(git_index_add_bypath(index, "A"));
122 cl_git_mkfile(path.ptr, "B");
123 cl_git_pass(git_index_write(index));
124
125 cl_git_mkfile(path.ptr, "");
126
127 cl_git_pass(p_stat(path.ptr, &st));
128 cl_assert(entry = git_index_get_bypath(index, "A", 0));
129 if (entry->mtime.seconds == (int32_t) st.st_mtime) {
130 found_race = 1;
131 break;
132 }
133
134 }
135
136 if (!found_race)
137 cl_fail("failed to find race after 10 attempts");
138
139 cl_assert_equal_i(0, entry->file_size);
140
141 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
142 cl_assert_equal_i(1, git_diff_num_deltas(diff));
143
144 git_buf_free(&path);
145 git_diff_free(diff);
146 }