Skip to content

Commit fcd19b0

Browse files
jeffhostetlergitster
authored andcommitted
fsmonitor: refactor initialization of fsmonitor_last_update token
Isolate and document initialization of `istate->fsmonitor_last_update`. This field should contain a fsmonitor-specific opaque token, but we need to initialize it before we can actually talk to a fsmonitor process, so we create a generic default value. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Reviewed-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ff03836 commit fcd19b0

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

fsmonitor.c

+32-3
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,45 @@ void refresh_fsmonitor(struct index_state *istate)
343343
istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
344344
}
345345

346+
/*
347+
* The caller wants to turn on FSMonitor. And when the caller writes
348+
* the index to disk, a FSMonitor extension should be included. This
349+
* requires that `istate->fsmonitor_last_update` not be NULL. But we
350+
* have not actually talked to a FSMonitor process yet, so we don't
351+
* have an initial value for this field.
352+
*
353+
* For a protocol V1 FSMonitor process, this field is a formatted
354+
* "nanoseconds since epoch" field. However, for a protocol V2
355+
* FSMonitor process, this field is an opaque token.
356+
*
357+
* Historically, `add_fsmonitor()` has initialized this field to the
358+
* current time for protocol V1 processes. There are lots of race
359+
* conditions here, but that code has shipped...
360+
*
361+
* The only true solution is to use a V2 FSMonitor and get a current
362+
* or default token value (that it understands), but we cannot do that
363+
* until we have actually talked to an instance of the FSMonitor process
364+
* (but the protocol requires that we send a token first...).
365+
*
366+
* For simplicity, just initialize like we have a V1 process and require
367+
* that V2 processes adapt.
368+
*/
369+
static void initialize_fsmonitor_last_update(struct index_state *istate)
370+
{
371+
struct strbuf last_update = STRBUF_INIT;
372+
373+
strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
374+
istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
375+
}
376+
346377
void add_fsmonitor(struct index_state *istate)
347378
{
348379
unsigned int i;
349-
struct strbuf last_update = STRBUF_INIT;
350380

351381
if (!istate->fsmonitor_last_update) {
352382
trace_printf_key(&trace_fsmonitor, "add fsmonitor");
353383
istate->cache_changed |= FSMONITOR_CHANGED;
354-
strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
355-
istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
384+
initialize_fsmonitor_last_update(istate);
356385

357386
/* reset the fsmonitor state */
358387
for (i = 0; i < istate->cache_nr; i++)

0 commit comments

Comments
 (0)