Debian Codegraph @

lib/src/fsmonitor.rs

raw
1 // Copyright 2023 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Filesystem monitor tool interface.
16 //!
17 //! Interfaces with a filesystem monitor tool to efficiently query for
18 //! filesystem updates, without having to crawl the entire working copy. This is
19 //! particularly useful for large working copies, or for working copies for
20 //! which it's expensive to materialize files, such those backed by a network or
21 //! virtualized filesystem.
22
23 #![warn(missing_docs)]
24
25 use std::path::PathBuf;
26
27 use crate::config::ConfigGetError;
28 use crate::settings::UserSettings;
29
30 /// Config for Watchman filesystem monitor (<https://facebook.github.io/watchman/>).
31 #[derive(Eq, PartialEq, Clone, Debug)]
32 pub struct WatchmanConfig {
33 /// Whether to use triggers to monitor for changes in the background.
34 pub register_trigger: bool,
35 }
36
37 /// The recognized kinds of filesystem monitors.
38 #[derive(Eq, PartialEq, Clone, Debug)]
39 pub enum FsmonitorSettings {
40 /// The Watchman filesystem monitor (<https://facebook.github.io/watchman/>).
41 Watchman(WatchmanConfig),
42
43 /// Only used in tests.
44 Test {
45 /// The set of changed files to pretend that the filesystem monitor is
46 /// reporting.
47 changed_files: Vec<PathBuf>,
48 },
49
50 /// No filesystem monitor. This is the default if nothing is configured, but
51 /// also makes it possible to turn off the monitor on a case-by-case basis
52 /// when the user gives an option like `--config=fsmonitor.backend=none`;
53 /// useful when e.g. doing analysis of snapshot performance.
54 None,
55 }
56
57 impl FsmonitorSettings {
58 /// Creates an `FsmonitorSettings` from a `config`.
59 pub fn from_settings(settings: &UserSettings) -> Result<Self, ConfigGetError> {
60 let name = "fsmonitor.backend";
61 match settings.get_string(name)?.as_ref() {
62 "watchman" => Ok(Self::Watchman(WatchmanConfig {
63 register_trigger: settings
64 .get_bool("fsmonitor.watchman.register-snapshot-trigger")?,
65 })),
66 "test" => Err(ConfigGetError::Type {
67 name: name.to_owned(),
68 error: "Cannot use test fsmonitor in real repository".into(),
69 source_path: None,
70 }),
71 "none" => Ok(Self::None),
72 other => Err(ConfigGetError::Type {
73 name: name.to_owned(),
74 error: format!("Unknown fsmonitor kind: {other}").into(),
75 source_path: None,
76 }),
77 }
78 }
79 }
80
81 /// Filesystem monitor integration using Watchman
82 /// (<https://facebook.github.io/watchman/>). Requires `watchman` to already be
83 /// installed on the system.
84 #[cfg(feature = "watchman")]
85 pub mod watchman {
86 use std::path::Path;
87 use std::path::PathBuf;
88
89 use itertools::Itertools as _;
90 use thiserror::Error;
91 use tracing::info;
92 use tracing::instrument;
93 use watchman_client::expr;
94 use watchman_client::prelude::Clock as InnerClock;
95 use watchman_client::prelude::ClockSpec;
96 use watchman_client::prelude::NameOnly;
97 use watchman_client::prelude::QueryRequestCommon;
98 use watchman_client::prelude::QueryResult;
99 use watchman_client::prelude::TriggerRequest;
100
101 /// Represents an instance in time from the perspective of the filesystem
102 /// monitor.
103 ///
104 /// This can be used to perform incremental queries. When making a query,
105 /// the result will include an associated "clock" representing the time
106 /// that the query was made. By passing the same clock into a future
107 /// query, we inform the filesystem monitor that we only wish to get
108 /// changed files since the previous point in time.
109 #[derive(Clone, Debug)]
110 pub struct Clock(InnerClock);
111
112 impl From<crate::protos::local_working_copy::WatchmanClock> for Clock {
113 fn from(clock: crate::protos::local_working_copy::WatchmanClock) -> Self {
114 use crate::protos::local_working_copy::watchman_clock::WatchmanClock;
115 let watchman_clock = clock.watchman_clock.unwrap();
116 let clock = match watchman_clock {
117 WatchmanClock::StringClock(string_clock) => {
118 InnerClock::Spec(ClockSpec::StringClock(string_clock))
119 }
120 WatchmanClock::UnixTimestamp(unix_timestamp) => {
121 InnerClock::Spec(ClockSpec::UnixTimestamp(unix_timestamp))
122 }
123 };
124 Self(clock)
125 }
126 }
127
128 impl From<Clock> for crate::protos::local_working_copy::WatchmanClock {
129 fn from(clock: Clock) -> Self {
130 use crate::protos::local_working_copy::watchman_clock;
131 let Clock(clock) = clock;
132 let watchman_clock = match clock {
133 InnerClock::Spec(ClockSpec::StringClock(string_clock)) => {
134 watchman_clock::WatchmanClock::StringClock(string_clock)
135 }
136 InnerClock::Spec(ClockSpec::UnixTimestamp(unix_timestamp)) => {
137 watchman_clock::WatchmanClock::UnixTimestamp(unix_timestamp)
138 }
139 InnerClock::ScmAware(_) => {
140 unimplemented!("SCM-aware Watchman clocks not supported")
141 }
142 };
143 Self {
144 watchman_clock: Some(watchman_clock),
145 }
146 }
147 }
148
149 #[expect(missing_docs)]
150 #[derive(Debug, Error)]
151 pub enum Error {
152 #[error("Could not connect to Watchman")]
153 WatchmanConnectError(#[source] watchman_client::Error),
154
155 #[error("Could not canonicalize working copy root path")]
156 CanonicalizeRootError(#[source] std::io::Error),
157
158 #[error("Watchman failed to resolve the working copy root path")]
159 ResolveRootError(#[source] watchman_client::Error),
160
161 #[error("Failed to query Watchman")]
162 WatchmanQueryError(#[source] watchman_client::Error),
163
164 #[error("Failed to register Watchman trigger")]
165 WatchmanTriggerError(#[source] watchman_client::Error),
166 }
167
168 /// Handle to the underlying Watchman instance.
169 pub struct Fsmonitor {
170 client: watchman_client::Client,
171 resolved_root: watchman_client::ResolvedRoot,
172 }
173
174 impl Fsmonitor {
175 /// Initialize the Watchman filesystem monitor. If it's not already
176 /// running, this will start it and have it crawl the working
177 /// copy to build up its in-memory representation of the
178 /// filesystem, which may take some time.
179 #[instrument]
180 pub async fn init(
181 working_copy_path: &Path,
182 config: &super::WatchmanConfig,
183 ) -> Result<Self, Error> {
184 info!("Initializing Watchman filesystem monitor...");
185 let connector = watchman_client::Connector::new();
186 let client = connector
187 .connect()
188 .await
189 .map_err(Error::WatchmanConnectError)?;
190 let working_copy_root = watchman_client::CanonicalPath::canonicalize(working_copy_path)
191 .map_err(Error::CanonicalizeRootError)?;
192 let resolved_root = client
193 .resolve_root(working_copy_root)
194 .await
195 .map_err(Error::ResolveRootError)?;
196
197 let monitor = Self {
198 client,
199 resolved_root,
200 };
201
202 // Registering the trigger causes an unconditional evaluation of the query, so
203 // test if it is already registered first.
204 if !config.register_trigger {
205 monitor.unregister_trigger().await?;
206 } else if !monitor.is_trigger_registered().await? {
207 monitor.register_trigger().await?;
208 }
209 Ok(monitor)
210 }
211
212 /// Query for changed files since the previous point in time.
213 ///
214 /// The returned list of paths is relative to the `working_copy_path`.
215 /// If it is `None`, then the caller must crawl the entire working copy
216 /// themselves.
217 #[instrument(skip(self))]
218 pub async fn query_changed_files(
219 &self,
220 previous_clock: Option<Clock>,
221 ) -> Result<(Clock, Option<Vec<PathBuf>>), Error> {
222 // TODO: might be better to specify query options by caller, but we
223 // shouldn't expose the underlying watchman API too much.
224 info!("Querying Watchman for changed files...");
225 let QueryResult {
226 version: _,
227 is_fresh_instance,
228 files,
229 clock,
230 state_enter: _,
231 state_leave: _,
232 state_metadata: _,
233 saved_state_info: _,
234 debug: _,
235 }: QueryResult<NameOnly> = self
236 .client
237 .query(
238 &self.resolved_root,
239 QueryRequestCommon {
240 since: previous_clock.map(|Clock(clock)| clock),
241 expression: Some(self.build_exclude_expr()),
242 ..Default::default()
243 },
244 )
245 .await
246 .map_err(Error::WatchmanQueryError)?;
247
248 let clock = Clock(clock);
249 if is_fresh_instance {
250 // The Watchman documentation states that if it was a fresh
251 // instance, we need to delete any tree entries that didn't appear
252 // in the returned list of changed files. For now, the caller will
253 // handle this by manually crawling the working copy again.
254 Ok((clock, None))
255 } else {
256 let paths = files
257 .unwrap_or_default()
258 .into_iter()
259 .map(|NameOnly { name }| name.into_inner())
260 .collect_vec();
261 Ok((clock, Some(paths)))
262 }
263 }
264
265 /// Return whether or not a trigger has been registered already.
266 #[instrument(skip(self))]
267 pub async fn is_trigger_registered(&self) -> Result<bool, Error> {
268 info!("Checking for an existing Watchman trigger...");
269 Ok(self
270 .client
271 .list_triggers(&self.resolved_root)
272 .await
273 .map_err(Error::WatchmanTriggerError)?
274 .triggers
275 .iter()
276 .any(|t| t.name == "jj-background-monitor"))
277 }
278
279 /// Register trigger for changed files.
280 #[instrument(skip(self))]
281 async fn register_trigger(&self) -> Result<(), Error> {
282 info!("Registering Watchman trigger...");
283 let null = if cfg!(windows) { ">NUL" } else { ">/dev/null" };
284 self.client
285 .register_trigger(
286 &self.resolved_root,
287 TriggerRequest {
288 name: "jj-background-monitor".to_string(),
289 command: vec![
290 "jj".to_string(),
291 "--quiet".to_string(),
292 "util".to_string(),
293 "snapshot".to_string(),
294 ],
295 expression: Some(self.build_exclude_expr()),
296 stderr: Some(null.into()),
297 stdout: Some(null.into()),
298 ..Default::default()
299 },
300 )
301 .await
302 .map_err(Error::WatchmanTriggerError)?;
303 Ok(())
304 }
305
306 /// Register trigger for changed files.
307 #[instrument(skip(self))]
308 async fn unregister_trigger(&self) -> Result<(), Error> {
309 info!("Unregistering Watchman trigger...");
310 self.client
311 .remove_trigger(&self.resolved_root, "jj-background-monitor")
312 .await
313 .map_err(Error::WatchmanTriggerError)?;
314 Ok(())
315 }
316
317 /// Build an exclude expr for `working_copy_path`.
318 fn build_exclude_expr(&self) -> expr::Expr {
319 // TODO: consider parsing `.gitignore`.
320 let exclude_dirs = [Path::new(".git"), Path::new(".jj")];
321 let excludes = itertools::chain(
322 // the directories themselves
323 [expr::Expr::Name(expr::NameTerm {
324 paths: exclude_dirs.iter().map(|&name| name.to_owned()).collect(),
325 wholename: true,
326 })],
327 // and all files under the directories
328 exclude_dirs.iter().map(|&name| {
329 expr::Expr::DirName(expr::DirNameTerm {
330 path: name.to_owned(),
331 depth: None,
332 })
333 }),
334 )
335 .collect();
336 expr::Expr::Not(Box::new(expr::Expr::Any(excludes)))
337 }
338 }
339 }