]> git.ipfire.org Git - thirdparty/git.git/blobdiff - fsmonitor.c
git-svn: stop passing "-m" to "git rev-list"
[thirdparty/git.git] / fsmonitor.c
index 64deeda597ead87ef0daa94cebb3b6e91b5a744b..ab9bfc60b34e3146f73194d01cf8963a90f1d768 100644 (file)
 
 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
 
+static void assert_index_minimum(struct index_state *istate, size_t pos)
+{
+       if (pos > istate->cache_nr)
+               BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
+                   (uintmax_t)pos, istate->cache_nr);
+}
+
 static void fsmonitor_ewah_callback(size_t pos, void *is)
 {
        struct index_state *istate = (struct index_state *)is;
        struct cache_entry *ce;
 
-       if (pos >= istate->cache_nr)
-               BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" >= %u)",
-                   (uintmax_t)pos, istate->cache_nr);
+       assert_index_minimum(istate, pos + 1);
 
        ce = istate->cache[pos];
        ce->ce_flags &= ~CE_FSMONITOR_VALID;
@@ -82,10 +87,8 @@ int read_fsmonitor_extension(struct index_state *istate, const void *data,
        }
        istate->fsmonitor_dirty = fsmonitor_dirty;
 
-       if (!istate->split_index &&
-           istate->fsmonitor_dirty->bit_size > istate->cache_nr)
-               BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
-                   (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
+       if (!istate->split_index)
+               assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
 
        trace2_data_string("index", NULL, "extension/fsmn/read/token",
                           istate->fsmonitor_last_update);
@@ -114,10 +117,8 @@ void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
        uint32_t ewah_size = 0;
        int fixup = 0;
 
-       if (!istate->split_index &&
-           istate->fsmonitor_dirty->bit_size > istate->cache_nr)
-               BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
-                   (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
+       if (!istate->split_index)
+               assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
 
        put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
        strbuf_add(sb, &hdr_version, sizeof(uint32_t));
@@ -184,10 +185,10 @@ static int query_fsmonitor(int version, const char *last_update, struct strbuf *
 int fsmonitor_is_trivial_response(const struct strbuf *query_result)
 {
        static char trivial_response[3] = { '\0', '/', '\0' };
-       int is_trivial = !memcmp(trivial_response,
-                                &query_result->buf[query_result->len - 3], 3);
 
-       return is_trivial;
+       return query_result->len >= 3 &&
+               !memcmp(trivial_response,
+                       &query_result->buf[query_result->len - 3], 3);
 }
 
 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
@@ -343,16 +344,45 @@ void refresh_fsmonitor(struct index_state *istate)
        istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
 }
 
+/*
+ * The caller wants to turn on FSMonitor.  And when the caller writes
+ * the index to disk, a FSMonitor extension should be included.  This
+ * requires that `istate->fsmonitor_last_update` not be NULL.  But we
+ * have not actually talked to a FSMonitor process yet, so we don't
+ * have an initial value for this field.
+ *
+ * For a protocol V1 FSMonitor process, this field is a formatted
+ * "nanoseconds since epoch" field.  However, for a protocol V2
+ * FSMonitor process, this field is an opaque token.
+ *
+ * Historically, `add_fsmonitor()` has initialized this field to the
+ * current time for protocol V1 processes.  There are lots of race
+ * conditions here, but that code has shipped...
+ *
+ * The only true solution is to use a V2 FSMonitor and get a current
+ * or default token value (that it understands), but we cannot do that
+ * until we have actually talked to an instance of the FSMonitor process
+ * (but the protocol requires that we send a token first...).
+ *
+ * For simplicity, just initialize like we have a V1 process and require
+ * that V2 processes adapt.
+ */
+static void initialize_fsmonitor_last_update(struct index_state *istate)
+{
+       struct strbuf last_update = STRBUF_INIT;
+
+       strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
+       istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
+}
+
 void add_fsmonitor(struct index_state *istate)
 {
        unsigned int i;
-       struct strbuf last_update = STRBUF_INIT;
 
        if (!istate->fsmonitor_last_update) {
                trace_printf_key(&trace_fsmonitor, "add fsmonitor");
                istate->cache_changed |= FSMONITOR_CHANGED;
-               strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
-               istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
+               initialize_fsmonitor_last_update(istate);
 
                /* reset the fsmonitor state */
                for (i = 0; i < istate->cache_nr; i++)
@@ -391,9 +421,7 @@ void tweak_fsmonitor(struct index_state *istate)
                        }
 
                        /* Mark all previously saved entries as dirty */
-                       if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
-                               BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
-                                   (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
+                       assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
                        ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
 
                        refresh_fsmonitor(istate);