Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: notification of file changes in linked directories #121

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/src/files/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, ffi::OsStr, fs::Metadata, path::PathBuf};
use std::{borrow::Cow, ffi::OsStr, fs::Metadata};

use anyhow::Result;
use shared::Url;
Expand All @@ -9,7 +9,7 @@ pub struct File {
pub(super) url: Url,
pub(super) meta: Metadata,
pub(super) length: Option<u64>,
pub(super) link_to: Option<PathBuf>,
pub(super) link_to: Option<Url>,
pub(super) is_link: bool,
pub(super) is_hidden: bool,
}
Expand All @@ -27,7 +27,7 @@ impl File {

if is_link {
meta = fs::metadata(&url).await.unwrap_or(meta);
link_to = fs::read_link(&url).await.ok();
link_to = fs::read_link(&url).await.map(Url::from).ok();
}

let length = if meta.is_dir() { None } else { Some(meta.len()) };
Expand All @@ -37,7 +37,7 @@ impl File {
}

impl File {
// --- Path
// --- Url
#[inline]
pub fn url(&self) -> &Url { &self.url }

Expand Down Expand Up @@ -80,5 +80,5 @@ impl File {

// --- Link to
#[inline]
pub fn link_to(&self) -> Option<&PathBuf> { self.link_to.as_ref() }
pub fn link_to(&self) -> Option<&Url> { self.link_to.as_ref() }
}
2 changes: 1 addition & 1 deletion core/src/manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Manager {
.map(|f| {
(
f.url_owned(),
if f.is_dir() { Some(MIME_DIR.to_owned()) } else { self.mimetype.get(f.url()).cloned() },
f.is_dir().then(|| MIME_DIR.to_owned()).or_else(|| self.mimetype.get(f.url()).cloned()),
)
})
.collect();
Expand Down
34 changes: 24 additions & 10 deletions core/src/manager/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ impl Watcher {
}
})
.collect();
guard.sort_unstable_by(|_, a, _, b| b.cmp(a));

let lock = self.watched.clone();
tokio::spawn(async move {
Expand All @@ -113,9 +112,7 @@ impl Watcher {
}
}

let mut guard = lock.write();
guard.extend(ext);
guard.sort_unstable_by(|_, a, _, b| b.cmp(a));
lock.write().extend(ext);
});
}

Expand Down Expand Up @@ -147,7 +144,7 @@ impl Watcher {
}
}

Self::file_changed(files.iter().collect()).await;
Self::file_changed(&files, watched.clone()).await;
for file in files {
emit!(Files(FilesOp::IOErr(file)));
}
Expand All @@ -158,17 +155,34 @@ impl Watcher {
}
}

async fn file_changed(urls: Vec<&Url>) {
if let Ok(mimes) = external::file(&urls).await {
emit!(Mimetype(mimes));
}
async fn file_changed(urls: &[Url], watched: Arc<RwLock<IndexMap<Url, Option<Url>>>>) {
let Ok(mut mimes) = external::file(urls).await else {
return;
};

let linked: Vec<_> = watched
.read()
.iter()
.filter_map(|(k, v)| v.as_ref().map(|v| (k, v)))
.fold(Vec::new(), |mut aac, (k, v)| {
mimes
.iter()
.filter(|(f, _)| f.parent().map(|p| p == **v) == Some(true))
.for_each(|(f, m)| aac.push((k.join(f.file_name().unwrap()), m.clone())));
aac
});

mimes.extend(linked);
emit!(Mimetype(mimes));
}

async fn dir_changed(url: &Url, watched: Arc<RwLock<IndexMap<Url, Option<Url>>>>) {
let linked: Vec<_> = watched
.read()
.iter()
.map_while(|(k, v)| v.as_ref().and_then(|v| url.strip_prefix(v)).map(|v| k.join(v)))
.filter_map(|(k, v)| v.as_ref().map(|v| (k, v)))
.filter(|(_, v)| *v == url)
.map(|(k, _)| k.clone())
.collect();

let Ok(rx) = Files::from_dir(url).await else {
Expand Down