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 assert_index_minimum(struct index_state
*istate
, size_t pos
)
18 if (pos
> istate
->cache_nr
)
19 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX
" > %u)",
20 (uintmax_t)pos
, istate
->cache_nr
);
23 static void fsmonitor_ewah_callback(size_t pos
, void *is
)
25 struct index_state
*istate
= (struct index_state
*)is
;
26 struct cache_entry
*ce
;
28 assert_index_minimum(istate
, pos
+ 1);
30 ce
= istate
->cache
[pos
];
31 ce
->ce_flags
&= ~CE_FSMONITOR_VALID
;
34 static int fsmonitor_hook_version(void)
38 if (git_config_get_int("core.fsmonitorhookversion", &hook_version
))
41 if (hook_version
== HOOK_INTERFACE_VERSION1
||
42 hook_version
== HOOK_INTERFACE_VERSION2
)
45 warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
46 "Must be 1 or 2.", hook_version
);
50 int read_fsmonitor_extension(struct index_state
*istate
, const void *data
,
53 const char *index
= data
;
56 struct ewah_bitmap
*fsmonitor_dirty
;
59 struct strbuf last_update
= STRBUF_INIT
;
61 if (sz
< sizeof(uint32_t) + 1 + sizeof(uint32_t))
62 return error("corrupt fsmonitor extension (too short)");
64 hdr_version
= get_be32(index
);
65 index
+= sizeof(uint32_t);
66 if (hdr_version
== INDEX_EXTENSION_VERSION1
) {
67 timestamp
= get_be64(index
);
68 strbuf_addf(&last_update
, "%"PRIu64
"", timestamp
);
69 index
+= sizeof(uint64_t);
70 } else if (hdr_version
== INDEX_EXTENSION_VERSION2
) {
71 strbuf_addstr(&last_update
, index
);
72 index
+= last_update
.len
+ 1;
74 return error("bad fsmonitor version %d", hdr_version
);
77 istate
->fsmonitor_last_update
= strbuf_detach(&last_update
, NULL
);
79 ewah_size
= get_be32(index
);
80 index
+= sizeof(uint32_t);
82 fsmonitor_dirty
= ewah_new();
83 ret
= ewah_read_mmap(fsmonitor_dirty
, index
, ewah_size
);
84 if (ret
!= ewah_size
) {
85 ewah_free(fsmonitor_dirty
);
86 return error("failed to parse ewah bitmap reading fsmonitor index extension");
88 istate
->fsmonitor_dirty
= fsmonitor_dirty
;
90 if (!istate
->split_index
)
91 assert_index_minimum(istate
, istate
->fsmonitor_dirty
->bit_size
);
93 trace_printf_key(&trace_fsmonitor
, "read fsmonitor extension successful");
97 void fill_fsmonitor_bitmap(struct index_state
*istate
)
99 unsigned int i
, skipped
= 0;
100 istate
->fsmonitor_dirty
= ewah_new();
101 for (i
= 0; i
< istate
->cache_nr
; i
++) {
102 if (istate
->cache
[i
]->ce_flags
& CE_REMOVE
)
104 else if (!(istate
->cache
[i
]->ce_flags
& CE_FSMONITOR_VALID
))
105 ewah_set(istate
->fsmonitor_dirty
, i
- skipped
);
109 void write_fsmonitor_extension(struct strbuf
*sb
, struct index_state
*istate
)
111 uint32_t hdr_version
;
113 uint32_t ewah_size
= 0;
116 if (!istate
->split_index
)
117 assert_index_minimum(istate
, istate
->fsmonitor_dirty
->bit_size
);
119 put_be32(&hdr_version
, INDEX_EXTENSION_VERSION2
);
120 strbuf_add(sb
, &hdr_version
, sizeof(uint32_t));
122 strbuf_addstr(sb
, istate
->fsmonitor_last_update
);
123 strbuf_addch(sb
, 0); /* Want to keep a NUL */
126 strbuf_add(sb
, &ewah_size
, sizeof(uint32_t)); /* we'll fix this up later */
128 ewah_start
= sb
->len
;
129 ewah_serialize_strbuf(istate
->fsmonitor_dirty
, sb
);
130 ewah_free(istate
->fsmonitor_dirty
);
131 istate
->fsmonitor_dirty
= NULL
;
133 /* fix up size field */
134 put_be32(&ewah_size
, sb
->len
- ewah_start
);
135 memcpy(sb
->buf
+ fixup
, &ewah_size
, sizeof(uint32_t));
137 trace_printf_key(&trace_fsmonitor
, "write fsmonitor extension successful");
141 * Call the query-fsmonitor hook passing the last update token of the saved results.
143 static int query_fsmonitor(int version
, const char *last_update
, struct strbuf
*query_result
)
145 struct child_process cp
= CHILD_PROCESS_INIT
;
150 strvec_push(&cp
.args
, core_fsmonitor
);
151 strvec_pushf(&cp
.args
, "%d", version
);
152 strvec_pushf(&cp
.args
, "%s", last_update
);
154 cp
.dir
= get_git_work_tree();
156 return capture_command(&cp
, query_result
, 1024);
159 static void fsmonitor_refresh_callback(struct index_state
*istate
, const char *name
)
161 int pos
= index_name_pos(istate
, name
, strlen(name
));
164 struct cache_entry
*ce
= istate
->cache
[pos
];
165 ce
->ce_flags
&= ~CE_FSMONITOR_VALID
;
169 * Mark the untracked cache dirty even if it wasn't found in the index
170 * as it could be a new untracked file.
172 trace_printf_key(&trace_fsmonitor
, "fsmonitor_refresh_callback '%s'", name
);
173 untracked_cache_invalidate_path(istate
, name
, 0);
176 void refresh_fsmonitor(struct index_state
*istate
)
178 struct strbuf query_result
= STRBUF_INIT
;
179 int query_success
= 0, hook_version
= -1;
180 size_t bol
= 0; /* beginning of line */
181 uint64_t last_update
;
182 struct strbuf last_update_token
= STRBUF_INIT
;
186 if (!core_fsmonitor
|| istate
->fsmonitor_has_run_once
)
189 hook_version
= fsmonitor_hook_version();
191 istate
->fsmonitor_has_run_once
= 1;
193 trace_printf_key(&trace_fsmonitor
, "refresh fsmonitor");
195 * This could be racy so save the date/time now and query_fsmonitor
196 * should be inclusive to ensure we don't miss potential changes.
198 last_update
= getnanotime();
199 if (hook_version
== HOOK_INTERFACE_VERSION1
)
200 strbuf_addf(&last_update_token
, "%"PRIu64
"", last_update
);
203 * If we have a last update token, call query_fsmonitor for the set of
204 * changes since that token, else assume everything is possibly dirty
207 if (istate
->fsmonitor_last_update
) {
208 if (hook_version
== -1 || hook_version
== HOOK_INTERFACE_VERSION2
) {
209 query_success
= !query_fsmonitor(HOOK_INTERFACE_VERSION2
,
210 istate
->fsmonitor_last_update
, &query_result
);
213 if (hook_version
< 0)
214 hook_version
= HOOK_INTERFACE_VERSION2
;
217 * First entry will be the last update token
218 * Need to use a char * variable because static
219 * analysis was suggesting to use strbuf_addbuf
220 * but we don't want to copy the entire strbuf
221 * only the chars up to the first NUL
223 buf
= query_result
.buf
;
224 strbuf_addstr(&last_update_token
, buf
);
225 if (!last_update_token
.len
) {
226 warning("Empty last update token.");
229 bol
= last_update_token
.len
+ 1;
231 } else if (hook_version
< 0) {
232 hook_version
= HOOK_INTERFACE_VERSION1
;
233 if (!last_update_token
.len
)
234 strbuf_addf(&last_update_token
, "%"PRIu64
"", last_update
);
238 if (hook_version
== HOOK_INTERFACE_VERSION1
) {
239 query_success
= !query_fsmonitor(HOOK_INTERFACE_VERSION1
,
240 istate
->fsmonitor_last_update
, &query_result
);
243 trace_performance_since(last_update
, "fsmonitor process '%s'", core_fsmonitor
);
244 trace_printf_key(&trace_fsmonitor
, "fsmonitor process '%s' returned %s",
245 core_fsmonitor
, query_success
? "success" : "failure");
248 /* a fsmonitor process can return '/' to indicate all entries are invalid */
249 if (query_success
&& query_result
.buf
[bol
] != '/') {
250 /* Mark all entries returned by the monitor as dirty */
251 buf
= query_result
.buf
;
252 for (i
= bol
; i
< query_result
.len
; i
++) {
255 fsmonitor_refresh_callback(istate
, buf
+ bol
);
258 if (bol
< query_result
.len
)
259 fsmonitor_refresh_callback(istate
, buf
+ bol
);
261 /* Now mark the untracked cache for fsmonitor usage */
262 if (istate
->untracked
)
263 istate
->untracked
->use_fsmonitor
= 1;
266 /* We only want to run the post index changed hook if we've actually changed entries, so keep track
267 * if we actually changed entries or not */
268 int is_cache_changed
= 0;
269 /* Mark all entries invalid */
270 for (i
= 0; i
< istate
->cache_nr
; i
++) {
271 if (istate
->cache
[i
]->ce_flags
& CE_FSMONITOR_VALID
) {
272 is_cache_changed
= 1;
273 istate
->cache
[i
]->ce_flags
&= ~CE_FSMONITOR_VALID
;
277 /* If we're going to check every file, ensure we save the results */
278 if (is_cache_changed
)
279 istate
->cache_changed
|= FSMONITOR_CHANGED
;
281 if (istate
->untracked
)
282 istate
->untracked
->use_fsmonitor
= 0;
284 strbuf_release(&query_result
);
286 /* Now that we've updated istate, save the last_update_token */
287 FREE_AND_NULL(istate
->fsmonitor_last_update
);
288 istate
->fsmonitor_last_update
= strbuf_detach(&last_update_token
, NULL
);
291 void add_fsmonitor(struct index_state
*istate
)
294 struct strbuf last_update
= STRBUF_INIT
;
296 if (!istate
->fsmonitor_last_update
) {
297 trace_printf_key(&trace_fsmonitor
, "add fsmonitor");
298 istate
->cache_changed
|= FSMONITOR_CHANGED
;
299 strbuf_addf(&last_update
, "%"PRIu64
"", getnanotime());
300 istate
->fsmonitor_last_update
= strbuf_detach(&last_update
, NULL
);
302 /* reset the fsmonitor state */
303 for (i
= 0; i
< istate
->cache_nr
; i
++)
304 istate
->cache
[i
]->ce_flags
&= ~CE_FSMONITOR_VALID
;
306 /* reset the untracked cache */
307 if (istate
->untracked
) {
308 add_untracked_cache(istate
);
309 istate
->untracked
->use_fsmonitor
= 1;
312 /* Update the fsmonitor state */
313 refresh_fsmonitor(istate
);
317 void remove_fsmonitor(struct index_state
*istate
)
319 if (istate
->fsmonitor_last_update
) {
320 trace_printf_key(&trace_fsmonitor
, "remove fsmonitor");
321 istate
->cache_changed
|= FSMONITOR_CHANGED
;
322 FREE_AND_NULL(istate
->fsmonitor_last_update
);
326 void tweak_fsmonitor(struct index_state
*istate
)
329 int fsmonitor_enabled
= git_config_get_fsmonitor();
331 if (istate
->fsmonitor_dirty
) {
332 if (fsmonitor_enabled
) {
333 /* Mark all entries valid */
334 for (i
= 0; i
< istate
->cache_nr
; i
++) {
335 istate
->cache
[i
]->ce_flags
|= CE_FSMONITOR_VALID
;
338 /* Mark all previously saved entries as dirty */
339 assert_index_minimum(istate
, istate
->fsmonitor_dirty
->bit_size
);
340 ewah_each_bit(istate
->fsmonitor_dirty
, fsmonitor_ewah_callback
, istate
);
342 refresh_fsmonitor(istate
);
345 ewah_free(istate
->fsmonitor_dirty
);
346 istate
->fsmonitor_dirty
= NULL
;
349 switch (fsmonitor_enabled
) {
350 case -1: /* keep: do nothing */
353 remove_fsmonitor(istate
);
356 add_fsmonitor(istate
);
358 default: /* unknown value: do nothing */