fsmonitor--daemon: cd out of worktree root
Teach the fsmonitor--daemon to CD outside of the worktree before starting up. The common Git startup mechanism causes the CWD of the daemon process to be in the root of the worktree. On Windows, this causes the daemon process to hold a locked handle on the CWD and prevents other processes from moving or deleting the worktree while the daemon is running. CD to HOME before entering main event loops. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff Hostetler committed
May 26, 2022 at 21:47 UTC
39664e93093bd9545ad4085523b122196c449508
3 files changed
+47
-8
builtin/fsmonitor--daemon.c
+30
-2
@@ -1181,11 +1181,11 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
1181
* before we need it.
1182
*/
1183
if (ipc_server_run_async(&state->ipc_server_data,
1184
- fsmonitor_ipc__get_path(), &ipc_opts,
1184
+ state->path_ipc.buf, &ipc_opts,
1185
handle_client, state))
1186
return error_errno(
1187
_("could not start IPC thread pool on '%s'"),
1188
- fsmonitor_ipc__get_path());
1188
+ state->path_ipc.buf);
1189
1190
/*
1191
* Start the fsmonitor listener thread to collect filesystem
@@ -1220,6 +1220,7 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
1220
static int fsmonitor_run_daemon(void)
1221
{
1222
struct fsmonitor_daemon_state state;
1223
+ const char *home;
1224
int err;
1225
1226
memset(&state, 0, sizeof(state));
@@ -1289,6 +1290,15 @@ static int fsmonitor_run_daemon(void)
1290
1291
strbuf_addch(&state.path_cookie_prefix, '/');
1292
1293
+ /*
1294
+ * We create a named-pipe or unix domain socket inside of the
1295
+ * ".git" directory. (Well, on Windows, we base our named
1296
+ * pipe in the NPFS on the absolute path of the git
1297
+ * directory.)
1298
+ */
1299
+ strbuf_init(&state.path_ipc, 0);
1300
+ strbuf_addstr(&state.path_ipc, absolute_path(fsmonitor_ipc__get_path()));
1301
+
1302
/*
1303
* Confirm that we can create platform-specific resources for the
1304
* filesystem listener before we bother starting all the threads.
@@ -1298,6 +1308,23 @@ static int fsmonitor_run_daemon(void)
1308
goto done;
1309
}
1310
1311
+ /*
1312
+ * CD out of the worktree root directory.
1313
+ *
1314
+ * The common Git startup mechanism causes our CWD to be the
1315
+ * root of the worktree. On Windows, this causes our process
1316
+ * to hold a locked handle on the CWD. This prevents the
1317
+ * worktree from being moved or deleted while the daemon is
1318
+ * running.
1319
+ *
1320
+ * We assume that our FS and IPC listener threads have either
1321
+ * opened all of the handles that they need or will do
1322
+ * everything using absolute paths.
1323
+ */
1324
+ home = getenv("HOME");
1325
+ if (home && *home && chdir(home))
1326
+ die_errno(_("could not cd home '%s'"), home);
1327
+
1328
err = fsmonitor_run_daemon_1(&state);
1329
1330
done:
@@ -1310,6 +1337,7 @@ done:
1337
strbuf_release(&state.path_worktree_watch);
1338
strbuf_release(&state.path_gitdir_watch);
1339
strbuf_release(&state.path_cookie_prefix);
1340
+ strbuf_release(&state.path_ipc);
1341
1342
return err;
1343
}
compat/fsmonitor/fsm-listen-win32.c
+16
-6
@@ -424,12 +424,22 @@ static int recv_rdcw_watch(struct one_watch *watch)
424
}
425
426
/*
427
- * NEEDSWORK: If an external <gitdir> is deleted, the above
428
- * returns an error. I'm not sure that there's anything that
429
- * we can do here other than failing -- the <worktree>/.git
430
- * link file would be broken anyway. We might try to check
431
- * for that and return a better error message, but I'm not
432
- * sure it is worth it.
427
+ * GetOverlappedResult() fails if the watched directory is
428
+ * deleted while we were waiting for an overlapped IO to
429
+ * complete. The documentation did not list specific errors,
430
+ * but I observed ERROR_ACCESS_DENIED (0x05) errors during
431
+ * testing.
432
+ *
433
+ * Note that we only get notificaiton events for events
434
+ * *within* the directory, not *on* the directory itself.
435
+ * (These might be properies of the parent directory, for
436
+ * example).
437
+ *
438
+ * NEEDSWORK: We might try to check for the deleted directory
439
+ * case and return a better error message, but I'm not sure it
440
+ * is worth it.
441
+ *
442
+ * Shutdown if we get any error.
443
*/
444
445
error(_("GetOverlappedResult failed on '%s' [GLE %ld]"),
fsmonitor--daemon.h
+1
@@ -54,6 +54,7 @@ struct fsmonitor_daemon_state {
54
struct fsmonitor_daemon_backend_data *backend_data;
55
56
struct ipc_server_data *ipc_server_data;
57
+ struct strbuf path_ipc;
58
};
59
60
/*