fsmonitor: change last update timestamp on the index_state to opaque token
[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_VERSION          (1)
12
13 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
14
15 static void fsmonitor_ewah_callback(size_t pos, void *is)
16 {
17         struct index_state *istate = (struct index_state *)is;
18         struct cache_entry *ce;
19
20         if (pos >= istate->cache_nr)
21                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" >= %u)",
22                     (uintmax_t)pos, istate->cache_nr);
23
24         ce = istate->cache[pos];
25         ce->ce_flags &= ~CE_FSMONITOR_VALID;
26 }
27
28 int read_fsmonitor_extension(struct index_state *istate, const void *data,
29         unsigned long sz)
30 {
31         const char *index = data;
32         uint32_t hdr_version;
33         uint32_t ewah_size;
34         struct ewah_bitmap *fsmonitor_dirty;
35         int ret;
36         uint64_t timestamp;
37         struct strbuf last_update = STRBUF_INIT;
38
39         if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
40                 return error("corrupt fsmonitor extension (too short)");
41
42         hdr_version = get_be32(index);
43         index += sizeof(uint32_t);
44         if (hdr_version == INDEX_EXTENSION_VERSION1) {
45                 timestamp = get_be64(index);
46                 strbuf_addf(&last_update, "%"PRIu64"", timestamp);
47                 index += sizeof(uint64_t);
48         } else if (hdr_version == INDEX_EXTENSION_VERSION2) {
49                 strbuf_addstr(&last_update, index);
50                 index += last_update.len + 1;
51         } else {
52                 return error("bad fsmonitor version %d", hdr_version);
53         }
54
55         istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
56
57         ewah_size = get_be32(index);
58         index += sizeof(uint32_t);
59
60         fsmonitor_dirty = ewah_new();
61         ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
62         if (ret != ewah_size) {
63                 ewah_free(fsmonitor_dirty);
64                 return error("failed to parse ewah bitmap reading fsmonitor index extension");
65         }
66         istate->fsmonitor_dirty = fsmonitor_dirty;
67
68         if (!istate->split_index &&
69             istate->fsmonitor_dirty->bit_size > istate->cache_nr)
70                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
71                     (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
72
73         trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
74         return 0;
75 }
76
77 void fill_fsmonitor_bitmap(struct index_state *istate)
78 {
79         unsigned int i, skipped = 0;
80         istate->fsmonitor_dirty = ewah_new();
81         for (i = 0; i < istate->cache_nr; i++) {
82                 if (istate->cache[i]->ce_flags & CE_REMOVE)
83                         skipped++;
84                 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
85                         ewah_set(istate->fsmonitor_dirty, i - skipped);
86         }
87 }
88
89 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
90 {
91         uint32_t hdr_version;
92         uint32_t ewah_start;
93         uint32_t ewah_size = 0;
94         int fixup = 0;
95
96         if (!istate->split_index &&
97             istate->fsmonitor_dirty->bit_size > istate->cache_nr)
98                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
99                     (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
100
101         put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
102         strbuf_add(sb, &hdr_version, sizeof(uint32_t));
103
104         strbuf_addstr(sb, istate->fsmonitor_last_update);
105         strbuf_addch(sb, 0); /* Want to keep a NUL */
106
107         fixup = sb->len;
108         strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
109
110         ewah_start = sb->len;
111         ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
112         ewah_free(istate->fsmonitor_dirty);
113         istate->fsmonitor_dirty = NULL;
114
115         /* fix up size field */
116         put_be32(&ewah_size, sb->len - ewah_start);
117         memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
118
119         trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
120 }
121
122 /*
123  * Call the query-fsmonitor hook passing the last update token of the saved results.
124  */
125 static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
126 {
127         struct child_process cp = CHILD_PROCESS_INIT;
128
129         if (!core_fsmonitor)
130                 return -1;
131
132         argv_array_push(&cp.args, core_fsmonitor);
133         argv_array_pushf(&cp.args, "%d", version);
134         argv_array_pushf(&cp.args, "%s", last_update);
135         cp.use_shell = 1;
136         cp.dir = get_git_work_tree();
137
138         return capture_command(&cp, query_result, 1024);
139 }
140
141 static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
142 {
143         int pos = index_name_pos(istate, name, strlen(name));
144
145         if (pos >= 0) {
146                 struct cache_entry *ce = istate->cache[pos];
147                 ce->ce_flags &= ~CE_FSMONITOR_VALID;
148         }
149
150         /*
151          * Mark the untracked cache dirty even if it wasn't found in the index
152          * as it could be a new untracked file.
153          */
154         trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
155         untracked_cache_invalidate_path(istate, name, 0);
156 }
157
158 void refresh_fsmonitor(struct index_state *istate)
159 {
160         struct strbuf query_result = STRBUF_INIT;
161         int query_success = 0;
162         size_t bol; /* beginning of line */
163         uint64_t last_update;
164         struct strbuf last_update_token = STRBUF_INIT;
165         char *buf;
166         unsigned int i;
167
168         if (!core_fsmonitor || istate->fsmonitor_has_run_once)
169                 return;
170         istate->fsmonitor_has_run_once = 1;
171
172         trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
173         /*
174          * This could be racy so save the date/time now and query_fsmonitor
175          * should be inclusive to ensure we don't miss potential changes.
176          */
177         last_update = getnanotime();
178         strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
179
180         /*
181          * If we have a last update time, call query_fsmonitor for the set of
182          * changes since that time, else assume everything is possibly dirty
183          * and check it all.
184          */
185         if (istate->fsmonitor_last_update) {
186                 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
187                         istate->fsmonitor_last_update, &query_result);
188                 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
189                 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
190                         core_fsmonitor, query_success ? "success" : "failure");
191         }
192
193         /* a fsmonitor process can return '/' to indicate all entries are invalid */
194         if (query_success && query_result.buf[0] != '/') {
195                 /* Mark all entries returned by the monitor as dirty */
196                 buf = query_result.buf;
197                 bol = 0;
198                 for (i = 0; i < query_result.len; i++) {
199                         if (buf[i] != '\0')
200                                 continue;
201                         fsmonitor_refresh_callback(istate, buf + bol);
202                         bol = i + 1;
203                 }
204                 if (bol < query_result.len)
205                         fsmonitor_refresh_callback(istate, buf + bol);
206
207                 /* Now mark the untracked cache for fsmonitor usage */
208                 if (istate->untracked)
209                         istate->untracked->use_fsmonitor = 1;
210         } else {
211
212                 /* We only want to run the post index changed hook if we've actually changed entries, so keep track
213                  * if we actually changed entries or not */
214                 int is_cache_changed = 0;
215                 /* Mark all entries invalid */
216                 for (i = 0; i < istate->cache_nr; i++) {
217                         if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
218                                 is_cache_changed = 1;
219                                 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
220                         }
221                 }
222
223                 /* If we're going to check every file, ensure we save the results */
224                 if (is_cache_changed)
225                         istate->cache_changed |= FSMONITOR_CHANGED;
226
227                 if (istate->untracked)
228                         istate->untracked->use_fsmonitor = 0;
229         }
230         strbuf_release(&query_result);
231
232         /* Now that we've updated istate, save the last_update_token */
233         FREE_AND_NULL(istate->fsmonitor_last_update);
234         istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
235 }
236
237 void add_fsmonitor(struct index_state *istate)
238 {
239         unsigned int i;
240         struct strbuf last_update = STRBUF_INIT;
241
242         if (!istate->fsmonitor_last_update) {
243                 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
244                 istate->cache_changed |= FSMONITOR_CHANGED;
245                 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
246                 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
247
248                 /* reset the fsmonitor state */
249                 for (i = 0; i < istate->cache_nr; i++)
250                         istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
251
252                 /* reset the untracked cache */
253                 if (istate->untracked) {
254                         add_untracked_cache(istate);
255                         istate->untracked->use_fsmonitor = 1;
256                 }
257
258                 /* Update the fsmonitor state */
259                 refresh_fsmonitor(istate);
260         }
261 }
262
263 void remove_fsmonitor(struct index_state *istate)
264 {
265         if (istate->fsmonitor_last_update) {
266                 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
267                 istate->cache_changed |= FSMONITOR_CHANGED;
268                 FREE_AND_NULL(istate->fsmonitor_last_update);
269         }
270 }
271
272 void tweak_fsmonitor(struct index_state *istate)
273 {
274         unsigned int i;
275         int fsmonitor_enabled = git_config_get_fsmonitor();
276
277         if (istate->fsmonitor_dirty) {
278                 if (fsmonitor_enabled) {
279                         /* Mark all entries valid */
280                         for (i = 0; i < istate->cache_nr; i++) {
281                                 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
282                         }
283
284                         /* Mark all previously saved entries as dirty */
285                         if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
286                                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
287                                     (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
288                         ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
289
290                         refresh_fsmonitor(istate);
291                 }
292
293                 ewah_free(istate->fsmonitor_dirty);
294                 istate->fsmonitor_dirty = NULL;
295         }
296
297         switch (fsmonitor_enabled) {
298         case -1: /* keep: do nothing */
299                 break;
300         case 0: /* false */
301                 remove_fsmonitor(istate);
302                 break;
303         case 1: /* true */
304                 add_fsmonitor(istate);
305                 break;
306         default: /* unknown value: do nothing */
307                 break;
308         }
309 }