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