]>
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 | ||
cae70acf DS |
16 | static void assert_index_minimum(struct index_state *istate, size_t pos) |
17 | { | |
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); | |
21 | } | |
22 | ||
883e248b BP |
23 | static void fsmonitor_ewah_callback(size_t pos, void *is) |
24 | { | |
25 | struct index_state *istate = (struct index_state *)is; | |
3444ec2e | 26 | struct cache_entry *ce; |
883e248b | 27 | |
cae70acf | 28 | assert_index_minimum(istate, pos + 1); |
3444ec2e WB |
29 | |
30 | ce = istate->cache[pos]; | |
883e248b BP |
31 | ce->ce_flags &= ~CE_FSMONITOR_VALID; |
32 | } | |
33 | ||
8da2c576 KW |
34 | static int fsmonitor_hook_version(void) |
35 | { | |
36 | int hook_version; | |
37 | ||
38 | if (git_config_get_int("core.fsmonitorhookversion", &hook_version)) | |
39 | return -1; | |
40 | ||
41 | if (hook_version == HOOK_INTERFACE_VERSION1 || | |
42 | hook_version == HOOK_INTERFACE_VERSION2) | |
43 | return hook_version; | |
44 | ||
45 | warning("Invalid hook version '%i' in core.fsmonitorhookversion. " | |
46 | "Must be 1 or 2.", hook_version); | |
47 | return -1; | |
48 | } | |
49 | ||
883e248b BP |
50 | int read_fsmonitor_extension(struct index_state *istate, const void *data, |
51 | unsigned long sz) | |
52 | { | |
53 | const char *index = data; | |
54 | uint32_t hdr_version; | |
55 | uint32_t ewah_size; | |
56 | struct ewah_bitmap *fsmonitor_dirty; | |
883e248b | 57 | int ret; |
56c69100 KW |
58 | uint64_t timestamp; |
59 | struct strbuf last_update = STRBUF_INIT; | |
883e248b | 60 | |
56c69100 | 61 | if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t)) |
883e248b BP |
62 | return error("corrupt fsmonitor extension (too short)"); |
63 | ||
64 | hdr_version = get_be32(index); | |
65 | index += sizeof(uint32_t); | |
56c69100 KW |
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; | |
73 | } else { | |
883e248b | 74 | return error("bad fsmonitor version %d", hdr_version); |
56c69100 | 75 | } |
883e248b | 76 | |
56c69100 | 77 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); |
883e248b BP |
78 | |
79 | ewah_size = get_be32(index); | |
80 | index += sizeof(uint32_t); | |
81 | ||
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"); | |
87 | } | |
ba1b9cac | 88 | istate->fsmonitor_dirty = fsmonitor_dirty; |
883e248b | 89 | |
cae70acf DS |
90 | if (!istate->split_index) |
91 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); | |
3444ec2e | 92 | |
883e248b BP |
93 | trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful"); |
94 | return 0; | |
95 | } | |
96 | ||
3bd28eb2 AV |
97 | void fill_fsmonitor_bitmap(struct index_state *istate) |
98 | { | |
3444ec2e | 99 | unsigned int i, skipped = 0; |
3bd28eb2 | 100 | istate->fsmonitor_dirty = ewah_new(); |
3444ec2e WB |
101 | for (i = 0; i < istate->cache_nr; i++) { |
102 | if (istate->cache[i]->ce_flags & CE_REMOVE) | |
103 | skipped++; | |
104 | else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID)) | |
105 | ewah_set(istate->fsmonitor_dirty, i - skipped); | |
106 | } | |
3bd28eb2 AV |
107 | } |
108 | ||
883e248b BP |
109 | void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate) |
110 | { | |
111 | uint32_t hdr_version; | |
883e248b BP |
112 | uint32_t ewah_start; |
113 | uint32_t ewah_size = 0; | |
114 | int fixup = 0; | |
115 | ||
cae70acf DS |
116 | if (!istate->split_index) |
117 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); | |
3444ec2e | 118 | |
56c69100 | 119 | put_be32(&hdr_version, INDEX_EXTENSION_VERSION2); |
883e248b BP |
120 | strbuf_add(sb, &hdr_version, sizeof(uint32_t)); |
121 | ||
56c69100 KW |
122 | strbuf_addstr(sb, istate->fsmonitor_last_update); |
123 | strbuf_addch(sb, 0); /* Want to keep a NUL */ | |
124 | ||
883e248b BP |
125 | fixup = sb->len; |
126 | strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */ | |
127 | ||
128 | ewah_start = sb->len; | |
3bd28eb2 AV |
129 | ewah_serialize_strbuf(istate->fsmonitor_dirty, sb); |
130 | ewah_free(istate->fsmonitor_dirty); | |
131 | istate->fsmonitor_dirty = NULL; | |
883e248b BP |
132 | |
133 | /* fix up size field */ | |
134 | put_be32(&ewah_size, sb->len - ewah_start); | |
135 | memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t)); | |
136 | ||
137 | trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful"); | |
138 | } | |
139 | ||
140 | /* | |
56c69100 | 141 | * Call the query-fsmonitor hook passing the last update token of the saved results. |
883e248b | 142 | */ |
56c69100 | 143 | static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result) |
883e248b BP |
144 | { |
145 | struct child_process cp = CHILD_PROCESS_INIT; | |
883e248b | 146 | |
735e4173 | 147 | if (!core_fsmonitor) |
883e248b BP |
148 | return -1; |
149 | ||
ef8d7ac4 JK |
150 | strvec_push(&cp.args, core_fsmonitor); |
151 | strvec_pushf(&cp.args, "%d", version); | |
152 | strvec_pushf(&cp.args, "%s", last_update); | |
883e248b | 153 | cp.use_shell = 1; |
11cf33be | 154 | cp.dir = get_git_work_tree(); |
883e248b BP |
155 | |
156 | return capture_command(&cp, query_result, 1024); | |
157 | } | |
158 | ||
159 | static void fsmonitor_refresh_callback(struct index_state *istate, const char *name) | |
160 | { | |
161 | int pos = index_name_pos(istate, name, strlen(name)); | |
162 | ||
163 | if (pos >= 0) { | |
164 | struct cache_entry *ce = istate->cache[pos]; | |
165 | ce->ce_flags &= ~CE_FSMONITOR_VALID; | |
166 | } | |
167 | ||
168 | /* | |
169 | * Mark the untracked cache dirty even if it wasn't found in the index | |
170 | * as it could be a new untracked file. | |
171 | */ | |
172 | trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name); | |
0cacebf0 | 173 | untracked_cache_invalidate_path(istate, name, 0); |
883e248b BP |
174 | } |
175 | ||
176 | void refresh_fsmonitor(struct index_state *istate) | |
177 | { | |
883e248b | 178 | struct strbuf query_result = STRBUF_INIT; |
8da2c576 KW |
179 | int query_success = 0, hook_version = -1; |
180 | size_t bol = 0; /* beginning of line */ | |
883e248b | 181 | uint64_t last_update; |
56c69100 | 182 | struct strbuf last_update_token = STRBUF_INIT; |
883e248b | 183 | char *buf; |
5d137fc2 | 184 | unsigned int i; |
883e248b | 185 | |
398a3b08 | 186 | if (!core_fsmonitor || istate->fsmonitor_has_run_once) |
883e248b | 187 | return; |
8da2c576 KW |
188 | |
189 | hook_version = fsmonitor_hook_version(); | |
190 | ||
398a3b08 | 191 | istate->fsmonitor_has_run_once = 1; |
883e248b BP |
192 | |
193 | trace_printf_key(&trace_fsmonitor, "refresh fsmonitor"); | |
194 | /* | |
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. | |
197 | */ | |
198 | last_update = getnanotime(); | |
8da2c576 KW |
199 | if (hook_version == HOOK_INTERFACE_VERSION1) |
200 | strbuf_addf(&last_update_token, "%"PRIu64"", last_update); | |
883e248b BP |
201 | |
202 | /* | |
8da2c576 KW |
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 | |
883e248b BP |
205 | * and check it all. |
206 | */ | |
207 | if (istate->fsmonitor_last_update) { | |
8da2c576 KW |
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); | |
211 | ||
212 | if (query_success) { | |
213 | if (hook_version < 0) | |
214 | hook_version = HOOK_INTERFACE_VERSION2; | |
215 | ||
216 | /* | |
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 | |
6d12b533 | 221 | * only the chars up to the first NUL |
8da2c576 KW |
222 | */ |
223 | buf = query_result.buf; | |
224 | strbuf_addstr(&last_update_token, buf); | |
225 | if (!last_update_token.len) { | |
226 | warning("Empty last update token."); | |
227 | query_success = 0; | |
228 | } else { | |
229 | bol = last_update_token.len + 1; | |
230 | } | |
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); | |
235 | } | |
236 | } | |
237 | ||
238 | if (hook_version == HOOK_INTERFACE_VERSION1) { | |
239 | query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION1, | |
240 | istate->fsmonitor_last_update, &query_result); | |
241 | } | |
242 | ||
883e248b BP |
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"); | |
246 | } | |
247 | ||
248 | /* a fsmonitor process can return '/' to indicate all entries are invalid */ | |
8da2c576 | 249 | if (query_success && query_result.buf[bol] != '/') { |
883e248b BP |
250 | /* Mark all entries returned by the monitor as dirty */ |
251 | buf = query_result.buf; | |
8da2c576 | 252 | for (i = bol; i < query_result.len; i++) { |
883e248b BP |
253 | if (buf[i] != '\0') |
254 | continue; | |
255 | fsmonitor_refresh_callback(istate, buf + bol); | |
256 | bol = i + 1; | |
257 | } | |
258 | if (bol < query_result.len) | |
259 | fsmonitor_refresh_callback(istate, buf + bol); | |
679f2f9f US |
260 | |
261 | /* Now mark the untracked cache for fsmonitor usage */ | |
262 | if (istate->untracked) | |
263 | istate->untracked->use_fsmonitor = 1; | |
883e248b | 264 | } else { |
679f2f9f US |
265 | |
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; | |
883e248b | 269 | /* Mark all entries invalid */ |
679f2f9f US |
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; | |
274 | } | |
275 | } | |
883e248b | 276 | |
ca598d5f | 277 | /* If we're going to check every file, ensure we save the results */ |
679f2f9f US |
278 | if (is_cache_changed) |
279 | istate->cache_changed |= FSMONITOR_CHANGED; | |
ca598d5f | 280 | |
883e248b BP |
281 | if (istate->untracked) |
282 | istate->untracked->use_fsmonitor = 0; | |
283 | } | |
284 | strbuf_release(&query_result); | |
285 | ||
56c69100 KW |
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); | |
883e248b BP |
289 | } |
290 | ||
291 | void add_fsmonitor(struct index_state *istate) | |
292 | { | |
5d137fc2 | 293 | unsigned int i; |
56c69100 | 294 | struct strbuf last_update = STRBUF_INIT; |
883e248b BP |
295 | |
296 | if (!istate->fsmonitor_last_update) { | |
297 | trace_printf_key(&trace_fsmonitor, "add fsmonitor"); | |
298 | istate->cache_changed |= FSMONITOR_CHANGED; | |
56c69100 KW |
299 | strbuf_addf(&last_update, "%"PRIu64"", getnanotime()); |
300 | istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL); | |
883e248b BP |
301 | |
302 | /* reset the fsmonitor state */ | |
303 | for (i = 0; i < istate->cache_nr; i++) | |
304 | istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; | |
305 | ||
306 | /* reset the untracked cache */ | |
307 | if (istate->untracked) { | |
308 | add_untracked_cache(istate); | |
309 | istate->untracked->use_fsmonitor = 1; | |
310 | } | |
311 | ||
312 | /* Update the fsmonitor state */ | |
313 | refresh_fsmonitor(istate); | |
314 | } | |
315 | } | |
316 | ||
317 | void remove_fsmonitor(struct index_state *istate) | |
318 | { | |
319 | if (istate->fsmonitor_last_update) { | |
320 | trace_printf_key(&trace_fsmonitor, "remove fsmonitor"); | |
321 | istate->cache_changed |= FSMONITOR_CHANGED; | |
56c69100 | 322 | FREE_AND_NULL(istate->fsmonitor_last_update); |
883e248b BP |
323 | } |
324 | } | |
325 | ||
326 | void tweak_fsmonitor(struct index_state *istate) | |
327 | { | |
5d137fc2 | 328 | unsigned int i; |
ba1b9cac AV |
329 | int fsmonitor_enabled = git_config_get_fsmonitor(); |
330 | ||
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; | |
336 | } | |
337 | ||
338 | /* Mark all previously saved entries as dirty */ | |
cae70acf | 339 | assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size); |
ba1b9cac AV |
340 | ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate); |
341 | ||
679f2f9f | 342 | refresh_fsmonitor(istate); |
ba1b9cac AV |
343 | } |
344 | ||
345 | ewah_free(istate->fsmonitor_dirty); | |
346 | istate->fsmonitor_dirty = NULL; | |
347 | } | |
348 | ||
349 | switch (fsmonitor_enabled) { | |
883e248b BP |
350 | case -1: /* keep: do nothing */ |
351 | break; | |
352 | case 0: /* false */ | |
353 | remove_fsmonitor(istate); | |
354 | break; | |
355 | case 1: /* true */ | |
356 | add_fsmonitor(istate); | |
357 | break; | |
358 | default: /* unknown value: do nothing */ | |
359 | break; | |
360 | } | |
361 | } |