t6429: testcases for remembering renames
[git] / fsmonitor.c
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
9 #define INDEX_EXTENSION_VERSION1        (1)
10 #define INDEX_EXTENSION_VERSION2        (2)
11 #define HOOK_INTERFACE_VERSION1         (1)
12 #define HOOK_INTERFACE_VERSION2         (2)
13
14 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
15
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
23 static void fsmonitor_ewah_callback(size_t pos, void *is)
24 {
25         struct index_state *istate = (struct index_state *)is;
26         struct cache_entry *ce;
27
28         assert_index_minimum(istate, pos + 1);
29
30         ce = istate->cache[pos];
31         ce->ce_flags &= ~CE_FSMONITOR_VALID;
32 }
33
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
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;
57         int ret;
58         uint64_t timestamp;
59         struct strbuf last_update = STRBUF_INIT;
60
61         if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
62                 return error("corrupt fsmonitor extension (too short)");
63
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;
73         } else {
74                 return error("bad fsmonitor version %d", hdr_version);
75         }
76
77         istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
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         }
88         istate->fsmonitor_dirty = fsmonitor_dirty;
89
90         if (!istate->split_index)
91                 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
92
93         trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
94         return 0;
95 }
96
97 void fill_fsmonitor_bitmap(struct index_state *istate)
98 {
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)
103                         skipped++;
104                 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
105                         ewah_set(istate->fsmonitor_dirty, i - skipped);
106         }
107 }
108
109 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
110 {
111         uint32_t hdr_version;
112         uint32_t ewah_start;
113         uint32_t ewah_size = 0;
114         int fixup = 0;
115
116         if (!istate->split_index)
117                 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
118
119         put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
120         strbuf_add(sb, &hdr_version, sizeof(uint32_t));
121
122         strbuf_addstr(sb, istate->fsmonitor_last_update);
123         strbuf_addch(sb, 0); /* Want to keep a NUL */
124
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;
129         ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
130         ewah_free(istate->fsmonitor_dirty);
131         istate->fsmonitor_dirty = NULL;
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 /*
141  * Call the query-fsmonitor hook passing the last update token of the saved results.
142  */
143 static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
144 {
145         struct child_process cp = CHILD_PROCESS_INIT;
146
147         if (!core_fsmonitor)
148                 return -1;
149
150         strvec_push(&cp.args, core_fsmonitor);
151         strvec_pushf(&cp.args, "%d", version);
152         strvec_pushf(&cp.args, "%s", last_update);
153         cp.use_shell = 1;
154         cp.dir = get_git_work_tree();
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);
173         untracked_cache_invalidate_path(istate, name, 0);
174 }
175
176 void refresh_fsmonitor(struct index_state *istate)
177 {
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;
183         char *buf;
184         unsigned int i;
185
186         if (!core_fsmonitor || istate->fsmonitor_has_run_once)
187                 return;
188
189         hook_version = fsmonitor_hook_version();
190
191         istate->fsmonitor_has_run_once = 1;
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();
199         if (hook_version == HOOK_INTERFACE_VERSION1)
200                 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
201
202         /*
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
205          * and check it all.
206          */
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);
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
221                                  * only the chars up to the first NUL
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
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 */
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++) {
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);
260
261                 /* Now mark the untracked cache for fsmonitor usage */
262                 if (istate->untracked)
263                         istate->untracked->use_fsmonitor = 1;
264         } else {
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;
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;
274                         }
275                 }
276
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;
280
281                 if (istate->untracked)
282                         istate->untracked->use_fsmonitor = 0;
283         }
284         strbuf_release(&query_result);
285
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);
289 }
290
291 void add_fsmonitor(struct index_state *istate)
292 {
293         unsigned int i;
294         struct strbuf last_update = STRBUF_INIT;
295
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);
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;
322                 FREE_AND_NULL(istate->fsmonitor_last_update);
323         }
324 }
325
326 void tweak_fsmonitor(struct index_state *istate)
327 {
328         unsigned int i;
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 */
339                         assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
340                         ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
341
342                         refresh_fsmonitor(istate);
343                 }
344
345                 ewah_free(istate->fsmonitor_dirty);
346                 istate->fsmonitor_dirty = NULL;
347         }
348
349         switch (fsmonitor_enabled) {
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 }