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