Merge branch 'sg/test-BUG'
[git.git] / fsmonitor.c
blob665bd2d4254b12edf38e94cb0e8b2200ae3bf34a
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"
9 #define INDEX_EXTENSION_VERSION (1)
10 #define HOOK_INTERFACE_VERSION (1)
12 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
14 static void fsmonitor_ewah_callback(size_t pos, void *is)
16 struct index_state *istate = (struct index_state *)is;
17 struct cache_entry *ce = istate->cache[pos];
19 ce->ce_flags &= ~CE_FSMONITOR_VALID;
22 int read_fsmonitor_extension(struct index_state *istate, const void *data,
23 unsigned long sz)
25 const char *index = data;
26 uint32_t hdr_version;
27 uint32_t ewah_size;
28 struct ewah_bitmap *fsmonitor_dirty;
29 int ret;
31 if (sz < sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
32 return error("corrupt fsmonitor extension (too short)");
34 hdr_version = get_be32(index);
35 index += sizeof(uint32_t);
36 if (hdr_version != INDEX_EXTENSION_VERSION)
37 return error("bad fsmonitor version %d", hdr_version);
39 istate->fsmonitor_last_update = get_be64(index);
40 index += sizeof(uint64_t);
42 ewah_size = get_be32(index);
43 index += sizeof(uint32_t);
45 fsmonitor_dirty = ewah_new();
46 ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
47 if (ret != ewah_size) {
48 ewah_free(fsmonitor_dirty);
49 return error("failed to parse ewah bitmap reading fsmonitor index extension");
51 istate->fsmonitor_dirty = fsmonitor_dirty;
53 trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
54 return 0;
57 void fill_fsmonitor_bitmap(struct index_state *istate)
59 int i;
60 istate->fsmonitor_dirty = ewah_new();
61 for (i = 0; i < istate->cache_nr; i++)
62 if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
63 ewah_set(istate->fsmonitor_dirty, i);
66 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
68 uint32_t hdr_version;
69 uint64_t tm;
70 uint32_t ewah_start;
71 uint32_t ewah_size = 0;
72 int fixup = 0;
74 put_be32(&hdr_version, INDEX_EXTENSION_VERSION);
75 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
77 put_be64(&tm, istate->fsmonitor_last_update);
78 strbuf_add(sb, &tm, sizeof(uint64_t));
79 fixup = sb->len;
80 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
82 ewah_start = sb->len;
83 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
84 ewah_free(istate->fsmonitor_dirty);
85 istate->fsmonitor_dirty = NULL;
87 /* fix up size field */
88 put_be32(&ewah_size, sb->len - ewah_start);
89 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
91 trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
95 * Call the query-fsmonitor hook passing the time of the last saved results.
97 static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result)
99 struct child_process cp = CHILD_PROCESS_INIT;
101 if (!core_fsmonitor)
102 return -1;
104 argv_array_push(&cp.args, core_fsmonitor);
105 argv_array_pushf(&cp.args, "%d", version);
106 argv_array_pushf(&cp.args, "%" PRIuMAX, (uintmax_t)last_update);
107 cp.use_shell = 1;
108 cp.dir = get_git_work_tree();
110 return capture_command(&cp, query_result, 1024);
113 static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
115 int pos = index_name_pos(istate, name, strlen(name));
117 if (pos >= 0) {
118 struct cache_entry *ce = istate->cache[pos];
119 ce->ce_flags &= ~CE_FSMONITOR_VALID;
123 * Mark the untracked cache dirty even if it wasn't found in the index
124 * as it could be a new untracked file.
126 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
127 untracked_cache_invalidate_path(istate, name, 0);
130 void refresh_fsmonitor(struct index_state *istate)
132 static int has_run_once = 0;
133 struct strbuf query_result = STRBUF_INIT;
134 int query_success = 0;
135 size_t bol; /* beginning of line */
136 uint64_t last_update;
137 char *buf;
138 int i;
140 if (!core_fsmonitor || has_run_once)
141 return;
142 has_run_once = 1;
144 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
146 * This could be racy so save the date/time now and query_fsmonitor
147 * should be inclusive to ensure we don't miss potential changes.
149 last_update = getnanotime();
152 * If we have a last update time, call query_fsmonitor for the set of
153 * changes since that time, else assume everything is possibly dirty
154 * and check it all.
156 if (istate->fsmonitor_last_update) {
157 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
158 istate->fsmonitor_last_update, &query_result);
159 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
160 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
161 core_fsmonitor, query_success ? "success" : "failure");
164 /* a fsmonitor process can return '/' to indicate all entries are invalid */
165 if (query_success && query_result.buf[0] != '/') {
166 /* Mark all entries returned by the monitor as dirty */
167 buf = query_result.buf;
168 bol = 0;
169 for (i = 0; i < query_result.len; i++) {
170 if (buf[i] != '\0')
171 continue;
172 fsmonitor_refresh_callback(istate, buf + bol);
173 bol = i + 1;
175 if (bol < query_result.len)
176 fsmonitor_refresh_callback(istate, buf + bol);
177 } else {
178 /* Mark all entries invalid */
179 for (i = 0; i < istate->cache_nr; i++)
180 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
182 /* If we're going to check every file, ensure we save the results */
183 istate->cache_changed |= FSMONITOR_CHANGED;
185 if (istate->untracked)
186 istate->untracked->use_fsmonitor = 0;
188 strbuf_release(&query_result);
190 /* Now that we've updated istate, save the last_update time */
191 istate->fsmonitor_last_update = last_update;
194 void add_fsmonitor(struct index_state *istate)
196 int i;
198 if (!istate->fsmonitor_last_update) {
199 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
200 istate->cache_changed |= FSMONITOR_CHANGED;
201 istate->fsmonitor_last_update = getnanotime();
203 /* reset the fsmonitor state */
204 for (i = 0; i < istate->cache_nr; i++)
205 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
207 /* reset the untracked cache */
208 if (istate->untracked) {
209 add_untracked_cache(istate);
210 istate->untracked->use_fsmonitor = 1;
213 /* Update the fsmonitor state */
214 refresh_fsmonitor(istate);
218 void remove_fsmonitor(struct index_state *istate)
220 if (istate->fsmonitor_last_update) {
221 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
222 istate->cache_changed |= FSMONITOR_CHANGED;
223 istate->fsmonitor_last_update = 0;
227 void tweak_fsmonitor(struct index_state *istate)
229 int i;
230 int fsmonitor_enabled = git_config_get_fsmonitor();
232 if (istate->fsmonitor_dirty) {
233 if (fsmonitor_enabled) {
234 /* Mark all entries valid */
235 for (i = 0; i < istate->cache_nr; i++) {
236 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
239 /* Mark all previously saved entries as dirty */
240 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
242 /* Now mark the untracked cache for fsmonitor usage */
243 if (istate->untracked)
244 istate->untracked->use_fsmonitor = 1;
247 ewah_free(istate->fsmonitor_dirty);
248 istate->fsmonitor_dirty = NULL;
251 switch (fsmonitor_enabled) {
252 case -1: /* keep: do nothing */
253 break;
254 case 0: /* false */
255 remove_fsmonitor(istate);
256 break;
257 case 1: /* true */
258 add_fsmonitor(istate);
259 break;
260 default: /* unknown value: do nothing */
261 break;