Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Allow ignoring directories #1931

Merged
merged 5 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 1 deletion src/agent/onefuzz/examples/dir-monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ struct Opt {
async fn main() -> Result<()> {
let opt = Opt::from_args();

let mut monitor = DirectoryMonitor::new(opt.path).await?;
let mut monitor = DirectoryMonitor::new(opt.path)
.await?
.set_report_directories(true);

while let Some(created) = monitor.next_file().await? {
println!("[create] {}", created.display());
Expand Down
15 changes: 14 additions & 1 deletion src/agent/onefuzz/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct DirectoryMonitor {
dir: PathBuf,
notify_events: UnboundedReceiver<notify::Result<Event>>,
watcher: notify::RecommendedWatcher,
report_directories: bool,
}

impl DirectoryMonitor {
Expand Down Expand Up @@ -43,13 +44,21 @@ impl DirectoryMonitor {
let mut watcher = notify::recommended_watcher(event_handler)?;
watcher.watch(&dir, RecursiveMode::NonRecursive)?;

let report_directories = true;
tevoinea marked this conversation as resolved.
Show resolved Hide resolved

Ok(Self {
dir,
notify_events,
watcher,
report_directories,
})
}

pub fn set_report_directories(mut self, report_directories: bool) -> Self {
tevoinea marked this conversation as resolved.
Show resolved Hide resolved
self.report_directories = report_directories;
self
}

pub fn stop(&mut self) -> Result<()> {
self.watcher.unwatch(&self.dir)?;
Ok(())
Expand Down Expand Up @@ -86,7 +95,11 @@ impl DirectoryMonitor {
.ok_or_else(|| format_err!("missing path for file create event"))?
.clone();

return Ok(Some(path));
if !self.report_directories && fs::metadata(&path).await?.is_dir() {
// Ignore
} else {
return Ok(Some(path));
}
}
EventKind::Remove(..) => {
let path = event
Expand Down