diff --git a/packaging/flatpak/README.md b/packaging/flatpak/README.md index 69a469a..66fff87 100644 --- a/packaging/flatpak/README.md +++ b/packaging/flatpak/README.md @@ -34,9 +34,12 @@ Choose vault folders using the folder picker. Flatpak grants access to the chose folders through its document portal; the package does not request access to the whole home directory. Its settings are separate from native installations. The document portal does not forward external file-change notifications, so this -build checks for changes every two seconds using the existing polling watcher. -Metadata polling can miss edits within the same timestamp second or edits that -preserve an earlier modification time. Reopen the vault to refresh those changes. +build checks visible notes and notebooks about every two seconds in small vaults. +The interval grows with scan cost in larger vaults to limit background CPU use. +It compares file +size and precise modification/change timestamps, including changes made +while preserving a note's modification time. Attachments and hidden app data are +excluded from these scans; note contents are read only when a change is detected. The build sets `HELIXNOTES_INSTALL_TYPE=flatpak`, which suppresses the in-app updater. Install a newer bundle to update this local package. Automatic Flatpak diff --git a/src-tauri/src/vault/mod.rs b/src-tauri/src/vault/mod.rs index e4e5348..25fd077 100644 --- a/src-tauri/src/vault/mod.rs +++ b/src-tauri/src/vault/mod.rs @@ -1,6 +1,8 @@ pub mod frontmatter; pub mod import; pub mod operations; +#[cfg(target_os = "linux")] +mod portal_watcher; pub mod watcher; #[cfg(test)] diff --git a/src-tauri/src/vault/portal_watcher.rs b/src-tauri/src/vault/portal_watcher.rs new file mode 100644 index 0000000..12c8991 --- /dev/null +++ b/src-tauri/src/vault/portal_watcher.rs @@ -0,0 +1,353 @@ +//! Metadata polling for portal directories that do not forward host inotify events. +use notify::event::{CreateKind, ModifyKind, RemoveKind}; +use notify::{Event, EventKind}; +use std::collections::HashMap; +use std::fs::{self, Metadata}; +use std::io::ErrorKind; +use std::os::unix::fs::MetadataExt; +use std::path::{Path, PathBuf}; +use std::sync::mpsc::{self, RecvTimeoutError}; +use std::time::{Duration, Instant}; +use walkdir::WalkDir; + +use super::operations::is_hidden; + +#[derive(Debug, PartialEq, Eq)] +enum Fingerprint { + Directory, + Note { + size: u64, + modified: (i64, i64), + changed: (i64, i64), + }, +} + +impl Fingerprint { + fn from_metadata(metadata: &Metadata) -> Self { + if metadata.is_dir() { + // Track directory presence, not child mutations or synthetic portal inode IDs. + Self::Directory + } else { + Self::Note { + size: metadata.len(), + modified: (metadata.mtime(), metadata.mtime_nsec()), + changed: (metadata.ctime(), metadata.ctime_nsec()), + } + } + } +} + +type Snapshot = HashMap; + +fn scan(root: &Path) -> notify::Result { + let metadata = + fs::metadata(root).map_err(|error| notify::Error::io(error).add_path(root.to_owned()))?; + if !metadata.is_dir() { + return Err( + notify::Error::generic("Vault root is not a directory").add_path(root.to_owned()) + ); + } + let mut snapshot = Snapshot::new(); + let entries = WalkDir::new(root) + .follow_links(false) + .into_iter() + .filter_entry(|entry| entry.depth() == 0 || !is_hidden(entry.path())); + for entry in entries { + let entry = match entry { + Ok(entry) => entry, + Err(error) + if error.depth() > 0 + && error + .io_error() + .is_some_and(|error| error.kind() == ErrorKind::NotFound) => + { + continue; + } + Err(error) => { + return Err(notify::Error::generic(&error.to_string()) + .add_path(error.path().unwrap_or(root).to_owned())); + } + }; + let path = entry.path(); + if !entry.file_type().is_dir() + && path.extension().and_then(|extension| extension.to_str()) != Some("md") + { + continue; + } + let metadata = match fs::metadata(path) { + Ok(metadata) => metadata, + Err(error) if entry.depth() > 0 && error.kind() == ErrorKind::NotFound => continue, + Err(error) => return Err(notify::Error::io(error).add_path(path.to_owned())), + }; + // Match note scanning's file symlinks without traversing directory symlinks. + if metadata.is_file() || (metadata.is_dir() && !entry.file_type().is_symlink()) { + snapshot.insert(path.to_owned(), Fingerprint::from_metadata(&metadata)); + } + } + Ok(snapshot) +} + +fn changes(before: &Snapshot, after: &Snapshot) -> Vec { + let mut events = Vec::new(); + for (path, fingerprint) in after { + let kind = match before.get(path) { + None => EventKind::Create(CreateKind::Any), + Some(previous) if previous != fingerprint => EventKind::Modify(ModifyKind::Any), + Some(_) => continue, + }; + events.push(Event::new(kind).add_path(path.clone())); + } + for path in before.keys().filter(|path| !after.contains_key(*path)) { + events.push(Event::new(EventKind::Remove(RemoveKind::Any)).add_path(path.clone())); + } + events +} + +pub struct PortalWatcher { + _stop: mpsc::Sender<()>, +} + +impl PortalWatcher { + pub fn start( + root: PathBuf, + events: mpsc::Sender>, + interval: Duration, + ) -> Result { + let started = Instant::now(); + let mut previous = scan(&root).map_err(|error| error.to_string())?; + // Portal metadata calls consume host daemon CPU too. Large vaults trade + // detection latency for idle time instead of scanning continuously. + let mut delay = interval.max(started.elapsed().saturating_mul(20)); + let (stop, stopped) = mpsc::channel(); + std::thread::Builder::new() + .name("portal-vault-watch".into()) + .spawn(move || { + while matches!(stopped.recv_timeout(delay), Err(RecvTimeoutError::Timeout)) { + let started = Instant::now(); + match scan(&root) { + Ok(current) => { + for event in changes(&previous, ¤t) { + if events.send(Ok(event)).is_err() { + return; + } + } + previous = current; + } + Err(error) => { + // A partial or inaccessible scan cannot prove that notes were deleted. + if events.send(Err(error)).is_err() { + return; + } + } + } + delay = interval.max(started.elapsed().saturating_mul(20)); + } + }) + .map_err(|error| error.to_string())?; + Ok(Self { _stop: stop }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::{File, FileTimes}; + use std::os::unix::fs::symlink; + use std::time::{Instant, SystemTime, UNIX_EPOCH}; + + fn set_modified(path: &Path, time: SystemTime) { + File::options() + .write(true) + .open(path) + .unwrap() + .set_times(FileTimes::new().set_modified(time)) + .unwrap(); + } + + fn assert_modified(before: &Snapshot, root: &Path, note: &Path) { + let events = changes(before, &scan(root).unwrap()); + assert_eq!(events.len(), 1, "{events:?}"); + assert_eq!(events[0].paths, [note]); + assert_eq!(events[0].kind, EventKind::Modify(ModifyKind::Any)); + } + + #[test] + fn same_size_edit_with_restored_mtime_uses_ctime() { + let vault = tempfile::tempdir().unwrap(); + let note = vault.path().join("Note.md"); + fs::write(¬e, "before").unwrap(); + let modified = fs::metadata(¬e).unwrap().modified().unwrap(); + let before = scan(vault.path()).unwrap(); + std::thread::sleep(Duration::from_millis(2)); + fs::write(¬e, "after!").unwrap(); + set_modified(¬e, modified); + assert_eq!(fs::metadata(¬e).unwrap().modified().unwrap(), modified); + assert_modified(&before, vault.path(), ¬e); + } + + #[test] + fn backdated_and_fractional_modification_times_are_detected() { + let vault = tempfile::tempdir().unwrap(); + let note = vault.path().join("Note.md"); + fs::write(¬e, "before").unwrap(); + set_modified(¬e, UNIX_EPOCH + Duration::new(1000, 100)); + let before = scan(vault.path()).unwrap(); + fs::write(¬e, "after!").unwrap(); + set_modified(¬e, UNIX_EPOCH + Duration::new(1000, 200)); + let metadata = fs::metadata(¬e).unwrap(); + assert_eq!((metadata.mtime(), metadata.mtime_nsec()), (1000, 200)); + assert_modified(&before, vault.path(), ¬e); + let before = scan(vault.path()).unwrap(); + set_modified(¬e, UNIX_EPOCH + Duration::from_secs(10)); + assert_modified(&before, vault.path(), ¬e); + } + + #[test] + fn atomic_replacement_with_preserved_mtime_changes_ctime() { + let vault = tempfile::tempdir().unwrap(); + let note = vault.path().join("Note.md"); + let temporary = vault.path().join(".replacement"); + fs::write(¬e, "before").unwrap(); + let metadata = fs::metadata(¬e).unwrap(); + let before = scan(vault.path()).unwrap(); + std::thread::sleep(Duration::from_millis(2)); + fs::write(&temporary, "after!").unwrap(); + set_modified(&temporary, metadata.modified().unwrap()); + fs::rename(&temporary, ¬e).unwrap(); + let replaced = fs::metadata(¬e).unwrap(); + assert_eq!(replaced.modified().unwrap(), metadata.modified().unwrap()); + assert_ne!( + (replaced.ctime(), replaced.ctime_nsec()), + (metadata.ctime(), metadata.ctime_nsec()) + ); + assert_modified(&before, vault.path(), ¬e); + } + + #[test] + fn nested_directory_create_rename_and_remove_keep_both_sides() { + let vault = tempfile::tempdir().unwrap(); + let before = scan(vault.path()).unwrap(); + let old = vault.path().join("Old"); + fs::create_dir_all(old.join("Nested")).unwrap(); + fs::write(old.join("Nested/Note.md"), "note").unwrap(); + let current = scan(vault.path()).unwrap(); + let created = changes(&before, ¤t); + assert_eq!(created.len(), 3); + assert!(created.iter().all(|event| event.kind.is_create())); + let new = vault.path().join("New"); + fs::rename(&old, &new).unwrap(); + let renamed = scan(vault.path()).unwrap(); + let events = changes(¤t, &renamed); + assert_eq!( + events.iter().filter(|event| event.kind.is_create()).count(), + 3 + ); + assert_eq!( + events.iter().filter(|event| event.kind.is_remove()).count(), + 3 + ); + fs::remove_dir_all(&new).unwrap(); + let events = changes(&renamed, &scan(vault.path()).unwrap()); + assert_eq!(events.len(), 3); + assert!(events.iter().all(|event| event.kind.is_remove())); + } + + #[test] + fn hidden_resources_and_attachment_changes_are_excluded() { + let vault = tempfile::tempdir().unwrap(); + for name in [ + ".helixnotes", + ".hidden", + "assets", + "_attachments", + "node_modules", + ] { + fs::create_dir(vault.path().join(name)).unwrap(); + fs::write(vault.path().join(name).join("Hidden.md"), "hidden").unwrap(); + } + let before = scan(vault.path()).unwrap(); + assert_eq!( + before.len(), + 1, + "only the explicitly selected vault root should be present" + ); + fs::write(vault.path().join("image.png"), "attachment").unwrap(); + fs::write(vault.path().join(".hidden.md"), "hidden").unwrap(); + assert!(changes(&before, &scan(vault.path()).unwrap()).is_empty()); + } + + #[test] + fn note_symlinks_are_watched_without_following_directory_symlinks() { + let vault = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + let note = vault.path().join("Note.md"); + let alias = vault.path().join("Alias.md"); + fs::write(¬e, "before").unwrap(); + fs::write(outside.path().join("Outside.md"), "outside").unwrap(); + symlink(¬e, &alias).unwrap(); + symlink(outside.path(), vault.path().join("Directory.md")).unwrap(); + symlink( + vault.path().join("Missing.md"), + vault.path().join("Broken.md"), + ) + .unwrap(); + let before = scan(vault.path()).unwrap(); + assert_eq!(before.len(), 3); + assert!(before.contains_key(&alias)); + set_modified(¬e, UNIX_EPOCH + Duration::from_secs(1000)); + let events = changes(&before, &scan(vault.path()).unwrap()); + assert_eq!(events.len(), 2); + assert!(events.iter().all(|event| event.kind.is_modify())); + } + + #[test] + fn scan_errors_do_not_publish_partial_snapshots_and_recovery_detects_edits() { + let vault = tempfile::tempdir().unwrap(); + let note = vault.path().join("Note.md"); + fs::write(¬e, "before").unwrap(); + let (sender, receiver) = mpsc::channel(); + let watcher = + PortalWatcher::start(vault.path().to_owned(), sender, Duration::from_millis(20)) + .unwrap(); + let broken = vault.path().join("Loop.md"); + symlink(&broken, &broken).unwrap(); + assert!(receiver + .recv_timeout(Duration::from_secs(2)) + .unwrap() + .is_err()); + set_modified(¬e, UNIX_EPOCH + Duration::from_secs(1000)); + fs::remove_file(&broken).unwrap(); + let deadline = Instant::now() + Duration::from_secs(2); + loop { + let event = receiver + .recv_timeout(deadline.saturating_duration_since(Instant::now())) + .unwrap(); + if let Ok(event) = event { + assert_eq!(event.paths.as_slice(), std::slice::from_ref(¬e)); + assert!(event.kind.is_modify()); + break; + } + } + drop(watcher); + } + + #[test] + fn initial_failure_is_synchronous_and_drop_wakes_a_long_poll() { + let vault = tempfile::tempdir().unwrap(); + let (sender, _receiver) = mpsc::channel(); + assert!(PortalWatcher::start( + vault.path().join("Missing"), + sender, + Duration::from_secs(60) + ) + .is_err()); + let (sender, receiver) = mpsc::channel(); + let watcher = + PortalWatcher::start(vault.path().to_owned(), sender, Duration::from_secs(60)).unwrap(); + drop(watcher); + assert!(matches!( + receiver.recv_timeout(Duration::from_secs(2)), + Err(mpsc::RecvTimeoutError::Disconnected) + )); + } +} diff --git a/src-tauri/src/vault/watcher.rs b/src-tauri/src/vault/watcher.rs index b6f069e..e1a0dc0 100644 --- a/src-tauri/src/vault/watcher.rs +++ b/src-tauri/src/vault/watcher.rs @@ -9,6 +9,8 @@ use tauri::{AppHandle, Emitter, Manager}; use crate::state::AppState; use crate::types::FileEvent; use crate::vault::operations::{is_hidden, read_note_entry_fast}; +#[cfg(target_os = "linux")] +use crate::vault::portal_watcher::PortalWatcher; const IOS_POLL_INTERVAL: Duration = Duration::from_secs(10); const FLATPAK_POLL_INTERVAL: Duration = Duration::from_secs(2); @@ -19,11 +21,22 @@ const EVENT_BATCH_INTERVAL: Duration = Duration::from_millis(50); enum WatcherBackend { Recommended, Poll, + #[cfg(target_os = "linux")] + Portal, } const fn watcher_backend_for_target(is_ios: bool, is_flatpak: bool) -> WatcherBackend { - if is_ios || is_flatpak { + if is_ios { WatcherBackend::Poll + } else if is_flatpak { + #[cfg(target_os = "linux")] + { + WatcherBackend::Portal + } + #[cfg(not(target_os = "linux"))] + { + WatcherBackend::Poll + } } else { WatcherBackend::Recommended } @@ -40,17 +53,16 @@ const fn poll_interval_for_target(is_ios: bool, is_flatpak: bool) -> Duration { } pub enum VaultWatcher { - Recommended(RecommendedWatcher), - Poll(PollWatcher), -} - -impl VaultWatcher { - fn watch(&mut self, path: &Path, recursive_mode: RecursiveMode) -> notify::Result<()> { - match self { - Self::Recommended(watcher) => watcher.watch(path, recursive_mode), - Self::Poll(watcher) => watcher.watch(path, recursive_mode), - } - } + Recommended { + _watcher: RecommendedWatcher, + }, + Poll { + _watcher: PollWatcher, + }, + #[cfg(target_os = "linux")] + Portal { + _watcher: PortalWatcher, + }, } fn collect_changes(event: Event, vault_root: &Path, changes: &mut HashMap) { @@ -120,24 +132,32 @@ pub fn start_watcher(app: AppHandle, vault_path: String) -> Result VaultWatcher::Recommended( - RecommendedWatcher::new(tx, config).map_err(|e| e.to_string())?, - ), - WatcherBackend::Poll => { - VaultWatcher::Poll(PollWatcher::new(tx, config).map_err(|e| e.to_string())?) + let watcher = match backend { + WatcherBackend::Recommended => { + let mut watcher = RecommendedWatcher::new(tx, config).map_err(|e| e.to_string())?; + watcher + .watch(Path::new(&vault_path), RecursiveMode::Recursive) + .map_err(|e| e.to_string())?; + VaultWatcher::Recommended { _watcher: watcher } } + WatcherBackend::Poll => { + let mut watcher = PollWatcher::new(tx, config).map_err(|e| e.to_string())?; + watcher + .watch(Path::new(&vault_path), RecursiveMode::Recursive) + .map_err(|e| e.to_string())?; + VaultWatcher::Poll { _watcher: watcher } + } + #[cfg(target_os = "linux")] + WatcherBackend::Portal => VaultWatcher::Portal { + _watcher: PortalWatcher::start(PathBuf::from(&vault_path), tx, FLATPAK_POLL_INTERVAL)?, + }, }; - watcher - .watch(Path::new(&vault_path), RecursiveMode::Recursive) - .map_err(|e| e.to_string())?; - std::thread::spawn(move || { let vault_root = Path::new(&vault_path); let mut changes = HashMap::new();