6 #include "run-command.h"
9 #define INDEX_EXTENSION_VERSION1 (1)
10 #define INDEX_EXTENSION_VERSION2 (2)
11 #define HOOK_INTERFACE_VERSION1 (1)
12 #define HOOK_INTERFACE_VERSION2 (2)
14 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
16 static void fsmonitor_ewah_callback(size_t pos, void *is)
18 struct index_state *istate = (struct index_state *)is;
19 struct cache_entry *ce;
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);
25 ce = istate->cache[pos];
26 ce->ce_flags &= ~CE_FSMONITOR_VALID;
29 static int fsmonitor_hook_version(void)
33 if (git_config_get_int("core.fsmonitorhookversion", &hook_version))
36 if (hook_version == HOOK_INTERFACE_VERSION1 ||
37 hook_version == HOOK_INTERFACE_VERSION2)
40 warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
41 "Must be 1 or 2.", hook_version);
45 int read_fsmonitor_extension(struct index_state *istate, const void *data,
48 const char *index = data;
51 struct ewah_bitmap *fsmonitor_dirty;
54 struct strbuf last_update = STRBUF_INIT;
56 if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
57 return error("corrupt fsmonitor extension (too short)");
59 hdr_version = get_be32(index);
60 index += sizeof(uint32_t);
61 if (hdr_version == INDEX_EXTENSION_VERSION1) {
62 timestamp = get_be64(index);
63 strbuf_addf(&last_update, "%"PRIu64"", timestamp);
64 index += sizeof(uint64_t);
65 } else if (hdr_version == INDEX_EXTENSION_VERSION2) {
66 strbuf_addstr(&last_update, index);
67 index += last_update.len + 1;
69 return error("bad fsmonitor version %d", hdr_version);
72 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
74 ewah_size = get_be32(index);
75 index += sizeof(uint32_t);
77 fsmonitor_dirty = ewah_new();
78 ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
79 if (ret != ewah_size) {
80 ewah_free(fsmonitor_dirty);
81 return error("failed to parse ewah bitmap reading fsmonitor index extension");
83 istate->fsmonitor_dirty = fsmonitor_dirty;
85 if (!istate->split_index &&
86 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
87 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
88 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
90 trace2_data_string("index", NULL, "extension/fsmn/read/token",
91 istate->fsmonitor_last_update);
92 trace_printf_key(&trace_fsmonitor,
93 "read fsmonitor extension successful '%s'",
94 istate->fsmonitor_last_update);
98 void fill_fsmonitor_bitmap(struct index_state *istate)
100 unsigned int i, skipped = 0;
101 istate->fsmonitor_dirty = ewah_new();
102 for (i = 0; i < istate->cache_nr; i++) {
103 if (istate->cache[i]->ce_flags & CE_REMOVE)
105 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
106 ewah_set(istate->fsmonitor_dirty, i - skipped);
110 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
112 uint32_t hdr_version;
114 uint32_t ewah_size = 0;
117 if (!istate->split_index &&
118 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
119 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
120 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
122 put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
123 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
125 strbuf_addstr(sb, istate->fsmonitor_last_update);
126 strbuf_addch(sb, 0); /* Want to keep a NUL */
129 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
131 ewah_start = sb->len;
132 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
133 ewah_free(istate->fsmonitor_dirty);
134 istate->fsmonitor_dirty = NULL;
136 /* fix up size field */
137 put_be32(&ewah_size, sb->len - ewah_start);
138 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
140 trace2_data_string("index", NULL, "extension/fsmn/write/token",
141 istate->fsmonitor_last_update);
142 trace_printf_key(&trace_fsmonitor,
143 "write fsmonitor extension successful '%s'",
144 istate->fsmonitor_last_update);
148 * Call the query-fsmonitor hook passing the last update token of the saved results.
150 static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
152 struct child_process cp = CHILD_PROCESS_INIT;
158 strvec_push(&cp.args, core_fsmonitor);
159 strvec_pushf(&cp.args, "%d", version);
160 strvec_pushf(&cp.args, "%s", last_update);
162 cp.dir = get_git_work_tree();
164 trace2_region_enter("fsm_hook", "query", NULL);
166 result = capture_command(&cp, query_result, 1024);
169 trace2_data_intmax("fsm_hook", NULL, "query/failed", result);
171 trace2_data_intmax("fsm_hook", NULL, "query/response-length",
174 if (fsmonitor_is_trivial_response(query_result))
175 trace2_data_intmax("fsm_hook", NULL,
176 "query/trivial-response", 1);
179 trace2_region_leave("fsm_hook", "query", NULL);
184 int fsmonitor_is_trivial_response(const struct strbuf *query_result)
186 static char trivial_response[3] = { '\0', '/', '\0' };
187 int is_trivial = !memcmp(trivial_response,
188 &query_result->buf[query_result->len - 3], 3);
193 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
195 int i, len = strlen(name);
196 if (name[len - 1] == '/') {
199 * TODO We should binary search to find the first path with
200 * TODO this directory prefix. Then linearly update entries
201 * TODO while the prefix matches. Taking care to search without
202 * TODO the trailing slash -- because '/' sorts after a few
203 * TODO interesting special chars, like '.' and ' '.
206 /* Mark all entries for the folder invalid */
207 for (i = 0; i < istate->cache_nr; i++) {
208 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID &&
209 starts_with(istate->cache[i]->name, name))
210 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
212 /* Need to remove the / from the path for the untracked cache */
213 name[len - 1] = '\0';
215 int pos = index_name_pos(istate, name, strlen(name));
218 struct cache_entry *ce = istate->cache[pos];
219 ce->ce_flags &= ~CE_FSMONITOR_VALID;
224 * Mark the untracked cache dirty even if it wasn't found in the index
225 * as it could be a new untracked file.
227 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
228 untracked_cache_invalidate_path(istate, name, 0);
231 void refresh_fsmonitor(struct index_state *istate)
233 struct strbuf query_result = STRBUF_INIT;
234 int query_success = 0, hook_version = -1;
235 size_t bol = 0; /* beginning of line */
236 uint64_t last_update;
237 struct strbuf last_update_token = STRBUF_INIT;
241 if (!core_fsmonitor || istate->fsmonitor_has_run_once)
244 hook_version = fsmonitor_hook_version();
246 istate->fsmonitor_has_run_once = 1;
248 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
250 * This could be racy so save the date/time now and query_fsmonitor
251 * should be inclusive to ensure we don't miss potential changes.
253 last_update = getnanotime();
254 if (hook_version == HOOK_INTERFACE_VERSION1)
255 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
258 * If we have a last update token, call query_fsmonitor for the set of
259 * changes since that token, else assume everything is possibly dirty
262 if (istate->fsmonitor_last_update) {
263 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
264 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION2,
265 istate->fsmonitor_last_update, &query_result);
268 if (hook_version < 0)
269 hook_version = HOOK_INTERFACE_VERSION2;
272 * First entry will be the last update token
273 * Need to use a char * variable because static
274 * analysis was suggesting to use strbuf_addbuf
275 * but we don't want to copy the entire strbuf
276 * only the chars up to the first NUL
278 buf = query_result.buf;
279 strbuf_addstr(&last_update_token, buf);
280 if (!last_update_token.len) {
281 warning("Empty last update token.");
284 bol = last_update_token.len + 1;
286 } else if (hook_version < 0) {
287 hook_version = HOOK_INTERFACE_VERSION1;
288 if (!last_update_token.len)
289 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
293 if (hook_version == HOOK_INTERFACE_VERSION1) {
294 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION1,
295 istate->fsmonitor_last_update, &query_result);
298 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
299 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
300 core_fsmonitor, query_success ? "success" : "failure");
303 /* a fsmonitor process can return '/' to indicate all entries are invalid */
304 if (query_success && query_result.buf[bol] != '/') {
305 /* Mark all entries returned by the monitor as dirty */
306 buf = query_result.buf;
307 for (i = bol; i < query_result.len; i++) {
310 fsmonitor_refresh_callback(istate, buf + bol);
313 if (bol < query_result.len)
314 fsmonitor_refresh_callback(istate, buf + bol);
316 /* Now mark the untracked cache for fsmonitor usage */
317 if (istate->untracked)
318 istate->untracked->use_fsmonitor = 1;
321 /* We only want to run the post index changed hook if we've actually changed entries, so keep track
322 * if we actually changed entries or not */
323 int is_cache_changed = 0;
324 /* Mark all entries invalid */
325 for (i = 0; i < istate->cache_nr; i++) {
326 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
327 is_cache_changed = 1;
328 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
332 /* If we're going to check every file, ensure we save the results */
333 if (is_cache_changed)
334 istate->cache_changed |= FSMONITOR_CHANGED;
336 if (istate->untracked)
337 istate->untracked->use_fsmonitor = 0;
339 strbuf_release(&query_result);
341 /* Now that we've updated istate, save the last_update_token */
342 FREE_AND_NULL(istate->fsmonitor_last_update);
343 istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
346 void add_fsmonitor(struct index_state *istate)
349 struct strbuf last_update = STRBUF_INIT;
351 if (!istate->fsmonitor_last_update) {
352 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
353 istate->cache_changed |= FSMONITOR_CHANGED;
354 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
355 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
357 /* reset the fsmonitor state */
358 for (i = 0; i < istate->cache_nr; i++)
359 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
361 /* reset the untracked cache */
362 if (istate->untracked) {
363 add_untracked_cache(istate);
364 istate->untracked->use_fsmonitor = 1;
367 /* Update the fsmonitor state */
368 refresh_fsmonitor(istate);
372 void remove_fsmonitor(struct index_state *istate)
374 if (istate->fsmonitor_last_update) {
375 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
376 istate->cache_changed |= FSMONITOR_CHANGED;
377 FREE_AND_NULL(istate->fsmonitor_last_update);
381 void tweak_fsmonitor(struct index_state *istate)
384 int fsmonitor_enabled = git_config_get_fsmonitor();
386 if (istate->fsmonitor_dirty) {
387 if (fsmonitor_enabled) {
388 /* Mark all entries valid */
389 for (i = 0; i < istate->cache_nr; i++) {
390 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
393 /* Mark all previously saved entries as dirty */
394 if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
395 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
396 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
397 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
399 refresh_fsmonitor(istate);
402 ewah_free(istate->fsmonitor_dirty);
403 istate->fsmonitor_dirty = NULL;
406 switch (fsmonitor_enabled) {
407 case -1: /* keep: do nothing */
410 remove_fsmonitor(istate);
413 add_fsmonitor(istate);
415 default: /* unknown value: do nothing */