Revert "Merge branch 'la/hide-trailer-info' into next"
[git.git] / fsmonitor.c
blob2b17d60bbbecb0e0e53a934b4376e2207649e826
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "environment.h"
5 #include "ewah/ewok.h"
6 #include "fsmonitor.h"
7 #include "fsmonitor-ipc.h"
8 #include "name-hash.h"
9 #include "run-command.h"
10 #include "strbuf.h"
11 #include "trace2.h"
13 #define INDEX_EXTENSION_VERSION1 (1)
14 #define INDEX_EXTENSION_VERSION2 (2)
15 #define HOOK_INTERFACE_VERSION1 (1)
16 #define HOOK_INTERFACE_VERSION2 (2)
18 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
20 static void assert_index_minimum(struct index_state *istate, size_t pos)
22 if (pos > istate->cache_nr)
23 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
24 (uintmax_t)pos, istate->cache_nr);
27 static void fsmonitor_ewah_callback(size_t pos, void *is)
29 struct index_state *istate = (struct index_state *)is;
30 struct cache_entry *ce;
32 assert_index_minimum(istate, pos + 1);
34 ce = istate->cache[pos];
35 ce->ce_flags &= ~CE_FSMONITOR_VALID;
38 static int fsmonitor_hook_version(void)
40 int hook_version;
42 if (git_config_get_int("core.fsmonitorhookversion", &hook_version))
43 return -1;
45 if (hook_version == HOOK_INTERFACE_VERSION1 ||
46 hook_version == HOOK_INTERFACE_VERSION2)
47 return hook_version;
49 warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
50 "Must be 1 or 2.", hook_version);
51 return -1;
54 int read_fsmonitor_extension(struct index_state *istate, const void *data,
55 unsigned long sz)
57 const char *index = data;
58 uint32_t hdr_version;
59 uint32_t ewah_size;
60 struct ewah_bitmap *fsmonitor_dirty;
61 int ret;
62 uint64_t timestamp;
63 struct strbuf last_update = STRBUF_INIT;
65 if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
66 return error("corrupt fsmonitor extension (too short)");
68 hdr_version = get_be32(index);
69 index += sizeof(uint32_t);
70 if (hdr_version == INDEX_EXTENSION_VERSION1) {
71 timestamp = get_be64(index);
72 strbuf_addf(&last_update, "%"PRIu64"", timestamp);
73 index += sizeof(uint64_t);
74 } else if (hdr_version == INDEX_EXTENSION_VERSION2) {
75 strbuf_addstr(&last_update, index);
76 index += last_update.len + 1;
77 } else {
78 return error("bad fsmonitor version %d", hdr_version);
81 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
83 ewah_size = get_be32(index);
84 index += sizeof(uint32_t);
86 fsmonitor_dirty = ewah_new();
87 ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
88 if (ret != ewah_size) {
89 ewah_free(fsmonitor_dirty);
90 return error("failed to parse ewah bitmap reading fsmonitor index extension");
92 istate->fsmonitor_dirty = fsmonitor_dirty;
94 if (!istate->split_index)
95 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
97 trace2_data_string("index", NULL, "extension/fsmn/read/token",
98 istate->fsmonitor_last_update);
99 trace_printf_key(&trace_fsmonitor,
100 "read fsmonitor extension successful '%s'",
101 istate->fsmonitor_last_update);
102 return 0;
105 void fill_fsmonitor_bitmap(struct index_state *istate)
107 unsigned int i, skipped = 0;
108 istate->fsmonitor_dirty = ewah_new();
109 for (i = 0; i < istate->cache_nr; i++) {
110 if (istate->cache[i]->ce_flags & CE_REMOVE)
111 skipped++;
112 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
113 ewah_set(istate->fsmonitor_dirty, i - skipped);
117 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
119 uint32_t hdr_version;
120 uint32_t ewah_start;
121 uint32_t ewah_size = 0;
122 int fixup = 0;
124 if (!istate->split_index)
125 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
127 put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
128 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
130 strbuf_addstr(sb, istate->fsmonitor_last_update);
131 strbuf_addch(sb, 0); /* Want to keep a NUL */
133 fixup = sb->len;
134 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
136 ewah_start = sb->len;
137 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
138 ewah_free(istate->fsmonitor_dirty);
139 istate->fsmonitor_dirty = NULL;
141 /* fix up size field */
142 put_be32(&ewah_size, sb->len - ewah_start);
143 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
145 trace2_data_string("index", NULL, "extension/fsmn/write/token",
146 istate->fsmonitor_last_update);
147 trace_printf_key(&trace_fsmonitor,
148 "write fsmonitor extension successful '%s'",
149 istate->fsmonitor_last_update);
153 * Call the query-fsmonitor hook passing the last update token of the saved results.
155 static int query_fsmonitor_hook(struct repository *r,
156 int version,
157 const char *last_update,
158 struct strbuf *query_result)
160 struct child_process cp = CHILD_PROCESS_INIT;
161 int result;
163 if (fsm_settings__get_mode(r) != FSMONITOR_MODE_HOOK)
164 return -1;
166 strvec_push(&cp.args, fsm_settings__get_hook_path(r));
167 strvec_pushf(&cp.args, "%d", version);
168 strvec_pushf(&cp.args, "%s", last_update);
169 cp.use_shell = 1;
170 cp.dir = get_git_work_tree();
172 trace2_region_enter("fsm_hook", "query", NULL);
174 result = capture_command(&cp, query_result, 1024);
176 if (result)
177 trace2_data_intmax("fsm_hook", NULL, "query/failed", result);
178 else
179 trace2_data_intmax("fsm_hook", NULL, "query/response-length",
180 query_result->len);
182 trace2_region_leave("fsm_hook", "query", NULL);
184 return result;
188 * Invalidate the FSM bit on this CE. This is like mark_fsmonitor_invalid()
189 * but we've already handled the untracked-cache, so let's not repeat that
190 * work. This also lets us have a different trace message so that we can
191 * see everything that was done as part of the refresh-callback.
193 static void invalidate_ce_fsm(struct cache_entry *ce)
195 if (ce->ce_flags & CE_FSMONITOR_VALID) {
196 trace_printf_key(&trace_fsmonitor,
197 "fsmonitor_refresh_callback INV: '%s'",
198 ce->name);
199 ce->ce_flags &= ~CE_FSMONITOR_VALID;
203 static size_t handle_path_with_trailing_slash(
204 struct index_state *istate, const char *name, int pos);
207 * Use the name-hash to do a case-insensitive cache-entry lookup with
208 * the pathname and invalidate the cache-entry.
210 * Returns the number of cache-entries that we invalidated.
212 static size_t handle_using_name_hash_icase(
213 struct index_state *istate, const char *name)
215 struct cache_entry *ce = NULL;
217 ce = index_file_exists(istate, name, strlen(name), 1);
218 if (!ce)
219 return 0;
222 * A case-insensitive search in the name-hash using the
223 * observed pathname found a cache-entry, so the observed path
224 * is case-incorrect. Invalidate the cache-entry and use the
225 * correct spelling from the cache-entry to invalidate the
226 * untracked-cache. Since we now have sparse-directories in
227 * the index, the observed pathname may represent a regular
228 * file or a sparse-index directory.
230 * Note that we should not have seen FSEvents for a
231 * sparse-index directory, but we handle it just in case.
233 * Either way, we know that there are not any cache-entries for
234 * children inside the cone of the directory, so we don't need to
235 * do the usual scan.
237 trace_printf_key(&trace_fsmonitor,
238 "fsmonitor_refresh_callback MAP: '%s' '%s'",
239 name, ce->name);
242 * NEEDSWORK: We used the name-hash to find the correct
243 * case-spelling of the pathname in the cache-entry[], so
244 * technically this is a tracked file or a sparse-directory.
245 * It should not have any entries in the untracked-cache, so
246 * we should not need to use the case-corrected spelling to
247 * invalidate the the untracked-cache. So we may not need to
248 * do this. For now, I'm going to be conservative and always
249 * do it; we can revisit this later.
251 untracked_cache_invalidate_trimmed_path(istate, ce->name, 0);
253 invalidate_ce_fsm(ce);
254 return 1;
258 * Use the dir-name-hash to find the correct-case spelling of the
259 * directory. Use the canonical spelling to invalidate all of the
260 * cache-entries within the matching cone.
262 * Returns the number of cache-entries that we invalidated.
264 static size_t handle_using_dir_name_hash_icase(
265 struct index_state *istate, const char *name)
267 struct strbuf canonical_path = STRBUF_INIT;
268 int pos;
269 size_t len = strlen(name);
270 size_t nr_in_cone;
272 if (name[len - 1] == '/')
273 len--;
275 if (!index_dir_find(istate, name, len, &canonical_path))
276 return 0; /* name is untracked */
278 if (!memcmp(name, canonical_path.buf, canonical_path.len)) {
279 strbuf_release(&canonical_path);
281 * NEEDSWORK: Our caller already tried an exact match
282 * and failed to find one. They called us to do an
283 * ICASE match, so we should never get an exact match,
284 * so we could promote this to a BUG() here if we
285 * wanted to. It doesn't hurt anything to just return
286 * 0 and go on because we should never get here. Or we
287 * could just get rid of the memcmp() and this "if"
288 * clause completely.
290 BUG("handle_using_dir_name_hash_icase(%s) did not exact match",
291 name);
294 trace_printf_key(&trace_fsmonitor,
295 "fsmonitor_refresh_callback MAP: '%s' '%s'",
296 name, canonical_path.buf);
299 * The dir-name-hash only tells us the corrected spelling of
300 * the prefix. We have to use this canonical path to do a
301 * lookup in the cache-entry array so that we repeat the
302 * original search using the case-corrected spelling.
304 strbuf_addch(&canonical_path, '/');
305 pos = index_name_pos(istate, canonical_path.buf,
306 canonical_path.len);
307 nr_in_cone = handle_path_with_trailing_slash(
308 istate, canonical_path.buf, pos);
309 strbuf_release(&canonical_path);
310 return nr_in_cone;
314 * The daemon sent an observed pathname without a trailing slash.
315 * (This is the normal case.) We do not know if it is a tracked or
316 * untracked file, a sparse-directory, or a populated directory (on a
317 * platform such as Windows where FSEvents are not qualified).
319 * The pathname contains the observed case reported by the FS. We
320 * do not know it is case-correct or -incorrect.
322 * Assume it is case-correct and try an exact match.
324 * Return the number of cache-entries that we invalidated.
326 static size_t handle_path_without_trailing_slash(
327 struct index_state *istate, const char *name, int pos)
330 * Mark the untracked cache dirty for this path (regardless of
331 * whether or not we find an exact match for it in the index).
332 * Since the path is unqualified (no trailing slash hint in the
333 * FSEvent), it may refer to a file or directory. So we should
334 * not assume one or the other and should always let the untracked
335 * cache decide what needs to invalidated.
337 untracked_cache_invalidate_trimmed_path(istate, name, 0);
339 if (pos >= 0) {
341 * An exact match on a tracked file. We assume that we
342 * do not need to scan forward for a sparse-directory
343 * cache-entry with the same pathname, nor for a cone
344 * at that directory. (That is, assume no D/F conflicts.)
346 invalidate_ce_fsm(istate->cache[pos]);
347 return 1;
348 } else {
349 size_t nr_in_cone;
350 struct strbuf work_path = STRBUF_INIT;
353 * The negative "pos" gives us the suggested insertion
354 * point for the pathname (without the trailing slash).
355 * We need to see if there is a directory with that
356 * prefix, but there can be lots of pathnames between
357 * "foo" and "foo/" like "foo-" or "foo-bar", so we
358 * don't want to do our own scan.
360 strbuf_add(&work_path, name, strlen(name));
361 strbuf_addch(&work_path, '/');
362 pos = index_name_pos(istate, work_path.buf, work_path.len);
363 nr_in_cone = handle_path_with_trailing_slash(
364 istate, work_path.buf, pos);
365 strbuf_release(&work_path);
366 return nr_in_cone;
371 * The daemon can decorate directory events, such as a move or rename,
372 * by adding a trailing slash to the observed name. Use this to
373 * explicitly invalidate the entire cone under that directory.
375 * The daemon can only reliably do that if the OS FSEvent contains
376 * sufficient information in the event.
378 * macOS FSEvents have enough information.
380 * Other platforms may or may not be able to do it (and it might
381 * depend on the type of event (for example, a daemon could lstat() an
382 * observed pathname after a rename, but not after a delete)).
384 * If we find an exact match in the index for a path with a trailing
385 * slash, it means that we matched a sparse-index directory in a
386 * cone-mode sparse-checkout (since that's the only time we have
387 * directories in the index). We should never see this in practice
388 * (because sparse directories should not be present and therefore
389 * not generating FS events). Either way, we can treat them in the
390 * same way and just invalidate the cache-entry and the untracked
391 * cache (and in this case, the forward cache-entry scan won't find
392 * anything and it doesn't hurt to let it run).
394 * Return the number of cache-entries that we invalidated. We will
395 * use this later to determine if we need to attempt a second
396 * case-insensitive search on case-insensitive file systems. That is,
397 * if the search using the observed-case in the FSEvent yields any
398 * results, we assume the prefix is case-correct. If there are no
399 * matches, we still don't know if the observed path is simply
400 * untracked or case-incorrect.
402 static size_t handle_path_with_trailing_slash(
403 struct index_state *istate, const char *name, int pos)
405 int i;
406 size_t nr_in_cone = 0;
409 * Mark the untracked cache dirty for this directory path
410 * (regardless of whether or not we find an exact match for it
411 * in the index or find it to be proper prefix of one or more
412 * files in the index), since the FSEvent is hinting that
413 * there may be changes on or within the directory.
415 untracked_cache_invalidate_trimmed_path(istate, name, 0);
417 if (pos < 0)
418 pos = -pos - 1;
420 /* Mark all entries for the folder invalid */
421 for (i = pos; i < istate->cache_nr; i++) {
422 if (!starts_with(istate->cache[i]->name, name))
423 break;
424 invalidate_ce_fsm(istate->cache[i]);
425 nr_in_cone++;
428 return nr_in_cone;
431 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
433 int len = strlen(name);
434 int pos = index_name_pos(istate, name, len);
435 size_t nr_in_cone;
437 trace_printf_key(&trace_fsmonitor,
438 "fsmonitor_refresh_callback '%s' (pos %d)",
439 name, pos);
441 if (name[len - 1] == '/')
442 nr_in_cone = handle_path_with_trailing_slash(istate, name, pos);
443 else
444 nr_in_cone = handle_path_without_trailing_slash(istate, name, pos);
447 * If we did not find an exact match for this pathname or any
448 * cache-entries with this directory prefix and we're on a
449 * case-insensitive file system, try again using the name-hash
450 * and dir-name-hash.
452 if (!nr_in_cone && ignore_case) {
453 nr_in_cone = handle_using_name_hash_icase(istate, name);
454 if (!nr_in_cone)
455 nr_in_cone = handle_using_dir_name_hash_icase(
456 istate, name);
459 if (nr_in_cone)
460 trace_printf_key(&trace_fsmonitor,
461 "fsmonitor_refresh_callback CNT: %d",
462 (int)nr_in_cone);
466 * The number of pathnames that we need to receive from FSMonitor
467 * before we force the index to be updated.
469 * Note that any pathname within the set of received paths MAY cause
470 * cache-entry or istate flag bits to be updated and thus cause the
471 * index to be updated on disk.
473 * However, the response may contain many paths (such as ignored
474 * paths) that will not update any flag bits. And thus not force the
475 * index to be updated. (This is fine and normal.) It also means
476 * that the token will not be updated in the FSMonitor index
477 * extension. So the next Git command will find the same token in the
478 * index, make the same token-relative request, and receive the same
479 * response (plus any newly changed paths). If this response is large
480 * (and continues to grow), performance could be impacted.
482 * For example, if the user runs a build and it writes 100K object
483 * files but doesn't modify any source files, the index would not need
484 * to be updated. The FSMonitor response (after the build and
485 * relative to a pre-build token) might be 5MB. Each subsequent Git
486 * command will receive that same 100K/5MB response until something
487 * causes the index to be updated. And `refresh_fsmonitor()` will
488 * have to iterate over those 100K paths each time.
490 * Performance could be improved if we optionally force update the
491 * index after a very large response and get an updated token into
492 * the FSMonitor index extension. This should allow subsequent
493 * commands to get smaller and more current responses.
495 * The value chosen here does not need to be precise. The index
496 * will be updated automatically the first time the user touches
497 * a tracked file and causes a command like `git status` to
498 * update an mtime to be updated and/or set a flag bit.
500 static int fsmonitor_force_update_threshold = 100;
502 void refresh_fsmonitor(struct index_state *istate)
504 static int warn_once = 0;
505 struct strbuf query_result = STRBUF_INIT;
506 int query_success = 0, hook_version = -1;
507 size_t bol = 0; /* beginning of line */
508 uint64_t last_update;
509 struct strbuf last_update_token = STRBUF_INIT;
510 char *buf;
511 unsigned int i;
512 int is_trivial = 0;
513 struct repository *r = istate->repo;
514 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
515 enum fsmonitor_reason reason = fsm_settings__get_reason(r);
517 if (!warn_once && reason > FSMONITOR_REASON_OK) {
518 char *msg = fsm_settings__get_incompatible_msg(r, reason);
519 warn_once = 1;
520 warning("%s", msg);
521 free(msg);
524 if (fsm_mode <= FSMONITOR_MODE_DISABLED ||
525 istate->fsmonitor_has_run_once)
526 return;
528 istate->fsmonitor_has_run_once = 1;
530 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
532 if (fsm_mode == FSMONITOR_MODE_IPC) {
533 query_success = !fsmonitor_ipc__send_query(
534 istate->fsmonitor_last_update ?
535 istate->fsmonitor_last_update : "builtin:fake",
536 &query_result);
537 if (query_success) {
539 * The response contains a series of nul terminated
540 * strings. The first is the new token.
542 * Use `char *buf` as an interlude to trick the CI
543 * static analysis to let us use `strbuf_addstr()`
544 * here (and only copy the token) rather than
545 * `strbuf_addbuf()`.
547 buf = query_result.buf;
548 strbuf_addstr(&last_update_token, buf);
549 bol = last_update_token.len + 1;
550 is_trivial = query_result.buf[bol] == '/';
551 if (is_trivial)
552 trace2_data_intmax("fsm_client", NULL,
553 "query/trivial-response", 1);
554 } else {
556 * The builtin daemon is not available on this
557 * platform -OR- we failed to get a response.
559 * Generate a fake token (rather than a V1
560 * timestamp) for the index extension. (If
561 * they switch back to the hook API, we don't
562 * want ambiguous state.)
564 strbuf_addstr(&last_update_token, "builtin:fake");
567 goto apply_results;
570 assert(fsm_mode == FSMONITOR_MODE_HOOK);
572 hook_version = fsmonitor_hook_version();
575 * This could be racy so save the date/time now and query_fsmonitor_hook
576 * should be inclusive to ensure we don't miss potential changes.
578 last_update = getnanotime();
579 if (hook_version == HOOK_INTERFACE_VERSION1)
580 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
583 * If we have a last update token, call query_fsmonitor_hook for the set of
584 * changes since that token, else assume everything is possibly dirty
585 * and check it all.
587 if (istate->fsmonitor_last_update) {
588 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
589 query_success = !query_fsmonitor_hook(
590 r, HOOK_INTERFACE_VERSION2,
591 istate->fsmonitor_last_update, &query_result);
593 if (query_success) {
594 if (hook_version < 0)
595 hook_version = HOOK_INTERFACE_VERSION2;
598 * First entry will be the last update token
599 * Need to use a char * variable because static
600 * analysis was suggesting to use strbuf_addbuf
601 * but we don't want to copy the entire strbuf
602 * only the chars up to the first NUL
604 buf = query_result.buf;
605 strbuf_addstr(&last_update_token, buf);
606 if (!last_update_token.len) {
607 warning("Empty last update token.");
608 query_success = 0;
609 } else {
610 bol = last_update_token.len + 1;
611 is_trivial = query_result.buf[bol] == '/';
613 } else if (hook_version < 0) {
614 hook_version = HOOK_INTERFACE_VERSION1;
615 if (!last_update_token.len)
616 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
620 if (hook_version == HOOK_INTERFACE_VERSION1) {
621 query_success = !query_fsmonitor_hook(
622 r, HOOK_INTERFACE_VERSION1,
623 istate->fsmonitor_last_update, &query_result);
624 if (query_success)
625 is_trivial = query_result.buf[0] == '/';
628 if (is_trivial)
629 trace2_data_intmax("fsm_hook", NULL,
630 "query/trivial-response", 1);
632 trace_performance_since(last_update, "fsmonitor process '%s'",
633 fsm_settings__get_hook_path(r));
634 trace_printf_key(&trace_fsmonitor,
635 "fsmonitor process '%s' returned %s",
636 fsm_settings__get_hook_path(r),
637 query_success ? "success" : "failure");
640 apply_results:
642 * The response from FSMonitor (excluding the header token) is
643 * either:
645 * [a] a (possibly empty) list of NUL delimited relative
646 * pathnames of changed paths. This list can contain
647 * files and directories. Directories have a trailing
648 * slash.
650 * [b] a single '/' to indicate the provider had no
651 * information and that we should consider everything
652 * invalid. We call this a trivial response.
654 trace2_region_enter("fsmonitor", "apply_results", istate->repo);
656 if (query_success && !is_trivial) {
658 * Mark all pathnames returned by the monitor as dirty.
660 * This updates both the cache-entries and the untracked-cache.
662 int count = 0;
664 buf = query_result.buf;
665 for (i = bol; i < query_result.len; i++) {
666 if (buf[i] != '\0')
667 continue;
668 fsmonitor_refresh_callback(istate, buf + bol);
669 bol = i + 1;
670 count++;
672 if (bol < query_result.len) {
673 fsmonitor_refresh_callback(istate, buf + bol);
674 count++;
677 /* Now mark the untracked cache for fsmonitor usage */
678 if (istate->untracked)
679 istate->untracked->use_fsmonitor = 1;
681 if (count > fsmonitor_force_update_threshold)
682 istate->cache_changed |= FSMONITOR_CHANGED;
684 trace2_data_intmax("fsmonitor", istate->repo, "apply_count",
685 count);
687 } else {
689 * We failed to get a response or received a trivial response,
690 * so invalidate everything.
692 * We only want to run the post index changed hook if
693 * we've actually changed entries, so keep track if we
694 * actually changed entries or not.
696 int is_cache_changed = 0;
698 for (i = 0; i < istate->cache_nr; i++) {
699 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
700 is_cache_changed = 1;
701 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
706 * If we're going to check every file, ensure we save
707 * the results.
709 if (is_cache_changed)
710 istate->cache_changed |= FSMONITOR_CHANGED;
712 if (istate->untracked)
713 istate->untracked->use_fsmonitor = 0;
715 trace2_region_leave("fsmonitor", "apply_results", istate->repo);
717 strbuf_release(&query_result);
719 /* Now that we've updated istate, save the last_update_token */
720 FREE_AND_NULL(istate->fsmonitor_last_update);
721 istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
725 * The caller wants to turn on FSMonitor. And when the caller writes
726 * the index to disk, a FSMonitor extension should be included. This
727 * requires that `istate->fsmonitor_last_update` not be NULL. But we
728 * have not actually talked to a FSMonitor process yet, so we don't
729 * have an initial value for this field.
731 * For a protocol V1 FSMonitor process, this field is a formatted
732 * "nanoseconds since epoch" field. However, for a protocol V2
733 * FSMonitor process, this field is an opaque token.
735 * Historically, `add_fsmonitor()` has initialized this field to the
736 * current time for protocol V1 processes. There are lots of race
737 * conditions here, but that code has shipped...
739 * The only true solution is to use a V2 FSMonitor and get a current
740 * or default token value (that it understands), but we cannot do that
741 * until we have actually talked to an instance of the FSMonitor process
742 * (but the protocol requires that we send a token first...).
744 * For simplicity, just initialize like we have a V1 process and require
745 * that V2 processes adapt.
747 static void initialize_fsmonitor_last_update(struct index_state *istate)
749 struct strbuf last_update = STRBUF_INIT;
751 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
752 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
755 void add_fsmonitor(struct index_state *istate)
757 unsigned int i;
759 if (!istate->fsmonitor_last_update) {
760 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
761 istate->cache_changed |= FSMONITOR_CHANGED;
762 initialize_fsmonitor_last_update(istate);
764 /* reset the fsmonitor state */
765 for (i = 0; i < istate->cache_nr; i++)
766 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
768 /* reset the untracked cache */
769 if (istate->untracked) {
770 add_untracked_cache(istate);
771 istate->untracked->use_fsmonitor = 1;
774 /* Update the fsmonitor state */
775 refresh_fsmonitor(istate);
779 void remove_fsmonitor(struct index_state *istate)
781 if (istate->fsmonitor_last_update) {
782 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
783 istate->cache_changed |= FSMONITOR_CHANGED;
784 FREE_AND_NULL(istate->fsmonitor_last_update);
788 void tweak_fsmonitor(struct index_state *istate)
790 unsigned int i;
791 int fsmonitor_enabled = (fsm_settings__get_mode(istate->repo)
792 > FSMONITOR_MODE_DISABLED);
794 if (istate->fsmonitor_dirty) {
795 if (fsmonitor_enabled) {
796 /* Mark all entries valid */
797 for (i = 0; i < istate->cache_nr; i++) {
798 if (S_ISGITLINK(istate->cache[i]->ce_mode))
799 continue;
800 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
803 /* Mark all previously saved entries as dirty */
804 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
805 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
807 refresh_fsmonitor(istate);
810 ewah_free(istate->fsmonitor_dirty);
811 istate->fsmonitor_dirty = NULL;
814 if (fsmonitor_enabled)
815 add_fsmonitor(istate);
816 else
817 remove_fsmonitor(istate);