diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-06-23 16:53:56 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-23 16:53:56 +0100 |
commit | 0c12c4f9609ee72487af9b55a558b01af73ffe3e (patch) | |
tree | 29be52a980b4cae72f46a48c48135a15e31641e0 /crates/vfs-notify/src | |
parent | 7aa66371ee3e8b31217513204c8b4f683584419d (diff) | |
parent | dad1333b48c38bc7a5628fc0ff5304d003776a85 (diff) |
Merge #4976
4976: New VFS r=matklad a=matklad
Still a draft, but mostly working already.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/vfs-notify/src')
-rw-r--r-- | crates/vfs-notify/src/include.rs | 43 | ||||
-rw-r--r-- | crates/vfs-notify/src/lib.rs | 247 |
2 files changed, 290 insertions, 0 deletions
diff --git a/crates/vfs-notify/src/include.rs b/crates/vfs-notify/src/include.rs new file mode 100644 index 000000000..7378766f5 --- /dev/null +++ b/crates/vfs-notify/src/include.rs | |||
@@ -0,0 +1,43 @@ | |||
1 | //! See `Include`. | ||
2 | |||
3 | use std::convert::TryFrom; | ||
4 | |||
5 | use globset::{Glob, GlobSet, GlobSetBuilder}; | ||
6 | use paths::{RelPath, RelPathBuf}; | ||
7 | |||
8 | /// `Include` is the opposite of .gitignore. | ||
9 | /// | ||
10 | /// It describes the set of files inside some directory. | ||
11 | /// | ||
12 | /// The current implementation is very limited, it allows white-listing file | ||
13 | /// globs and black-listing directories. | ||
14 | #[derive(Debug, Clone)] | ||
15 | pub(crate) struct Include { | ||
16 | include_files: GlobSet, | ||
17 | exclude_dirs: Vec<RelPathBuf>, | ||
18 | } | ||
19 | |||
20 | impl Include { | ||
21 | pub(crate) fn new(include: Vec<String>) -> Include { | ||
22 | let mut include_files = GlobSetBuilder::new(); | ||
23 | let mut exclude_dirs = Vec::new(); | ||
24 | |||
25 | for glob in include { | ||
26 | if glob.starts_with("!/") { | ||
27 | if let Ok(path) = RelPathBuf::try_from(&glob["!/".len()..]) { | ||
28 | exclude_dirs.push(path) | ||
29 | } | ||
30 | } else { | ||
31 | include_files.add(Glob::new(&glob).unwrap()); | ||
32 | } | ||
33 | } | ||
34 | let include_files = include_files.build().unwrap(); | ||
35 | Include { include_files, exclude_dirs } | ||
36 | } | ||
37 | pub(crate) fn include_file(&self, path: &RelPath) -> bool { | ||
38 | self.include_files.is_match(path) | ||
39 | } | ||
40 | pub(crate) fn exclude_dir(&self, path: &RelPath) -> bool { | ||
41 | self.exclude_dirs.iter().any(|excluded| path.starts_with(excluded)) | ||
42 | } | ||
43 | } | ||
diff --git a/crates/vfs-notify/src/lib.rs b/crates/vfs-notify/src/lib.rs new file mode 100644 index 000000000..baee6ddc8 --- /dev/null +++ b/crates/vfs-notify/src/lib.rs | |||
@@ -0,0 +1,247 @@ | |||
1 | //! An implementation of `loader::Handle`, based on `walkdir` and `notify`. | ||
2 | //! | ||
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 | ||
5 | //! capabilities. | ||
6 | //! | ||
7 | //! Hopefully, one day a reliable file watching/walking crate appears on | ||
8 | //! crates.io, and we can reduce this to trivial glue code. | ||
9 | mod include; | ||
10 | |||
11 | use std::convert::{TryFrom, TryInto}; | ||
12 | |||
13 | use crossbeam_channel::{select, unbounded, Receiver}; | ||
14 | use notify::{RecommendedWatcher, RecursiveMode, Watcher}; | ||
15 | use paths::{AbsPath, AbsPathBuf}; | ||
16 | use rustc_hash::FxHashSet; | ||
17 | use vfs::loader; | ||
18 | use walkdir::WalkDir; | ||
19 | |||
20 | use crate::include::Include; | ||
21 | |||
22 | #[derive(Debug)] | ||
23 | pub struct LoaderHandle { | ||
24 | // Relative order of fields below is significant. | ||
25 | sender: crossbeam_channel::Sender<Message>, | ||
26 | _thread: jod_thread::JoinHandle, | ||
27 | } | ||
28 | |||
29 | #[derive(Debug)] | ||
30 | enum Message { | ||
31 | Config(loader::Config), | ||
32 | Invalidate(AbsPathBuf), | ||
33 | } | ||
34 | |||
35 | impl loader::Handle for LoaderHandle { | ||
36 | fn spawn(sender: loader::Sender) -> LoaderHandle { | ||
37 | let actor = LoaderActor::new(sender); | ||
38 | let (sender, receiver) = unbounded::<Message>(); | ||
39 | let thread = jod_thread::spawn(move || actor.run(receiver)); | ||
40 | LoaderHandle { sender, _thread: thread } | ||
41 | } | ||
42 | fn set_config(&mut self, config: loader::Config) { | ||
43 | self.sender.send(Message::Config(config)).unwrap() | ||
44 | } | ||
45 | fn invalidate(&mut self, path: AbsPathBuf) { | ||
46 | self.sender.send(Message::Invalidate(path)).unwrap(); | ||
47 | } | ||
48 | fn load_sync(&mut self, path: &AbsPathBuf) -> Option<Vec<u8>> { | ||
49 | read(path) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | type NotifyEvent = notify::Result<notify::Event>; | ||
54 | |||
55 | struct LoaderActor { | ||
56 | config: Vec<(AbsPathBuf, Include, bool)>, | ||
57 | watched_paths: FxHashSet<AbsPathBuf>, | ||
58 | sender: loader::Sender, | ||
59 | // Drop order of fields bellow is significant, | ||
60 | watcher: Option<RecommendedWatcher>, | ||
61 | watcher_receiver: Receiver<NotifyEvent>, | ||
62 | } | ||
63 | |||
64 | #[derive(Debug)] | ||
65 | enum Event { | ||
66 | Message(Message), | ||
67 | NotifyEvent(NotifyEvent), | ||
68 | } | ||
69 | |||
70 | impl LoaderActor { | ||
71 | fn new(sender: loader::Sender) -> LoaderActor { | ||
72 | let (watcher_sender, watcher_receiver) = unbounded(); | ||
73 | let watcher = log_notify_error(Watcher::new_immediate(move |event| { | ||
74 | watcher_sender.send(event).unwrap() | ||
75 | })); | ||
76 | |||
77 | LoaderActor { | ||
78 | watcher, | ||
79 | watcher_receiver, | ||
80 | watched_paths: FxHashSet::default(), | ||
81 | sender, | ||
82 | config: Vec::new(), | ||
83 | } | ||
84 | } | ||
85 | |||
86 | fn run(mut self, receiver: Receiver<Message>) { | ||
87 | while let Some(event) = self.next_event(&receiver) { | ||
88 | log::debug!("vfs-notify event: {:?}", event); | ||
89 | match event { | ||
90 | Event::Message(msg) => match msg { | ||
91 | Message::Config(config) => { | ||
92 | let n_entries_total = config.load.len(); | ||
93 | self.send(loader::Message::Progress { n_entries_total, n_entries_done: 0 }); | ||
94 | |||
95 | self.unwatch_all(); | ||
96 | self.config.clear(); | ||
97 | |||
98 | for (i, entry) in config.load.into_iter().enumerate() { | ||
99 | let watch = config.watch.contains(&i); | ||
100 | let files = self.load_entry(entry, watch); | ||
101 | self.send(loader::Message::Loaded { files }); | ||
102 | self.send(loader::Message::Progress { | ||
103 | n_entries_total, | ||
104 | n_entries_done: i + 1, | ||
105 | }); | ||
106 | } | ||
107 | self.config.sort_by(|x, y| x.0.cmp(&y.0)); | ||
108 | } | ||
109 | Message::Invalidate(path) => { | ||
110 | let contents = read(path.as_path()); | ||
111 | let files = vec![(path, contents)]; | ||
112 | self.send(loader::Message::Loaded { files }); | ||
113 | } | ||
114 | }, | ||
115 | Event::NotifyEvent(event) => { | ||
116 | if let Some(event) = log_notify_error(event) { | ||
117 | let files = event | ||
118 | .paths | ||
119 | .into_iter() | ||
120 | .map(|path| AbsPathBuf::try_from(path).unwrap()) | ||
121 | .filter_map(|path| { | ||
122 | let is_dir = path.is_dir(); | ||
123 | let is_file = path.is_file(); | ||
124 | |||
125 | let config_idx = | ||
126 | match self.config.binary_search_by(|it| it.0.cmp(&path)) { | ||
127 | Ok(it) => it, | ||
128 | Err(it) => it.saturating_sub(1), | ||
129 | }; | ||
130 | let include = self.config.get(config_idx).and_then(|it| { | ||
131 | let rel_path = path.strip_prefix(&it.0)?; | ||
132 | Some((rel_path, &it.1)) | ||
133 | }); | ||
134 | |||
135 | if let Some((rel_path, include)) = include { | ||
136 | if is_dir && include.exclude_dir(&rel_path) | ||
137 | || is_file && !include.include_file(&rel_path) | ||
138 | { | ||
139 | return None; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | if is_dir { | ||
144 | self.watch(path); | ||
145 | return None; | ||
146 | } | ||
147 | if !is_file { | ||
148 | return None; | ||
149 | } | ||
150 | let contents = read(&path); | ||
151 | Some((path, contents)) | ||
152 | }) | ||
153 | .collect(); | ||
154 | self.send(loader::Message::Loaded { files }) | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | } | ||
160 | fn next_event(&self, receiver: &Receiver<Message>) -> Option<Event> { | ||
161 | select! { | ||
162 | recv(receiver) -> it => it.ok().map(Event::Message), | ||
163 | recv(&self.watcher_receiver) -> it => Some(Event::NotifyEvent(it.unwrap())), | ||
164 | } | ||
165 | } | ||
166 | fn load_entry( | ||
167 | &mut self, | ||
168 | entry: loader::Entry, | ||
169 | watch: bool, | ||
170 | ) -> Vec<(AbsPathBuf, Option<Vec<u8>>)> { | ||
171 | match entry { | ||
172 | loader::Entry::Files(files) => files | ||
173 | .into_iter() | ||
174 | .map(|file| { | ||
175 | if watch { | ||
176 | self.watch(file.clone()) | ||
177 | } | ||
178 | let contents = read(file.as_path()); | ||
179 | (file, contents) | ||
180 | }) | ||
181 | .collect::<Vec<_>>(), | ||
182 | loader::Entry::Directory { path, include } => { | ||
183 | let include = Include::new(include); | ||
184 | self.config.push((path.clone(), include.clone(), watch)); | ||
185 | |||
186 | let files = WalkDir::new(&path) | ||
187 | .into_iter() | ||
188 | .filter_entry(|entry| { | ||
189 | let abs_path: &AbsPath = entry.path().try_into().unwrap(); | ||
190 | match abs_path.strip_prefix(&path) { | ||
191 | Some(rel_path) => { | ||
192 | !(entry.file_type().is_dir() && include.exclude_dir(rel_path)) | ||
193 | } | ||
194 | None => false, | ||
195 | } | ||
196 | }) | ||
197 | .filter_map(|entry| entry.ok()) | ||
198 | .filter_map(|entry| { | ||
199 | let is_dir = entry.file_type().is_dir(); | ||
200 | let is_file = entry.file_type().is_file(); | ||
201 | let abs_path = AbsPathBuf::try_from(entry.into_path()).unwrap(); | ||
202 | if is_dir { | ||
203 | self.watch(abs_path.clone()); | ||
204 | } | ||
205 | let rel_path = abs_path.strip_prefix(&path)?; | ||
206 | if is_file && include.include_file(&rel_path) { | ||
207 | Some(abs_path) | ||
208 | } else { | ||
209 | None | ||
210 | } | ||
211 | }); | ||
212 | |||
213 | files | ||
214 | .map(|file| { | ||
215 | let contents = read(file.as_path()); | ||
216 | (file, contents) | ||
217 | }) | ||
218 | .collect() | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | |||
223 | fn watch(&mut self, path: AbsPathBuf) { | ||
224 | if let Some(watcher) = &mut self.watcher { | ||
225 | log_notify_error(watcher.watch(&path, RecursiveMode::NonRecursive)); | ||
226 | self.watched_paths.insert(path); | ||
227 | } | ||
228 | } | ||
229 | fn unwatch_all(&mut self) { | ||
230 | if let Some(watcher) = &mut self.watcher { | ||
231 | for path in self.watched_paths.drain() { | ||
232 | log_notify_error(watcher.unwatch(path)); | ||
233 | } | ||
234 | } | ||
235 | } | ||
236 | fn send(&mut self, msg: loader::Message) { | ||
237 | (self.sender)(msg) | ||
238 | } | ||
239 | } | ||
240 | |||
241 | fn read(path: &AbsPath) -> Option<Vec<u8>> { | ||
242 | std::fs::read(path).ok() | ||
243 | } | ||
244 | |||
245 | fn log_notify_error<T>(res: notify::Result<T>) -> Option<T> { | ||
246 | res.map_err(|err| log::warn!("notify error: {}", err)).ok() | ||
247 | } | ||