6 #include "run-command.h"
9 #define INDEX_EXTENSION_VERSION1 (1)
10 #define INDEX_EXTENSION_VERSION2 (2)
11 #define HOOK_INTERFACE_VERSION (1)
13 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
15 static void fsmonitor_ewah_callback(size_t pos, void *is)
17 struct index_state *istate = (struct index_state *)is;
18 struct cache_entry *ce;
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);
24 ce = istate->cache[pos];
25 ce->ce_flags &= ~CE_FSMONITOR_VALID;
28 int read_fsmonitor_extension(struct index_state *istate, const void *data,
31 const char *index = data;
34 struct ewah_bitmap *fsmonitor_dirty;
37 struct strbuf last_update = STRBUF_INIT;
39 if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
40 return error("corrupt fsmonitor extension (too short)");
42 hdr_version = get_be32(index);
43 index += sizeof(uint32_t);
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;
52 return error("bad fsmonitor version %d", hdr_version);
55 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
57 ewah_size = get_be32(index);
58 index += sizeof(uint32_t);
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");
66 istate->fsmonitor_dirty = fsmonitor_dirty;
68 if (!istate->split_index &&
69 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
70 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
71 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
73 trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
77 void fill_fsmonitor_bitmap(struct index_state *istate)
79 unsigned int i, skipped = 0;
80 istate->fsmonitor_dirty = ewah_new();
81 for (i = 0; i < istate->cache_nr; i++) {
82 if (istate->cache[i]->ce_flags & CE_REMOVE)
84 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
85 ewah_set(istate->fsmonitor_dirty, i - skipped);
89 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
93 uint32_t ewah_size = 0;
96 if (!istate->split_index &&
97 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
98 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
99 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
101 put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
102 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
104 strbuf_addstr(sb, istate->fsmonitor_last_update);
105 strbuf_addch(sb, 0); /* Want to keep a NUL */
108 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
110 ewah_start = sb->len;
111 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
112 ewah_free(istate->fsmonitor_dirty);
113 istate->fsmonitor_dirty = NULL;
115 /* fix up size field */
116 put_be32(&ewah_size, sb->len - ewah_start);
117 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
119 trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
123 * Call the query-fsmonitor hook passing the last update token of the saved results.
125 static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
127 struct child_process cp = CHILD_PROCESS_INIT;
132 argv_array_push(&cp.args, core_fsmonitor);
133 argv_array_pushf(&cp.args, "%d", version);
134 argv_array_pushf(&cp.args, "%s", last_update);
136 cp.dir = get_git_work_tree();
138 return capture_command(&cp, query_result, 1024);
141 static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
143 int pos = index_name_pos(istate, name, strlen(name));
146 struct cache_entry *ce = istate->cache[pos];
147 ce->ce_flags &= ~CE_FSMONITOR_VALID;
151 * Mark the untracked cache dirty even if it wasn't found in the index
152 * as it could be a new untracked file.
154 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
155 untracked_cache_invalidate_path(istate, name, 0);
158 void refresh_fsmonitor(struct index_state *istate)
160 struct strbuf query_result = STRBUF_INIT;
161 int query_success = 0;
162 size_t bol; /* beginning of line */
163 uint64_t last_update;
164 struct strbuf last_update_token = STRBUF_INIT;
168 if (!core_fsmonitor || istate->fsmonitor_has_run_once)
170 istate->fsmonitor_has_run_once = 1;
172 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
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.
177 last_update = getnanotime();
178 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
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
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");
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;
198 for (i = 0; i < query_result.len; i++) {
201 fsmonitor_refresh_callback(istate, buf + bol);
204 if (bol < query_result.len)
205 fsmonitor_refresh_callback(istate, buf + bol);
207 /* Now mark the untracked cache for fsmonitor usage */
208 if (istate->untracked)
209 istate->untracked->use_fsmonitor = 1;
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;
215 /* Mark all entries invalid */
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;
223 /* If we're going to check every file, ensure we save the results */
224 if (is_cache_changed)
225 istate->cache_changed |= FSMONITOR_CHANGED;
227 if (istate->untracked)
228 istate->untracked->use_fsmonitor = 0;
230 strbuf_release(&query_result);
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);
237 void add_fsmonitor(struct index_state *istate)
240 struct strbuf last_update = STRBUF_INIT;
242 if (!istate->fsmonitor_last_update) {
243 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
244 istate->cache_changed |= FSMONITOR_CHANGED;
245 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
246 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
248 /* reset the fsmonitor state */
249 for (i = 0; i < istate->cache_nr; i++)
250 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
252 /* reset the untracked cache */
253 if (istate->untracked) {
254 add_untracked_cache(istate);
255 istate->untracked->use_fsmonitor = 1;
258 /* Update the fsmonitor state */
259 refresh_fsmonitor(istate);
263 void remove_fsmonitor(struct index_state *istate)
265 if (istate->fsmonitor_last_update) {
266 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
267 istate->cache_changed |= FSMONITOR_CHANGED;
268 FREE_AND_NULL(istate->fsmonitor_last_update);
272 void tweak_fsmonitor(struct index_state *istate)
275 int fsmonitor_enabled = git_config_get_fsmonitor();
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;
284 /* Mark all previously saved entries as dirty */
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);
288 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
290 refresh_fsmonitor(istate);
293 ewah_free(istate->fsmonitor_dirty);
294 istate->fsmonitor_dirty = NULL;
297 switch (fsmonitor_enabled) {
298 case -1: /* keep: do nothing */
301 remove_fsmonitor(istate);
304 add_fsmonitor(istate);
306 default: /* unknown value: do nothing */