blob: 67faf592e1277a30c5ea517a58961430d6cea350 [file] [log] [blame]
Ben Peart883e2482017-09-22 12:35:40 -04001#ifndef FSMONITOR_H
2#define FSMONITOR_H
3
Elijah Newrenef3ca952018-08-15 10:54:05 -07004#include "cache.h"
5#include "dir.h"
Jeff Hostetler1e0ea5c2022-03-25 18:02:46 +00006#include "fsmonitor-settings.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +00007#include "trace.h"
Elijah Newrenef3ca952018-08-15 10:54:05 -07008
Ben Peart883e2482017-09-22 12:35:40 -04009extern struct trace_key trace_fsmonitor;
10
11/*
12 * Read the fsmonitor index extension and (if configured) restore the
13 * CE_FSMONITOR_VALID state.
14 */
Denton Liu55454422019-04-29 04:28:14 -040015int read_fsmonitor_extension(struct index_state *istate, const void *data, unsigned long sz);
Ben Peart883e2482017-09-22 12:35:40 -040016
17/*
Alex Vandiver3bd28eb2017-11-09 11:58:10 -080018 * Fill the fsmonitor_dirty ewah bits with their state from the index,
19 * before it is split during writing.
20 */
Denton Liu55454422019-04-29 04:28:14 -040021void fill_fsmonitor_bitmap(struct index_state *istate);
Alex Vandiver3bd28eb2017-11-09 11:58:10 -080022
23/*
24 * Write the CE_FSMONITOR_VALID state into the fsmonitor index
25 * extension. Reads from the fsmonitor_dirty ewah in the index.
Ben Peart883e2482017-09-22 12:35:40 -040026 */
Denton Liu55454422019-04-29 04:28:14 -040027void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate);
Ben Peart883e2482017-09-22 12:35:40 -040028
29/*
30 * Add/remove the fsmonitor index extension
31 */
Denton Liu55454422019-04-29 04:28:14 -040032void add_fsmonitor(struct index_state *istate);
33void remove_fsmonitor(struct index_state *istate);
Ben Peart883e2482017-09-22 12:35:40 -040034
35/*
36 * Add/remove the fsmonitor index extension as necessary based on the current
37 * core.fsmonitor setting.
38 */
Denton Liu55454422019-04-29 04:28:14 -040039void tweak_fsmonitor(struct index_state *istate);
Ben Peart883e2482017-09-22 12:35:40 -040040
41/*
42 * Run the configured fsmonitor integration script and clear the
43 * CE_FSMONITOR_VALID bit for any files returned as dirty. Also invalidate
44 * any corresponding untracked cache directory structures. Optimized to only
45 * run the first time it is called.
46 */
Denton Liu55454422019-04-29 04:28:14 -040047void refresh_fsmonitor(struct index_state *istate);
Ben Peart883e2482017-09-22 12:35:40 -040048
49/*
Jeff Hostetler940b94f2021-02-03 15:34:47 +000050 * Does the received result contain the "trivial" response?
51 */
52int fsmonitor_is_trivial_response(const struct strbuf *query_result);
53
54/*
Nipunn Koorapati0ec99492021-03-17 21:22:22 +000055 * Check if refresh_fsmonitor has been called at least once.
56 * refresh_fsmonitor is idempotent. Returns true if fsmonitor is
57 * not enabled (since the state will be "fresh" w/ CE_FSMONITOR_VALID unset)
58 * This version is useful for assertions
59 */
60static inline int is_fsmonitor_refreshed(const struct index_state *istate)
61{
Jeff Hostetler1e0ea5c2022-03-25 18:02:46 +000062 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(istate->repo);
63
64 return fsm_mode <= FSMONITOR_MODE_DISABLED ||
65 istate->fsmonitor_has_run_once;
Nipunn Koorapati0ec99492021-03-17 21:22:22 +000066}
67
68/*
Ben Peart883e2482017-09-22 12:35:40 -040069 * Set the given cache entries CE_FSMONITOR_VALID bit. This should be
70 * called any time the cache entry has been updated to reflect the
71 * current state of the file on disk.
Jeff Hostetlerf954c7b2022-05-26 21:47:17 +000072 *
73 * However, never mark submodules as valid. When commands like "git
74 * status" run they might need to recurse into the submodule (using a
75 * child process) to get a summary of the submodule state. We don't
76 * have (and don't want to create) the facility to translate every
77 * FS event that we receive and that happens to be deep inside of a
78 * submodule back to the submodule root, so we cannot correctly keep
79 * track of this bit on the gitlink directory. Therefore, we never
80 * set it on submodules.
Ben Peart883e2482017-09-22 12:35:40 -040081 */
Johannes Schindelinb5a81692019-05-24 05:23:48 -070082static inline void mark_fsmonitor_valid(struct index_state *istate, struct cache_entry *ce)
Ben Peart883e2482017-09-22 12:35:40 -040083{
Jeff Hostetler1e0ea5c2022-03-25 18:02:46 +000084 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(istate->repo);
85
86 if (fsm_mode > FSMONITOR_MODE_DISABLED &&
87 !(ce->ce_flags & CE_FSMONITOR_VALID)) {
Jeff Hostetlerf954c7b2022-05-26 21:47:17 +000088 if (S_ISGITLINK(ce->ce_mode))
89 return;
Johannes Schindelinb5a81692019-05-24 05:23:48 -070090 istate->cache_changed = 1;
Ben Peart883e2482017-09-22 12:35:40 -040091 ce->ce_flags |= CE_FSMONITOR_VALID;
92 trace_printf_key(&trace_fsmonitor, "mark_fsmonitor_clean '%s'", ce->name);
93 }
94}
95
96/*
97 * Clear the given cache entry's CE_FSMONITOR_VALID bit and invalidate
98 * any corresponding untracked cache directory structures. This should
99 * be called any time git creates or modifies a file that should
100 * trigger an lstat() or invalidate the untracked cache for the
101 * corresponding directory
102 */
103static inline void mark_fsmonitor_invalid(struct index_state *istate, struct cache_entry *ce)
104{
Jeff Hostetler1e0ea5c2022-03-25 18:02:46 +0000105 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(istate->repo);
106
107 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
Ben Peart883e2482017-09-22 12:35:40 -0400108 ce->ce_flags &= ~CE_FSMONITOR_VALID;
Nguyễn Thái Ngọc Duy0cacebf2018-02-07 16:21:40 +0700109 untracked_cache_invalidate_path(istate, ce->name, 1);
Ben Peart883e2482017-09-22 12:35:40 -0400110 trace_printf_key(&trace_fsmonitor, "mark_fsmonitor_invalid '%s'", ce->name);
111 }
112}
113
114#endif