Merge branch 'rs/archive-with-attr-pathspec-fix'
[git/gitster.git] / fsmonitor.c
blob237ca59d004582de93624477259f9da595af05ce
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "dir.h"
6 #include "environment.h"
7 #include "ewah/ewok.h"
8 #include "fsmonitor.h"
9 #include "fsmonitor-ipc.h"
10 #include "name-hash.h"
11 #include "repository.h"
12 #include "run-command.h"
13 #include "strbuf.h"
14 #include "trace2.h"
16 #define INDEX_EXTENSION_VERSION1 (1)
17 #define INDEX_EXTENSION_VERSION2 (2)
18 #define HOOK_INTERFACE_VERSION1 (1)
19 #define HOOK_INTERFACE_VERSION2 (2)
21 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
23 static void assert_index_minimum(struct index_state *istate, size_t pos)
25 if (pos > istate->cache_nr)
26 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
27 (uintmax_t)pos, istate->cache_nr);
30 static void fsmonitor_ewah_callback(size_t pos, void *is)
32 struct index_state *istate = (struct index_state *)is;
33 struct cache_entry *ce;
35 assert_index_minimum(istate, pos + 1);
37 ce = istate->cache[pos];
38 ce->ce_flags &= ~CE_FSMONITOR_VALID;
41 static int fsmonitor_hook_version(void)
43 int hook_version;
45 if (git_config_get_int("core.fsmonitorhookversion", &hook_version))
46 return -1;
48 if (hook_version == HOOK_INTERFACE_VERSION1 ||
49 hook_version == HOOK_INTERFACE_VERSION2)
50 return hook_version;
52 warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
53 "Must be 1 or 2.", hook_version);
54 return -1;
57 int read_fsmonitor_extension(struct index_state *istate, const void *data,
58 unsigned long sz)
60 const char *index = data;
61 uint32_t hdr_version;
62 uint32_t ewah_size;
63 struct ewah_bitmap *fsmonitor_dirty;
64 int ret;
65 uint64_t timestamp;
66 struct strbuf last_update = STRBUF_INIT;
68 if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
69 return error("corrupt fsmonitor extension (too short)");
71 hdr_version = get_be32(index);
72 index += sizeof(uint32_t);
73 if (hdr_version == INDEX_EXTENSION_VERSION1) {
74 timestamp = get_be64(index);
75 strbuf_addf(&last_update, "%"PRIu64"", timestamp);
76 index += sizeof(uint64_t);
77 } else if (hdr_version == INDEX_EXTENSION_VERSION2) {
78 strbuf_addstr(&last_update, index);
79 index += last_update.len + 1;
80 } else {
81 return error("bad fsmonitor version %d", hdr_version);
84 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
86 ewah_size = get_be32(index);
87 index += sizeof(uint32_t);
89 fsmonitor_dirty = ewah_new();
90 ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
91 if (ret != ewah_size) {
92 ewah_free(fsmonitor_dirty);
93 return error("failed to parse ewah bitmap reading fsmonitor index extension");
95 istate->fsmonitor_dirty = fsmonitor_dirty;
97 if (!istate->split_index)
98 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
100 trace2_data_string("index", NULL, "extension/fsmn/read/token",
101 istate->fsmonitor_last_update);
102 trace_printf_key(&trace_fsmonitor,
103 "read fsmonitor extension successful '%s'",
104 istate->fsmonitor_last_update);
105 return 0;
108 void fill_fsmonitor_bitmap(struct index_state *istate)
110 unsigned int i, skipped = 0;
111 istate->fsmonitor_dirty = ewah_new();
112 for (i = 0; i < istate->cache_nr; i++) {
113 if (istate->cache[i]->ce_flags & CE_REMOVE)
114 skipped++;
115 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
116 ewah_set(istate->fsmonitor_dirty, i - skipped);
120 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
122 uint32_t hdr_version;
123 uint32_t ewah_start;
124 uint32_t ewah_size = 0;
125 int fixup = 0;
127 if (!istate->split_index)
128 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
130 put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
131 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
133 strbuf_addstr(sb, istate->fsmonitor_last_update);
134 strbuf_addch(sb, 0); /* Want to keep a NUL */
136 fixup = sb->len;
137 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
139 ewah_start = sb->len;
140 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
141 ewah_free(istate->fsmonitor_dirty);
142 istate->fsmonitor_dirty = NULL;
144 /* fix up size field */
145 put_be32(&ewah_size, sb->len - ewah_start);
146 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
148 trace2_data_string("index", NULL, "extension/fsmn/write/token",
149 istate->fsmonitor_last_update);
150 trace_printf_key(&trace_fsmonitor,
151 "write fsmonitor extension successful '%s'",
152 istate->fsmonitor_last_update);
156 * Call the query-fsmonitor hook passing the last update token of the saved results.
158 static int query_fsmonitor_hook(struct repository *r,
159 int version,
160 const char *last_update,
161 struct strbuf *query_result)
163 struct child_process cp = CHILD_PROCESS_INIT;
164 int result;
166 if (fsm_settings__get_mode(r) != FSMONITOR_MODE_HOOK)
167 return -1;
169 strvec_push(&cp.args, fsm_settings__get_hook_path(r));
170 strvec_pushf(&cp.args, "%d", version);
171 strvec_pushf(&cp.args, "%s", last_update);
172 cp.use_shell = 1;
173 cp.dir = repo_get_work_tree(the_repository);
175 trace2_region_enter("fsm_hook", "query", NULL);
177 result = capture_command(&cp, query_result, 1024);
179 if (result)
180 trace2_data_intmax("fsm_hook", NULL, "query/failed", result);
181 else
182 trace2_data_intmax("fsm_hook", NULL, "query/response-length",
183 query_result->len);
185 trace2_region_leave("fsm_hook", "query", NULL);
187 return result;
191 * Invalidate the FSM bit on this CE. This is like mark_fsmonitor_invalid()
192 * but we've already handled the untracked-cache, so let's not repeat that
193 * work. This also lets us have a different trace message so that we can
194 * see everything that was done as part of the refresh-callback.
196 static void invalidate_ce_fsm(struct cache_entry *ce)
198 if (ce->ce_flags & CE_FSMONITOR_VALID) {
199 trace_printf_key(&trace_fsmonitor,
200 "fsmonitor_refresh_callback INV: '%s'",
201 ce->name);
202 ce->ce_flags &= ~CE_FSMONITOR_VALID;
206 static size_t handle_path_with_trailing_slash(
207 struct index_state *istate, const char *name, int pos);
210 * Use the name-hash to do a case-insensitive cache-entry lookup with
211 * the pathname and invalidate the cache-entry.
213 * Returns the number of cache-entries that we invalidated.
215 static size_t handle_using_name_hash_icase(
216 struct index_state *istate, const char *name)
218 struct cache_entry *ce = NULL;
220 ce = index_file_exists(istate, name, strlen(name), 1);
221 if (!ce)
222 return 0;
225 * A case-insensitive search in the name-hash using the
226 * observed pathname found a cache-entry, so the observed path
227 * is case-incorrect. Invalidate the cache-entry and use the
228 * correct spelling from the cache-entry to invalidate the
229 * untracked-cache. Since we now have sparse-directories in
230 * the index, the observed pathname may represent a regular
231 * file or a sparse-index directory.
233 * Note that we should not have seen FSEvents for a
234 * sparse-index directory, but we handle it just in case.
236 * Either way, we know that there are not any cache-entries for
237 * children inside the cone of the directory, so we don't need to
238 * do the usual scan.
240 trace_printf_key(&trace_fsmonitor,
241 "fsmonitor_refresh_callback MAP: '%s' '%s'",
242 name, ce->name);
245 * NEEDSWORK: We used the name-hash to find the correct
246 * case-spelling of the pathname in the cache-entry[], so
247 * technically this is a tracked file or a sparse-directory.
248 * It should not have any entries in the untracked-cache, so
249 * we should not need to use the case-corrected spelling to
250 * invalidate the the untracked-cache. So we may not need to
251 * do this. For now, I'm going to be conservative and always
252 * do it; we can revisit this later.
254 untracked_cache_invalidate_trimmed_path(istate, ce->name, 0);
256 invalidate_ce_fsm(ce);
257 return 1;
261 * Use the dir-name-hash to find the correct-case spelling of the
262 * directory. Use the canonical spelling to invalidate all of the
263 * cache-entries within the matching cone.
265 * Returns the number of cache-entries that we invalidated.
267 static size_t handle_using_dir_name_hash_icase(
268 struct index_state *istate, const char *name)
270 struct strbuf canonical_path = STRBUF_INIT;
271 int pos;
272 size_t len = strlen(name);
273 size_t nr_in_cone;
275 if (name[len - 1] == '/')
276 len--;
278 if (!index_dir_find(istate, name, len, &canonical_path))
279 return 0; /* name is untracked */
281 if (!memcmp(name, canonical_path.buf, canonical_path.len)) {
282 strbuf_release(&canonical_path);
284 * NEEDSWORK: Our caller already tried an exact match
285 * and failed to find one. They called us to do an
286 * ICASE match, so we should never get an exact match,
287 * so we could promote this to a BUG() here if we
288 * wanted to. It doesn't hurt anything to just return
289 * 0 and go on because we should never get here. Or we
290 * could just get rid of the memcmp() and this "if"
291 * clause completely.
293 BUG("handle_using_dir_name_hash_icase(%s) did not exact match",
294 name);
297 trace_printf_key(&trace_fsmonitor,
298 "fsmonitor_refresh_callback MAP: '%s' '%s'",
299 name, canonical_path.buf);
302 * The dir-name-hash only tells us the corrected spelling of
303 * the prefix. We have to use this canonical path to do a
304 * lookup in the cache-entry array so that we repeat the
305 * original search using the case-corrected spelling.
307 strbuf_addch(&canonical_path, '/');
308 pos = index_name_pos(istate, canonical_path.buf,
309 canonical_path.len);
310 nr_in_cone = handle_path_with_trailing_slash(
311 istate, canonical_path.buf, pos);
312 strbuf_release(&canonical_path);
313 return nr_in_cone;
317 * The daemon sent an observed pathname without a trailing slash.
318 * (This is the normal case.) We do not know if it is a tracked or
319 * untracked file, a sparse-directory, or a populated directory (on a
320 * platform such as Windows where FSEvents are not qualified).
322 * The pathname contains the observed case reported by the FS. We
323 * do not know it is case-correct or -incorrect.
325 * Assume it is case-correct and try an exact match.
327 * Return the number of cache-entries that we invalidated.
329 static size_t handle_path_without_trailing_slash(
330 struct index_state *istate, const char *name, int pos)
333 * Mark the untracked cache dirty for this path (regardless of
334 * whether or not we find an exact match for it in the index).
335 * Since the path is unqualified (no trailing slash hint in the
336 * FSEvent), it may refer to a file or directory. So we should
337 * not assume one or the other and should always let the untracked
338 * cache decide what needs to invalidated.
340 untracked_cache_invalidate_trimmed_path(istate, name, 0);
342 if (pos >= 0) {
344 * An exact match on a tracked file. We assume that we
345 * do not need to scan forward for a sparse-directory
346 * cache-entry with the same pathname, nor for a cone
347 * at that directory. (That is, assume no D/F conflicts.)
349 invalidate_ce_fsm(istate->cache[pos]);
350 return 1;
351 } else {
352 size_t nr_in_cone;
353 struct strbuf work_path = STRBUF_INIT;
356 * The negative "pos" gives us the suggested insertion
357 * point for the pathname (without the trailing slash).
358 * We need to see if there is a directory with that
359 * prefix, but there can be lots of pathnames between
360 * "foo" and "foo/" like "foo-" or "foo-bar", so we
361 * don't want to do our own scan.
363 strbuf_add(&work_path, name, strlen(name));
364 strbuf_addch(&work_path, '/');
365 pos = index_name_pos(istate, work_path.buf, work_path.len);
366 nr_in_cone = handle_path_with_trailing_slash(
367 istate, work_path.buf, pos);
368 strbuf_release(&work_path);
369 return nr_in_cone;
374 * The daemon can decorate directory events, such as a move or rename,
375 * by adding a trailing slash to the observed name. Use this to
376 * explicitly invalidate the entire cone under that directory.
378 * The daemon can only reliably do that if the OS FSEvent contains
379 * sufficient information in the event.
381 * macOS FSEvents have enough information.
383 * Other platforms may or may not be able to do it (and it might
384 * depend on the type of event (for example, a daemon could lstat() an
385 * observed pathname after a rename, but not after a delete)).
387 * If we find an exact match in the index for a path with a trailing
388 * slash, it means that we matched a sparse-index directory in a
389 * cone-mode sparse-checkout (since that's the only time we have
390 * directories in the index). We should never see this in practice
391 * (because sparse directories should not be present and therefore
392 * not generating FS events). Either way, we can treat them in the
393 * same way and just invalidate the cache-entry and the untracked
394 * cache (and in this case, the forward cache-entry scan won't find
395 * anything and it doesn't hurt to let it run).
397 * Return the number of cache-entries that we invalidated. We will
398 * use this later to determine if we need to attempt a second
399 * case-insensitive search on case-insensitive file systems. That is,
400 * if the search using the observed-case in the FSEvent yields any
401 * results, we assume the prefix is case-correct. If there are no
402 * matches, we still don't know if the observed path is simply
403 * untracked or case-incorrect.
405 static size_t handle_path_with_trailing_slash(
406 struct index_state *istate, const char *name, int pos)
408 int i;
409 size_t nr_in_cone = 0;
412 * Mark the untracked cache dirty for this directory path
413 * (regardless of whether or not we find an exact match for it
414 * in the index or find it to be proper prefix of one or more
415 * files in the index), since the FSEvent is hinting that
416 * there may be changes on or within the directory.
418 untracked_cache_invalidate_trimmed_path(istate, name, 0);
420 if (pos < 0)
421 pos = -pos - 1;
423 /* Mark all entries for the folder invalid */
424 for (i = pos; i < istate->cache_nr; i++) {
425 if (!starts_with(istate->cache[i]->name, name))
426 break;
427 invalidate_ce_fsm(istate->cache[i]);
428 nr_in_cone++;
431 return nr_in_cone;
434 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
436 int len = strlen(name);
437 int pos = index_name_pos(istate, name, len);
438 size_t nr_in_cone;
440 trace_printf_key(&trace_fsmonitor,
441 "fsmonitor_refresh_callback '%s' (pos %d)",
442 name, pos);
444 if (name[len - 1] == '/')
445 nr_in_cone = handle_path_with_trailing_slash(istate, name, pos);
446 else
447 nr_in_cone = handle_path_without_trailing_slash(istate, name, pos);
450 * If we did not find an exact match for this pathname or any
451 * cache-entries with this directory prefix and we're on a
452 * case-insensitive file system, try again using the name-hash
453 * and dir-name-hash.
455 if (!nr_in_cone && ignore_case) {
456 nr_in_cone = handle_using_name_hash_icase(istate, name);
457 if (!nr_in_cone)
458 nr_in_cone = handle_using_dir_name_hash_icase(
459 istate, name);
462 if (nr_in_cone)
463 trace_printf_key(&trace_fsmonitor,
464 "fsmonitor_refresh_callback CNT: %d",
465 (int)nr_in_cone);
469 * The number of pathnames that we need to receive from FSMonitor
470 * before we force the index to be updated.
472 * Note that any pathname within the set of received paths MAY cause
473 * cache-entry or istate flag bits to be updated and thus cause the
474 * index to be updated on disk.
476 * However, the response may contain many paths (such as ignored
477 * paths) that will not update any flag bits. And thus not force the
478 * index to be updated. (This is fine and normal.) It also means
479 * that the token will not be updated in the FSMonitor index
480 * extension. So the next Git command will find the same token in the
481 * index, make the same token-relative request, and receive the same
482 * response (plus any newly changed paths). If this response is large
483 * (and continues to grow), performance could be impacted.
485 * For example, if the user runs a build and it writes 100K object
486 * files but doesn't modify any source files, the index would not need
487 * to be updated. The FSMonitor response (after the build and
488 * relative to a pre-build token) might be 5MB. Each subsequent Git
489 * command will receive that same 100K/5MB response until something
490 * causes the index to be updated. And `refresh_fsmonitor()` will
491 * have to iterate over those 100K paths each time.
493 * Performance could be improved if we optionally force update the
494 * index after a very large response and get an updated token into
495 * the FSMonitor index extension. This should allow subsequent
496 * commands to get smaller and more current responses.
498 * The value chosen here does not need to be precise. The index
499 * will be updated automatically the first time the user touches
500 * a tracked file and causes a command like `git status` to
501 * update an mtime to be updated and/or set a flag bit.
503 static int fsmonitor_force_update_threshold = 100;
505 void refresh_fsmonitor(struct index_state *istate)
507 static int warn_once = 0;
508 struct strbuf query_result = STRBUF_INIT;
509 int query_success = 0, hook_version = -1;
510 size_t bol = 0; /* beginning of line */
511 uint64_t last_update;
512 struct strbuf last_update_token = STRBUF_INIT;
513 char *buf;
514 unsigned int i;
515 int is_trivial = 0;
516 struct repository *r = istate->repo;
517 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
518 enum fsmonitor_reason reason = fsm_settings__get_reason(r);
520 if (!warn_once && reason > FSMONITOR_REASON_OK) {
521 char *msg = fsm_settings__get_incompatible_msg(r, reason);
522 warn_once = 1;
523 warning("%s", msg);
524 free(msg);
527 if (fsm_mode <= FSMONITOR_MODE_DISABLED ||
528 istate->fsmonitor_has_run_once)
529 return;
531 istate->fsmonitor_has_run_once = 1;
533 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
535 if (fsm_mode == FSMONITOR_MODE_IPC) {
536 query_success = !fsmonitor_ipc__send_query(
537 istate->fsmonitor_last_update ?
538 istate->fsmonitor_last_update : "builtin:fake",
539 &query_result);
540 if (query_success) {
542 * The response contains a series of nul terminated
543 * strings. The first is the new token.
545 * Use `char *buf` as an interlude to trick the CI
546 * static analysis to let us use `strbuf_addstr()`
547 * here (and only copy the token) rather than
548 * `strbuf_addbuf()`.
550 buf = query_result.buf;
551 strbuf_addstr(&last_update_token, buf);
552 bol = last_update_token.len + 1;
553 is_trivial = query_result.buf[bol] == '/';
554 if (is_trivial)
555 trace2_data_intmax("fsm_client", NULL,
556 "query/trivial-response", 1);
557 } else {
559 * The builtin daemon is not available on this
560 * platform -OR- we failed to get a response.
562 * Generate a fake token (rather than a V1
563 * timestamp) for the index extension. (If
564 * they switch back to the hook API, we don't
565 * want ambiguous state.)
567 strbuf_addstr(&last_update_token, "builtin:fake");
570 goto apply_results;
573 assert(fsm_mode == FSMONITOR_MODE_HOOK);
575 hook_version = fsmonitor_hook_version();
578 * This could be racy so save the date/time now and query_fsmonitor_hook
579 * should be inclusive to ensure we don't miss potential changes.
581 last_update = getnanotime();
582 if (hook_version == HOOK_INTERFACE_VERSION1)
583 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
586 * If we have a last update token, call query_fsmonitor_hook for the set of
587 * changes since that token, else assume everything is possibly dirty
588 * and check it all.
590 if (istate->fsmonitor_last_update) {
591 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
592 query_success = !query_fsmonitor_hook(
593 r, HOOK_INTERFACE_VERSION2,
594 istate->fsmonitor_last_update, &query_result);
596 if (query_success) {
597 if (hook_version < 0)
598 hook_version = HOOK_INTERFACE_VERSION2;
601 * First entry will be the last update token
602 * Need to use a char * variable because static
603 * analysis was suggesting to use strbuf_addbuf
604 * but we don't want to copy the entire strbuf
605 * only the chars up to the first NUL
607 buf = query_result.buf;
608 strbuf_addstr(&last_update_token, buf);
609 if (!last_update_token.len) {
610 warning("Empty last update token.");
611 query_success = 0;
612 } else {
613 bol = last_update_token.len + 1;
614 is_trivial = query_result.buf[bol] == '/';
616 } else if (hook_version < 0) {
617 hook_version = HOOK_INTERFACE_VERSION1;
618 if (!last_update_token.len)
619 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
623 if (hook_version == HOOK_INTERFACE_VERSION1) {
624 query_success = !query_fsmonitor_hook(
625 r, HOOK_INTERFACE_VERSION1,
626 istate->fsmonitor_last_update, &query_result);
627 if (query_success)
628 is_trivial = query_result.buf[0] == '/';
631 if (is_trivial)
632 trace2_data_intmax("fsm_hook", NULL,
633 "query/trivial-response", 1);
635 trace_performance_since(last_update, "fsmonitor process '%s'",
636 fsm_settings__get_hook_path(r));
637 trace_printf_key(&trace_fsmonitor,
638 "fsmonitor process '%s' returned %s",
639 fsm_settings__get_hook_path(r),
640 query_success ? "success" : "failure");
643 apply_results:
645 * The response from FSMonitor (excluding the header token) is
646 * either:
648 * [a] a (possibly empty) list of NUL delimited relative
649 * pathnames of changed paths. This list can contain
650 * files and directories. Directories have a trailing
651 * slash.
653 * [b] a single '/' to indicate the provider had no
654 * information and that we should consider everything
655 * invalid. We call this a trivial response.
657 trace2_region_enter("fsmonitor", "apply_results", istate->repo);
659 if (query_success && !is_trivial) {
661 * Mark all pathnames returned by the monitor as dirty.
663 * This updates both the cache-entries and the untracked-cache.
665 int count = 0;
667 buf = query_result.buf;
668 for (i = bol; i < query_result.len; i++) {
669 if (buf[i] != '\0')
670 continue;
671 fsmonitor_refresh_callback(istate, buf + bol);
672 bol = i + 1;
673 count++;
675 if (bol < query_result.len) {
676 fsmonitor_refresh_callback(istate, buf + bol);
677 count++;
680 /* Now mark the untracked cache for fsmonitor usage */
681 if (istate->untracked)
682 istate->untracked->use_fsmonitor = 1;
684 if (count > fsmonitor_force_update_threshold)
685 istate->cache_changed |= FSMONITOR_CHANGED;
687 trace2_data_intmax("fsmonitor", istate->repo, "apply_count",
688 count);
690 } else {
692 * We failed to get a response or received a trivial response,
693 * so invalidate everything.
695 * We only want to run the post index changed hook if
696 * we've actually changed entries, so keep track if we
697 * actually changed entries or not.
699 int is_cache_changed = 0;
701 for (i = 0; i < istate->cache_nr; i++) {
702 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
703 is_cache_changed = 1;
704 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
709 * If we're going to check every file, ensure we save
710 * the results.
712 if (is_cache_changed)
713 istate->cache_changed |= FSMONITOR_CHANGED;
715 if (istate->untracked)
716 istate->untracked->use_fsmonitor = 0;
718 trace2_region_leave("fsmonitor", "apply_results", istate->repo);
720 strbuf_release(&query_result);
722 /* Now that we've updated istate, save the last_update_token */
723 FREE_AND_NULL(istate->fsmonitor_last_update);
724 istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
728 * The caller wants to turn on FSMonitor. And when the caller writes
729 * the index to disk, a FSMonitor extension should be included. This
730 * requires that `istate->fsmonitor_last_update` not be NULL. But we
731 * have not actually talked to a FSMonitor process yet, so we don't
732 * have an initial value for this field.
734 * For a protocol V1 FSMonitor process, this field is a formatted
735 * "nanoseconds since epoch" field. However, for a protocol V2
736 * FSMonitor process, this field is an opaque token.
738 * Historically, `add_fsmonitor()` has initialized this field to the
739 * current time for protocol V1 processes. There are lots of race
740 * conditions here, but that code has shipped...
742 * The only true solution is to use a V2 FSMonitor and get a current
743 * or default token value (that it understands), but we cannot do that
744 * until we have actually talked to an instance of the FSMonitor process
745 * (but the protocol requires that we send a token first...).
747 * For simplicity, just initialize like we have a V1 process and require
748 * that V2 processes adapt.
750 static void initialize_fsmonitor_last_update(struct index_state *istate)
752 struct strbuf last_update = STRBUF_INIT;
754 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
755 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
758 void add_fsmonitor(struct index_state *istate)
760 unsigned int i;
762 if (!istate->fsmonitor_last_update) {
763 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
764 istate->cache_changed |= FSMONITOR_CHANGED;
765 initialize_fsmonitor_last_update(istate);
767 /* reset the fsmonitor state */
768 for (i = 0; i < istate->cache_nr; i++)
769 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
771 /* reset the untracked cache */
772 if (istate->untracked) {
773 add_untracked_cache(istate);
774 istate->untracked->use_fsmonitor = 1;
777 /* Update the fsmonitor state */
778 refresh_fsmonitor(istate);
782 void remove_fsmonitor(struct index_state *istate)
784 if (istate->fsmonitor_last_update) {
785 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
786 istate->cache_changed |= FSMONITOR_CHANGED;
787 FREE_AND_NULL(istate->fsmonitor_last_update);
791 void tweak_fsmonitor(struct index_state *istate)
793 unsigned int i;
794 int fsmonitor_enabled = (fsm_settings__get_mode(istate->repo)
795 > FSMONITOR_MODE_DISABLED);
797 if (istate->fsmonitor_dirty) {
798 if (fsmonitor_enabled) {
799 /* Mark all entries valid */
800 for (i = 0; i < istate->cache_nr; i++) {
801 if (S_ISGITLINK(istate->cache[i]->ce_mode))
802 continue;
803 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
806 /* Mark all previously saved entries as dirty */
807 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
808 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
810 refresh_fsmonitor(istate);
813 ewah_free(istate->fsmonitor_dirty);
814 istate->fsmonitor_dirty = NULL;
817 if (fsmonitor_enabled)
818 add_fsmonitor(istate);
819 else
820 remove_fsmonitor(istate);