1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::{
Repository, Result,
error::{OptionExt, ResultExt, message},
fsmonitor::server,
};
impl Repository {
/// Resolve daemon policy and startup timing from repository configuration.
///
/// `fsmonitor.allowRemote` defaults to false. `fsmonitor.startTimeout` is a nonnegative
/// number of seconds, defaulting to 60; zero allows one readiness check without waiting.
/// Only trusted configuration sections are used. Invalid values are rejected with
/// configuration-key context. No filesystem is probed.
#[cfg(feature = "fsmonitor-daemon")]
pub fn fsmonitor_daemon_options(&self) -> Result<crate::fsmonitor::daemon::Options> {
use crate::config::tree::FsMonitor;
let defaults = crate::fsmonitor::daemon::Options::default();
let mut trusted = self.filter_config_section();
Ok(crate::fsmonitor::daemon::Options {
allow_remote: FsMonitor::ALLOW_REMOTE
.enrich_error(
self.config
.resolved
.boolean_filter(FsMonitor::ALLOW_REMOTE, &mut trusted),
)?
.unwrap_or(defaults.allow_remote),
start_timeout: FsMonitor::START_TIMEOUT
.try_into_u64(
self.config
.resolved
.integer_filter(FsMonitor::START_TIMEOUT, &mut trusted),
)?
.map_or(defaults.start_timeout, std::time::Duration::from_secs),
})
}
/// Execute a foreground, detached-startup, or client filesystem-monitor command.
///
/// Captures [`fsmonitor_server_context()`](Self::fsmonitor_server_context) and
/// [`fsmonitor_daemon_options()`](Self::fsmonitor_daemon_options), then drops this repository
/// before serving or launching a child, retaining no repository caches.
/// `should_interrupt` supplies cancellation during serving and startup.
///
/// # Embedding a daemon
///
/// For an application with a `--repository PATH fsmonitor run` foreground command:
///
/// ```no_run
/// use gix::fsmonitor::daemon::Command;
///
/// fn fsmonitor(repo: gix::Repository, command: Command) -> gix::Result<()> {
/// repo.fsmonitor_daemon_execute(
/// command,
/// std::io::stdout().lock(),
/// || false,
/// |context| {
/// Ok(vec![
/// "--repository".into(),
/// context.worktree.as_os_str().to_owned(),
/// "fsmonitor".into(),
/// "run".into(),
/// ])
/// },
/// )
/// }
/// ```
///
/// Replace `|| false` with the application's cancellation check when needed. Select `sha1`
/// and/or `sha256` features for the object hashes the application needs.
///
/// `child_arguments` returns an **argument prefix**, not an executable path: the launcher
/// reexecutes the **current executable**, without requiring a standalone daemon binary.
/// The prefix must select that application's foreground `run` command and preserve any
/// application-specific options or configuration needed by the child. The launcher preserves
/// this repository's layout and conditional configuration, and appends `--idle-timeout` and
/// `--ready-file`; do not include either option in the prefix. The child must pass both to
/// [`Command::Run`](crate::fsmonitor::daemon::Command::Run), which reports its PID only after
/// server startup succeeds. Detached startup waits for that child's readiness notification,
/// not just an existing endpoint, and cleans up unsuccessful children. The callback is called
/// at most once, only when starting a child; commands such as `Path` do not invoke it.
///
/// See [`daemon::execute()`](crate::fsmonitor::daemon::execute) for output, startup readiness,
/// cleanup, and hook autostart behavior.
#[cfg(feature = "fsmonitor-daemon")]
pub fn fsmonitor_daemon_execute(
self,
command: crate::fsmonitor::daemon::Command,
out: impl std::io::Write,
should_interrupt: impl FnMut() -> bool,
child_arguments: impl FnOnce(&server::Context) -> Result<Vec<std::ffi::OsString>>,
) -> Result<()> {
let reopen_git_dir = self.current_dir().join(self.git_dir());
let context = self.fsmonitor_server_context()?;
let options = if matches!(command, crate::fsmonitor::daemon::Command::Path) {
Default::default()
} else {
self.fsmonitor_daemon_options()?
};
drop(self);
crate::fsmonitor::daemon::execute(
context,
&reopen_git_dir,
command,
options,
out,
should_interrupt,
child_arguments,
)
}
/// Resolve canonical repository paths, filesystem capabilities, and Git's native server endpoint.
///
/// Local Git directories use `fsmonitor--daemon.ipc`, ignoring `fsmonitor.socketDir`. On
/// supported Unix platforms, remote Git directories use the trusted, interpolated socket
/// directory or the permitted home directory. Relative socket directories use [`Self::current_dir()`].
/// Bare repositories and path/configuration errors are rejected. No server is started and no
/// repository handle is retained in the context.
pub fn fsmonitor_server_context(&self) -> Result<server::Context> {
let worktree = self
.workdir()
.ok_or_raise(|| message("filesystem monitoring requires a worktree"))?;
let worktree = self
.current_dir()
.join(worktree)
.canonicalize()
.or_raise(|| message("resolving the filesystem monitor worktree"))?;
let git_dir = self.current_dir().join(self.git_dir()).canonicalize().or_error()?;
let endpoint = server::endpoint_for_canonical_paths(&git_dir, &worktree, || {
match self
.config_snapshot()
.trusted_path(crate::config::tree::FsMonitor::SOCKET_DIR)?
{
Some(path) => Ok(self.current_dir().join(path)),
None => self.interpolate_path("~"),
}
})?;
let capabilities = self.config.fs_capabilities(None)?;
Ok(server::Context {
worktree,
git_dir,
common_dir: self.current_dir().join(self.common_dir()).canonicalize().or_error()?,
endpoint,
precompose_unicode: capabilities.precompose_unicode,
ignore_case: capabilities.ignore_case,
})
}
/// Start an application-owned (embedded) filesystem monitor serving this worktree to Git clients.
///
/// The server registers its native watches before returning and retains no repository handle.
/// Dropping the returned handle stops it; `options` can also configure shutdown after client
/// inactivity. An occupied endpoint or a repository without a worktree is an error.
///
/// Uses [`fsmonitor_server_context()`](Self::fsmonitor_server_context) to select the same native
/// endpoint that Git clients resolve, including alternate socket locations for remote Git directories.
pub fn fsmonitor_server(&self, options: server::Options) -> Result<server::Handle> {
server::spawn(self.fsmonitor_server_context()?, options)
.or_raise(|| message("starting the repository's filesystem monitor"))
}
/// Start a Tokio filesystem monitor and await complete native registration.
///
/// Requires `async-fsmonitor-server` and a Tokio runtime with I/O and timers enabled through
/// shutdown. The blocking [`Self::fsmonitor_server()`] remains available. Cancelling startup
/// requests safe cleanup; dropping the handle requests shutdown without joining this thread.
/// Await its `shutdown()` method to observe release of all watches and the endpoint.
#[cfg(feature = "async-fsmonitor-server")]
pub fn fsmonitor_server_async(
&self,
options: server::Options,
) -> impl std::future::Future<Output = Result<server::asynchronous::Handle>> + Send + 'static {
let context = self.fsmonitor_server_context();
async move {
server::asynchronous::spawn(context?, options)
.await
.or_raise(|| message("starting the repository's asynchronous filesystem monitor"))
}
}
}