2017-09-23 00:35:40 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "ewah/ewok.h"
|
|
|
|
#include "fsmonitor.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
|
|
|
|
#define INDEX_EXTENSION_VERSION (1)
|
|
|
|
#define HOOK_INTERFACE_VERSION (1)
|
|
|
|
|
|
|
|
struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
|
|
|
|
|
|
|
|
static void fsmonitor_ewah_callback(size_t pos, void *is)
|
|
|
|
{
|
|
|
|
struct index_state *istate = (struct index_state *)is;
|
2019-10-12 04:11:23 +08:00
|
|
|
struct cache_entry *ce;
|
2017-09-23 00:35:40 +08:00
|
|
|
|
2019-10-12 04:11:23 +08:00
|
|
|
if (pos >= istate->cache_nr)
|
|
|
|
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" >= %u)",
|
|
|
|
(uintmax_t)pos, istate->cache_nr);
|
|
|
|
|
|
|
|
ce = istate->cache[pos];
|
2017-09-23 00:35:40 +08:00
|
|
|
ce->ce_flags &= ~CE_FSMONITOR_VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_fsmonitor_extension(struct index_state *istate, const void *data,
|
|
|
|
unsigned long sz)
|
|
|
|
{
|
|
|
|
const char *index = data;
|
|
|
|
uint32_t hdr_version;
|
|
|
|
uint32_t ewah_size;
|
|
|
|
struct ewah_bitmap *fsmonitor_dirty;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (sz < sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
|
|
|
|
return error("corrupt fsmonitor extension (too short)");
|
|
|
|
|
|
|
|
hdr_version = get_be32(index);
|
|
|
|
index += sizeof(uint32_t);
|
|
|
|
if (hdr_version != INDEX_EXTENSION_VERSION)
|
|
|
|
return error("bad fsmonitor version %d", hdr_version);
|
|
|
|
|
|
|
|
istate->fsmonitor_last_update = get_be64(index);
|
|
|
|
index += sizeof(uint64_t);
|
|
|
|
|
|
|
|
ewah_size = get_be32(index);
|
|
|
|
index += sizeof(uint32_t);
|
|
|
|
|
|
|
|
fsmonitor_dirty = ewah_new();
|
|
|
|
ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
|
|
|
|
if (ret != ewah_size) {
|
|
|
|
ewah_free(fsmonitor_dirty);
|
|
|
|
return error("failed to parse ewah bitmap reading fsmonitor index extension");
|
|
|
|
}
|
2017-10-28 07:26:37 +08:00
|
|
|
istate->fsmonitor_dirty = fsmonitor_dirty;
|
2017-09-23 00:35:40 +08:00
|
|
|
|
2019-11-13 09:30:05 +08:00
|
|
|
if (!istate->split_index &&
|
|
|
|
istate->fsmonitor_dirty->bit_size > istate->cache_nr)
|
2019-10-12 04:11:23 +08:00
|
|
|
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
|
|
|
|
(uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
|
|
|
|
|
2017-09-23 00:35:40 +08:00
|
|
|
trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:58:10 +08:00
|
|
|
void fill_fsmonitor_bitmap(struct index_state *istate)
|
|
|
|
{
|
2019-10-12 04:11:23 +08:00
|
|
|
unsigned int i, skipped = 0;
|
2017-11-10 03:58:10 +08:00
|
|
|
istate->fsmonitor_dirty = ewah_new();
|
2019-10-12 04:11:23 +08:00
|
|
|
for (i = 0; i < istate->cache_nr; i++) {
|
|
|
|
if (istate->cache[i]->ce_flags & CE_REMOVE)
|
|
|
|
skipped++;
|
|
|
|
else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
|
|
|
|
ewah_set(istate->fsmonitor_dirty, i - skipped);
|
|
|
|
}
|
2017-11-10 03:58:10 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 00:35:40 +08:00
|
|
|
void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
|
|
|
|
{
|
|
|
|
uint32_t hdr_version;
|
|
|
|
uint64_t tm;
|
|
|
|
uint32_t ewah_start;
|
|
|
|
uint32_t ewah_size = 0;
|
|
|
|
int fixup = 0;
|
|
|
|
|
2019-11-13 09:30:05 +08:00
|
|
|
if (!istate->split_index &&
|
|
|
|
istate->fsmonitor_dirty->bit_size > istate->cache_nr)
|
2019-10-12 04:11:23 +08:00
|
|
|
BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
|
|
|
|
(uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
|
|
|
|
|
2017-09-23 00:35:40 +08:00
|
|
|
put_be32(&hdr_version, INDEX_EXTENSION_VERSION);
|
|
|
|
strbuf_add(sb, &hdr_version, sizeof(uint32_t));
|
|
|
|
|
|
|
|
put_be64(&tm, istate->fsmonitor_last_update);
|
|
|
|
strbuf_add(sb, &tm, sizeof(uint64_t));
|
|
|
|
fixup = sb->len;
|
|
|
|
strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
|
|
|
|
|
|
|
|
ewah_start = sb->len;
|
2017-11-10 03:58:10 +08:00
|
|
|
ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
|
|
|
|
ewah_free(istate->fsmonitor_dirty);
|
|
|
|
istate->fsmonitor_dirty = NULL;
|
2017-09-23 00:35:40 +08:00
|
|
|
|
|
|
|
/* fix up size field */
|
|
|
|
put_be32(&ewah_size, sb->len - ewah_start);
|
|
|
|
memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
|
|
|
|
|
|
|
|
trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call the query-fsmonitor hook passing the time of the last saved results.
|
|
|
|
*/
|
|
|
|
static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result)
|
|
|
|
{
|
|
|
|
struct child_process cp = CHILD_PROCESS_INIT;
|
|
|
|
|
2018-05-19 16:27:46 +08:00
|
|
|
if (!core_fsmonitor)
|
2017-09-23 00:35:40 +08:00
|
|
|
return -1;
|
|
|
|
|
2018-05-19 16:27:46 +08:00
|
|
|
argv_array_push(&cp.args, core_fsmonitor);
|
|
|
|
argv_array_pushf(&cp.args, "%d", version);
|
|
|
|
argv_array_pushf(&cp.args, "%" PRIuMAX, (uintmax_t)last_update);
|
2017-09-23 00:35:40 +08:00
|
|
|
cp.use_shell = 1;
|
2017-10-28 07:26:34 +08:00
|
|
|
cp.dir = get_git_work_tree();
|
2017-09-23 00:35:40 +08:00
|
|
|
|
|
|
|
return capture_command(&cp, query_result, 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
|
|
|
|
{
|
|
|
|
int pos = index_name_pos(istate, name, strlen(name));
|
|
|
|
|
|
|
|
if (pos >= 0) {
|
|
|
|
struct cache_entry *ce = istate->cache[pos];
|
|
|
|
ce->ce_flags &= ~CE_FSMONITOR_VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark the untracked cache dirty even if it wasn't found in the index
|
|
|
|
* as it could be a new untracked file.
|
|
|
|
*/
|
|
|
|
trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
|
dir.c: ignore paths containing .git when invalidating untracked cache
read_directory() code ignores all paths named ".git" even if it's not
a valid git repository. See treat_path() for details. Since ".git" is
basically invisible to read_directory(), when we are asked to
invalidate a path that contains ".git", we can safely ignore it
because the slow path would not consider it anyway.
This helps when fsmonitor is used and we have a real ".git" repo at
worktree top. Occasionally .git/index will be updated and if the
fsmonitor hook does not filter it, untracked cache is asked to
invalidate the path ".git/index".
Without this patch, we invalidate the root directory unncessarily,
which:
- makes read_directory() fall back to slow path for root directory
(slower)
- makes the index dirty (because UNTR extension is updated). Depending
on the index size, writing it down could also be slow.
A note about the new "safe_path" knob. Since this new check could be
relatively expensive, avoid it when we know it's not needed. If the
path comes from the index, it can't contain ".git". If it does
contain, we may be screwed up at many more levels, not just this one.
Noticed-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-02-07 17:21:40 +08:00
|
|
|
untracked_cache_invalidate_path(istate, name, 0);
|
2017-09-23 00:35:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void refresh_fsmonitor(struct index_state *istate)
|
|
|
|
{
|
|
|
|
struct strbuf query_result = STRBUF_INIT;
|
|
|
|
int query_success = 0;
|
|
|
|
size_t bol; /* beginning of line */
|
|
|
|
uint64_t last_update;
|
|
|
|
char *buf;
|
2019-06-16 00:11:35 +08:00
|
|
|
unsigned int i;
|
2017-09-23 00:35:40 +08:00
|
|
|
|
2019-05-07 19:10:21 +08:00
|
|
|
if (!core_fsmonitor || istate->fsmonitor_has_run_once)
|
2017-09-23 00:35:40 +08:00
|
|
|
return;
|
2019-05-07 19:10:21 +08:00
|
|
|
istate->fsmonitor_has_run_once = 1;
|
2017-09-23 00:35:40 +08:00
|
|
|
|
|
|
|
trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
|
|
|
|
/*
|
|
|
|
* This could be racy so save the date/time now and query_fsmonitor
|
|
|
|
* should be inclusive to ensure we don't miss potential changes.
|
|
|
|
*/
|
|
|
|
last_update = getnanotime();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have a last update time, call query_fsmonitor for the set of
|
|
|
|
* changes since that time, else assume everything is possibly dirty
|
|
|
|
* and check it all.
|
|
|
|
*/
|
|
|
|
if (istate->fsmonitor_last_update) {
|
|
|
|
query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
|
|
|
|
istate->fsmonitor_last_update, &query_result);
|
|
|
|
trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
|
|
|
|
trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
|
|
|
|
core_fsmonitor, query_success ? "success" : "failure");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* a fsmonitor process can return '/' to indicate all entries are invalid */
|
|
|
|
if (query_success && query_result.buf[0] != '/') {
|
|
|
|
/* Mark all entries returned by the monitor as dirty */
|
|
|
|
buf = query_result.buf;
|
|
|
|
bol = 0;
|
|
|
|
for (i = 0; i < query_result.len; i++) {
|
|
|
|
if (buf[i] != '\0')
|
|
|
|
continue;
|
|
|
|
fsmonitor_refresh_callback(istate, buf + bol);
|
|
|
|
bol = i + 1;
|
|
|
|
}
|
|
|
|
if (bol < query_result.len)
|
|
|
|
fsmonitor_refresh_callback(istate, buf + bol);
|
2019-11-20 16:32:17 +08:00
|
|
|
|
|
|
|
/* Now mark the untracked cache for fsmonitor usage */
|
|
|
|
if (istate->untracked)
|
|
|
|
istate->untracked->use_fsmonitor = 1;
|
2017-09-23 00:35:40 +08:00
|
|
|
} else {
|
2019-11-20 16:32:17 +08:00
|
|
|
|
|
|
|
/* We only want to run the post index changed hook if we've actually changed entries, so keep track
|
|
|
|
* if we actually changed entries or not */
|
|
|
|
int is_cache_changed = 0;
|
2017-09-23 00:35:40 +08:00
|
|
|
/* Mark all entries invalid */
|
2019-11-20 16:32:17 +08:00
|
|
|
for (i = 0; i < istate->cache_nr; i++) {
|
|
|
|
if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
|
|
|
|
is_cache_changed = 1;
|
|
|
|
istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
|
|
|
|
}
|
|
|
|
}
|
2017-09-23 00:35:40 +08:00
|
|
|
|
2018-04-11 02:14:31 +08:00
|
|
|
/* If we're going to check every file, ensure we save the results */
|
2019-11-20 16:32:17 +08:00
|
|
|
if (is_cache_changed)
|
|
|
|
istate->cache_changed |= FSMONITOR_CHANGED;
|
2018-04-11 02:14:31 +08:00
|
|
|
|
2017-09-23 00:35:40 +08:00
|
|
|
if (istate->untracked)
|
|
|
|
istate->untracked->use_fsmonitor = 0;
|
|
|
|
}
|
|
|
|
strbuf_release(&query_result);
|
|
|
|
|
|
|
|
/* Now that we've updated istate, save the last_update time */
|
|
|
|
istate->fsmonitor_last_update = last_update;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_fsmonitor(struct index_state *istate)
|
|
|
|
{
|
2019-06-16 00:11:35 +08:00
|
|
|
unsigned int i;
|
2017-09-23 00:35:40 +08:00
|
|
|
|
|
|
|
if (!istate->fsmonitor_last_update) {
|
|
|
|
trace_printf_key(&trace_fsmonitor, "add fsmonitor");
|
|
|
|
istate->cache_changed |= FSMONITOR_CHANGED;
|
|
|
|
istate->fsmonitor_last_update = getnanotime();
|
|
|
|
|
|
|
|
/* reset the fsmonitor state */
|
|
|
|
for (i = 0; i < istate->cache_nr; i++)
|
|
|
|
istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
|
|
|
|
|
|
|
|
/* reset the untracked cache */
|
|
|
|
if (istate->untracked) {
|
|
|
|
add_untracked_cache(istate);
|
|
|
|
istate->untracked->use_fsmonitor = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the fsmonitor state */
|
|
|
|
refresh_fsmonitor(istate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_fsmonitor(struct index_state *istate)
|
|
|
|
{
|
|
|
|
if (istate->fsmonitor_last_update) {
|
|
|
|
trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
|
|
|
|
istate->cache_changed |= FSMONITOR_CHANGED;
|
|
|
|
istate->fsmonitor_last_update = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tweak_fsmonitor(struct index_state *istate)
|
|
|
|
{
|
2019-06-16 00:11:35 +08:00
|
|
|
unsigned int i;
|
2017-10-28 07:26:37 +08:00
|
|
|
int fsmonitor_enabled = git_config_get_fsmonitor();
|
|
|
|
|
|
|
|
if (istate->fsmonitor_dirty) {
|
|
|
|
if (fsmonitor_enabled) {
|
|
|
|
/* Mark all entries valid */
|
|
|
|
for (i = 0; i < istate->cache_nr; i++) {
|
|
|
|
istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark all previously saved entries as dirty */
|
2019-10-12 04:11:23 +08:00
|
|
|
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);
|
2017-10-28 07:26:37 +08:00
|
|
|
ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
|
|
|
|
|
2019-11-20 16:32:17 +08:00
|
|
|
refresh_fsmonitor(istate);
|
2017-10-28 07:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ewah_free(istate->fsmonitor_dirty);
|
|
|
|
istate->fsmonitor_dirty = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (fsmonitor_enabled) {
|
2017-09-23 00:35:40 +08:00
|
|
|
case -1: /* keep: do nothing */
|
|
|
|
break;
|
|
|
|
case 0: /* false */
|
|
|
|
remove_fsmonitor(istate);
|
|
|
|
break;
|
|
|
|
case 1: /* true */
|
|
|
|
add_fsmonitor(istate);
|
|
|
|
break;
|
|
|
|
default: /* unknown value: do nothing */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|