6 #include "run-command.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
= istate
->cache
[pos
];
19 ce
->ce_flags
&= ~CE_FSMONITOR_VALID
;
22 int read_fsmonitor_extension(struct index_state
*istate
, const void *data
,
25 const char *index
= data
;
28 struct ewah_bitmap
*fsmonitor_dirty
;
31 if (sz
< sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
32 return error("corrupt fsmonitor extension (too short)");
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
);
39 istate
->fsmonitor_last_update
= get_be64(index
);
40 index
+= sizeof(uint64_t);
42 ewah_size
= get_be32(index
);
43 index
+= sizeof(uint32_t);
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");
51 istate
->fsmonitor_dirty
= fsmonitor_dirty
;
53 trace_printf_key(&trace_fsmonitor
, "read fsmonitor extension successful");
57 void fill_fsmonitor_bitmap(struct index_state
*istate
)
60 istate
->fsmonitor_dirty
= ewah_new();
61 for (i
= 0; i
< istate
->cache_nr
; i
++)
62 if (!(istate
->cache
[i
]->ce_flags
& CE_FSMONITOR_VALID
))
63 ewah_set(istate
->fsmonitor_dirty
, i
);
66 void write_fsmonitor_extension(struct strbuf
*sb
, struct index_state
*istate
)
71 uint32_t ewah_size
= 0;
74 put_be32(&hdr_version
, INDEX_EXTENSION_VERSION
);
75 strbuf_add(sb
, &hdr_version
, sizeof(uint32_t));
77 put_be64(&tm
, istate
->fsmonitor_last_update
);
78 strbuf_add(sb
, &tm
, sizeof(uint64_t));
80 strbuf_add(sb
, &ewah_size
, sizeof(uint32_t)); /* we'll fix this up later */
83 ewah_serialize_strbuf(istate
->fsmonitor_dirty
, sb
);
84 ewah_free(istate
->fsmonitor_dirty
);
85 istate
->fsmonitor_dirty
= NULL
;
87 /* fix up size field */
88 put_be32(&ewah_size
, sb
->len
- ewah_start
);
89 memcpy(sb
->buf
+ fixup
, &ewah_size
, sizeof(uint32_t));
91 trace_printf_key(&trace_fsmonitor
, "write fsmonitor extension successful");
95 * Call the query-fsmonitor hook passing the time of the last saved results.
97 static int query_fsmonitor(int version
, uint64_t last_update
, struct strbuf
*query_result
)
99 struct child_process cp
= CHILD_PROCESS_INIT
;
104 if (!(argv
[0] = core_fsmonitor
))
107 snprintf(ver
, sizeof(version
), "%d", version
);
108 snprintf(date
, sizeof(date
), "%" PRIuMAX
, (uintmax_t)last_update
);
114 cp
.dir
= get_git_work_tree();
116 return capture_command(&cp
, query_result
, 1024);
119 static void fsmonitor_refresh_callback(struct index_state
*istate
, const char *name
)
121 int pos
= index_name_pos(istate
, name
, strlen(name
));
124 struct cache_entry
*ce
= istate
->cache
[pos
];
125 ce
->ce_flags
&= ~CE_FSMONITOR_VALID
;
129 * Mark the untracked cache dirty even if it wasn't found in the index
130 * as it could be a new untracked file.
132 trace_printf_key(&trace_fsmonitor
, "fsmonitor_refresh_callback '%s'", name
);
133 untracked_cache_invalidate_path(istate
, name
);
136 void refresh_fsmonitor(struct index_state
*istate
)
138 static int has_run_once
= 0;
139 struct strbuf query_result
= STRBUF_INIT
;
140 int query_success
= 0;
141 size_t bol
; /* beginning of line */
142 uint64_t last_update
;
146 if (!core_fsmonitor
|| has_run_once
)
150 trace_printf_key(&trace_fsmonitor
, "refresh fsmonitor");
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.
155 last_update
= getnanotime();
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
162 if (istate
->fsmonitor_last_update
) {
163 query_success
= !query_fsmonitor(HOOK_INTERFACE_VERSION
,
164 istate
->fsmonitor_last_update
, &query_result
);
165 trace_performance_since(last_update
, "fsmonitor process '%s'", core_fsmonitor
);
166 trace_printf_key(&trace_fsmonitor
, "fsmonitor process '%s' returned %s",
167 core_fsmonitor
, query_success
? "success" : "failure");
170 /* a fsmonitor process can return '/' to indicate all entries are invalid */
171 if (query_success
&& query_result
.buf
[0] != '/') {
172 /* Mark all entries returned by the monitor as dirty */
173 buf
= query_result
.buf
;
175 for (i
= 0; i
< query_result
.len
; i
++) {
178 fsmonitor_refresh_callback(istate
, buf
+ bol
);
181 if (bol
< query_result
.len
)
182 fsmonitor_refresh_callback(istate
, buf
+ bol
);
184 /* Mark all entries invalid */
185 for (i
= 0; i
< istate
->cache_nr
; i
++)
186 istate
->cache
[i
]->ce_flags
&= ~CE_FSMONITOR_VALID
;
188 if (istate
->untracked
)
189 istate
->untracked
->use_fsmonitor
= 0;
191 strbuf_release(&query_result
);
193 /* Now that we've updated istate, save the last_update time */
194 istate
->fsmonitor_last_update
= last_update
;
197 void add_fsmonitor(struct index_state
*istate
)
201 if (!istate
->fsmonitor_last_update
) {
202 trace_printf_key(&trace_fsmonitor
, "add fsmonitor");
203 istate
->cache_changed
|= FSMONITOR_CHANGED
;
204 istate
->fsmonitor_last_update
= getnanotime();
206 /* reset the fsmonitor state */
207 for (i
= 0; i
< istate
->cache_nr
; i
++)
208 istate
->cache
[i
]->ce_flags
&= ~CE_FSMONITOR_VALID
;
210 /* reset the untracked cache */
211 if (istate
->untracked
) {
212 add_untracked_cache(istate
);
213 istate
->untracked
->use_fsmonitor
= 1;
216 /* Update the fsmonitor state */
217 refresh_fsmonitor(istate
);
221 void remove_fsmonitor(struct index_state
*istate
)
223 if (istate
->fsmonitor_last_update
) {
224 trace_printf_key(&trace_fsmonitor
, "remove fsmonitor");
225 istate
->cache_changed
|= FSMONITOR_CHANGED
;
226 istate
->fsmonitor_last_update
= 0;
230 void tweak_fsmonitor(struct index_state
*istate
)
233 int fsmonitor_enabled
= git_config_get_fsmonitor();
235 if (istate
->fsmonitor_dirty
) {
236 if (fsmonitor_enabled
) {
237 /* Mark all entries valid */
238 for (i
= 0; i
< istate
->cache_nr
; i
++) {
239 istate
->cache
[i
]->ce_flags
|= CE_FSMONITOR_VALID
;
242 /* Mark all previously saved entries as dirty */
243 ewah_each_bit(istate
->fsmonitor_dirty
, fsmonitor_ewah_callback
, istate
);
245 /* Now mark the untracked cache for fsmonitor usage */
246 if (istate
->untracked
)
247 istate
->untracked
->use_fsmonitor
= 1;
250 ewah_free(istate
->fsmonitor_dirty
);
251 istate
->fsmonitor_dirty
= NULL
;
254 switch (fsmonitor_enabled
) {
255 case -1: /* keep: do nothing */
258 remove_fsmonitor(istate
);
261 add_fsmonitor(istate
);
263 default: /* unknown value: do nothing */