]>
Commit | Line | Data |
---|---|---|
bc5c5ec0 | 1 | #include "git-compat-util.h" |
883e248b BP |
2 | #include "config.h" |
3 | #include "dir.h" | |
32a8f510 | 4 | #include "environment.h" |
883e248b BP |
5 | #include "ewah/ewok.h" |
6 | #include "fsmonitor.h" | |
1e0ea5c4 | 7 | #include "fsmonitor-ipc.h" |
883e248b BP |
8 | #include "run-command.h" |
9 | #include "strbuf.h" | |
74ea5c95 | 10 | #include "trace2.h" |
883e248b | 11 | |
56c69100 KW |
12 | #define INDEX_EXTENSION_VERSION1 (1) |
13 | #define INDEX_EXTENSION_VERSION2 (2) | |
8da2c576 KW |
14 | #define HOOK_INTERFACE_VERSION1 (1) |
15 | #define HOOK_INTERFACE_VERSION2 (2) | |
883e248b BP |
16 | |
17 | struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR); | |
18 | ||
cae70acf DS |
19 | static void assert_index_minimum(struct index_state *istate, size_t pos) |
20 | { | |
21 | if (pos > istate->cache_nr) | |
22 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", | |
23 | (uintmax_t)pos, istate->cache_nr); | |
24 | } | |
25 | ||
883e248b BP |
26 | static void fsmonitor_ewah_callback(size_t pos, void *is) |
27 | { | |
28 | struct index_state *istate = (struct index_state *)is; | |
3444ec2e | 29 | struct cache_entry *ce; |
883e248b | 30 | |
cae70acf | 31 | assert_index_minimum(istate, pos + 1); |
3444ec2e WB |
32 | |
33 | ce = istate->cache[pos]; | |
883e248b BP |
34 | ce->ce_flags &= ~CE_FSMONITOR_VALID; |
35 | } | |
36 | ||
8da2c576 KW |
37 | static int fsmonitor_hook_version(void) |
38 | { | |
39 | int hook_version; | |
40 | ||
41 | if (git_config_get_int("core.fsmonitorhookversion", &hook_version)) | |
42 | return -1; | |
43 | ||
44 | if (hook_version == HOOK_INTERFACE_VERSION1 || | |
45 | hook_version == HOOK_INTERFACE_VERSION2) | |
46 | return hook_version; | |
47 | ||
48 | warning("Invalid hook version '%i' in core.fsmonitorhookversion. " | |
49 | "Must be 1 or 2.", hook_version); | |
50 | return -1; | |
51 | } | |
52 | ||
883e248b BP |
53 | int read_fsmonitor_extension(struct index_state *istate, const void *data, |
54 | unsigned long sz) | |
55 | { | |
56 | const char *index = data; | |
57 | uint32_t hdr_version; | |
58 | uint32_t ewah_size; | |
59 | struct ewah_bitmap *fsmonitor_dirty; | |
883e248b | 60 | int ret; |
56c69100 KW |
61 | uint64_t timestamp; |
62 | struct strbuf last_update = STRBUF_INIT; | |
883e248b | 63 | |
56c69100 | 64 | if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t)) |
883e248b BP |
65 | return error("corrupt fsmonitor extension (too short)"); |
66 | ||
67 | hdr_version = get_be32(index); | |
68 | index += sizeof(uint32_t); | |
56c69100 KW |
69 | if (hdr_version == INDEX_EXTENSION_VERSION1) { |
70 | timestamp = get_be64(index); | |
71 | strbuf_addf(&last_update, "%"PRIu64"", timestamp); | |
72 | index += sizeof(uint64_t); | |
73 | } else if (hdr_version == INDEX_EXTENSION_VERSION2) { | |
74 | strbuf_addstr(&last_update, index); | |
75 | index += last_update.len + 1; | |
76 | } else { | |
883e248b | 77 | return error("bad fsmonitor version %d", hdr_version); |
56c69100 | 78 | } |
883e248b | 79 | |
56c69100 | 80 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); |
883e248b BP |
81 | |
82 | ewah_size = get_be32(index); | |
83 | index += sizeof(uint32_t); | |
84 | ||
85 | fsmonitor_dirty = ewah_new(); | |
86 | ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size); | |
87 | if (ret != ewah_size) { | |
88 | ewah_free(fsmonitor_dirty); | |
89 | return error("failed to parse ewah bitmap reading fsmonitor index extension"); | |
90 | } | |
ba1b9cac | 91 | istate->fsmonitor_dirty = fsmonitor_dirty; |
883e248b | 92 | |
cae70acf DS |
93 | if (!istate->split_index) |
94 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); | |
3444ec2e | 95 | |
29fbbf43 JH |
96 | trace2_data_string("index", NULL, "extension/fsmn/read/token", |
97 | istate->fsmonitor_last_update); | |
98 | trace_printf_key(&trace_fsmonitor, | |
99 | "read fsmonitor extension successful '%s'", | |
100 | istate->fsmonitor_last_update); | |
883e248b BP |
101 | return 0; |
102 | } | |
103 | ||
3bd28eb2 AV |
104 | void fill_fsmonitor_bitmap(struct index_state *istate) |
105 | { | |
3444ec2e | 106 | unsigned int i, skipped = 0; |
3bd28eb2 | 107 | istate->fsmonitor_dirty = ewah_new(); |
3444ec2e WB |
108 | for (i = 0; i < istate->cache_nr; i++) { |
109 | if (istate->cache[i]->ce_flags & CE_REMOVE) | |
110 | skipped++; | |
111 | else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID)) | |
112 | ewah_set(istate->fsmonitor_dirty, i - skipped); | |
113 | } | |
3bd28eb2 AV |
114 | } |
115 | ||
883e248b BP |
116 | void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate) |
117 | { | |
118 | uint32_t hdr_version; | |
883e248b BP |
119 | uint32_t ewah_start; |
120 | uint32_t ewah_size = 0; | |
121 | int fixup = 0; | |
122 | ||
cae70acf DS |
123 | if (!istate->split_index) |
124 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); | |
3444ec2e | 125 | |
56c69100 | 126 | put_be32(&hdr_version, INDEX_EXTENSION_VERSION2); |
883e248b BP |
127 | strbuf_add(sb, &hdr_version, sizeof(uint32_t)); |
128 | ||
56c69100 KW |
129 | strbuf_addstr(sb, istate->fsmonitor_last_update); |
130 | strbuf_addch(sb, 0); /* Want to keep a NUL */ | |
131 | ||
883e248b BP |
132 | fixup = sb->len; |
133 | strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */ | |
134 | ||
135 | ewah_start = sb->len; | |
3bd28eb2 AV |
136 | ewah_serialize_strbuf(istate->fsmonitor_dirty, sb); |
137 | ewah_free(istate->fsmonitor_dirty); | |
138 | istate->fsmonitor_dirty = NULL; | |
883e248b BP |
139 | |
140 | /* fix up size field */ | |
141 | put_be32(&ewah_size, sb->len - ewah_start); | |
142 | memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t)); | |
143 | ||
29fbbf43 JH |
144 | trace2_data_string("index", NULL, "extension/fsmn/write/token", |
145 | istate->fsmonitor_last_update); | |
146 | trace_printf_key(&trace_fsmonitor, | |
147 | "write fsmonitor extension successful '%s'", | |
148 | istate->fsmonitor_last_update); | |
883e248b BP |
149 | } |
150 | ||
151 | /* | |
56c69100 | 152 | * Call the query-fsmonitor hook passing the last update token of the saved results. |
883e248b | 153 | */ |
1e0ea5c4 JH |
154 | static int query_fsmonitor_hook(struct repository *r, |
155 | int version, | |
156 | const char *last_update, | |
157 | struct strbuf *query_result) | |
883e248b BP |
158 | { |
159 | struct child_process cp = CHILD_PROCESS_INIT; | |
940b94f3 | 160 | int result; |
883e248b | 161 | |
1e0ea5c4 | 162 | if (fsm_settings__get_mode(r) != FSMONITOR_MODE_HOOK) |
883e248b BP |
163 | return -1; |
164 | ||
1e0ea5c4 | 165 | strvec_push(&cp.args, fsm_settings__get_hook_path(r)); |
ef8d7ac4 JK |
166 | strvec_pushf(&cp.args, "%d", version); |
167 | strvec_pushf(&cp.args, "%s", last_update); | |
883e248b | 168 | cp.use_shell = 1; |
11cf33be | 169 | cp.dir = get_git_work_tree(); |
883e248b | 170 | |
940b94f3 JH |
171 | trace2_region_enter("fsm_hook", "query", NULL); |
172 | ||
173 | result = capture_command(&cp, query_result, 1024); | |
174 | ||
175 | if (result) | |
176 | trace2_data_intmax("fsm_hook", NULL, "query/failed", result); | |
974c1b39 | 177 | else |
940b94f3 JH |
178 | trace2_data_intmax("fsm_hook", NULL, "query/response-length", |
179 | query_result->len); | |
180 | ||
940b94f3 JH |
181 | trace2_region_leave("fsm_hook", "query", NULL); |
182 | ||
183 | return result; | |
883e248b BP |
184 | } |
185 | ||
558d146d JH |
186 | static size_t handle_path_with_trailing_slash( |
187 | struct index_state *istate, const char *name, int pos); | |
188 | ||
189 | /* | |
190 | * The daemon sent an observed pathname without a trailing slash. | |
191 | * (This is the normal case.) We do not know if it is a tracked or | |
192 | * untracked file, a sparse-directory, or a populated directory (on a | |
193 | * platform such as Windows where FSEvents are not qualified). | |
194 | * | |
195 | * The pathname contains the observed case reported by the FS. We | |
196 | * do not know it is case-correct or -incorrect. | |
197 | * | |
198 | * Assume it is case-correct and try an exact match. | |
9e34e562 JH |
199 | * |
200 | * Return the number of cache-entries that we invalidated. | |
558d146d | 201 | */ |
9e34e562 | 202 | static size_t handle_path_without_trailing_slash( |
8687c2b0 JH |
203 | struct index_state *istate, const char *name, int pos) |
204 | { | |
7c97174d JH |
205 | /* |
206 | * Mark the untracked cache dirty for this path (regardless of | |
207 | * whether or not we find an exact match for it in the index). | |
208 | * Since the path is unqualified (no trailing slash hint in the | |
209 | * FSEvent), it may refer to a file or directory. So we should | |
210 | * not assume one or the other and should always let the untracked | |
211 | * cache decide what needs to invalidated. | |
212 | */ | |
213 | untracked_cache_invalidate_trimmed_path(istate, name, 0); | |
214 | ||
8687c2b0 JH |
215 | if (pos >= 0) { |
216 | /* | |
558d146d JH |
217 | * An exact match on a tracked file. We assume that we |
218 | * do not need to scan forward for a sparse-directory | |
219 | * cache-entry with the same pathname, nor for a cone | |
220 | * at that directory. (That is, assume no D/F conflicts.) | |
8687c2b0 JH |
221 | */ |
222 | istate->cache[pos]->ce_flags &= ~CE_FSMONITOR_VALID; | |
9e34e562 | 223 | return 1; |
8687c2b0 | 224 | } else { |
9e34e562 | 225 | size_t nr_in_cone; |
558d146d JH |
226 | struct strbuf work_path = STRBUF_INIT; |
227 | ||
8687c2b0 | 228 | /* |
558d146d JH |
229 | * The negative "pos" gives us the suggested insertion |
230 | * point for the pathname (without the trailing slash). | |
231 | * We need to see if there is a directory with that | |
232 | * prefix, but there can be lots of pathnames between | |
233 | * "foo" and "foo/" like "foo-" or "foo-bar", so we | |
234 | * don't want to do our own scan. | |
8687c2b0 | 235 | */ |
558d146d JH |
236 | strbuf_add(&work_path, name, strlen(name)); |
237 | strbuf_addch(&work_path, '/'); | |
238 | pos = index_name_pos(istate, work_path.buf, work_path.len); | |
9e34e562 JH |
239 | nr_in_cone = handle_path_with_trailing_slash( |
240 | istate, work_path.buf, pos); | |
558d146d | 241 | strbuf_release(&work_path); |
9e34e562 | 242 | return nr_in_cone; |
8687c2b0 JH |
243 | } |
244 | } | |
245 | ||
7a15a62a JH |
246 | /* |
247 | * The daemon can decorate directory events, such as a move or rename, | |
248 | * by adding a trailing slash to the observed name. Use this to | |
249 | * explicitly invalidate the entire cone under that directory. | |
250 | * | |
251 | * The daemon can only reliably do that if the OS FSEvent contains | |
252 | * sufficient information in the event. | |
253 | * | |
254 | * macOS FSEvents have enough information. | |
255 | * | |
256 | * Other platforms may or may not be able to do it (and it might | |
257 | * depend on the type of event (for example, a daemon could lstat() an | |
258 | * observed pathname after a rename, but not after a delete)). | |
259 | * | |
260 | * If we find an exact match in the index for a path with a trailing | |
261 | * slash, it means that we matched a sparse-index directory in a | |
262 | * cone-mode sparse-checkout (since that's the only time we have | |
263 | * directories in the index). We should never see this in practice | |
264 | * (because sparse directories should not be present and therefore | |
265 | * not generating FS events). Either way, we can treat them in the | |
266 | * same way and just invalidate the cache-entry and the untracked | |
267 | * cache (and in this case, the forward cache-entry scan won't find | |
268 | * anything and it doesn't hurt to let it run). | |
a5248203 JH |
269 | * |
270 | * Return the number of cache-entries that we invalidated. We will | |
271 | * use this later to determine if we need to attempt a second | |
272 | * case-insensitive search on case-insensitive file systems. That is, | |
273 | * if the search using the observed-case in the FSEvent yields any | |
274 | * results, we assume the prefix is case-correct. If there are no | |
275 | * matches, we still don't know if the observed path is simply | |
276 | * untracked or case-incorrect. | |
7a15a62a | 277 | */ |
a5248203 | 278 | static size_t handle_path_with_trailing_slash( |
e5da3ddb JH |
279 | struct index_state *istate, const char *name, int pos) |
280 | { | |
281 | int i; | |
a5248203 | 282 | size_t nr_in_cone = 0; |
e5da3ddb | 283 | |
7c97174d JH |
284 | /* |
285 | * Mark the untracked cache dirty for this directory path | |
286 | * (regardless of whether or not we find an exact match for it | |
287 | * in the index or find it to be proper prefix of one or more | |
288 | * files in the index), since the FSEvent is hinting that | |
289 | * there may be changes on or within the directory. | |
290 | */ | |
291 | untracked_cache_invalidate_trimmed_path(istate, name, 0); | |
292 | ||
e5da3ddb JH |
293 | if (pos < 0) |
294 | pos = -pos - 1; | |
295 | ||
296 | /* Mark all entries for the folder invalid */ | |
297 | for (i = pos; i < istate->cache_nr; i++) { | |
298 | if (!starts_with(istate->cache[i]->name, name)) | |
299 | break; | |
300 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
a5248203 | 301 | nr_in_cone++; |
e5da3ddb | 302 | } |
a5248203 JH |
303 | |
304 | return nr_in_cone; | |
e5da3ddb JH |
305 | } |
306 | ||
ff03836b | 307 | static void fsmonitor_refresh_callback(struct index_state *istate, char *name) |
883e248b | 308 | { |
8687c2b0 | 309 | int len = strlen(name); |
95a4e78a JH |
310 | int pos = index_name_pos(istate, name, len); |
311 | ||
312 | trace_printf_key(&trace_fsmonitor, | |
313 | "fsmonitor_refresh_callback '%s' (pos %d)", | |
314 | name, pos); | |
ff03836b | 315 | |
95a4e78a | 316 | if (name[len - 1] == '/') { |
e5da3ddb | 317 | handle_path_with_trailing_slash(istate, name, pos); |
ff03836b | 318 | } else { |
8687c2b0 | 319 | handle_path_without_trailing_slash(istate, name, pos); |
883e248b | 320 | } |
883e248b BP |
321 | } |
322 | ||
26b9f34a JH |
323 | /* |
324 | * The number of pathnames that we need to receive from FSMonitor | |
325 | * before we force the index to be updated. | |
326 | * | |
327 | * Note that any pathname within the set of received paths MAY cause | |
328 | * cache-entry or istate flag bits to be updated and thus cause the | |
329 | * index to be updated on disk. | |
330 | * | |
331 | * However, the response may contain many paths (such as ignored | |
332 | * paths) that will not update any flag bits. And thus not force the | |
333 | * index to be updated. (This is fine and normal.) It also means | |
334 | * that the token will not be updated in the FSMonitor index | |
335 | * extension. So the next Git command will find the same token in the | |
336 | * index, make the same token-relative request, and receive the same | |
337 | * response (plus any newly changed paths). If this response is large | |
338 | * (and continues to grow), performance could be impacted. | |
339 | * | |
340 | * For example, if the user runs a build and it writes 100K object | |
341 | * files but doesn't modify any source files, the index would not need | |
342 | * to be updated. The FSMonitor response (after the build and | |
343 | * relative to a pre-build token) might be 5MB. Each subsequent Git | |
344 | * command will receive that same 100K/5MB response until something | |
345 | * causes the index to be updated. And `refresh_fsmonitor()` will | |
346 | * have to iterate over those 100K paths each time. | |
347 | * | |
348 | * Performance could be improved if we optionally force update the | |
349 | * index after a very large response and get an updated token into | |
350 | * the FSMonitor index extension. This should allow subsequent | |
351 | * commands to get smaller and more current responses. | |
352 | * | |
353 | * The value chosen here does not need to be precise. The index | |
354 | * will be updated automatically the first time the user touches | |
355 | * a tracked file and causes a command like `git status` to | |
356 | * update an mtime to be updated and/or set a flag bit. | |
357 | */ | |
358 | static int fsmonitor_force_update_threshold = 100; | |
359 | ||
883e248b BP |
360 | void refresh_fsmonitor(struct index_state *istate) |
361 | { | |
25c2cab0 | 362 | static int warn_once = 0; |
883e248b | 363 | struct strbuf query_result = STRBUF_INIT; |
8da2c576 KW |
364 | int query_success = 0, hook_version = -1; |
365 | size_t bol = 0; /* beginning of line */ | |
883e248b | 366 | uint64_t last_update; |
56c69100 | 367 | struct strbuf last_update_token = STRBUF_INIT; |
883e248b | 368 | char *buf; |
5d137fc2 | 369 | unsigned int i; |
974c1b39 | 370 | int is_trivial = 0; |
6269f8ea | 371 | struct repository *r = istate->repo; |
1e0ea5c4 | 372 | enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r); |
25c2cab0 ED |
373 | enum fsmonitor_reason reason = fsm_settings__get_reason(r); |
374 | ||
375 | if (!warn_once && reason > FSMONITOR_REASON_OK) { | |
c4f94907 | 376 | char *msg = fsm_settings__get_incompatible_msg(r, reason); |
25c2cab0 | 377 | warn_once = 1; |
c4f94907 JK |
378 | warning("%s", msg); |
379 | free(msg); | |
25c2cab0 | 380 | } |
883e248b | 381 | |
1e0ea5c4 JH |
382 | if (fsm_mode <= FSMONITOR_MODE_DISABLED || |
383 | istate->fsmonitor_has_run_once) | |
883e248b | 384 | return; |
8da2c576 | 385 | |
398a3b08 | 386 | istate->fsmonitor_has_run_once = 1; |
883e248b BP |
387 | |
388 | trace_printf_key(&trace_fsmonitor, "refresh fsmonitor"); | |
1e0ea5c4 JH |
389 | |
390 | if (fsm_mode == FSMONITOR_MODE_IPC) { | |
9c307e8a JH |
391 | query_success = !fsmonitor_ipc__send_query( |
392 | istate->fsmonitor_last_update ? | |
393 | istate->fsmonitor_last_update : "builtin:fake", | |
394 | &query_result); | |
395 | if (query_success) { | |
396 | /* | |
397 | * The response contains a series of nul terminated | |
398 | * strings. The first is the new token. | |
399 | * | |
400 | * Use `char *buf` as an interlude to trick the CI | |
401 | * static analysis to let us use `strbuf_addstr()` | |
402 | * here (and only copy the token) rather than | |
403 | * `strbuf_addbuf()`. | |
404 | */ | |
405 | buf = query_result.buf; | |
406 | strbuf_addstr(&last_update_token, buf); | |
407 | bol = last_update_token.len + 1; | |
408 | is_trivial = query_result.buf[bol] == '/'; | |
409 | if (is_trivial) | |
410 | trace2_data_intmax("fsm_client", NULL, | |
411 | "query/trivial-response", 1); | |
412 | } else { | |
413 | /* | |
414 | * The builtin daemon is not available on this | |
415 | * platform -OR- we failed to get a response. | |
416 | * | |
417 | * Generate a fake token (rather than a V1 | |
418 | * timestamp) for the index extension. (If | |
419 | * they switch back to the hook API, we don't | |
420 | * want ambiguous state.) | |
421 | */ | |
422 | strbuf_addstr(&last_update_token, "builtin:fake"); | |
423 | } | |
424 | ||
425 | goto apply_results; | |
1e0ea5c4 JH |
426 | } |
427 | ||
428 | assert(fsm_mode == FSMONITOR_MODE_HOOK); | |
429 | ||
430 | hook_version = fsmonitor_hook_version(); | |
431 | ||
883e248b | 432 | /* |
1e0ea5c4 | 433 | * This could be racy so save the date/time now and query_fsmonitor_hook |
883e248b BP |
434 | * should be inclusive to ensure we don't miss potential changes. |
435 | */ | |
436 | last_update = getnanotime(); | |
8da2c576 KW |
437 | if (hook_version == HOOK_INTERFACE_VERSION1) |
438 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); | |
883e248b BP |
439 | |
440 | /* | |
1e0ea5c4 | 441 | * If we have a last update token, call query_fsmonitor_hook for the set of |
8da2c576 | 442 | * changes since that token, else assume everything is possibly dirty |
883e248b BP |
443 | * and check it all. |
444 | */ | |
445 | if (istate->fsmonitor_last_update) { | |
8da2c576 | 446 | if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) { |
1e0ea5c4 JH |
447 | query_success = !query_fsmonitor_hook( |
448 | r, HOOK_INTERFACE_VERSION2, | |
8da2c576 KW |
449 | istate->fsmonitor_last_update, &query_result); |
450 | ||
451 | if (query_success) { | |
452 | if (hook_version < 0) | |
453 | hook_version = HOOK_INTERFACE_VERSION2; | |
454 | ||
455 | /* | |
456 | * First entry will be the last update token | |
457 | * Need to use a char * variable because static | |
458 | * analysis was suggesting to use strbuf_addbuf | |
459 | * but we don't want to copy the entire strbuf | |
6d12b533 | 460 | * only the chars up to the first NUL |
8da2c576 KW |
461 | */ |
462 | buf = query_result.buf; | |
463 | strbuf_addstr(&last_update_token, buf); | |
464 | if (!last_update_token.len) { | |
465 | warning("Empty last update token."); | |
466 | query_success = 0; | |
467 | } else { | |
468 | bol = last_update_token.len + 1; | |
974c1b39 | 469 | is_trivial = query_result.buf[bol] == '/'; |
8da2c576 KW |
470 | } |
471 | } else if (hook_version < 0) { | |
472 | hook_version = HOOK_INTERFACE_VERSION1; | |
473 | if (!last_update_token.len) | |
474 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); | |
475 | } | |
476 | } | |
477 | ||
478 | if (hook_version == HOOK_INTERFACE_VERSION1) { | |
1e0ea5c4 JH |
479 | query_success = !query_fsmonitor_hook( |
480 | r, HOOK_INTERFACE_VERSION1, | |
8da2c576 | 481 | istate->fsmonitor_last_update, &query_result); |
974c1b39 JH |
482 | if (query_success) |
483 | is_trivial = query_result.buf[0] == '/'; | |
8da2c576 KW |
484 | } |
485 | ||
974c1b39 JH |
486 | if (is_trivial) |
487 | trace2_data_intmax("fsm_hook", NULL, | |
488 | "query/trivial-response", 1); | |
489 | ||
1e0ea5c4 JH |
490 | trace_performance_since(last_update, "fsmonitor process '%s'", |
491 | fsm_settings__get_hook_path(r)); | |
492 | trace_printf_key(&trace_fsmonitor, | |
493 | "fsmonitor process '%s' returned %s", | |
494 | fsm_settings__get_hook_path(r), | |
495 | query_success ? "success" : "failure"); | |
883e248b BP |
496 | } |
497 | ||
9c307e8a | 498 | apply_results: |
974c1b39 JH |
499 | /* |
500 | * The response from FSMonitor (excluding the header token) is | |
501 | * either: | |
502 | * | |
503 | * [a] a (possibly empty) list of NUL delimited relative | |
504 | * pathnames of changed paths. This list can contain | |
505 | * files and directories. Directories have a trailing | |
506 | * slash. | |
507 | * | |
508 | * [b] a single '/' to indicate the provider had no | |
509 | * information and that we should consider everything | |
510 | * invalid. We call this a trivial response. | |
511 | */ | |
26b9f34a JH |
512 | trace2_region_enter("fsmonitor", "apply_results", istate->repo); |
513 | ||
974c1b39 JH |
514 | if (query_success && !is_trivial) { |
515 | /* | |
516 | * Mark all pathnames returned by the monitor as dirty. | |
517 | * | |
518 | * This updates both the cache-entries and the untracked-cache. | |
519 | */ | |
26b9f34a JH |
520 | int count = 0; |
521 | ||
883e248b | 522 | buf = query_result.buf; |
8da2c576 | 523 | for (i = bol; i < query_result.len; i++) { |
883e248b BP |
524 | if (buf[i] != '\0') |
525 | continue; | |
526 | fsmonitor_refresh_callback(istate, buf + bol); | |
527 | bol = i + 1; | |
26b9f34a | 528 | count++; |
883e248b | 529 | } |
26b9f34a | 530 | if (bol < query_result.len) { |
883e248b | 531 | fsmonitor_refresh_callback(istate, buf + bol); |
26b9f34a JH |
532 | count++; |
533 | } | |
679f2f9f US |
534 | |
535 | /* Now mark the untracked cache for fsmonitor usage */ | |
536 | if (istate->untracked) | |
537 | istate->untracked->use_fsmonitor = 1; | |
26b9f34a JH |
538 | |
539 | if (count > fsmonitor_force_update_threshold) | |
540 | istate->cache_changed |= FSMONITOR_CHANGED; | |
541 | ||
542 | trace2_data_intmax("fsmonitor", istate->repo, "apply_count", | |
543 | count); | |
544 | ||
883e248b | 545 | } else { |
974c1b39 JH |
546 | /* |
547 | * We failed to get a response or received a trivial response, | |
548 | * so invalidate everything. | |
549 | * | |
550 | * We only want to run the post index changed hook if | |
551 | * we've actually changed entries, so keep track if we | |
552 | * actually changed entries or not. | |
553 | */ | |
679f2f9f | 554 | int is_cache_changed = 0; |
974c1b39 | 555 | |
679f2f9f US |
556 | for (i = 0; i < istate->cache_nr; i++) { |
557 | if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) { | |
558 | is_cache_changed = 1; | |
559 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
560 | } | |
561 | } | |
883e248b | 562 | |
974c1b39 JH |
563 | /* |
564 | * If we're going to check every file, ensure we save | |
565 | * the results. | |
566 | */ | |
679f2f9f US |
567 | if (is_cache_changed) |
568 | istate->cache_changed |= FSMONITOR_CHANGED; | |
ca598d5f | 569 | |
883e248b BP |
570 | if (istate->untracked) |
571 | istate->untracked->use_fsmonitor = 0; | |
572 | } | |
26b9f34a JH |
573 | trace2_region_leave("fsmonitor", "apply_results", istate->repo); |
574 | ||
883e248b BP |
575 | strbuf_release(&query_result); |
576 | ||
56c69100 KW |
577 | /* Now that we've updated istate, save the last_update_token */ |
578 | FREE_AND_NULL(istate->fsmonitor_last_update); | |
579 | istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL); | |
883e248b BP |
580 | } |
581 | ||
fcd19b09 JH |
582 | /* |
583 | * The caller wants to turn on FSMonitor. And when the caller writes | |
584 | * the index to disk, a FSMonitor extension should be included. This | |
585 | * requires that `istate->fsmonitor_last_update` not be NULL. But we | |
586 | * have not actually talked to a FSMonitor process yet, so we don't | |
587 | * have an initial value for this field. | |
588 | * | |
589 | * For a protocol V1 FSMonitor process, this field is a formatted | |
590 | * "nanoseconds since epoch" field. However, for a protocol V2 | |
591 | * FSMonitor process, this field is an opaque token. | |
592 | * | |
593 | * Historically, `add_fsmonitor()` has initialized this field to the | |
594 | * current time for protocol V1 processes. There are lots of race | |
595 | * conditions here, but that code has shipped... | |
596 | * | |
597 | * The only true solution is to use a V2 FSMonitor and get a current | |
598 | * or default token value (that it understands), but we cannot do that | |
599 | * until we have actually talked to an instance of the FSMonitor process | |
600 | * (but the protocol requires that we send a token first...). | |
601 | * | |
602 | * For simplicity, just initialize like we have a V1 process and require | |
603 | * that V2 processes adapt. | |
604 | */ | |
605 | static void initialize_fsmonitor_last_update(struct index_state *istate) | |
606 | { | |
607 | struct strbuf last_update = STRBUF_INIT; | |
608 | ||
609 | strbuf_addf(&last_update, "%"PRIu64"", getnanotime()); | |
610 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); | |
611 | } | |
612 | ||
883e248b BP |
613 | void add_fsmonitor(struct index_state *istate) |
614 | { | |
5d137fc2 | 615 | unsigned int i; |
883e248b BP |
616 | |
617 | if (!istate->fsmonitor_last_update) { | |
618 | trace_printf_key(&trace_fsmonitor, "add fsmonitor"); | |
619 | istate->cache_changed |= FSMONITOR_CHANGED; | |
fcd19b09 | 620 | initialize_fsmonitor_last_update(istate); |
883e248b BP |
621 | |
622 | /* reset the fsmonitor state */ | |
623 | for (i = 0; i < istate->cache_nr; i++) | |
624 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
625 | ||
626 | /* reset the untracked cache */ | |
627 | if (istate->untracked) { | |
628 | add_untracked_cache(istate); | |
629 | istate->untracked->use_fsmonitor = 1; | |
630 | } | |
631 | ||
632 | /* Update the fsmonitor state */ | |
633 | refresh_fsmonitor(istate); | |
634 | } | |
635 | } | |
636 | ||
637 | void remove_fsmonitor(struct index_state *istate) | |
638 | { | |
639 | if (istate->fsmonitor_last_update) { | |
640 | trace_printf_key(&trace_fsmonitor, "remove fsmonitor"); | |
641 | istate->cache_changed |= FSMONITOR_CHANGED; | |
56c69100 | 642 | FREE_AND_NULL(istate->fsmonitor_last_update); |
883e248b BP |
643 | } |
644 | } | |
645 | ||
646 | void tweak_fsmonitor(struct index_state *istate) | |
647 | { | |
5d137fc2 | 648 | unsigned int i; |
1e0ea5c4 JH |
649 | int fsmonitor_enabled = (fsm_settings__get_mode(istate->repo) |
650 | > FSMONITOR_MODE_DISABLED); | |
ba1b9cac AV |
651 | |
652 | if (istate->fsmonitor_dirty) { | |
653 | if (fsmonitor_enabled) { | |
654 | /* Mark all entries valid */ | |
655 | for (i = 0; i < istate->cache_nr; i++) { | |
f954c7b8 JH |
656 | if (S_ISGITLINK(istate->cache[i]->ce_mode)) |
657 | continue; | |
ba1b9cac AV |
658 | istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID; |
659 | } | |
660 | ||
661 | /* Mark all previously saved entries as dirty */ | |
cae70acf | 662 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); |
ba1b9cac AV |
663 | ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate); |
664 | ||
679f2f9f | 665 | refresh_fsmonitor(istate); |
ba1b9cac AV |
666 | } |
667 | ||
668 | ewah_free(istate->fsmonitor_dirty); | |
669 | istate->fsmonitor_dirty = NULL; | |
670 | } | |
671 | ||
1e0ea5c4 | 672 | if (fsmonitor_enabled) |
883e248b | 673 | add_fsmonitor(istate); |
1e0ea5c4 JH |
674 | else |
675 | remove_fsmonitor(istate); | |
883e248b | 676 | } |