Merge branch 'master' of github.com:vnwildman/git
[git/raj.git] / fsmonitor.c
blob1f4aa1b150dbe948910705c9ff4e75ad0a6a12a1
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;
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);
23 ce = istate->cache[pos];
24 ce->ce_flags &= ~CE_FSMONITOR_VALID;
27 int read_fsmonitor_extension(struct index_state *istate, const void *data,
28 unsigned long sz)
30 const char *index = data;
31 uint32_t hdr_version;
32 uint32_t ewah_size;
33 struct ewah_bitmap *fsmonitor_dirty;
34 int ret;
36 if (sz < sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
37 return error("corrupt fsmonitor extension (too short)");
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);
44 istate->fsmonitor_last_update = get_be64(index);
45 index += sizeof(uint64_t);
47 ewah_size = get_be32(index);
48 index += sizeof(uint32_t);
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");
56 istate->fsmonitor_dirty = fsmonitor_dirty;
58 if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
59 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
60 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
62 trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
63 return 0;
66 void fill_fsmonitor_bitmap(struct index_state *istate)
68 unsigned int i, skipped = 0;
69 istate->fsmonitor_dirty = ewah_new();
70 for (i = 0; i < istate->cache_nr; i++) {
71 if (istate->cache[i]->ce_flags & CE_REMOVE)
72 skipped++;
73 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
74 ewah_set(istate->fsmonitor_dirty, i - skipped);
78 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
80 uint32_t hdr_version;
81 uint64_t tm;
82 uint32_t ewah_start;
83 uint32_t ewah_size = 0;
84 int fixup = 0;
86 if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
87 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
88 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
90 put_be32(&hdr_version, INDEX_EXTENSION_VERSION);
91 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
93 put_be64(&tm, istate->fsmonitor_last_update);
94 strbuf_add(sb, &tm, sizeof(uint64_t));
95 fixup = sb->len;
96 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
98 ewah_start = sb->len;
99 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
100 ewah_free(istate->fsmonitor_dirty);
101 istate->fsmonitor_dirty = NULL;
103 /* fix up size field */
104 put_be32(&ewah_size, sb->len - ewah_start);
105 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
107 trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
111 * Call the query-fsmonitor hook passing the time of the last saved results.
113 static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result)
115 struct child_process cp = CHILD_PROCESS_INIT;
117 if (!core_fsmonitor)
118 return -1;
120 argv_array_push(&cp.args, core_fsmonitor);
121 argv_array_pushf(&cp.args, "%d", version);
122 argv_array_pushf(&cp.args, "%" PRIuMAX, (uintmax_t)last_update);
123 cp.use_shell = 1;
124 cp.dir = get_git_work_tree();
126 return capture_command(&cp, query_result, 1024);
129 static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
131 int pos = index_name_pos(istate, name, strlen(name));
133 if (pos >= 0) {
134 struct cache_entry *ce = istate->cache[pos];
135 ce->ce_flags &= ~CE_FSMONITOR_VALID;
139 * Mark the untracked cache dirty even if it wasn't found in the index
140 * as it could be a new untracked file.
142 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
143 untracked_cache_invalidate_path(istate, name, 0);
146 void refresh_fsmonitor(struct index_state *istate)
148 struct strbuf query_result = STRBUF_INIT;
149 int query_success = 0;
150 size_t bol; /* beginning of line */
151 uint64_t last_update;
152 char *buf;
153 unsigned int i;
155 if (!core_fsmonitor || istate->fsmonitor_has_run_once)
156 return;
157 istate->fsmonitor_has_run_once = 1;
159 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
161 * This could be racy so save the date/time now and query_fsmonitor
162 * should be inclusive to ensure we don't miss potential changes.
164 last_update = getnanotime();
167 * If we have a last update time, call query_fsmonitor for the set of
168 * changes since that time, else assume everything is possibly dirty
169 * and check it all.
171 if (istate->fsmonitor_last_update) {
172 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
173 istate->fsmonitor_last_update, &query_result);
174 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
175 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
176 core_fsmonitor, query_success ? "success" : "failure");
179 /* a fsmonitor process can return '/' to indicate all entries are invalid */
180 if (query_success && query_result.buf[0] != '/') {
181 /* Mark all entries returned by the monitor as dirty */
182 buf = query_result.buf;
183 bol = 0;
184 for (i = 0; i < query_result.len; i++) {
185 if (buf[i] != '\0')
186 continue;
187 fsmonitor_refresh_callback(istate, buf + bol);
188 bol = i + 1;
190 if (bol < query_result.len)
191 fsmonitor_refresh_callback(istate, buf + bol);
192 } else {
193 /* Mark all entries invalid */
194 for (i = 0; i < istate->cache_nr; i++)
195 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
197 /* If we're going to check every file, ensure we save the results */
198 istate->cache_changed |= FSMONITOR_CHANGED;
200 if (istate->untracked)
201 istate->untracked->use_fsmonitor = 0;
203 strbuf_release(&query_result);
205 /* Now that we've updated istate, save the last_update time */
206 istate->fsmonitor_last_update = last_update;
209 void add_fsmonitor(struct index_state *istate)
211 unsigned int i;
213 if (!istate->fsmonitor_last_update) {
214 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
215 istate->cache_changed |= FSMONITOR_CHANGED;
216 istate->fsmonitor_last_update = getnanotime();
218 /* reset the fsmonitor state */
219 for (i = 0; i < istate->cache_nr; i++)
220 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
222 /* reset the untracked cache */
223 if (istate->untracked) {
224 add_untracked_cache(istate);
225 istate->untracked->use_fsmonitor = 1;
228 /* Update the fsmonitor state */
229 refresh_fsmonitor(istate);
233 void remove_fsmonitor(struct index_state *istate)
235 if (istate->fsmonitor_last_update) {
236 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
237 istate->cache_changed |= FSMONITOR_CHANGED;
238 istate->fsmonitor_last_update = 0;
242 void tweak_fsmonitor(struct index_state *istate)
244 unsigned int i;
245 int fsmonitor_enabled = git_config_get_fsmonitor();
247 if (istate->fsmonitor_dirty) {
248 if (fsmonitor_enabled) {
249 /* Mark all entries valid */
250 for (i = 0; i < istate->cache_nr; i++) {
251 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
254 /* Mark all previously saved entries as dirty */
255 if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
256 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
257 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
258 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
260 /* Now mark the untracked cache for fsmonitor usage */
261 if (istate->untracked)
262 istate->untracked->use_fsmonitor = 1;
265 ewah_free(istate->fsmonitor_dirty);
266 istate->fsmonitor_dirty = NULL;
269 switch (fsmonitor_enabled) {
270 case -1: /* keep: do nothing */
271 break;
272 case 0: /* false */
273 remove_fsmonitor(istate);
274 break;
275 case 1: /* true */
276 add_fsmonitor(istate);
277 break;
278 default: /* unknown value: do nothing */
279 break;