Skip to content
Open
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
4 changes: 2 additions & 2 deletions objdiff-cli/src/cmd/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use objdiff_core::{
bindings::diff::DiffResult,
build::{
BuildConfig, BuildStatus,
watcher::{Watcher, create_watcher},
watcher::{AnyDebouncer, create_watcher},
},
config::{
ProjectConfig, ProjectObject, ProjectObjectMetadata, ProjectOptions, apply_project_options,
Expand Down Expand Up @@ -271,7 +271,7 @@ pub struct AppState {
pub prev_obj: Option<(Object, ObjectDiff)>,
pub reload_time: Option<time::OffsetDateTime>,
pub time_format: Vec<time::format_description::FormatItem<'static>>,
pub watcher: Option<Watcher>,
pub watcher: Option<AnyDebouncer>,
pub modified: Arc<AtomicBool>,
pub diff_obj_config: DiffObjConfig,
pub mapping_config: MappingConfig,
Expand Down
148 changes: 111 additions & 37 deletions objdiff-core/src/build/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
error::Error,
fs,
path::{Path, PathBuf},
path::{Component, Path, PathBuf, Prefix},
sync::{
Arc,
atomic::{AtomicBool, Ordering},
Expand All @@ -13,10 +14,26 @@ use globset::GlobSet;
use notify::RecursiveMode;
use notify_debouncer_full::{DebounceEventResult, new_debouncer_opt};

pub type Watcher = notify_debouncer_full::Debouncer<
notify::RecommendedWatcher,
notify_debouncer_full::RecommendedCache,
>;
pub enum AnyDebouncer {
NativeDebouncer(
notify_debouncer_full::Debouncer<
notify::RecommendedWatcher,
notify_debouncer_full::RecommendedCache,
>,
),
NetworkFSDebouncer(
notify_debouncer_full::Debouncer<notify::PollWatcher, notify_debouncer_full::NoCache>,
),
}

impl AnyDebouncer {
pub fn watch(&mut self, path: impl AsRef<Path>, mode: RecursiveMode) -> notify::Result<()> {
match self {
Self::NativeDebouncer(d) => d.watch(path, mode),
Self::NetworkFSDebouncer(d) => d.watch(path, mode),
}
}
}

pub struct WatcherState {
pub config_path: Option<PathBuf>,
Expand All @@ -25,52 +42,109 @@ pub struct WatcherState {
pub patterns: GlobSet,
}

pub fn is_win_networkfs_path(path: &Path) -> Result<bool, Box<dyn Error>> {
let mut components = path.components();
let Some(first_component) = components.next() else {
return Err("Path should not be empty")?;
};
match first_component {
Component::Prefix(prefix) => Ok(matches!(prefix.kind(), Prefix::VerbatimUNC(..))),
_ => Err("Path should have a prefix (use canonicalize)")?,
}
}

pub fn create_watcher(
modified: Arc<AtomicBool>,
project_dir: &Path,
patterns: GlobSet,
ignore_patterns: GlobSet,
waker: Waker,
) -> notify::Result<Watcher> {
) -> notify::Result<AnyDebouncer> {
let base_dir = fs::canonicalize(project_dir)?;
let base_dir_clone = base_dir.clone();
let timeout = Duration::from_millis(200);
let config = notify::Config::default().with_poll_interval(Duration::from_secs(2));
let mut debouncer = new_debouncer_opt(
timeout,
None,
move |result: DebounceEventResult| match result {
Ok(events) => {
let mut any_match = false;
for event in events.iter() {
if !matches!(
event.kind,
notify::EventKind::Modify(..)
| notify::EventKind::Create(..)
| notify::EventKind::Remove(..)
) {
let handler = move |result: DebounceEventResult| match result {
Ok(events) => {
let mut any_match = false;
for event in events.iter() {
if !matches!(
event.kind,
notify::EventKind::Modify(..)
| notify::EventKind::Create(..)
| notify::EventKind::Remove(..)
) {
continue;
}
for path in &event.paths {
let Ok(path) = path.strip_prefix(&base_dir_clone) else {
continue;
};
if patterns.is_match(path) && !ignore_patterns.is_match(path) {
log::debug!("File modified: {}", path.display());
any_match = true;
}
for path in &event.paths {
let Ok(path) = path.strip_prefix(&base_dir_clone) else {
continue;
};
if patterns.is_match(path) && !ignore_patterns.is_match(path) {
log::debug!("File modified: {}", path.display());
any_match = true;
}
}
}
if any_match {
modified.store(true, Ordering::Relaxed);
waker.wake_by_ref();
}
}
Err(errors) => errors.iter().for_each(|e| log::error!("Watch error: {e:?}")),
},
notify_debouncer_full::RecommendedCache::new(),
config,
)?;
if any_match {
modified.store(true, Ordering::Relaxed);
waker.wake_by_ref();
}
}
Err(errors) => errors.iter().for_each(|e| {
let path_is_ignored = e.paths.iter().all(|absolute_path| {
let Ok(path) = absolute_path.strip_prefix(&base_dir_clone) else {
return false;
};
ignore_patterns.is_match(path)
});
// Don't directly report watch errors for path that are ignored
if path_is_ignored {
log::debug!("Watch error (ignored path) : {e:?}");
return;
}
log::error!("Watch error: {e:?}");
}),
};

let mut debouncer;
// On Windows (only), check if base_dir is inside a network fs, if so switch to polling watcher
#[cfg(windows)]
{
let use_polling = is_win_networkfs_path(base_dir.as_path()).unwrap_or_default();
if use_polling {
// Use more frequent polling to compensate the slow polls
let poll_config = config.with_poll_interval(Duration::from_secs(1));
log::debug!("Using NetworkFS debouncer");
debouncer = AnyDebouncer::NetworkFSDebouncer(new_debouncer_opt(
timeout,
None,
handler,
notify_debouncer_full::NoCache::new(),
poll_config,
)?);
} else {
log::debug!("Using Native debouncer");
debouncer = AnyDebouncer::NativeDebouncer(new_debouncer_opt(
timeout,
None,
handler,
notify_debouncer_full::RecommendedCache::new(),
config,
)?);
}
}
#[cfg(not(windows))]
{
debouncer = AnyDebouncer::NativeDebouncer(new_debouncer_opt(
timeout,
None,
handler,
notify_debouncer_full::RecommendedCache::new(),
config,
)?);
}

debouncer.watch(base_dir, RecursiveMode::Recursive)?;
Ok(debouncer)
}
4 changes: 2 additions & 2 deletions objdiff-gui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use egui::text::LayoutJob;
use filetime::FileTime;
use globset::Glob;
use objdiff_core::{
build::watcher::{Watcher, create_watcher},
build::watcher::{AnyDebouncer, create_watcher},
config::{
ProjectConfig, ProjectConfigInfo, ProjectObject, ScratchConfig, apply_project_options,
build_globset, default_ignore_patterns, default_watch_patterns,
Expand Down Expand Up @@ -461,7 +461,7 @@ pub struct App {
view_state: ViewState,
state: AppStateRef,
modified: Arc<AtomicBool>,
watcher: Option<Watcher>,
watcher: Option<AnyDebouncer>,
app_path: Option<PathBuf>,
relaunch_path: Rc<Mutex<Option<PathBuf>>>,
should_relaunch: bool,
Expand Down
Loading