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