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
;
32 if (sz
< sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
33 return error("corrupt fsmonitor extension (too short)");
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
);
40 istate
->fsmonitor_last_update
= get_be64(index
);
41 index
+= sizeof(uint64_t);
43 ewah_size
= get_be32(index
);
44 index
+= sizeof(uint32_t);
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");
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
;
58 /* Mark all previously saved entries as dirty */
59 ewah_each_bit(fsmonitor_dirty
, fsmonitor_ewah_callback
, istate
);
61 /* Now mark the untracked cache for fsmonitor usage */
62 if (istate
->untracked
)
63 istate
->untracked
->use_fsmonitor
= 1;
65 ewah_free(fsmonitor_dirty
);
67 trace_printf_key(&trace_fsmonitor
, "read fsmonitor extension successful");
71 void write_fsmonitor_extension(struct strbuf
*sb
, struct index_state
*istate
)
75 struct ewah_bitmap
*bitmap
;
78 uint32_t ewah_size
= 0;
81 put_be32(&hdr_version
, INDEX_EXTENSION_VERSION
);
82 strbuf_add(sb
, &hdr_version
, sizeof(uint32_t));
84 put_be64(&tm
, istate
->fsmonitor_last_update
);
85 strbuf_add(sb
, &tm
, sizeof(uint64_t));
87 strbuf_add(sb
, &ewah_size
, sizeof(uint32_t)); /* we'll fix this up later */
91 for (i
= 0; i
< istate
->cache_nr
; i
++)
92 if (!(istate
->cache
[i
]->ce_flags
& CE_FSMONITOR_VALID
))
94 ewah_serialize_strbuf(bitmap
, sb
);
97 /* fix up size field */
98 put_be32(&ewah_size
, sb
->len
- ewah_start
);
99 memcpy(sb
->buf
+ fixup
, &ewah_size
, sizeof(uint32_t));
101 trace_printf_key(&trace_fsmonitor
, "write fsmonitor extension successful");
105 * Call the query-fsmonitor hook passing the time of the last saved results.
107 static int query_fsmonitor(int version
, uint64_t last_update
, struct strbuf
*query_result
)
109 struct child_process cp
= CHILD_PROCESS_INIT
;
114 if (!(argv
[0] = core_fsmonitor
))
117 snprintf(ver
, sizeof(version
), "%d", version
);
118 snprintf(date
, sizeof(date
), "%" PRIuMAX
, (uintmax_t)last_update
);
125 return capture_command(&cp
, query_result
, 1024);
128 static void fsmonitor_refresh_callback(struct index_state
*istate
, const char *name
)
130 int pos
= index_name_pos(istate
, name
, strlen(name
));
133 struct cache_entry
*ce
= istate
->cache
[pos
];
134 ce
->ce_flags
&= ~CE_FSMONITOR_VALID
;
138 * Mark the untracked cache dirty even if it wasn't found in the index
139 * as it could be a new untracked file.
141 trace_printf_key(&trace_fsmonitor
, "fsmonitor_refresh_callback '%s'", name
);
142 untracked_cache_invalidate_path(istate
, name
);
145 void refresh_fsmonitor(struct index_state
*istate
)
147 static int has_run_once
= 0;
148 struct strbuf query_result
= STRBUF_INIT
;
149 int query_success
= 0;
150 size_t bol
; /* beginning of line */
151 uint64_t last_update
;
155 if (!core_fsmonitor
|| has_run_once
)
159 trace_printf_key(&trace_fsmonitor
, "refresh fsmonitor");
161 * This could be racy so save the date/time now and query_fsmonitor
162 * should be inclusive to ensure we don't miss potential changes.
164 last_update
= getnanotime();
167 * If we have a last update time, call query_fsmonitor for the set of
168 * changes since that time, else assume everything is possibly dirty
171 if (istate
->fsmonitor_last_update
) {
172 query_success
= !query_fsmonitor(HOOK_INTERFACE_VERSION
,
173 istate
->fsmonitor_last_update
, &query_result
);
174 trace_performance_since(last_update
, "fsmonitor process '%s'", core_fsmonitor
);
175 trace_printf_key(&trace_fsmonitor
, "fsmonitor process '%s' returned %s",
176 core_fsmonitor
, query_success
? "success" : "failure");
179 /* a fsmonitor process can return '/' to indicate all entries are invalid */
180 if (query_success
&& query_result
.buf
[0] != '/') {
181 /* Mark all entries returned by the monitor as dirty */
182 buf
= query_result
.buf
;
184 for (i
= 0; i
< query_result
.len
; i
++) {
187 fsmonitor_refresh_callback(istate
, buf
+ bol
);
190 if (bol
< query_result
.len
)
191 fsmonitor_refresh_callback(istate
, buf
+ bol
);
193 /* Mark all entries invalid */
194 for (i
= 0; i
< istate
->cache_nr
; i
++)
195 istate
->cache
[i
]->ce_flags
&= ~CE_FSMONITOR_VALID
;
197 if (istate
->untracked
)
198 istate
->untracked
->use_fsmonitor
= 0;
200 strbuf_release(&query_result
);
202 /* Now that we've updated istate, save the last_update time */
203 istate
->fsmonitor_last_update
= last_update
;
206 void add_fsmonitor(struct index_state
*istate
)
210 if (!istate
->fsmonitor_last_update
) {
211 trace_printf_key(&trace_fsmonitor
, "add fsmonitor");
212 istate
->cache_changed
|= FSMONITOR_CHANGED
;
213 istate
->fsmonitor_last_update
= getnanotime();
215 /* reset the fsmonitor state */
216 for (i
= 0; i
< istate
->cache_nr
; i
++)
217 istate
->cache
[i
]->ce_flags
&= ~CE_FSMONITOR_VALID
;
219 /* reset the untracked cache */
220 if (istate
->untracked
) {
221 add_untracked_cache(istate
);
222 istate
->untracked
->use_fsmonitor
= 1;
225 /* Update the fsmonitor state */
226 refresh_fsmonitor(istate
);
230 void remove_fsmonitor(struct index_state
*istate
)
232 if (istate
->fsmonitor_last_update
) {
233 trace_printf_key(&trace_fsmonitor
, "remove fsmonitor");
234 istate
->cache_changed
|= FSMONITOR_CHANGED
;
235 istate
->fsmonitor_last_update
= 0;
239 void tweak_fsmonitor(struct index_state
*istate
)
241 switch (git_config_get_fsmonitor()) {
242 case -1: /* keep: do nothing */
245 remove_fsmonitor(istate
);
248 add_fsmonitor(istate
);
250 default: /* unknown value: do nothing */