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"
89
#define INDEX_EXTENSION_VERSION (1)
10#define HOOK_INTERFACE_VERSION (1)
1112
struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
1314
static void fsmonitor_ewah_callback(size_t pos, void *is)
15{
16struct index_state *istate = (struct index_state *)is;
17struct cache_entry *ce = istate->cache[pos];
1819
ce->ce_flags &= ~CE_FSMONITOR_VALID;
20}
2122
int read_fsmonitor_extension(struct index_state *istate, const void *data,
23unsigned long sz)
24{
25const char *index = data;
26uint32_t hdr_version;
27uint32_t ewah_size;
28struct ewah_bitmap *fsmonitor_dirty;
29int ret;
3031
if (sz < sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
32return error("corrupt fsmonitor extension (too short)");
3334
hdr_version = get_be32(index);
35index += sizeof(uint32_t);
36if (hdr_version != INDEX_EXTENSION_VERSION)
37return error("bad fsmonitor version %d", hdr_version);
3839
istate->fsmonitor_last_update = get_be64(index);
40index += sizeof(uint64_t);
4142
ewah_size = get_be32(index);
43index += sizeof(uint32_t);
4445
fsmonitor_dirty = ewah_new();
46ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
47if (ret != ewah_size) {
48ewah_free(fsmonitor_dirty);
49return error("failed to parse ewah bitmap reading fsmonitor index extension");
50}
51istate->fsmonitor_dirty = fsmonitor_dirty;
5253
trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
54return 0;
55}
5657
void fill_fsmonitor_bitmap(struct index_state *istate)
58{
59int i;
60istate->fsmonitor_dirty = ewah_new();
61for (i = 0; i < istate->cache_nr; i++)
62if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
63ewah_set(istate->fsmonitor_dirty, i);
64}
6566
void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
67{
68uint32_t hdr_version;
69uint64_t tm;
70uint32_t ewah_start;
71uint32_t ewah_size = 0;
72int fixup = 0;
7374
put_be32(&hdr_version, INDEX_EXTENSION_VERSION);
75strbuf_add(sb, &hdr_version, sizeof(uint32_t));
7677
put_be64(&tm, istate->fsmonitor_last_update);
78strbuf_add(sb, &tm, sizeof(uint64_t));
79fixup = sb->len;
80strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
8182
ewah_start = sb->len;
83ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
84ewah_free(istate->fsmonitor_dirty);
85istate->fsmonitor_dirty = NULL;
8687
/* fix up size field */
88put_be32(&ewah_size, sb->len - ewah_start);
89memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
9091
trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
92}
9394
/*
95* Call the query-fsmonitor hook passing the time of the last saved results.
96*/
97static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result)
98{
99struct child_process cp = CHILD_PROCESS_INIT;
100101
if (!core_fsmonitor)
102return -1;
103104
argv_array_push(&cp.args, core_fsmonitor);
105argv_array_pushf(&cp.args, "%d", version);
106argv_array_pushf(&cp.args, "%" PRIuMAX, (uintmax_t)last_update);
107cp.use_shell = 1;
108cp.dir = get_git_work_tree();
109110
return capture_command(&cp, query_result, 1024);
111}
112113
static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
114{
115int pos = index_name_pos(istate, name, strlen(name));
116117
if (pos >= 0) {
118struct cache_entry *ce = istate->cache[pos];
119ce->ce_flags &= ~CE_FSMONITOR_VALID;
120}
121122
/*
123* Mark the untracked cache dirty even if it wasn't found in the index
124* as it could be a new untracked file.
125*/
126trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
127untracked_cache_invalidate_path(istate, name, 0);
128}
129130
void refresh_fsmonitor(struct index_state *istate)
131{
132static int has_run_once = 0;
133struct strbuf query_result = STRBUF_INIT;
134int query_success = 0;
135size_t bol; /* beginning of line */
136uint64_t last_update;
137char *buf;
138int i;
139140
if (!core_fsmonitor || has_run_once)
141return;
142has_run_once = 1;
143144
trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
145/*
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.
148*/
149last_update = getnanotime();
150151
/*
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.
155*/
156if (istate->fsmonitor_last_update) {
157query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
158istate->fsmonitor_last_update, &query_result);
159trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
160trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
161core_fsmonitor, query_success ? "success" : "failure");
162}
163164
/* a fsmonitor process can return '/' to indicate all entries are invalid */
165if (query_success && query_result.buf[0] != '/') {
166/* Mark all entries returned by the monitor as dirty */
167buf = query_result.buf;
168bol = 0;
169for (i = 0; i < query_result.len; i++) {
170if (buf[i] != '\0')
171continue;
172fsmonitor_refresh_callback(istate, buf + bol);
173bol = i + 1;
174}
175if (bol < query_result.len)
176fsmonitor_refresh_callback(istate, buf + bol);
177} else {
178/* Mark all entries invalid */
179for (i = 0; i < istate->cache_nr; i++)
180istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
181182
/* If we're going to check every file, ensure we save the results */
183istate->cache_changed |= FSMONITOR_CHANGED;
184185
if (istate->untracked)
186istate->untracked->use_fsmonitor = 0;
187}
188strbuf_release(&query_result);
189190
/* Now that we've updated istate, save the last_update time */
191istate->fsmonitor_last_update = last_update;
192}
193194
void add_fsmonitor(struct index_state *istate)
195{
196int i;
197198
if (!istate->fsmonitor_last_update) {
199trace_printf_key(&trace_fsmonitor, "add fsmonitor");
200istate->cache_changed |= FSMONITOR_CHANGED;
201istate->fsmonitor_last_update = getnanotime();
202203
/* reset the fsmonitor state */
204for (i = 0; i < istate->cache_nr; i++)
205istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
206207
/* reset the untracked cache */
208if (istate->untracked) {
209add_untracked_cache(istate);
210istate->untracked->use_fsmonitor = 1;
211}
212213
/* Update the fsmonitor state */
214refresh_fsmonitor(istate);
215}
216}
217218
void remove_fsmonitor(struct index_state *istate)
219{
220if (istate->fsmonitor_last_update) {
221trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
222istate->cache_changed |= FSMONITOR_CHANGED;
223istate->fsmonitor_last_update = 0;
224}
225}
226227
void tweak_fsmonitor(struct index_state *istate)
228{
229int i;
230int fsmonitor_enabled = git_config_get_fsmonitor();
231232
if (istate->fsmonitor_dirty) {
233if (fsmonitor_enabled) {
234/* Mark all entries valid */
235for (i = 0; i < istate->cache_nr; i++) {
236istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
237}
238239
/* Mark all previously saved entries as dirty */
240ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
241242
/* Now mark the untracked cache for fsmonitor usage */
243if (istate->untracked)
244istate->untracked->use_fsmonitor = 1;
245}
246247
ewah_free(istate->fsmonitor_dirty);
248istate->fsmonitor_dirty = NULL;
249}
250251
switch (fsmonitor_enabled) {
252case -1: /* keep: do nothing */
253break;
254case 0: /* false */
255remove_fsmonitor(istate);
256break;
257case 1: /* true */
258add_fsmonitor(istate);
259break;
260default: /* unknown value: do nothing */
261break;
262}
263}