mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-27 13:54:11 +02:00
Fix sync watcher panic when the sync directory path traverses a symlink (#541)
This commit is contained in:
@@ -27,7 +27,10 @@ pub async fn watch_directory<F>(
|
|||||||
where
|
where
|
||||||
F: Fn(WatchEvent) + Send + 'static,
|
F: Fn(WatchEvent) + Send + 'static,
|
||||||
{
|
{
|
||||||
let dir = dir.to_owned();
|
// Canonicalize so reported event paths match the watched root. Notify reports
|
||||||
|
// resolved paths, so watching through a symlink (e.g. /tmp -> /private/tmp on
|
||||||
|
// macOS) would otherwise make every strip_prefix below fail.
|
||||||
|
let dir = dir.canonicalize().unwrap_or_else(|_| dir.to_owned());
|
||||||
let (tx, rx) = mpsc::channel::<notify::Result<notify::Event>>();
|
let (tx, rx) = mpsc::channel::<notify::Result<notify::Event>>();
|
||||||
let mut watcher = notify::recommended_watcher(tx)?;
|
let mut watcher = notify::recommended_watcher(tx)?;
|
||||||
|
|
||||||
@@ -41,9 +44,12 @@ where
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
watcher.watch(&dir, notify::RecursiveMode::Recursive)?;
|
||||||
|
info!("Watching directory {:?}", dir);
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
watcher.watch(&dir, notify::RecursiveMode::Recursive).expect("Failed to watch directory");
|
// Keep the watcher alive for the lifetime of the task
|
||||||
info!("Watching directory {:?}", dir);
|
let _watcher = watcher;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
select! {
|
select! {
|
||||||
@@ -53,7 +59,7 @@ where
|
|||||||
Ok(event) => {
|
Ok(event) => {
|
||||||
// Filter out any ignored directories and see if we still get a result
|
// Filter out any ignored directories and see if we still get a result
|
||||||
let paths = event.paths.into_iter()
|
let paths = event.paths.into_iter()
|
||||||
.map(|p| p.strip_prefix(&dir).unwrap().to_path_buf())
|
.map(|p| p.strip_prefix(&dir).unwrap_or(&p).to_path_buf())
|
||||||
.filter(|p| !p.starts_with(".git") && !p.starts_with("node_modules"))
|
.filter(|p| !p.starts_with(".git") && !p.starts_with("node_modules"))
|
||||||
.collect::<Vec<PathBuf>>();
|
.collect::<Vec<PathBuf>>();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user