mirror of
https://github.com/PR0M3TH3AN/Marlin.git
synced 2025-09-09 15:48:43 +00:00
Make watcher handle cross-platform
This commit is contained in:
@@ -86,6 +86,21 @@ struct EventDebouncer {
|
|||||||
last_flush: Instant,
|
last_flush: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "redox", unix))]
|
||||||
|
fn handle_key(h: &Handle) -> u64 {
|
||||||
|
h.ino()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(target_os = "redox", unix)))]
|
||||||
|
fn handle_key(h: &Handle) -> u64 {
|
||||||
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
|
let mut hasher = DefaultHasher::new();
|
||||||
|
h.hash(&mut hasher);
|
||||||
|
hasher.finish()
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct RemoveTracker {
|
struct RemoveTracker {
|
||||||
map: HashMap<u64, (PathBuf, Instant)>,
|
map: HashMap<u64, (PathBuf, Instant)>,
|
||||||
@@ -94,7 +109,7 @@ struct RemoveTracker {
|
|||||||
impl RemoveTracker {
|
impl RemoveTracker {
|
||||||
fn record(&mut self, path: &PathBuf) {
|
fn record(&mut self, path: &PathBuf) {
|
||||||
if let Ok(h) = Handle::from_path(path) {
|
if let Ok(h) = Handle::from_path(path) {
|
||||||
self.map.insert(h.ino(), (path.clone(), Instant::now()));
|
self.map.insert(handle_key(&h), (path.clone(), Instant::now()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +124,7 @@ impl RemoveTracker {
|
|||||||
|
|
||||||
fn match_create(&mut self, path: &PathBuf, window: Duration) -> Option<PathBuf> {
|
fn match_create(&mut self, path: &PathBuf, window: Duration) -> Option<PathBuf> {
|
||||||
if let Ok(h) = Handle::from_path(path) {
|
if let Ok(h) = Handle::from_path(path) {
|
||||||
if let Some((old, ts)) = self.map.remove(&h.ino()) {
|
if let Some((old, ts)) = self.map.remove(&handle_key(&h)) {
|
||||||
if Instant::now().duration_since(ts) <= window {
|
if Instant::now().duration_since(ts) <= window {
|
||||||
return Some(old);
|
return Some(old);
|
||||||
} else {
|
} else {
|
||||||
@@ -136,7 +151,7 @@ impl RemoveTracker {
|
|||||||
fn flush_expired(&mut self, window: Duration, debouncer: &mut EventDebouncer) {
|
fn flush_expired(&mut self, window: Duration, debouncer: &mut EventDebouncer) {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let mut expired = Vec::new();
|
let mut expired = Vec::new();
|
||||||
for (ino, (path, ts)) in &self.map {
|
for (key, (path, ts)) in &self.map {
|
||||||
if now.duration_since(*ts) > window {
|
if now.duration_since(*ts) > window {
|
||||||
debouncer.add_event(ProcessedEvent {
|
debouncer.add_event(ProcessedEvent {
|
||||||
path: path.clone(),
|
path: path.clone(),
|
||||||
@@ -146,11 +161,11 @@ impl RemoveTracker {
|
|||||||
priority: EventPriority::Delete,
|
priority: EventPriority::Delete,
|
||||||
timestamp: *ts,
|
timestamp: *ts,
|
||||||
});
|
});
|
||||||
expired.push(*ino);
|
expired.push(*key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for ino in expired {
|
for key in expired {
|
||||||
self.map.remove(&ino);
|
self.map.remove(&key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user