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

Enable PollWatcher to identify folder and file events #578

Closed
wants to merge 10 commits into from
10 changes: 5 additions & 5 deletions notify-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ serialization-compat-6 = []

[dependencies]
instant = "0.1.12"
serde = { version = "1.0.89", features = ["derive"], optional = true }
mock_instant = { version = "0.3.0", optional = true }
serde = { version = "1.0.201", features = ["derive"], optional = true }
mock_instant = { version = "0.4.0", optional = true }

[dev-dependencies]
serde_json = "1.0.39"
insta = "1.34.0"
rstest = "0.17.0"
erde_json = "1.0.117"
insta = "1.38.0"
rstest = "0.19.0"
22 changes: 11 additions & 11 deletions notify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ edition = "2021"

[dependencies]
notify-types = { version = "1.0.0", path = "../notify-types" }
crossbeam-channel = { version = "0.5.0", optional = true }
filetime = "0.2.22"
libc = "0.2.4"
log = "0.4.17"
walkdir = "2.2.2"
crossbeam-channel = { version = "0.5.12", optional = true }
filetime = "0.2.23"
libc = "0.2.154"
log = "0.4.21"
walkdir = "2.5.0"

[target.'cfg(any(target_os="linux", target_os="android"))'.dependencies]
inotify = { version = "0.10", default-features = false }
Expand All @@ -37,18 +37,18 @@ kqueue = { version = "1.0", optional = true }
mio = { version = "0.8", features = ["os-ext"], optional = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.48.0", features = ["Win32_System_Threading", "Win32_Foundation", "Win32_Storage_FileSystem", "Win32_Security", "Win32_System_WindowsProgramming", "Win32_System_IO"] }
windows-sys = { version = "0.52.0", features = ["Win32_System_Threading", "Win32_Foundation", "Win32_Storage_FileSystem", "Win32_Security", "Win32_System_WindowsProgramming", "Win32_System_IO"] }

[target.'cfg(any(target_os="freebsd", target_os="openbsd", target_os = "netbsd", target_os = "dragonflybsd", target_os = "ios"))'.dependencies]
kqueue = "^1.0.4" # fix for #344
mio = { version = "0.8", features = ["os-ext"] }

[dev-dependencies]
serde_json = "1.0.39"
tempfile = "3.2.0"
nix = "0.23.1"
insta = "1.34.0"
rstest = "0.17.0"
serde_json = "1.0.117"
tempfile = "3.10.1"
nix = "0.28.0"
insta = "1.38.0"
rstest = "0.19.0"

[features]
default = ["macos_fsevent","crossbeam-channel"]
Expand Down
43 changes: 37 additions & 6 deletions notify/src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ mod data {
}

/// Create [`PathData`].
fn build_path_data(&self, meta_path: &MetaPath) -> PathData {
PathData::new(self, meta_path)
fn build_path_data(&self, meta_path: &MetaPath, file_type: &fs::FileType) -> PathData {
PathData::new(self, meta_path, file_type)
}
}

Expand Down Expand Up @@ -189,6 +189,9 @@ mod data {
let all_path_data =
Self::scan_all_path_data(data_builder, root.clone(), is_recursive, true).collect();

let close_initial_scan_evt = Event::new(EventKind::Other).set_info(&format!("initial_scan_complete"));
data_builder.emitter.emit(Ok(close_initial_scan_evt));

Some(Self {
root,
is_recursive,
Expand Down Expand Up @@ -283,15 +286,17 @@ mod data {
})
.filter_map(move |entry| match entry.metadata() {
Ok(metadata) => {
let file_type = entry.file_type();
let path = entry.into_path();
if is_initial {
// emit initial scans
if let Some(ref emitter) = data_builder.scan_emitter {
emitter.borrow_mut().handle_event(Ok(path.clone()));
}
}

let meta_path = MetaPath::from_parts_unchecked(path, metadata);
let data_path = data_builder.build_path_data(&meta_path);
let data_path = data_builder.build_path_data(&meta_path, &file_type);

Some((meta_path.into_path(), data_path))
}
Expand Down Expand Up @@ -328,11 +333,14 @@ mod data {

/// Checked time.
last_check: Instant,

// Type of object
path_type: fs::FileType,
}

impl PathData {
/// Create a new `PathData`.
fn new(data_builder: &DataBuilder, meta_path: &MetaPath) -> PathData {
fn new(data_builder: &DataBuilder, meta_path: &MetaPath, file_type: &fs::FileType) -> PathData {
let metadata = meta_path.metadata();

PathData {
Expand All @@ -346,6 +354,7 @@ mod data {
}),

last_check: data_builder.now,
path_type: *file_type,
}
}

Expand Down Expand Up @@ -390,8 +399,30 @@ mod data {
None
}
}
(None, Some(_new)) => Some(EventKind::Create(CreateKind::Any)),
(Some(_old), None) => Some(EventKind::Remove(RemoveKind::Any)),
(None, Some(_new)) => {

if _new.path_type.is_dir() {
Some(EventKind::Create(CreateKind::Folder))
}
else if _new.path_type.is_file() {
Some(EventKind::Create(CreateKind::File))
}
else {
Some(EventKind::Create(CreateKind::Any))
}
},
(Some(_old), None) => {

if _old.path_type.is_dir() {
Some(EventKind::Remove(RemoveKind::Folder))
}
else if _old.path_type.is_file() {
Some(EventKind::Remove(RemoveKind::File))
}
else {
Some(EventKind::Remove(RemoveKind::Any))
}
},
(None, None) => None,
}
.map(|event_kind| Event::new(event_kind).add_path(path.into()))
Expand Down