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;
100char ver[64];
101char date[64];
102const char *argv[4];
103104
if (!(argv[0] = core_fsmonitor))
105return -1;
106107
snprintf(ver, sizeof(version), "%d", version);
108snprintf(date, sizeof(date), "%" PRIuMAX, (uintmax_t)last_update);
109argv[1] = ver;
110argv[2] = date;
111argv[3] = NULL;
112cp.argv = argv;
113cp.use_shell = 1;
114cp.dir = get_git_work_tree();
115116
return capture_command(&cp, query_result, 1024);
117}
118119
static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
120{
121int pos = index_name_pos(istate, name, strlen(name));
122123
if (pos >= 0) {
124struct cache_entry *ce = istate->cache[pos];
125ce->ce_flags &= ~CE_FSMONITOR_VALID;
126}
127128
/*
129* Mark the untracked cache dirty even if it wasn't found in the index
130* as it could be a new untracked file.
131*/
132trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
133untracked_cache_invalidate_path(istate, name, 0);
134}
135136
void refresh_fsmonitor(struct index_state *istate)
137{
138static int has_run_once = 0;
139struct strbuf query_result = STRBUF_INIT;
140int query_success = 0;
141size_t bol; /* beginning of line */
142uint64_t last_update;
143char *buf;
144int i;
145146
if (!core_fsmonitor || has_run_once)
147return;
148has_run_once = 1;
149150
trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
151/*
152* This could be racy so save the date/time now and query_fsmonitor
153* should be inclusive to ensure we don't miss potential changes.
154*/
155last_update = getnanotime();
156157
/*
158* If we have a last update time, call query_fsmonitor for the set of
159* changes since that time, else assume everything is possibly dirty
160* and check it all.
161*/
162if (istate->fsmonitor_last_update) {
163query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
164istate->fsmonitor_last_update, &query_result);
165trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
166trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
167core_fsmonitor, query_success ? "success" : "failure");
168}
169170
/* a fsmonitor process can return '/' to indicate all entries are invalid */
171if (query_success && query_result.buf[0] != '/') {
172/* Mark all entries returned by the monitor as dirty */
173buf = query_result.buf;
174bol = 0;
175for (i = 0; i < query_result.len; i++) {
176if (buf[i] != '\0')
177continue;
178fsmonitor_refresh_callback(istate, buf + bol);
179bol = i + 1;
180}
181if (bol < query_result.len)
182fsmonitor_refresh_callback(istate, buf + bol);
183} else {
184/* Mark all entries invalid */
185for (i = 0; i < istate->cache_nr; i++)
186istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
187188
if (istate->untracked)
189istate->untracked->use_fsmonitor = 0;
190}
191strbuf_release(&query_result);
192193
/* Now that we've updated istate, save the last_update time */
194istate->fsmonitor_last_update = last_update;
195}
196197
void add_fsmonitor(struct index_state *istate)
198{
199int i;
200201
if (!istate->fsmonitor_last_update) {
202trace_printf_key(&trace_fsmonitor, "add fsmonitor");
203istate->cache_changed |= FSMONITOR_CHANGED;
204istate->fsmonitor_last_update = getnanotime();
205206
/* reset the fsmonitor state */
207for (i = 0; i < istate->cache_nr; i++)
208istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
209210
/* reset the untracked cache */
211if (istate->untracked) {
212add_untracked_cache(istate);
213istate->untracked->use_fsmonitor = 1;
214}
215216
/* Update the fsmonitor state */
217refresh_fsmonitor(istate);
218}
219}
220221
void remove_fsmonitor(struct index_state *istate)
222{
223if (istate->fsmonitor_last_update) {
224trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
225istate->cache_changed |= FSMONITOR_CHANGED;
226istate->fsmonitor_last_update = 0;
227}
228}
229230
void tweak_fsmonitor(struct index_state *istate)
231{
232int i;
233int fsmonitor_enabled = git_config_get_fsmonitor();
234235
if (istate->fsmonitor_dirty) {
236if (fsmonitor_enabled) {
237/* Mark all entries valid */
238for (i = 0; i < istate->cache_nr; i++) {
239istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
240}
241242
/* Mark all previously saved entries as dirty */
243ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
244245
/* Now mark the untracked cache for fsmonitor usage */
246if (istate->untracked)
247istate->untracked->use_fsmonitor = 1;
248}
249250
ewah_free(istate->fsmonitor_dirty);
251istate->fsmonitor_dirty = NULL;
252}
253254
switch (fsmonitor_enabled) {
255case -1: /* keep: do nothing */
256break;
257case 0: /* false */
258remove_fsmonitor(istate);
259break;
260case 1: /* true */
261add_fsmonitor(istate);
262break;
263default: /* unknown value: do nothing */
264break;
265}
266}