1 //! An implementation of `loader::Handle`, based on `walkdir` and `notify`.
3 //! The file watching bits here are untested and quite probably buggy. For this
4 //! reason, by default we don't watch files and rely on editor's file watching
7 //! Hopefully, one day a reliable file watching/walking crate appears on
8 //! crates.io, and we can reduce this to trivial glue code.
10 #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
14 use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
15 use notify::{RecommendedWatcher, RecursiveMode, Watcher};
16 use paths::{AbsPath, AbsPathBuf};
21 pub struct NotifyHandle {
22 // Relative order of fields below is significant.
23 sender: Sender<Message>,
24 _thread: jod_thread::JoinHandle,
29 Config(loader::Config),
30 Invalidate(AbsPathBuf),
33 impl loader::Handle for NotifyHandle {
34 fn spawn(sender: loader::Sender) -> NotifyHandle {
35 let actor = NotifyActor::new(sender);
36 let (sender, receiver) = unbounded::<Message>();
37 let thread = jod_thread::Builder::new()
38 .name("VfsLoader".to_owned())
39 .spawn(move || actor.run(receiver))
40 .expect("failed to spawn thread");
41 NotifyHandle { sender, _thread: thread }
44 fn set_config(&mut self, config: loader::Config) {
45 self.sender.send(Message::Config(config)).unwrap();
48 fn invalidate(&mut self, path: AbsPathBuf) {
49 self.sender.send(Message::Invalidate(path)).unwrap();
52 fn load_sync(&mut self, path: &AbsPath) -> Option<Vec<u8>> {
57 type NotifyEvent = notify::Result<notify::Event>;
60 sender: loader::Sender,
61 watched_entries: Vec<loader::Entry>,
62 // Drop order is significant.
63 watcher: Option<(RecommendedWatcher, Receiver<NotifyEvent>)>,
69 NotifyEvent(NotifyEvent),
73 fn new(sender: loader::Sender) -> NotifyActor {
74 NotifyActor { sender, watched_entries: Vec::new(), watcher: None }
77 fn next_event(&self, receiver: &Receiver<Message>) -> Option<Event> {
78 let watcher_receiver = self.watcher.as_ref().map(|(_, receiver)| receiver);
80 recv(receiver) -> it => it.ok().map(Event::Message),
81 recv(watcher_receiver.unwrap_or(&never())) -> it => Some(Event::NotifyEvent(it.unwrap())),
85 fn run(mut self, inbox: Receiver<Message>) {
86 while let Some(event) = self.next_event(&inbox) {
87 tracing::debug!(?event, "vfs-notify event");
89 Event::Message(msg) => match msg {
90 Message::Config(config) => {
92 if !config.watch.is_empty() {
93 let (watcher_sender, watcher_receiver) = unbounded();
94 let watcher = log_notify_error(RecommendedWatcher::new(move |event| {
95 watcher_sender.send(event).unwrap();
97 self.watcher = watcher.map(|it| (it, watcher_receiver));
100 let config_version = config.version;
102 let n_total = config.load.len();
103 self.send(loader::Message::Progress { n_total, n_done: 0, config_version });
105 self.watched_entries.clear();
107 for (i, entry) in config.load.into_iter().enumerate() {
108 let watch = config.watch.contains(&i);
110 self.watched_entries.push(entry.clone());
112 let files = self.load_entry(entry, watch);
113 self.send(loader::Message::Loaded { files });
114 self.send(loader::Message::Progress {
121 Message::Invalidate(path) => {
122 let contents = read(path.as_path());
123 let files = vec![(path, contents)];
124 self.send(loader::Message::Loaded { files });
127 Event::NotifyEvent(event) => {
128 if let Some(event) = log_notify_error(event) {
132 .map(|path| AbsPathBuf::try_from(path).unwrap())
134 let meta = fs::metadata(&path).ok()?;
135 if meta.file_type().is_dir()
139 .any(|entry| entry.contains_dir(&path))
145 if !meta.file_type().is_file() {
151 .any(|entry| entry.contains_file(&path))
156 let contents = read(&path);
157 Some((path, contents))
160 self.send(loader::Message::Loaded { files });
168 entry: loader::Entry,
170 ) -> Vec<(AbsPathBuf, Option<Vec<u8>>)> {
172 loader::Entry::Files(files) => files
176 self.watch(file.clone());
178 let contents = read(file.as_path());
181 .collect::<Vec<_>>(),
182 loader::Entry::Directories(dirs) => {
183 let mut res = Vec::new();
185 for root in &dirs.include {
187 WalkDir::new(root).follow_links(true).into_iter().filter_entry(|entry| {
188 if !entry.file_type().is_dir() {
191 let path = AbsPath::assert(entry.path());
193 || dirs.exclude.iter().chain(&dirs.include).all(|it| it != path)
196 let files = walkdir.filter_map(|it| it.ok()).filter_map(|entry| {
197 let is_dir = entry.file_type().is_dir();
198 let is_file = entry.file_type().is_file();
199 let abs_path = AbsPathBuf::assert(entry.into_path());
201 self.watch(abs_path.clone());
206 let ext = abs_path.extension().unwrap_or_default();
207 if dirs.extensions.iter().all(|it| it.as_str() != ext) {
213 res.extend(files.map(|file| {
214 let contents = read(file.as_path());
223 fn watch(&mut self, path: AbsPathBuf) {
224 if let Some((watcher, _)) = &mut self.watcher {
225 log_notify_error(watcher.watch(path.as_ref(), RecursiveMode::NonRecursive));
228 fn send(&mut self, msg: loader::Message) {
233 fn read(path: &AbsPath) -> Option<Vec<u8>> {
234 std::fs::read(path).ok()
237 fn log_notify_error<T>(res: notify::Result<T>) -> Option<T> {
238 res.map_err(|err| tracing::warn!("notify error: {}", err)).ok()