]>
Commit | Line | Data |
---|---|---|
883e248b BP |
1 | #include "cache.h" |
2 | #include "config.h" | |
3 | #include "dir.h" | |
4 | #include "ewah/ewok.h" | |
5 | #include "fsmonitor.h" | |
6 | #include "run-command.h" | |
7 | #include "strbuf.h" | |
8 | ||
56c69100 KW |
9 | #define INDEX_EXTENSION_VERSION1 (1) |
10 | #define INDEX_EXTENSION_VERSION2 (2) | |
8da2c576 KW |
11 | #define HOOK_INTERFACE_VERSION1 (1) |
12 | #define HOOK_INTERFACE_VERSION2 (2) | |
883e248b BP |
13 | |
14 | struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR); | |
15 | ||
16 | static void fsmonitor_ewah_callback(size_t pos, void *is) | |
17 | { | |
18 | struct index_state *istate = (struct index_state *)is; | |
3444ec2e | 19 | struct cache_entry *ce; |
883e248b | 20 | |
3444ec2e WB |
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); | |
24 | ||
25 | ce = istate->cache[pos]; | |
883e248b BP |
26 | ce->ce_flags &= ~CE_FSMONITOR_VALID; |
27 | } | |
28 | ||
8da2c576 KW |
29 | static int fsmonitor_hook_version(void) |
30 | { | |
31 | int hook_version; | |
32 | ||
33 | if (git_config_get_int("core.fsmonitorhookversion", &hook_version)) | |
34 | return -1; | |
35 | ||
36 | if (hook_version == HOOK_INTERFACE_VERSION1 || | |
37 | hook_version == HOOK_INTERFACE_VERSION2) | |
38 | return hook_version; | |
39 | ||
40 | warning("Invalid hook version '%i' in core.fsmonitorhookversion. " | |
41 | "Must be 1 or 2.", hook_version); | |
42 | return -1; | |
43 | } | |
44 | ||
883e248b BP |
45 | int read_fsmonitor_extension(struct index_state *istate, const void *data, |
46 | unsigned long sz) | |
47 | { | |
48 | const char *index = data; | |
49 | uint32_t hdr_version; | |
50 | uint32_t ewah_size; | |
51 | struct ewah_bitmap *fsmonitor_dirty; | |
883e248b | 52 | int ret; |
56c69100 KW |
53 | uint64_t timestamp; |
54 | struct strbuf last_update = STRBUF_INIT; | |
883e248b | 55 | |
56c69100 | 56 | if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t)) |
883e248b BP |
57 | return error("corrupt fsmonitor extension (too short)"); |
58 | ||
59 | hdr_version = get_be32(index); | |
60 | index += sizeof(uint32_t); | |
56c69100 KW |
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; | |
68 | } else { | |
883e248b | 69 | return error("bad fsmonitor version %d", hdr_version); |
56c69100 | 70 | } |
883e248b | 71 | |
56c69100 | 72 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); |
883e248b BP |
73 | |
74 | ewah_size = get_be32(index); | |
75 | index += sizeof(uint32_t); | |
76 | ||
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"); | |
82 | } | |
ba1b9cac | 83 | istate->fsmonitor_dirty = fsmonitor_dirty; |
883e248b | 84 | |
61eea521 JH |
85 | if (!istate->split_index && |
86 | istate->fsmonitor_dirty->bit_size > istate->cache_nr) | |
3444ec2e WB |
87 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", |
88 | (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr); | |
89 | ||
883e248b BP |
90 | trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful"); |
91 | return 0; | |
92 | } | |
93 | ||
3bd28eb2 AV |
94 | void fill_fsmonitor_bitmap(struct index_state *istate) |
95 | { | |
3444ec2e | 96 | unsigned int i, skipped = 0; |
3bd28eb2 | 97 | istate->fsmonitor_dirty = ewah_new(); |
3444ec2e WB |
98 | for (i = 0; i < istate->cache_nr; i++) { |
99 | if (istate->cache[i]->ce_flags & CE_REMOVE) | |
100 | skipped++; | |
101 | else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID)) | |
102 | ewah_set(istate->fsmonitor_dirty, i - skipped); | |
103 | } | |
3bd28eb2 AV |
104 | } |
105 | ||
883e248b BP |
106 | void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate) |
107 | { | |
108 | uint32_t hdr_version; | |
883e248b BP |
109 | uint32_t ewah_start; |
110 | uint32_t ewah_size = 0; | |
111 | int fixup = 0; | |
112 | ||
61eea521 JH |
113 | if (!istate->split_index && |
114 | istate->fsmonitor_dirty->bit_size > istate->cache_nr) | |
3444ec2e WB |
115 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", |
116 | (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr); | |
117 | ||
56c69100 | 118 | put_be32(&hdr_version, INDEX_EXTENSION_VERSION2); |
883e248b BP |
119 | strbuf_add(sb, &hdr_version, sizeof(uint32_t)); |
120 | ||
56c69100 KW |
121 | strbuf_addstr(sb, istate->fsmonitor_last_update); |
122 | strbuf_addch(sb, 0); /* Want to keep a NUL */ | |
123 | ||
883e248b BP |
124 | fixup = sb->len; |
125 | strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */ | |
126 | ||
127 | ewah_start = sb->len; | |
3bd28eb2 AV |
128 | ewah_serialize_strbuf(istate->fsmonitor_dirty, sb); |
129 | ewah_free(istate->fsmonitor_dirty); | |
130 | istate->fsmonitor_dirty = NULL; | |
883e248b BP |
131 | |
132 | /* fix up size field */ | |
133 | put_be32(&ewah_size, sb->len - ewah_start); | |
134 | memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t)); | |
135 | ||
136 | trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful"); | |
137 | } | |
138 | ||
139 | /* | |
56c69100 | 140 | * Call the query-fsmonitor hook passing the last update token of the saved results. |
883e248b | 141 | */ |
56c69100 | 142 | static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result) |
883e248b BP |
143 | { |
144 | struct child_process cp = CHILD_PROCESS_INIT; | |
883e248b | 145 | |
735e4173 | 146 | if (!core_fsmonitor) |
883e248b BP |
147 | return -1; |
148 | ||
ef8d7ac4 JK |
149 | strvec_push(&cp.args, core_fsmonitor); |
150 | strvec_pushf(&cp.args, "%d", version); | |
151 | strvec_pushf(&cp.args, "%s", last_update); | |
883e248b | 152 | cp.use_shell = 1; |
11cf33be | 153 | cp.dir = get_git_work_tree(); |
883e248b BP |
154 | |
155 | return capture_command(&cp, query_result, 1024); | |
156 | } | |
157 | ||
158 | static void fsmonitor_refresh_callback(struct index_state *istate, const char *name) | |
159 | { | |
160 | int pos = index_name_pos(istate, name, strlen(name)); | |
161 | ||
162 | if (pos >= 0) { | |
163 | struct cache_entry *ce = istate->cache[pos]; | |
164 | ce->ce_flags &= ~CE_FSMONITOR_VALID; | |
165 | } | |
166 | ||
167 | /* | |
168 | * Mark the untracked cache dirty even if it wasn't found in the index | |
169 | * as it could be a new untracked file. | |
170 | */ | |
171 | trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name); | |
0cacebf0 | 172 | untracked_cache_invalidate_path(istate, name, 0); |
883e248b BP |
173 | } |
174 | ||
175 | void refresh_fsmonitor(struct index_state *istate) | |
176 | { | |
883e248b | 177 | struct strbuf query_result = STRBUF_INIT; |
8da2c576 KW |
178 | int query_success = 0, hook_version = -1; |
179 | size_t bol = 0; /* beginning of line */ | |
883e248b | 180 | uint64_t last_update; |
56c69100 | 181 | struct strbuf last_update_token = STRBUF_INIT; |
883e248b | 182 | char *buf; |
5d137fc2 | 183 | unsigned int i; |
883e248b | 184 | |
398a3b08 | 185 | if (!core_fsmonitor || istate->fsmonitor_has_run_once) |
883e248b | 186 | return; |
8da2c576 KW |
187 | |
188 | hook_version = fsmonitor_hook_version(); | |
189 | ||
398a3b08 | 190 | istate->fsmonitor_has_run_once = 1; |
883e248b BP |
191 | |
192 | trace_printf_key(&trace_fsmonitor, "refresh fsmonitor"); | |
193 | /* | |
194 | * This could be racy so save the date/time now and query_fsmonitor | |
195 | * should be inclusive to ensure we don't miss potential changes. | |
196 | */ | |
197 | last_update = getnanotime(); | |
8da2c576 KW |
198 | if (hook_version == HOOK_INTERFACE_VERSION1) |
199 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); | |
883e248b BP |
200 | |
201 | /* | |
8da2c576 KW |
202 | * If we have a last update token, call query_fsmonitor for the set of |
203 | * changes since that token, else assume everything is possibly dirty | |
883e248b BP |
204 | * and check it all. |
205 | */ | |
206 | if (istate->fsmonitor_last_update) { | |
8da2c576 KW |
207 | if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) { |
208 | query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION2, | |
209 | istate->fsmonitor_last_update, &query_result); | |
210 | ||
211 | if (query_success) { | |
212 | if (hook_version < 0) | |
213 | hook_version = HOOK_INTERFACE_VERSION2; | |
214 | ||
215 | /* | |
216 | * First entry will be the last update token | |
217 | * Need to use a char * variable because static | |
218 | * analysis was suggesting to use strbuf_addbuf | |
219 | * but we don't want to copy the entire strbuf | |
6d12b533 | 220 | * only the chars up to the first NUL |
8da2c576 KW |
221 | */ |
222 | buf = query_result.buf; | |
223 | strbuf_addstr(&last_update_token, buf); | |
224 | if (!last_update_token.len) { | |
225 | warning("Empty last update token."); | |
226 | query_success = 0; | |
227 | } else { | |
228 | bol = last_update_token.len + 1; | |
229 | } | |
230 | } else if (hook_version < 0) { | |
231 | hook_version = HOOK_INTERFACE_VERSION1; | |
232 | if (!last_update_token.len) | |
233 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); | |
234 | } | |
235 | } | |
236 | ||
237 | if (hook_version == HOOK_INTERFACE_VERSION1) { | |
238 | query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION1, | |
239 | istate->fsmonitor_last_update, &query_result); | |
240 | } | |
241 | ||
883e248b BP |
242 | trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor); |
243 | trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s", | |
244 | core_fsmonitor, query_success ? "success" : "failure"); | |
245 | } | |
246 | ||
247 | /* a fsmonitor process can return '/' to indicate all entries are invalid */ | |
8da2c576 | 248 | if (query_success && query_result.buf[bol] != '/') { |
883e248b BP |
249 | /* Mark all entries returned by the monitor as dirty */ |
250 | buf = query_result.buf; | |
8da2c576 | 251 | for (i = bol; i < query_result.len; i++) { |
883e248b BP |
252 | if (buf[i] != '\0') |
253 | continue; | |
254 | fsmonitor_refresh_callback(istate, buf + bol); | |
255 | bol = i + 1; | |
256 | } | |
257 | if (bol < query_result.len) | |
258 | fsmonitor_refresh_callback(istate, buf + bol); | |
679f2f9f US |
259 | |
260 | /* Now mark the untracked cache for fsmonitor usage */ | |
261 | if (istate->untracked) | |
262 | istate->untracked->use_fsmonitor = 1; | |
883e248b | 263 | } else { |
679f2f9f US |
264 | |
265 | /* We only want to run the post index changed hook if we've actually changed entries, so keep track | |
266 | * if we actually changed entries or not */ | |
267 | int is_cache_changed = 0; | |
883e248b | 268 | /* Mark all entries invalid */ |
679f2f9f US |
269 | for (i = 0; i < istate->cache_nr; i++) { |
270 | if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) { | |
271 | is_cache_changed = 1; | |
272 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
273 | } | |
274 | } | |
883e248b | 275 | |
ca598d5f | 276 | /* If we're going to check every file, ensure we save the results */ |
679f2f9f US |
277 | if (is_cache_changed) |
278 | istate->cache_changed |= FSMONITOR_CHANGED; | |
ca598d5f | 279 | |
883e248b BP |
280 | if (istate->untracked) |
281 | istate->untracked->use_fsmonitor = 0; | |
282 | } | |
283 | strbuf_release(&query_result); | |
284 | ||
56c69100 KW |
285 | /* Now that we've updated istate, save the last_update_token */ |
286 | FREE_AND_NULL(istate->fsmonitor_last_update); | |
287 | istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL); | |
883e248b BP |
288 | } |
289 | ||
290 | void add_fsmonitor(struct index_state *istate) | |
291 | { | |
5d137fc2 | 292 | unsigned int i; |
56c69100 | 293 | struct strbuf last_update = STRBUF_INIT; |
883e248b BP |
294 | |
295 | if (!istate->fsmonitor_last_update) { | |
296 | trace_printf_key(&trace_fsmonitor, "add fsmonitor"); | |
297 | istate->cache_changed |= FSMONITOR_CHANGED; | |
56c69100 KW |
298 | strbuf_addf(&last_update, "%"PRIu64"", getnanotime()); |
299 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); | |
883e248b BP |
300 | |
301 | /* reset the fsmonitor state */ | |
302 | for (i = 0; i < istate->cache_nr; i++) | |
303 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
304 | ||
305 | /* reset the untracked cache */ | |
306 | if (istate->untracked) { | |
307 | add_untracked_cache(istate); | |
308 | istate->untracked->use_fsmonitor = 1; | |
309 | } | |
310 | ||
311 | /* Update the fsmonitor state */ | |
312 | refresh_fsmonitor(istate); | |
313 | } | |
314 | } | |
315 | ||
316 | void remove_fsmonitor(struct index_state *istate) | |
317 | { | |
318 | if (istate->fsmonitor_last_update) { | |
319 | trace_printf_key(&trace_fsmonitor, "remove fsmonitor"); | |
320 | istate->cache_changed |= FSMONITOR_CHANGED; | |
56c69100 | 321 | FREE_AND_NULL(istate->fsmonitor_last_update); |
883e248b BP |
322 | } |
323 | } | |
324 | ||
325 | void tweak_fsmonitor(struct index_state *istate) | |
326 | { | |
5d137fc2 | 327 | unsigned int i; |
ba1b9cac AV |
328 | int fsmonitor_enabled = git_config_get_fsmonitor(); |
329 | ||
330 | if (istate->fsmonitor_dirty) { | |
331 | if (fsmonitor_enabled) { | |
332 | /* Mark all entries valid */ | |
333 | for (i = 0; i < istate->cache_nr; i++) { | |
334 | istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID; | |
335 | } | |
336 | ||
337 | /* Mark all previously saved entries as dirty */ | |
3444ec2e WB |
338 | if (istate->fsmonitor_dirty->bit_size > istate->cache_nr) |
339 | BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)", | |
340 | (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr); | |
ba1b9cac AV |
341 | ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate); |
342 | ||
679f2f9f | 343 | refresh_fsmonitor(istate); |
ba1b9cac AV |
344 | } |
345 | ||
346 | ewah_free(istate->fsmonitor_dirty); | |
347 | istate->fsmonitor_dirty = NULL; | |
348 | } | |
349 | ||
350 | switch (fsmonitor_enabled) { | |
883e248b BP |
351 | case -1: /* keep: do nothing */ |
352 | break; | |
353 | case 0: /* false */ | |
354 | remove_fsmonitor(istate); | |
355 | break; | |
356 | case 1: /* true */ | |
357 | add_fsmonitor(istate); | |
358 | break; | |
359 | default: /* unknown value: do nothing */ | |
360 | break; | |
361 | } | |
362 | } |