2018-12-18 10:29:14 +01:00
|
|
|
use std::{
|
2019-01-12 18:17:52 +01:00
|
|
|
fmt, fs,
|
2018-12-18 10:29:14 +01:00
|
|
|
path::{Path, PathBuf},
|
2019-01-20 22:13:21 +01:00
|
|
|
sync::Arc,
|
|
|
|
thread,
|
2018-12-18 10:29:14 +01:00
|
|
|
};
|
|
|
|
|
2019-01-20 22:13:21 +01:00
|
|
|
use crossbeam_channel::{Receiver, Sender};
|
|
|
|
use parking_lot::Mutex;
|
2018-12-18 14:38:05 +01:00
|
|
|
use relative_path::RelativePathBuf;
|
2019-01-12 18:17:52 +01:00
|
|
|
use thread_worker::WorkerHandle;
|
2019-01-21 19:11:39 +01:00
|
|
|
use walkdir::WalkDir;
|
2018-12-18 11:35:05 +01:00
|
|
|
|
2019-01-21 18:59:54 +01:00
|
|
|
mod watcher;
|
|
|
|
use watcher::Watcher;
|
|
|
|
pub use watcher::WatcherChange;
|
|
|
|
|
2019-01-21 19:11:39 +01:00
|
|
|
use crate::{RootFilter, VfsRoot};
|
2018-12-18 11:35:05 +01:00
|
|
|
|
2019-01-07 21:35:18 +01:00
|
|
|
pub(crate) enum Task {
|
|
|
|
AddRoot {
|
|
|
|
root: VfsRoot,
|
|
|
|
path: PathBuf,
|
2019-01-21 19:11:39 +01:00
|
|
|
root_filter: Arc<RootFilter>,
|
|
|
|
nested_roots: Vec<PathBuf>,
|
2019-01-07 21:35:18 +01:00
|
|
|
},
|
2019-01-21 18:59:54 +01:00
|
|
|
/// this variant should only be created by the watcher
|
2019-01-16 19:30:20 +01:00
|
|
|
HandleChange(WatcherChange),
|
|
|
|
LoadChange(WatcherChange),
|
2019-01-20 22:13:21 +01:00
|
|
|
Watch {
|
|
|
|
dir: PathBuf,
|
2019-01-21 19:11:39 +01:00
|
|
|
root_filter: Arc<RootFilter>,
|
2019-01-20 22:13:21 +01:00
|
|
|
},
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
2018-12-18 10:29:14 +01:00
|
|
|
|
2019-01-07 21:35:18 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AddRootResult {
|
2018-12-18 14:38:05 +01:00
|
|
|
pub(crate) root: VfsRoot,
|
|
|
|
pub(crate) files: Vec<(RelativePathBuf, String)>,
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
2018-12-18 10:29:14 +01:00
|
|
|
|
2019-01-07 21:35:18 +01:00
|
|
|
#[derive(Debug)]
|
2019-01-12 18:17:52 +01:00
|
|
|
pub enum WatcherChangeData {
|
|
|
|
Create { path: PathBuf, text: String },
|
|
|
|
Write { path: PathBuf, text: String },
|
|
|
|
Remove { path: PathBuf },
|
2019-01-07 21:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum TaskResult {
|
|
|
|
AddRoot(AddRootResult),
|
2019-01-12 18:17:52 +01:00
|
|
|
HandleChange(WatcherChange),
|
2019-01-20 22:13:21 +01:00
|
|
|
LoadChange(WatcherChangeData),
|
|
|
|
NoOp,
|
2019-01-07 21:35:18 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:04:15 +01:00
|
|
|
impl fmt::Debug for TaskResult {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-01-22 18:38:34 +01:00
|
|
|
match self {
|
|
|
|
TaskResult::AddRoot(..) => f.write_str("TaskResult::AddRoot(..)"),
|
|
|
|
TaskResult::HandleChange(c) => write!(f, "TaskResult::HandleChange({:?})", c),
|
|
|
|
TaskResult::LoadChange(c) => write!(f, "TaskResult::LoadChange({:?})", c),
|
|
|
|
TaskResult::NoOp => f.write_str("TaskResult::NoOp"),
|
|
|
|
}
|
2018-12-19 13:04:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 22:13:21 +01:00
|
|
|
pub(crate) struct Worker {
|
|
|
|
worker: thread_worker::Worker<Task, TaskResult>,
|
|
|
|
worker_handle: WorkerHandle,
|
|
|
|
watcher: Arc<Mutex<Option<Watcher>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Worker {
|
|
|
|
pub(crate) fn start() -> Worker {
|
|
|
|
let watcher = Arc::new(Mutex::new(None));
|
|
|
|
let watcher_clone = watcher.clone();
|
|
|
|
let (worker, worker_handle) =
|
|
|
|
thread_worker::spawn("vfs", 128, move |input_receiver, output_sender| {
|
2019-01-21 18:37:46 +01:00
|
|
|
input_receiver
|
2019-01-20 22:13:21 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(|t| handle_task(t, &watcher_clone))
|
2019-01-21 18:37:46 +01:00
|
|
|
.try_for_each(|it| output_sender.send(it))
|
|
|
|
.unwrap()
|
2019-01-20 22:13:21 +01:00
|
|
|
});
|
|
|
|
match Watcher::start(worker.inp.clone()) {
|
|
|
|
Ok(w) => {
|
|
|
|
watcher.lock().replace(w);
|
|
|
|
}
|
|
|
|
Err(e) => log::error!("could not start watcher: {}", e),
|
|
|
|
};
|
|
|
|
Worker {
|
|
|
|
worker,
|
|
|
|
worker_handle,
|
|
|
|
watcher,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sender(&self) -> &Sender<Task> {
|
|
|
|
&self.worker.inp
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn receiver(&self) -> &Receiver<TaskResult> {
|
|
|
|
&self.worker.out
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn shutdown(self) -> thread::Result<()> {
|
|
|
|
if let Some(watcher) = self.watcher.lock().take() {
|
|
|
|
let _ = watcher.shutdown();
|
|
|
|
}
|
2019-01-21 18:37:46 +01:00
|
|
|
let _ = self.worker.shutdown();
|
2019-01-20 22:13:21 +01:00
|
|
|
self.worker_handle.shutdown()
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:35:05 +01:00
|
|
|
|
2019-01-20 22:13:21 +01:00
|
|
|
fn watch(
|
|
|
|
watcher: &Arc<Mutex<Option<Watcher>>>,
|
|
|
|
dir: &Path,
|
2019-01-21 19:11:39 +01:00
|
|
|
filter_entry: &RootFilter,
|
2019-01-20 22:13:21 +01:00
|
|
|
emit_for_existing: bool,
|
|
|
|
) {
|
|
|
|
let mut watcher = watcher.lock();
|
|
|
|
let watcher = match *watcher {
|
|
|
|
Some(ref mut w) => w,
|
|
|
|
None => {
|
|
|
|
// watcher dropped or couldn't start
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
watcher.watch_recursive(dir, filter_entry, emit_for_existing)
|
2018-12-18 11:35:05 +01:00
|
|
|
}
|
2018-12-18 11:23:23 +01:00
|
|
|
|
2019-01-20 22:13:21 +01:00
|
|
|
fn handle_task(task: Task, watcher: &Arc<Mutex<Option<Watcher>>>) -> TaskResult {
|
2019-01-07 21:35:18 +01:00
|
|
|
match task {
|
2019-01-21 19:11:39 +01:00
|
|
|
Task::AddRoot {
|
|
|
|
root,
|
|
|
|
path,
|
|
|
|
root_filter,
|
|
|
|
nested_roots,
|
|
|
|
} => {
|
2019-01-22 18:14:31 +01:00
|
|
|
watch(watcher, &path, root_filter.as_ref(), false);
|
2019-01-07 21:35:18 +01:00
|
|
|
log::debug!("loading {} ...", path.as_path().display());
|
2019-01-21 19:11:39 +01:00
|
|
|
let files = load_root(
|
|
|
|
path.as_path(),
|
|
|
|
root_filter.as_ref(),
|
|
|
|
nested_roots.as_slice(),
|
|
|
|
);
|
2019-01-07 21:35:18 +01:00
|
|
|
log::debug!("... loaded {}", path.as_path().display());
|
|
|
|
TaskResult::AddRoot(AddRootResult { root, files })
|
|
|
|
}
|
2019-01-16 19:30:20 +01:00
|
|
|
Task::HandleChange(change) => {
|
|
|
|
// forward as is because Vfs has to decide if we should load it
|
|
|
|
TaskResult::HandleChange(change)
|
|
|
|
}
|
2019-01-12 18:17:52 +01:00
|
|
|
Task::LoadChange(change) => {
|
|
|
|
log::debug!("loading {:?} ...", change);
|
2019-01-20 22:13:21 +01:00
|
|
|
match load_change(change) {
|
|
|
|
Some(data) => TaskResult::LoadChange(data),
|
|
|
|
None => TaskResult::NoOp,
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 19:11:39 +01:00
|
|
|
Task::Watch { dir, root_filter } => {
|
|
|
|
watch(watcher, &dir, root_filter.as_ref(), true);
|
2019-01-20 22:13:21 +01:00
|
|
|
TaskResult::NoOp
|
2019-01-07 21:35:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
2018-12-18 10:29:14 +01:00
|
|
|
|
2019-01-21 19:11:39 +01:00
|
|
|
fn load_root(
|
|
|
|
root: &Path,
|
|
|
|
root_filter: &RootFilter,
|
|
|
|
nested_roots: &[PathBuf],
|
|
|
|
) -> Vec<(RelativePathBuf, String)> {
|
2018-12-18 11:18:55 +01:00
|
|
|
let mut res = Vec::new();
|
2019-01-21 19:11:39 +01:00
|
|
|
for entry in WalkDir::new(root).into_iter().filter_entry(|entry| {
|
|
|
|
if entry.file_type().is_dir() && nested_roots.iter().any(|it| it == entry.path()) {
|
|
|
|
// do not load files of a nested root
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
root_filter.can_contain(entry.path()).is_some()
|
|
|
|
}
|
|
|
|
}) {
|
2018-12-18 11:18:55 +01:00
|
|
|
let entry = match entry {
|
|
|
|
Ok(entry) => entry,
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("watcher error: {}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !entry.file_type().is_file() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let path = entry.path();
|
|
|
|
let text = match fs::read_to_string(path) {
|
|
|
|
Ok(text) => text,
|
|
|
|
Err(e) => {
|
|
|
|
log::warn!("watcher error: {}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2018-12-18 14:38:05 +01:00
|
|
|
let path = RelativePathBuf::from_path(path.strip_prefix(root).unwrap()).unwrap();
|
|
|
|
res.push((path.to_owned(), text))
|
2018-12-18 11:18:55 +01:00
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
2019-01-12 18:17:52 +01:00
|
|
|
|
|
|
|
fn load_change(change: WatcherChange) -> Option<WatcherChangeData> {
|
|
|
|
let data = match change {
|
|
|
|
WatcherChange::Create(path) => {
|
2019-01-20 22:13:21 +01:00
|
|
|
if path.is_dir() {
|
|
|
|
return None;
|
|
|
|
}
|
2019-01-12 18:17:52 +01:00
|
|
|
let text = match fs::read_to_string(&path) {
|
|
|
|
Ok(text) => text,
|
|
|
|
Err(e) => {
|
2019-01-16 19:30:20 +01:00
|
|
|
log::warn!("watcher error \"{}\": {}", path.display(), e);
|
2019-01-12 18:17:52 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
WatcherChangeData::Create { path, text }
|
|
|
|
}
|
|
|
|
WatcherChange::Write(path) => {
|
|
|
|
let text = match fs::read_to_string(&path) {
|
|
|
|
Ok(text) => text,
|
|
|
|
Err(e) => {
|
2019-01-16 19:30:20 +01:00
|
|
|
log::warn!("watcher error \"{}\": {}", path.display(), e);
|
2019-01-12 18:17:52 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
WatcherChangeData::Write { path, text }
|
|
|
|
}
|
|
|
|
WatcherChange::Remove(path) => WatcherChangeData::Remove { path },
|
|
|
|
WatcherChange::Rescan => {
|
|
|
|
// this should be handled by Vfs::handle_task
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(data)
|
|
|
|
}
|