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
65 changes: 5 additions & 60 deletions notify-debouncer-full/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use file_id::{get_file_id, FileId};
use file_id::FileId;
use notify::RecursiveMode;
use walkdir::WalkDir;
use std::path::{Path, PathBuf};

/// The interface of a file ID cache.
///
Expand Down Expand Up @@ -38,57 +34,6 @@ pub trait FileIdCache {
}
}

/// A cache to hold the file system IDs of all watched files.
///
/// The file ID cache uses unique file IDs provided by the file system and is used to stich together
/// rename events in case the notification back-end doesn't emit rename cookies.
#[derive(Debug, Clone, Default)]
pub struct FileIdMap {
paths: HashMap<PathBuf, FileId>,
}

impl FileIdMap {
/// Construct an empty cache.
pub fn new() -> Self {
Default::default()
}

fn dir_scan_depth(is_recursive: bool) -> usize {
if is_recursive {
usize::MAX
} else {
1
}
}
}

impl FileIdCache for FileIdMap {
fn cached_file_id(&self, path: &Path) -> Option<impl AsRef<FileId>> {
self.paths.get(path)
}

fn add_path(&mut self, path: &Path, recursive_mode: RecursiveMode) {
let is_recursive = recursive_mode == RecursiveMode::Recursive;

for (path, file_id) in WalkDir::new(path)
.follow_links(true)
.max_depth(Self::dir_scan_depth(is_recursive))
.into_iter()
.filter_map(|entry| {
let path = entry.ok()?.into_path();
let file_id = get_file_id(&path).ok()?;
Some((path, file_id))
})
{
self.paths.insert(path, file_id);
}
}

fn remove_path(&mut self, path: &Path) {
self.paths.retain(|p, _| !p.starts_with(path));
}
}

/// An implementation of the `FileIdCache` trait that doesn't hold any data.
///
/// This pseudo cache can be used to disable the file tracking using file system IDs.
Expand All @@ -113,8 +58,8 @@ impl FileIdCache for NoCache {
}

/// The recommended file ID cache implementation for the current platform
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(any(target_os = "linux", target_os = "android", target_family = "wasm"))]
pub type RecommendedCache = NoCache;
/// The recommended file ID cache implementation for the current platform
#[cfg(not(any(target_os = "linux", target_os = "android")))]
pub type RecommendedCache = FileIdMap;
#[cfg(not(any(target_os = "linux", target_os = "android", target_family = "wasm")))]
pub type RecommendedCache = crate::file_id_map::FileIdMap;
59 changes: 59 additions & 0 deletions notify-debouncer-full/src/file_id_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::FileIdCache;
use file_id::{get_file_id, FileId};
use notify::RecursiveMode;
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use walkdir::WalkDir;

/// A cache to hold the file system IDs of all watched files.
///
/// The file ID cache uses unique file IDs provided by the file system and is used to stich together
/// rename events in case the notification back-end doesn't emit rename cookies.
#[derive(Debug, Clone, Default)]
pub struct FileIdMap {
paths: HashMap<PathBuf, FileId>,
}

impl FileIdMap {
/// Construct an empty cache.
pub fn new() -> Self {
Default::default()
}

fn dir_scan_depth(is_recursive: bool) -> usize {
if is_recursive {
usize::MAX
} else {
1
}
}
}

impl FileIdCache for FileIdMap {
fn cached_file_id(&self, path: &Path) -> Option<impl AsRef<FileId>> {
self.paths.get(path)
}

fn add_path(&mut self, path: &Path, recursive_mode: RecursiveMode) {
let is_recursive = recursive_mode == RecursiveMode::Recursive;

for (path, file_id) in WalkDir::new(path)
.follow_links(true)
.max_depth(Self::dir_scan_depth(is_recursive))
.into_iter()
.filter_map(|entry| {
let path = entry.ok()?.into_path();
let file_id = get_file_id(&path).ok()?;
Some((path, file_id))
})
{
self.paths.insert(path, file_id);
}
}

fn remove_path(&mut self, path: &Path) {
self.paths.retain(|p, _| !p.starts_with(path));
}
}
8 changes: 7 additions & 1 deletion notify-debouncer-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ mod time;
#[cfg(test)]
mod testing;

#[cfg(not(target_family = "wasm"))]
mod file_id_map;

use std::{
cmp::Reverse,
collections::{BinaryHeap, HashMap, VecDeque},
Expand All @@ -79,7 +82,10 @@ use std::{

use time::now;

pub use cache::{FileIdCache, FileIdMap, NoCache, RecommendedCache};
pub use cache::{FileIdCache, NoCache, RecommendedCache};

#[cfg(not(target_family = "wasm"))]
pub use file_id_map::FileIdMap;

pub use file_id;
pub use notify;
Expand Down