Merge branch 'jh/builtin-fsmonitor-part3'

More fsmonitor--daemon.

* jh/builtin-fsmonitor-part3: (30 commits)
  t7527: improve implicit shutdown testing in fsmonitor--daemon
  fsmonitor--daemon: allow --super-prefix argument
  t7527: test Unicode NFC/NFD handling on MacOS
  t/lib-unicode-nfc-nfd: helper prereqs for testing unicode nfc/nfd
  t/helper/hexdump: add helper to print hexdump of stdin
  fsmonitor: on macOS also emit NFC spelling for NFD pathname
  t7527: test FSMonitor on case insensitive+preserving file system
  fsmonitor: never set CE_FSMONITOR_VALID on submodules
  t/perf/p7527: add perf test for builtin FSMonitor
  t7527: FSMonitor tests for directory moves
  fsmonitor: optimize processing of directory events
  fsm-listen-darwin: shutdown daemon if worktree root is moved/renamed
  fsm-health-win32: force shutdown daemon if worktree root moves
  fsm-health-win32: add polling framework to monitor daemon health
  fsmonitor--daemon: stub in health thread
  fsmonitor--daemon: rename listener thread related variables
  fsmonitor--daemon: prepare for adding health thread
  fsmonitor--daemon: cd out of worktree root
  fsm-listen-darwin: ignore FSEvents caused by xattr changes on macOS
  unpack-trees: initialize fsmonitor_has_run_once in o->result
  ...
This commit is contained in:
Junio C Hamano
2022-06-10 15:04:14 -07:00
28 changed files with 2442 additions and 152 deletions

View File

@ -0,0 +1,24 @@
#include "cache.h"
#include "config.h"
#include "fsmonitor.h"
#include "fsm-health.h"
#include "fsmonitor--daemon.h"
int fsm_health__ctor(struct fsmonitor_daemon_state *state)
{
return 0;
}
void fsm_health__dtor(struct fsmonitor_daemon_state *state)
{
return;
}
void fsm_health__loop(struct fsmonitor_daemon_state *state)
{
return;
}
void fsm_health__stop_async(struct fsmonitor_daemon_state *state)
{
}

View File

@ -0,0 +1,278 @@
#include "cache.h"
#include "config.h"
#include "fsmonitor.h"
#include "fsm-health.h"
#include "fsmonitor--daemon.h"
/*
* Every minute wake up and test our health.
*/
#define WAIT_FREQ_MS (60 * 1000)
/*
* State machine states for each of the interval functions
* used for polling our health.
*/
enum interval_fn_ctx {
CTX_INIT = 0,
CTX_TERM,
CTX_TIMER
};
typedef int (interval_fn)(struct fsmonitor_daemon_state *state,
enum interval_fn_ctx ctx);
struct fsm_health_data
{
HANDLE hEventShutdown;
HANDLE hHandles[1]; /* the array does not own these handles */
#define HEALTH_SHUTDOWN 0
int nr_handles; /* number of active event handles */
struct wt_moved
{
wchar_t wpath[MAX_PATH + 1];
BY_HANDLE_FILE_INFORMATION bhfi;
} wt_moved;
};
/*
* Lookup the system unique ID for the path. This is as close as
* we get to an inode number, but this also contains volume info,
* so it is a little stronger.
*/
static int lookup_bhfi(wchar_t *wpath,
BY_HANDLE_FILE_INFORMATION *bhfi)
{
DWORD desired_access = FILE_LIST_DIRECTORY;
DWORD share_mode =
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
HANDLE hDir;
hDir = CreateFileW(wpath, desired_access, share_mode, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hDir == INVALID_HANDLE_VALUE) {
error(_("[GLE %ld] health thread could not open '%ls'"),
GetLastError(), wpath);
return -1;
}
if (!GetFileInformationByHandle(hDir, bhfi)) {
error(_("[GLE %ld] health thread getting BHFI for '%ls'"),
GetLastError(), wpath);
CloseHandle(hDir);
return -1;
}
CloseHandle(hDir);
return 0;
}
/*
* Compare the relevant fields from two system unique IDs.
* We use this to see if two different handles to the same
* path actually refer to the same *instance* of the file
* or directory.
*/
static int bhfi_eq(const BY_HANDLE_FILE_INFORMATION *bhfi_1,
const BY_HANDLE_FILE_INFORMATION *bhfi_2)
{
return (bhfi_1->dwVolumeSerialNumber == bhfi_2->dwVolumeSerialNumber &&
bhfi_1->nFileIndexHigh == bhfi_2->nFileIndexHigh &&
bhfi_1->nFileIndexLow == bhfi_2->nFileIndexLow);
}
/*
* Shutdown if the original worktree root directory been deleted,
* moved, or renamed?
*
* Since the main thread did a "chdir(getenv($HOME))" and our CWD
* is not in the worktree root directory and because the listener
* thread added FILE_SHARE_DELETE to the watch handle, it is possible
* for the root directory to be moved or deleted while we are still
* watching it. We want to detect that here and force a shutdown.
*
* Granted, a delete MAY cause some operations to fail, such as
* GetOverlappedResult(), but it is not guaranteed. And because
* ReadDirectoryChangesW() only reports on changes *WITHIN* the
* directory, not changes *ON* the directory, our watch will not
* receive a delete event for it.
*
* A move/rename of the worktree root will also not generate an event.
* And since the listener thread already has an open handle, it may
* continue to receive events for events within the directory.
* However, the pathname of the named-pipe was constructed using the
* original location of the worktree root. (Remember named-pipes are
* stored in the NPFS and not in the actual file system.) Clients
* trying to talk to the worktree after the move/rename will not
* reach our daemon process, since we're still listening on the
* pipe with original path.
*
* Furthermore, if the user does something like:
*
* $ mv repo repo.old
* $ git init repo
*
* A new daemon cannot be started in the new instance of "repo"
* because the named-pipe is still being used by the daemon on
* the original instance.
*
* So, detect move/rename/delete and shutdown. This should also
* handle unsafe drive removal.
*
* We use the file system unique ID to distinguish the original
* directory instance from a new instance and force a shutdown
* if the unique ID changes.
*
* Since a worktree move/rename/delete/unmount doesn't happen
* that often (and we can't get an immediate event anyway), we
* use a timeout and periodically poll it.
*/
static int has_worktree_moved(struct fsmonitor_daemon_state *state,
enum interval_fn_ctx ctx)
{
struct fsm_health_data *data = state->health_data;
BY_HANDLE_FILE_INFORMATION bhfi;
int r;
switch (ctx) {
case CTX_TERM:
return 0;
case CTX_INIT:
if (xutftowcs_path(data->wt_moved.wpath,
state->path_worktree_watch.buf) < 0) {
error(_("could not convert to wide characters: '%s'"),
state->path_worktree_watch.buf);
return -1;
}
/*
* On the first call we lookup the unique sequence ID for
* the worktree root directory.
*/
return lookup_bhfi(data->wt_moved.wpath, &data->wt_moved.bhfi);
case CTX_TIMER:
r = lookup_bhfi(data->wt_moved.wpath, &bhfi);
if (r)
return r;
if (!bhfi_eq(&data->wt_moved.bhfi, &bhfi)) {
error(_("BHFI changed '%ls'"), data->wt_moved.wpath);
return -1;
}
return 0;
default:
die(_("unhandled case in 'has_worktree_moved': %d"),
(int)ctx);
}
return 0;
}
int fsm_health__ctor(struct fsmonitor_daemon_state *state)
{
struct fsm_health_data *data;
CALLOC_ARRAY(data, 1);
data->hEventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
data->hHandles[HEALTH_SHUTDOWN] = data->hEventShutdown;
data->nr_handles++;
state->health_data = data;
return 0;
}
void fsm_health__dtor(struct fsmonitor_daemon_state *state)
{
struct fsm_health_data *data;
if (!state || !state->health_data)
return;
data = state->health_data;
CloseHandle(data->hEventShutdown);
FREE_AND_NULL(state->health_data);
}
/*
* A table of the polling functions.
*/
static interval_fn *table[] = {
has_worktree_moved,
NULL, /* must be last */
};
/*
* Call all of the polling functions in the table.
* Shortcut and return first error.
*
* Return 0 if all succeeded.
*/
static int call_all(struct fsmonitor_daemon_state *state,
enum interval_fn_ctx ctx)
{
int k;
for (k = 0; table[k]; k++) {
int r = table[k](state, ctx);
if (r)
return r;
}
return 0;
}
void fsm_health__loop(struct fsmonitor_daemon_state *state)
{
struct fsm_health_data *data = state->health_data;
int r;
r = call_all(state, CTX_INIT);
if (r < 0)
goto force_error_stop;
if (r > 0)
goto force_shutdown;
for (;;) {
DWORD dwWait = WaitForMultipleObjects(data->nr_handles,
data->hHandles,
FALSE, WAIT_FREQ_MS);
if (dwWait == WAIT_OBJECT_0 + HEALTH_SHUTDOWN)
goto clean_shutdown;
if (dwWait == WAIT_TIMEOUT) {
r = call_all(state, CTX_TIMER);
if (r < 0)
goto force_error_stop;
if (r > 0)
goto force_shutdown;
continue;
}
error(_("health thread wait failed [GLE %ld]"),
GetLastError());
goto force_error_stop;
}
force_error_stop:
state->health_error_code = -1;
force_shutdown:
ipc_server_stop_async(state->ipc_server_data);
clean_shutdown:
call_all(state, CTX_TERM);
return;
}
void fsm_health__stop_async(struct fsmonitor_daemon_state *state)
{
SetEvent(state->health_data->hHandles[HEALTH_SHUTDOWN]);
}

View File

@ -0,0 +1,47 @@
#ifndef FSM_HEALTH_H
#define FSM_HEALTH_H
/* This needs to be implemented by each backend */
#ifdef HAVE_FSMONITOR_DAEMON_BACKEND
struct fsmonitor_daemon_state;
/*
* Initialize platform-specific data for the fsmonitor health thread.
* This will be called from the main thread PRIOR to staring the
* thread.
*
* Returns 0 if successful.
* Returns -1 otherwise.
*/
int fsm_health__ctor(struct fsmonitor_daemon_state *state);
/*
* Cleanup platform-specific data for the health thread.
* This will be called from the main thread AFTER joining the thread.
*/
void fsm_health__dtor(struct fsmonitor_daemon_state *state);
/*
* The main body of the platform-specific event loop to monitor the
* health of the daemon process. This will run in the health thread.
*
* The health thread should call `ipc_server_stop_async()` if it needs
* to cause a shutdown. (It should NOT do so if it receives a shutdown
* shutdown signal.)
*
* It should set `state->health_error_code` to -1 if the daemon should exit
* with an error.
*/
void fsm_health__loop(struct fsmonitor_daemon_state *state);
/*
* Gently request that the health thread shutdown.
* It does not wait for it to stop. The caller should do a JOIN
* to wait for it.
*/
void fsm_health__stop_async(struct fsmonitor_daemon_state *state);
#endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
#endif /* FSM_HEALTH_H */

View File

@ -27,7 +27,7 @@
#include "fsm-listen.h"
#include "fsmonitor--daemon.h"
struct fsmonitor_daemon_backend_data
struct fsm_listen_data
{
CFStringRef cfsr_worktree_path;
CFStringRef cfsr_gitdir_path;
@ -100,12 +100,17 @@ static void log_flags_set(const char *path, const FSEventStreamEventFlags flag)
if (flag & kFSEventStreamEventFlagItemCloned)
strbuf_addstr(&msg, "ItemCloned|");
trace_printf_key(&trace_fsmonitor, "fsevent: '%s', flags=%u %s",
trace_printf_key(&trace_fsmonitor, "fsevent: '%s', flags=0x%x %s",
path, flag, msg.buf);
strbuf_release(&msg);
}
static int ef_is_root_changed(const FSEventStreamEventFlags ef)
{
return (ef & kFSEventStreamEventFlagRootChanged);
}
static int ef_is_root_delete(const FSEventStreamEventFlags ef)
{
return (ef & kFSEventStreamEventFlagItemIsDir &&
@ -125,6 +130,60 @@ static int ef_is_dropped(const FSEventStreamEventFlags ef)
ef & kFSEventStreamEventFlagUserDropped);
}
/*
* If an `xattr` change is the only reason we received this event,
* then silently ignore it. Git doesn't care about xattr's. We
* have to be careful here because the kernel can combine multiple
* events for a single path. And because events always have certain
* bits set, such as `ItemIsFile` or `ItemIsDir`.
*
* Return 1 if we should ignore it.
*/
static int ef_ignore_xattr(const FSEventStreamEventFlags ef)
{
static const FSEventStreamEventFlags mask =
kFSEventStreamEventFlagItemChangeOwner |
kFSEventStreamEventFlagItemCreated |
kFSEventStreamEventFlagItemFinderInfoMod |
kFSEventStreamEventFlagItemInodeMetaMod |
kFSEventStreamEventFlagItemModified |
kFSEventStreamEventFlagItemRemoved |
kFSEventStreamEventFlagItemRenamed |
kFSEventStreamEventFlagItemXattrMod |
kFSEventStreamEventFlagItemCloned;
return ((ef & mask) == kFSEventStreamEventFlagItemXattrMod);
}
/*
* On MacOS we have to adjust for Unicode composition insensitivity
* (where NFC and NFD spellings are not respected). The different
* spellings are essentially aliases regardless of how the path is
* actually stored on the disk.
*
* This is related to "core.precomposeUnicode" (which wants to try
* to hide NFD completely and treat everything as NFC). Here, we
* don't know what the value the client has (or will have) for this
* config setting when they make a query, so assume the worst and
* emit both when the OS gives us an NFD path.
*/
static void my_add_path(struct fsmonitor_batch *batch, const char *path)
{
char *composed;
/* add the NFC or NFD path as received from the OS */
fsmonitor_batch__add_path(batch, path);
/* if NFD, also add the corresponding NFC spelling */
composed = (char *)precompose_string_if_needed(path);
if (!composed || composed == path)
return;
fsmonitor_batch__add_path(batch, composed);
free(composed);
}
static void fsevent_callback(ConstFSEventStreamRef streamRef,
void *ctx,
size_t num_of_events,
@ -133,7 +192,7 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
const FSEventStreamEventId event_ids[])
{
struct fsmonitor_daemon_state *state = ctx;
struct fsmonitor_daemon_backend_data *data = state->backend_data;
struct fsm_listen_data *data = state->listen_data;
char **paths = (char **)event_paths;
struct fsmonitor_batch *batch = NULL;
struct string_list cookie_list = STRING_LIST_INIT_DUP;
@ -190,6 +249,33 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
continue;
}
if (ef_is_root_changed(event_flags[k])) {
/*
* The spelling of the pathname of the root directory
* has changed. This includes the name of the root
* directory itself or of any parent directory in the
* path.
*
* (There may be other conditions that throw this,
* but I couldn't find any information on it.)
*
* Force a shutdown now and avoid things getting
* out of sync. The Unix domain socket is inside
* the .git directory and a spelling change will make
* it hard for clients to rendezvous with us.
*/
trace_printf_key(&trace_fsmonitor,
"event: root changed");
goto force_shutdown;
}
if (ef_ignore_xattr(event_flags[k])) {
trace_printf_key(&trace_fsmonitor,
"ignore-xattr: '%s', flags=0x%x",
path_k, event_flags[k]);
continue;
}
switch (fsmonitor_classify_path_absolute(state, path_k)) {
case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
@ -248,7 +334,7 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
if (!batch)
batch = fsmonitor_batch__new();
fsmonitor_batch__add_path(batch, rel);
my_add_path(batch, rel);
}
if (event_flags[k] & kFSEventStreamEventFlagItemIsDir) {
@ -261,7 +347,7 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
if (!batch)
batch = fsmonitor_batch__new();
fsmonitor_batch__add_path(batch, tmp.buf);
my_add_path(batch, tmp.buf);
}
break;
@ -318,11 +404,11 @@ int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
NULL,
NULL
};
struct fsmonitor_daemon_backend_data *data;
struct fsm_listen_data *data;
const void *dir_array[2];
CALLOC_ARRAY(data, 1);
state->backend_data = data;
state->listen_data = data;
data->cfsr_worktree_path = CFStringCreateWithCString(
NULL, state->path_worktree_watch.buf, kCFStringEncodingUTF8);
@ -354,18 +440,18 @@ int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
failed:
error(_("Unable to create FSEventStream."));
FREE_AND_NULL(state->backend_data);
FREE_AND_NULL(state->listen_data);
return -1;
}
void fsm_listen__dtor(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
struct fsm_listen_data *data;
if (!state || !state->backend_data)
if (!state || !state->listen_data)
return;
data = state->backend_data;
data = state->listen_data;
if (data->stream) {
if (data->stream_started)
@ -375,14 +461,14 @@ void fsm_listen__dtor(struct fsmonitor_daemon_state *state)
FSEventStreamRelease(data->stream);
}
FREE_AND_NULL(state->backend_data);
FREE_AND_NULL(state->listen_data);
}
void fsm_listen__stop_async(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
struct fsm_listen_data *data;
data = state->backend_data;
data = state->listen_data;
data->shutdown_style = SHUTDOWN_EVENT;
CFRunLoopStop(data->rl);
@ -390,9 +476,9 @@ void fsm_listen__stop_async(struct fsmonitor_daemon_state *state)
void fsm_listen__loop(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
struct fsm_listen_data *data;
data = state->backend_data;
data = state->listen_data;
data->rl = CFRunLoopGetCurrent();
@ -409,7 +495,7 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
switch (data->shutdown_style) {
case FORCE_ERROR_STOP:
state->error_code = -1;
state->listen_error_code = -1;
/* fall thru */
case FORCE_SHUTDOWN:
ipc_server_stop_async(state->ipc_server_data);
@ -421,7 +507,7 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
return;
force_error_stop_without_loop:
state->error_code = -1;
state->listen_error_code = -1;
ipc_server_stop_async(state->ipc_server_data);
return;
}

View File

@ -25,6 +25,9 @@ struct one_watch
DWORD count;
struct strbuf path;
wchar_t wpath_longname[MAX_PATH + 1];
DWORD wpath_longname_len;
HANDLE hDir;
HANDLE hEvent;
OVERLAPPED overlapped;
@ -34,9 +37,24 @@ struct one_watch
* need to later call GetOverlappedResult() and possibly CancelIoEx().
*/
BOOL is_active;
/*
* Are shortnames enabled on the containing drive? This is
* always true for "C:/" drives and usually never true for
* other drives.
*
* We only set this for the worktree because we only need to
* convert shortname paths to longname paths for items we send
* to clients. (We don't care about shortname expansion for
* paths inside a GITDIR because we never send them to
* clients.)
*/
BOOL has_shortnames;
BOOL has_tilde;
wchar_t dotgit_shortname[16]; /* for 8.3 name */
};
struct fsmonitor_daemon_backend_data
struct fsm_listen_data
{
struct one_watch *watch_worktree;
struct one_watch *watch_gitdir;
@ -51,17 +69,18 @@ struct fsmonitor_daemon_backend_data
};
/*
* Convert the WCHAR path from the notification into UTF8 and
* then normalize it.
* Convert the WCHAR path from the event into UTF8 and normalize it.
*
* `wpath_len` is in WCHARS not bytes.
*/
static int normalize_path_in_utf8(FILE_NOTIFY_INFORMATION *info,
static int normalize_path_in_utf8(wchar_t *wpath, DWORD wpath_len,
struct strbuf *normalized_path)
{
int reserve;
int len = 0;
strbuf_reset(normalized_path);
if (!info->FileNameLength)
if (!wpath_len)
goto normalize;
/*
@ -70,12 +89,12 @@ static int normalize_path_in_utf8(FILE_NOTIFY_INFORMATION *info,
* sequence of 2 UTF8 characters. That should let us
* avoid ERROR_INSUFFICIENT_BUFFER 99.9+% of the time.
*/
reserve = info->FileNameLength + 1;
reserve = 2 * wpath_len + 1;
strbuf_grow(normalized_path, reserve);
for (;;) {
len = WideCharToMultiByte(CP_UTF8, 0, info->FileName,
info->FileNameLength / sizeof(WCHAR),
len = WideCharToMultiByte(CP_UTF8, 0,
wpath, wpath_len,
normalized_path->buf,
strbuf_avail(normalized_path) - 1,
NULL, NULL);
@ -83,9 +102,7 @@ static int normalize_path_in_utf8(FILE_NOTIFY_INFORMATION *info,
goto normalize;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
error(_("[GLE %ld] could not convert path to UTF-8: '%.*ls'"),
GetLastError(),
(int)(info->FileNameLength / sizeof(WCHAR)),
info->FileName);
GetLastError(), (int)wpath_len, wpath);
return -1;
}
@ -98,9 +115,176 @@ normalize:
return strbuf_normalize_path(normalized_path);
}
/*
* See if the worktree root directory has shortnames enabled.
* This will help us decide if we need to do an expensive shortname
* to longname conversion on every notification event.
*
* We do not want to create a file to test this, so we assume that the
* root directory contains a ".git" file or directory. (Our caller
* only calls us for the worktree root, so this should be fine.)
*
* Remember the spelling of the shortname for ".git" if it exists.
*/
static void check_for_shortnames(struct one_watch *watch)
{
wchar_t buf_in[MAX_PATH + 1];
wchar_t buf_out[MAX_PATH + 1];
wchar_t *last;
wchar_t *p;
/* build L"<wt-root-path>/.git" */
swprintf(buf_in, ARRAY_SIZE(buf_in) - 1, L"%ls.git",
watch->wpath_longname);
if (!GetShortPathNameW(buf_in, buf_out, ARRAY_SIZE(buf_out)))
return;
/*
* Get the final filename component of the shortpath.
* We know that the path does not have a final slash.
*/
for (last = p = buf_out; *p; p++)
if (*p == L'/' || *p == '\\')
last = p + 1;
if (!wcscmp(last, L".git"))
return;
watch->has_shortnames = 1;
wcsncpy(watch->dotgit_shortname, last,
ARRAY_SIZE(watch->dotgit_shortname));
/*
* The shortname for ".git" is usually of the form "GIT~1", so
* we should be able to avoid shortname to longname mapping on
* every notification event if the source string does not
* contain a "~".
*
* However, the documentation for GetLongPathNameW() says
* that there are filesystems that don't follow that pattern
* and warns against this optimization.
*
* Lets test this.
*/
if (wcschr(watch->dotgit_shortname, L'~'))
watch->has_tilde = 1;
}
enum get_relative_result {
GRR_NO_CONVERSION_NEEDED,
GRR_HAVE_CONVERSION,
GRR_SHUTDOWN,
};
/*
* Info notification paths are relative to the root of the watch.
* If our CWD is still at the root, then we can use relative paths
* to convert from shortnames to longnames. If our process has a
* different CWD, then we need to construct an absolute path, do
* the conversion, and then return the root-relative portion.
*
* We use the longname form of the root as our basis and assume that
* it already has a trailing slash.
*
* `wpath_len` is in WCHARS not bytes.
*/
static enum get_relative_result get_relative_longname(
struct one_watch *watch,
const wchar_t *wpath, DWORD wpath_len,
wchar_t *wpath_longname, size_t bufsize_wpath_longname)
{
wchar_t buf_in[2 * MAX_PATH + 1];
wchar_t buf_out[MAX_PATH + 1];
DWORD root_len;
DWORD out_len;
/*
* Build L"<wt-root-path>/<event-rel-path>"
* Note that the <event-rel-path> might not be null terminated
* so we avoid swprintf() constructions.
*/
root_len = watch->wpath_longname_len;
if (root_len + wpath_len >= ARRAY_SIZE(buf_in)) {
/*
* This should not happen. We cannot append the observed
* relative path onto the end of the worktree root path
* without overflowing the buffer. Just give up.
*/
return GRR_SHUTDOWN;
}
wcsncpy(buf_in, watch->wpath_longname, root_len);
wcsncpy(buf_in + root_len, wpath, wpath_len);
buf_in[root_len + wpath_len] = 0;
/*
* We don't actually know if the source pathname is a
* shortname or a longname. This Windows routine allows
* either to be given as input.
*/
out_len = GetLongPathNameW(buf_in, buf_out, ARRAY_SIZE(buf_out));
if (!out_len) {
/*
* The shortname to longname conversion can fail for
* various reasons, for example if the file has been
* deleted. (That is, if we just received a
* delete-file notification event and the file is
* already gone, we can't ask the file system to
* lookup the longname for it. Likewise, for moves
* and renames where we are given the old name.)
*
* Since deleting or moving a file or directory by its
* shortname is rather obscure, I'm going ignore the
* failure and ask the caller to report the original
* relative path. This seems kinder than failing here
* and forcing a resync. Besides, forcing a resync on
* every file/directory delete would effectively
* cripple monitoring.
*
* We might revisit this in the future.
*/
return GRR_NO_CONVERSION_NEEDED;
}
if (!wcscmp(buf_in, buf_out)) {
/*
* The path does not have a shortname alias.
*/
return GRR_NO_CONVERSION_NEEDED;
}
if (wcsncmp(buf_in, buf_out, root_len)) {
/*
* The spelling of the root directory portion of the computed
* longname has changed. This should not happen. Basically,
* it means that we don't know where (without recomputing the
* longname of just the root directory) to split out the
* relative path. Since this should not happen, I'm just
* going to let this fail and force a shutdown (because all
* subsequent events are probably going to see the same
* mismatch).
*/
return GRR_SHUTDOWN;
}
if (out_len - root_len >= bufsize_wpath_longname) {
/*
* This should not happen. We cannot copy the root-relative
* portion of the path into the provided buffer without an
* overrun. Just give up.
*/
return GRR_SHUTDOWN;
}
/* Return the worktree root-relative portion of the longname. */
wcscpy(wpath_longname, buf_out + root_len);
return GRR_HAVE_CONVERSION;
}
void fsm_listen__stop_async(struct fsmonitor_daemon_state *state)
{
SetEvent(state->backend_data->hListener[LISTENER_SHUTDOWN]);
SetEvent(state->listen_data->hListener[LISTENER_SHUTDOWN]);
}
static struct one_watch *create_watch(struct fsmonitor_daemon_state *state,
@ -111,7 +295,9 @@ static struct one_watch *create_watch(struct fsmonitor_daemon_state *state,
DWORD share_mode =
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
HANDLE hDir;
wchar_t wpath[MAX_PATH];
DWORD len_longname;
wchar_t wpath[MAX_PATH + 1];
wchar_t wpath_longname[MAX_PATH + 1];
if (xutftowcs_path(wpath, path) < 0) {
error(_("could not convert to wide characters: '%s'"), path);
@ -128,6 +314,21 @@ static struct one_watch *create_watch(struct fsmonitor_daemon_state *state,
return NULL;
}
len_longname = GetLongPathNameW(wpath, wpath_longname,
ARRAY_SIZE(wpath_longname));
if (!len_longname) {
error(_("[GLE %ld] could not get longname of '%s'"),
GetLastError(), path);
CloseHandle(hDir);
return NULL;
}
if (wpath_longname[len_longname - 1] != L'/' &&
wpath_longname[len_longname - 1] != L'\\') {
wpath_longname[len_longname++] = L'/';
wpath_longname[len_longname] = 0;
}
CALLOC_ARRAY(watch, 1);
watch->buf_len = sizeof(watch->buffer); /* assume full MAX_RDCW_BUF */
@ -135,6 +336,9 @@ static struct one_watch *create_watch(struct fsmonitor_daemon_state *state,
strbuf_init(&watch->path, 0);
strbuf_addstr(&watch->path, path);
wcscpy(watch->wpath_longname, wpath_longname);
watch->wpath_longname_len = len_longname;
watch->hDir = hDir;
watch->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
@ -155,7 +359,7 @@ static void destroy_watch(struct one_watch *watch)
free(watch);
}
static int start_rdcw_watch(struct fsmonitor_daemon_backend_data *data,
static int start_rdcw_watch(struct fsm_listen_data *data,
struct one_watch *watch)
{
DWORD dwNotifyFilter =
@ -220,12 +424,22 @@ static int recv_rdcw_watch(struct one_watch *watch)
}
/*
* NEEDSWORK: If an external <gitdir> is deleted, the above
* returns an error. I'm not sure that there's anything that
* we can do here other than failing -- the <worktree>/.git
* link file would be broken anyway. We might try to check
* for that and return a better error message, but I'm not
* sure it is worth it.
* GetOverlappedResult() fails if the watched directory is
* deleted while we were waiting for an overlapped IO to
* complete. The documentation did not list specific errors,
* but I observed ERROR_ACCESS_DENIED (0x05) errors during
* testing.
*
* Note that we only get notificaiton events for events
* *within* the directory, not *on* the directory itself.
* (These might be properies of the parent directory, for
* example).
*
* NEEDSWORK: We might try to check for the deleted directory
* case and return a better error message, but I'm not sure it
* is worth it.
*
* Shutdown if we get any error.
*/
error(_("GetOverlappedResult failed on '%s' [GLE %ld]"),
@ -258,6 +472,62 @@ static void cancel_rdcw_watch(struct one_watch *watch)
watch->is_active = FALSE;
}
/*
* Process a single relative pathname event.
* Return 1 if we should shutdown.
*/
static int process_1_worktree_event(
struct string_list *cookie_list,
struct fsmonitor_batch **batch,
const struct strbuf *path,
enum fsmonitor_path_type t,
DWORD info_action)
{
const char *slash;
switch (t) {
case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
/* special case cookie files within .git */
/* Use just the filename of the cookie file. */
slash = find_last_dir_sep(path->buf);
string_list_append(cookie_list,
slash ? slash + 1 : path->buf);
break;
case IS_INSIDE_DOT_GIT:
/* ignore everything inside of "<worktree>/.git/" */
break;
case IS_DOT_GIT:
/* "<worktree>/.git" was deleted (or renamed away) */
if ((info_action == FILE_ACTION_REMOVED) ||
(info_action == FILE_ACTION_RENAMED_OLD_NAME)) {
trace2_data_string("fsmonitor", NULL,
"fsm-listen/dotgit",
"removed");
return 1;
}
break;
case IS_WORKDIR_PATH:
/* queue normal pathname */
if (!*batch)
*batch = fsmonitor_batch__new();
fsmonitor_batch__add_path(*batch, path->buf);
break;
case IS_GITDIR:
case IS_INSIDE_GITDIR:
case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
default:
BUG("unexpected path classification '%d' for '%s'",
t, path->buf);
}
return 0;
}
/*
* Process filesystem events that happen anywhere (recursively) under the
* <worktree> root directory. For a normal working directory, this includes
@ -268,12 +538,13 @@ static void cancel_rdcw_watch(struct one_watch *watch)
*/
static int process_worktree_events(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data = state->backend_data;
struct fsm_listen_data *data = state->listen_data;
struct one_watch *watch = data->watch_worktree;
struct strbuf path = STRBUF_INIT;
struct string_list cookie_list = STRING_LIST_INIT_DUP;
struct fsmonitor_batch *batch = NULL;
const char *p = watch->buffer;
wchar_t wpath_longname[MAX_PATH + 1];
/*
* If the kernel gets more events than will fit in the kernel
@ -306,54 +577,64 @@ static int process_worktree_events(struct fsmonitor_daemon_state *state)
*/
for (;;) {
FILE_NOTIFY_INFORMATION *info = (void *)p;
const char *slash;
wchar_t *wpath = info->FileName;
DWORD wpath_len = info->FileNameLength / sizeof(WCHAR);
enum fsmonitor_path_type t;
enum get_relative_result grr;
strbuf_reset(&path);
if (normalize_path_in_utf8(info, &path) == -1)
if (watch->has_shortnames) {
if (!wcscmp(wpath, watch->dotgit_shortname)) {
/*
* This event exactly matches the
* spelling of the shortname of
* ".git", so we can skip some steps.
*
* (This case is odd because the user
* can "rm -rf GIT~1" and we cannot
* use the filesystem to map it back
* to ".git".)
*/
strbuf_reset(&path);
strbuf_addstr(&path, ".git");
t = IS_DOT_GIT;
goto process_it;
}
if (watch->has_tilde && !wcschr(wpath, L'~')) {
/*
* Shortnames on this filesystem have tildes
* and the notification path does not have
* one, so we assume that it is a longname.
*/
goto normalize_it;
}
grr = get_relative_longname(watch, wpath, wpath_len,
wpath_longname,
ARRAY_SIZE(wpath_longname));
switch (grr) {
case GRR_NO_CONVERSION_NEEDED: /* use info buffer as is */
break;
case GRR_HAVE_CONVERSION:
wpath = wpath_longname;
wpath_len = wcslen(wpath);
break;
default:
case GRR_SHUTDOWN:
goto force_shutdown;
}
}
normalize_it:
if (normalize_path_in_utf8(wpath, wpath_len, &path) == -1)
goto skip_this_path;
t = fsmonitor_classify_path_workdir_relative(path.buf);
switch (t) {
case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
/* special case cookie files within .git */
/* Use just the filename of the cookie file. */
slash = find_last_dir_sep(path.buf);
string_list_append(&cookie_list,
slash ? slash + 1 : path.buf);
break;
case IS_INSIDE_DOT_GIT:
/* ignore everything inside of "<worktree>/.git/" */
break;
case IS_DOT_GIT:
/* "<worktree>/.git" was deleted (or renamed away) */
if ((info->Action == FILE_ACTION_REMOVED) ||
(info->Action == FILE_ACTION_RENAMED_OLD_NAME)) {
trace2_data_string("fsmonitor", NULL,
"fsm-listen/dotgit",
"removed");
goto force_shutdown;
}
break;
case IS_WORKDIR_PATH:
/* queue normal pathname */
if (!batch)
batch = fsmonitor_batch__new();
fsmonitor_batch__add_path(batch, path.buf);
break;
case IS_GITDIR:
case IS_INSIDE_GITDIR:
case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
default:
BUG("unexpected path classification '%d' for '%s'",
t, path.buf);
}
process_it:
if (process_1_worktree_event(&cookie_list, &batch, &path, t,
info->Action))
goto force_shutdown;
skip_this_path:
if (!info->NextEntryOffset)
@ -382,10 +663,13 @@ force_shutdown:
* Note that we DO NOT get filesystem events on the external <gitdir>
* itself (it is not inside something that we are watching). In particular,
* we do not get an event if the external <gitdir> is deleted.
*
* Also, we do not care about shortnames within the external <gitdir>, since
* we never send these paths to clients.
*/
static int process_gitdir_events(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data = state->backend_data;
struct fsm_listen_data *data = state->listen_data;
struct one_watch *watch = data->watch_gitdir;
struct strbuf path = STRBUF_INIT;
struct string_list cookie_list = STRING_LIST_INIT_DUP;
@ -403,8 +687,10 @@ static int process_gitdir_events(struct fsmonitor_daemon_state *state)
const char *slash;
enum fsmonitor_path_type t;
strbuf_reset(&path);
if (normalize_path_in_utf8(info, &path) == -1)
if (normalize_path_in_utf8(
info->FileName,
info->FileNameLength / sizeof(WCHAR),
&path) == -1)
goto skip_this_path;
t = fsmonitor_classify_path_gitdir_relative(path.buf);
@ -441,11 +727,11 @@ skip_this_path:
void fsm_listen__loop(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data = state->backend_data;
struct fsm_listen_data *data = state->listen_data;
DWORD dwWait;
int result;
state->error_code = 0;
state->listen_error_code = 0;
if (start_rdcw_watch(data, data->watch_worktree) == -1)
goto force_error_stop;
@ -510,7 +796,7 @@ void fsm_listen__loop(struct fsmonitor_daemon_state *state)
}
force_error_stop:
state->error_code = -1;
state->listen_error_code = -1;
force_shutdown:
/*
@ -527,7 +813,7 @@ clean_shutdown:
int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
struct fsm_listen_data *data;
CALLOC_ARRAY(data, 1);
@ -538,6 +824,8 @@ int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
if (!data->watch_worktree)
goto failed;
check_for_shortnames(data->watch_worktree);
if (state->nr_paths_watching > 1) {
data->watch_gitdir = create_watch(state,
state->path_gitdir_watch.buf);
@ -558,7 +846,7 @@ int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
data->nr_listener_handles++;
}
state->backend_data = data;
state->listen_data = data;
return 0;
failed:
@ -571,16 +859,16 @@ failed:
void fsm_listen__dtor(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
struct fsm_listen_data *data;
if (!state || !state->backend_data)
if (!state || !state->listen_data)
return;
data = state->backend_data;
data = state->listen_data;
CloseHandle(data->hEventShutdown);
destroy_watch(data->watch_worktree);
destroy_watch(data->watch_gitdir);
FREE_AND_NULL(state->backend_data);
FREE_AND_NULL(state->listen_data);
}

View File

@ -33,7 +33,7 @@ void fsm_listen__dtor(struct fsmonitor_daemon_state *state);
* do so if the listener thread receives a normal shutdown signal from
* the IPC layer.)
*
* It should set `state->error_code` to -1 if the daemon should exit
* It should set `state->listen_error_code` to -1 if the daemon should exit
* with an error.
*/
void fsm_listen__loop(struct fsmonitor_daemon_state *state);

View File

@ -0,0 +1,89 @@
#include "cache.h"
#include "config.h"
#include "repository.h"
#include "fsmonitor-settings.h"
#include "fsmonitor.h"
#include <sys/param.h>
#include <sys/mount.h>
/*
* [1] Remote working directories are problematic for FSMonitor.
*
* The underlying file system on the server machine and/or the remote
* mount type (NFS, SAMBA, etc.) dictates whether notification events
* are available at all to remote client machines.
*
* Kernel differences between the server and client machines also
* dictate the how (buffering, frequency, de-dup) the events are
* delivered to client machine processes.
*
* A client machine (such as a laptop) may choose to suspend/resume
* and it is unclear (without lots of testing) whether the watcher can
* resync after a resume. We might be able to treat this as a normal
* "events were dropped by the kernel" event and do our normal "flush
* and resync" --or-- we might need to close the existing (zombie?)
* notification fd and create a new one.
*
* In theory, the above issues need to be addressed whether we are
* using the Hook or IPC API.
*
* For the builtin FSMonitor, we create the Unix domain socket for the
* IPC in the .git directory. If the working directory is remote,
* then the socket will be created on the remote file system. This
* can fail if the remote file system does not support UDS file types
* (e.g. smbfs to a Windows server) or if the remote kernel does not
* allow a non-local process to bind() the socket. (These problems
* could be fixed by moving the UDS out of the .git directory and to a
* well-known local directory on the client machine, but care should
* be taken to ensure that $HOME is actually local and not a managed
* file share.)
*
* So (for now at least), mark remote working directories as
* incompatible.
*
*
* [2] FAT32 and NTFS working directories are problematic too.
*
* The builtin FSMonitor uses a Unix domain socket in the .git
* directory for IPC. These Windows drive formats do not support
* Unix domain sockets, so mark them as incompatible for the daemon.
*
*/
static enum fsmonitor_reason check_volume(struct repository *r)
{
struct statfs fs;
if (statfs(r->worktree, &fs) == -1) {
int saved_errno = errno;
trace_printf_key(&trace_fsmonitor, "statfs('%s') failed: %s",
r->worktree, strerror(saved_errno));
errno = saved_errno;
return FSMONITOR_REASON_ERROR;
}
trace_printf_key(&trace_fsmonitor,
"statfs('%s') [type 0x%08x][flags 0x%08x] '%s'",
r->worktree, fs.f_type, fs.f_flags, fs.f_fstypename);
if (!(fs.f_flags & MNT_LOCAL))
return FSMONITOR_REASON_REMOTE;
if (!strcmp(fs.f_fstypename, "msdos")) /* aka FAT32 */
return FSMONITOR_REASON_NOSOCKETS;
if (!strcmp(fs.f_fstypename, "ntfs"))
return FSMONITOR_REASON_NOSOCKETS;
return FSMONITOR_REASON_OK;
}
enum fsmonitor_reason fsm_os__incompatible(struct repository *r)
{
enum fsmonitor_reason reason;
reason = check_volume(r);
if (reason != FSMONITOR_REASON_OK)
return reason;
return FSMONITOR_REASON_OK;
}

View File

@ -0,0 +1,137 @@
#include "cache.h"
#include "config.h"
#include "repository.h"
#include "fsmonitor-settings.h"
#include "fsmonitor.h"
/*
* VFS for Git is incompatible with FSMonitor.
*
* Granted, core Git does not know anything about VFS for Git and we
* shouldn't make assumptions about a downstream feature, but users
* can install both versions. And this can lead to incorrect results
* from core Git commands. So, without bringing in any of the VFS for
* Git code, do a simple config test for a published config setting.
* (We do not look at the various *_TEST_* environment variables.)
*/
static enum fsmonitor_reason check_vfs4git(struct repository *r)
{
const char *const_str;
if (!repo_config_get_value(r, "core.virtualfilesystem", &const_str))
return FSMONITOR_REASON_VFS4GIT;
return FSMONITOR_REASON_OK;
}
/*
* Remote working directories are problematic for FSMonitor.
*
* The underlying file system on the server machine and/or the remote
* mount type dictates whether notification events are available at
* all to remote client machines.
*
* Kernel differences between the server and client machines also
* dictate the how (buffering, frequency, de-dup) the events are
* delivered to client machine processes.
*
* A client machine (such as a laptop) may choose to suspend/resume
* and it is unclear (without lots of testing) whether the watcher can
* resync after a resume. We might be able to treat this as a normal
* "events were dropped by the kernel" event and do our normal "flush
* and resync" --or-- we might need to close the existing (zombie?)
* notification fd and create a new one.
*
* In theory, the above issues need to be addressed whether we are
* using the Hook or IPC API.
*
* So (for now at least), mark remote working directories as
* incompatible.
*
* Notes for testing:
*
* (a) Windows allows a network share to be mapped to a drive letter.
* (This is the normal method to access it.)
*
* $ NET USE Z: \\server\share
* $ git -C Z:/repo status
*
* (b) Windows allows a network share to be referenced WITHOUT mapping
* it to drive letter.
*
* $ NET USE \\server\share\dir
* $ git -C //server/share/repo status
*
* (c) Windows allows "SUBST" to create a fake drive mapping to an
* arbitrary path (which may be remote)
*
* $ SUBST Q: Z:\repo
* $ git -C Q:/ status
*
* (d) Windows allows a directory symlink to be created on a local
* file system that points to a remote repo.
*
* $ mklink /d ./link //server/share/repo
* $ git -C ./link status
*/
static enum fsmonitor_reason check_remote(struct repository *r)
{
wchar_t wpath[MAX_PATH];
wchar_t wfullpath[MAX_PATH];
size_t wlen;
UINT driveType;
/*
* Do everything in wide chars because the drive letter might be
* a multi-byte sequence. See win32_has_dos_drive_prefix().
*/
if (xutftowcs_path(wpath, r->worktree) < 0)
return FSMONITOR_REASON_ERROR;
/*
* GetDriveTypeW() requires a final slash. We assume that the
* worktree pathname points to an actual directory.
*/
wlen = wcslen(wpath);
if (wpath[wlen - 1] != L'\\' && wpath[wlen - 1] != L'/') {
wpath[wlen++] = L'\\';
wpath[wlen] = 0;
}
/*
* Normalize the path. If nothing else, this converts forward
* slashes to backslashes. This is essential to get GetDriveTypeW()
* correctly handle some UNC "\\server\share\..." paths.
*/
if (!GetFullPathNameW(wpath, MAX_PATH, wfullpath, NULL))
return FSMONITOR_REASON_ERROR;
driveType = GetDriveTypeW(wfullpath);
trace_printf_key(&trace_fsmonitor,
"DriveType '%s' L'%ls' (%u)",
r->worktree, wfullpath, driveType);
if (driveType == DRIVE_REMOTE) {
trace_printf_key(&trace_fsmonitor,
"check_remote('%s') true",
r->worktree);
return FSMONITOR_REASON_REMOTE;
}
return FSMONITOR_REASON_OK;
}
enum fsmonitor_reason fsm_os__incompatible(struct repository *r)
{
enum fsmonitor_reason reason;
reason = check_vfs4git(r);
if (reason != FSMONITOR_REASON_OK)
return reason;
reason = check_remote(r);
if (reason != FSMONITOR_REASON_OK)
return reason;
return FSMONITOR_REASON_OK;
}