]>
Commit | Line | Data |
---|---|---|
883e248b BP |
1 | #include "cache.h" |
2 | #include "config.h" | |
3 | #include "dir.h" | |
4 | #include "ewah/ewok.h" | |
5 | #include "fsmonitor.h" | |
6 | #include "run-command.h" | |
7 | #include "strbuf.h" | |
8 | ||
56c69100 KW |
9 | #define INDEX_EXTENSION_VERSION1 (1) |
10 | #define INDEX_EXTENSION_VERSION2 (2) | |
11 | #define HOOK_INTERFACE_VERSION (1) | |
883e248b BP |
12 | |
13 | struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR); | |
14 | ||
15 | static void fsmonitor_ewah_callback(size_t pos, void *is) | |
16 | { | |
17 | struct index_state *istate = (struct index_state *)is; | |
3444ec2e | 18 | struct cache_entry *ce; |
883e248b | 19 | |
3444ec2e WB |
20 | if (pos >= istate->cache_nr) |
21 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" >= %u)", | |
22 | (uintmax_t)pos, istate->cache_nr); | |
23 | ||
24 | ce = istate->cache[pos]; | |
883e248b BP |
25 | ce->ce_flags &= ~CE_FSMONITOR_VALID; |
26 | } | |
27 | ||
28 | int read_fsmonitor_extension(struct index_state *istate, const void *data, | |
29 | unsigned long sz) | |
30 | { | |
31 | const char *index = data; | |
32 | uint32_t hdr_version; | |
33 | uint32_t ewah_size; | |
34 | struct ewah_bitmap *fsmonitor_dirty; | |
883e248b | 35 | int ret; |
56c69100 KW |
36 | uint64_t timestamp; |
37 | struct strbuf last_update = STRBUF_INIT; | |
883e248b | 38 | |
56c69100 | 39 | if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t)) |
883e248b BP |
40 | return error("corrupt fsmonitor extension (too short)"); |
41 | ||
42 | hdr_version = get_be32(index); | |
43 | index += sizeof(uint32_t); | |
56c69100 KW |
44 | if (hdr_version == INDEX_EXTENSION_VERSION1) { |
45 | timestamp = get_be64(index); | |
46 | strbuf_addf(&last_update, "%"PRIu64"", timestamp); | |
47 | index += sizeof(uint64_t); | |
48 | } else if (hdr_version == INDEX_EXTENSION_VERSION2) { | |
49 | strbuf_addstr(&last_update, index); | |
50 | index += last_update.len + 1; | |
51 | } else { | |
883e248b | 52 | return error("bad fsmonitor version %d", hdr_version); |
56c69100 | 53 | } |
883e248b | 54 | |
56c69100 | 55 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); |
883e248b BP |
56 | |
57 | ewah_size = get_be32(index); | |
58 | index += sizeof(uint32_t); | |
59 | ||
60 | fsmonitor_dirty = ewah_new(); | |
61 | ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size); | |
62 | if (ret != ewah_size) { | |
63 | ewah_free(fsmonitor_dirty); | |
64 | return error("failed to parse ewah bitmap reading fsmonitor index extension"); | |
65 | } | |
ba1b9cac | 66 | istate->fsmonitor_dirty = fsmonitor_dirty; |
883e248b | 67 | |
61eea521 JH |
68 | if (!istate->split_index && |
69 | istate->fsmonitor_dirty->bit_size > istate->cache_nr) | |
3444ec2e WB |
70 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", |
71 | (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr); | |
72 | ||
883e248b BP |
73 | trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful"); |
74 | return 0; | |
75 | } | |
76 | ||
3bd28eb2 AV |
77 | void fill_fsmonitor_bitmap(struct index_state *istate) |
78 | { | |
3444ec2e | 79 | unsigned int i, skipped = 0; |
3bd28eb2 | 80 | istate->fsmonitor_dirty = ewah_new(); |
3444ec2e WB |
81 | for (i = 0; i < istate->cache_nr; i++) { |
82 | if (istate->cache[i]->ce_flags & CE_REMOVE) | |
83 | skipped++; | |
84 | else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID)) | |
85 | ewah_set(istate->fsmonitor_dirty, i - skipped); | |
86 | } | |
3bd28eb2 AV |
87 | } |
88 | ||
883e248b BP |
89 | void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate) |
90 | { | |
91 | uint32_t hdr_version; | |
883e248b BP |
92 | uint32_t ewah_start; |
93 | uint32_t ewah_size = 0; | |
94 | int fixup = 0; | |
95 | ||
61eea521 JH |
96 | if (!istate->split_index && |
97 | istate->fsmonitor_dirty->bit_size > istate->cache_nr) | |
3444ec2e WB |
98 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", |
99 | (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr); | |
100 | ||
56c69100 | 101 | put_be32(&hdr_version, INDEX_EXTENSION_VERSION2); |
883e248b BP |
102 | strbuf_add(sb, &hdr_version, sizeof(uint32_t)); |
103 | ||
56c69100 KW |
104 | strbuf_addstr(sb, istate->fsmonitor_last_update); |
105 | strbuf_addch(sb, 0); /* Want to keep a NUL */ | |
106 | ||
883e248b BP |
107 | fixup = sb->len; |
108 | strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */ | |
109 | ||
110 | ewah_start = sb->len; | |
3bd28eb2 AV |
111 | ewah_serialize_strbuf(istate->fsmonitor_dirty, sb); |
112 | ewah_free(istate->fsmonitor_dirty); | |
113 | istate->fsmonitor_dirty = NULL; | |
883e248b BP |
114 | |
115 | /* fix up size field */ | |
116 | put_be32(&ewah_size, sb->len - ewah_start); | |
117 | memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t)); | |
118 | ||
119 | trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful"); | |
120 | } | |
121 | ||
122 | /* | |
56c69100 | 123 | * Call the query-fsmonitor hook passing the last update token of the saved results. |
883e248b | 124 | */ |
56c69100 | 125 | static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result) |
883e248b BP |
126 | { |
127 | struct child_process cp = CHILD_PROCESS_INIT; | |
883e248b | 128 | |
735e4173 | 129 | if (!core_fsmonitor) |
883e248b BP |
130 | return -1; |
131 | ||
735e4173 RS |
132 | argv_array_push(&cp.args, core_fsmonitor); |
133 | argv_array_pushf(&cp.args, "%d", version); | |
56c69100 | 134 | argv_array_pushf(&cp.args, "%s", last_update); |
883e248b | 135 | cp.use_shell = 1; |
11cf33be | 136 | cp.dir = get_git_work_tree(); |
883e248b BP |
137 | |
138 | return capture_command(&cp, query_result, 1024); | |
139 | } | |
140 | ||
141 | static void fsmonitor_refresh_callback(struct index_state *istate, const char *name) | |
142 | { | |
143 | int pos = index_name_pos(istate, name, strlen(name)); | |
144 | ||
145 | if (pos >= 0) { | |
146 | struct cache_entry *ce = istate->cache[pos]; | |
147 | ce->ce_flags &= ~CE_FSMONITOR_VALID; | |
148 | } | |
149 | ||
150 | /* | |
151 | * Mark the untracked cache dirty even if it wasn't found in the index | |
152 | * as it could be a new untracked file. | |
153 | */ | |
154 | trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name); | |
0cacebf0 | 155 | untracked_cache_invalidate_path(istate, name, 0); |
883e248b BP |
156 | } |
157 | ||
158 | void refresh_fsmonitor(struct index_state *istate) | |
159 | { | |
883e248b BP |
160 | struct strbuf query_result = STRBUF_INIT; |
161 | int query_success = 0; | |
162 | size_t bol; /* beginning of line */ | |
163 | uint64_t last_update; | |
56c69100 | 164 | struct strbuf last_update_token = STRBUF_INIT; |
883e248b | 165 | char *buf; |
5d137fc2 | 166 | unsigned int i; |
883e248b | 167 | |
398a3b08 | 168 | if (!core_fsmonitor || istate->fsmonitor_has_run_once) |
883e248b | 169 | return; |
398a3b08 | 170 | istate->fsmonitor_has_run_once = 1; |
883e248b BP |
171 | |
172 | trace_printf_key(&trace_fsmonitor, "refresh fsmonitor"); | |
173 | /* | |
174 | * This could be racy so save the date/time now and query_fsmonitor | |
175 | * should be inclusive to ensure we don't miss potential changes. | |
176 | */ | |
177 | last_update = getnanotime(); | |
56c69100 | 178 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); |
883e248b BP |
179 | |
180 | /* | |
181 | * If we have a last update time, call query_fsmonitor for the set of | |
182 | * changes since that time, else assume everything is possibly dirty | |
183 | * and check it all. | |
184 | */ | |
185 | if (istate->fsmonitor_last_update) { | |
186 | query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION, | |
187 | istate->fsmonitor_last_update, &query_result); | |
188 | trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor); | |
189 | trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s", | |
190 | core_fsmonitor, query_success ? "success" : "failure"); | |
191 | } | |
192 | ||
193 | /* a fsmonitor process can return '/' to indicate all entries are invalid */ | |
194 | if (query_success && query_result.buf[0] != '/') { | |
195 | /* Mark all entries returned by the monitor as dirty */ | |
196 | buf = query_result.buf; | |
197 | bol = 0; | |
198 | for (i = 0; i < query_result.len; i++) { | |
199 | if (buf[i] != '\0') | |
200 | continue; | |
201 | fsmonitor_refresh_callback(istate, buf + bol); | |
202 | bol = i + 1; | |
203 | } | |
204 | if (bol < query_result.len) | |
205 | fsmonitor_refresh_callback(istate, buf + bol); | |
679f2f9f US |
206 | |
207 | /* Now mark the untracked cache for fsmonitor usage */ | |
208 | if (istate->untracked) | |
209 | istate->untracked->use_fsmonitor = 1; | |
883e248b | 210 | } else { |
679f2f9f US |
211 | |
212 | /* We only want to run the post index changed hook if we've actually changed entries, so keep track | |
213 | * if we actually changed entries or not */ | |
214 | int is_cache_changed = 0; | |
883e248b | 215 | /* Mark all entries invalid */ |
679f2f9f US |
216 | for (i = 0; i < istate->cache_nr; i++) { |
217 | if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) { | |
218 | is_cache_changed = 1; | |
219 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
220 | } | |
221 | } | |
883e248b | 222 | |
ca598d5f | 223 | /* If we're going to check every file, ensure we save the results */ |
679f2f9f US |
224 | if (is_cache_changed) |
225 | istate->cache_changed |= FSMONITOR_CHANGED; | |
ca598d5f | 226 | |
883e248b BP |
227 | if (istate->untracked) |
228 | istate->untracked->use_fsmonitor = 0; | |
229 | } | |
230 | strbuf_release(&query_result); | |
231 | ||
56c69100 KW |
232 | /* Now that we've updated istate, save the last_update_token */ |
233 | FREE_AND_NULL(istate->fsmonitor_last_update); | |
234 | istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL); | |
883e248b BP |
235 | } |
236 | ||
237 | void add_fsmonitor(struct index_state *istate) | |
238 | { | |
5d137fc2 | 239 | unsigned int i; |
56c69100 | 240 | struct strbuf last_update = STRBUF_INIT; |
883e248b BP |
241 | |
242 | if (!istate->fsmonitor_last_update) { | |
243 | trace_printf_key(&trace_fsmonitor, "add fsmonitor"); | |
244 | istate->cache_changed |= FSMONITOR_CHANGED; | |
56c69100 KW |
245 | strbuf_addf(&last_update, "%"PRIu64"", getnanotime()); |
246 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); | |
883e248b BP |
247 | |
248 | /* reset the fsmonitor state */ | |
249 | for (i = 0; i < istate->cache_nr; i++) | |
250 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
251 | ||
252 | /* reset the untracked cache */ | |
253 | if (istate->untracked) { | |
254 | add_untracked_cache(istate); | |
255 | istate->untracked->use_fsmonitor = 1; | |
256 | } | |
257 | ||
258 | /* Update the fsmonitor state */ | |
259 | refresh_fsmonitor(istate); | |
260 | } | |
261 | } | |
262 | ||
263 | void remove_fsmonitor(struct index_state *istate) | |
264 | { | |
265 | if (istate->fsmonitor_last_update) { | |
266 | trace_printf_key(&trace_fsmonitor, "remove fsmonitor"); | |
267 | istate->cache_changed |= FSMONITOR_CHANGED; | |
56c69100 | 268 | FREE_AND_NULL(istate->fsmonitor_last_update); |
883e248b BP |
269 | } |
270 | } | |
271 | ||
272 | void tweak_fsmonitor(struct index_state *istate) | |
273 | { | |
5d137fc2 | 274 | unsigned int i; |
ba1b9cac AV |
275 | int fsmonitor_enabled = git_config_get_fsmonitor(); |
276 | ||
277 | if (istate->fsmonitor_dirty) { | |
278 | if (fsmonitor_enabled) { | |
279 | /* Mark all entries valid */ | |
280 | for (i = 0; i < istate->cache_nr; i++) { | |
281 | istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID; | |
282 | } | |
283 | ||
284 | /* Mark all previously saved entries as dirty */ | |
3444ec2e WB |
285 | if (istate->fsmonitor_dirty->bit_size > istate->cache_nr) |
286 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", | |
287 | (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr); | |
ba1b9cac AV |
288 | ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate); |
289 | ||
679f2f9f | 290 | refresh_fsmonitor(istate); |
ba1b9cac AV |
291 | } |
292 | ||
293 | ewah_free(istate->fsmonitor_dirty); | |
294 | istate->fsmonitor_dirty = NULL; | |
295 | } | |
296 | ||
297 | switch (fsmonitor_enabled) { | |
883e248b BP |
298 | case -1: /* keep: do nothing */ |
299 | break; | |
300 | case 0: /* false */ | |
301 | remove_fsmonitor(istate); | |
302 | break; | |
303 | case 1: /* true */ | |
304 | add_fsmonitor(istate); | |
305 | break; | |
306 | default: /* unknown value: do nothing */ | |
307 | break; | |
308 | } | |
309 | } |