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

Commit

Permalink
Allow ignoring directories (#1931)
Browse files Browse the repository at this point in the history
* Allow ingoring directories

* PR comments
  • Loading branch information
tevoinea authored May 12, 2022
1 parent 70d6ff0 commit 8fbe049
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/agent/onefuzz/examples/dir-monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async fn main() -> Result<()> {
let opt = Opt::from_args();

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

while let Some(created) = monitor.next_file().await? {
println!("[create] {}", created.display());
Expand Down
14 changes: 13 additions & 1 deletion src/agent/onefuzz/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ use tokio::{
sync::mpsc::{unbounded_channel, UnboundedReceiver},
};

const DEFAULT_REPORT_DIRECTORIES: bool = false;

/// Watches a directory, and on file creation, emits the path to the file.
pub struct DirectoryMonitor {
dir: PathBuf,
notify_events: UnboundedReceiver<notify::Result<Event>>,
watcher: notify::RecommendedWatcher,
report_directories: bool,
}

impl DirectoryMonitor {
Expand Down Expand Up @@ -47,9 +50,14 @@ impl DirectoryMonitor {
dir,
notify_events,
watcher,
report_directories: DEFAULT_REPORT_DIRECTORIES,
})
}

pub fn set_report_directories(&mut self, report_directories: bool) {
self.report_directories = report_directories;
}

pub fn stop(&mut self) -> Result<()> {
self.watcher.unwatch(&self.dir)?;
Ok(())
Expand Down Expand Up @@ -86,7 +94,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
40 changes: 40 additions & 0 deletions src/agent/onefuzz/src/monitor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ macro_rules! timed_test {
};
}

macro_rules! expected_timeout_test {
($test_name: ident, $future: expr) => {
#[tokio::test]
async fn $test_name() -> Result<()> {
let result = tokio::time::timeout(TEST_TIMEOUT, $future).await;

result.expect_err("Expected test to time out");
Ok(())
}
};
}

timed_test!(test_monitor_empty_path, async move {
let monitor = DirectoryMonitor::new("").await;

Expand Down Expand Up @@ -115,3 +127,31 @@ timed_test!(test_monitor_dir_create_files, async move {

Ok(())
});

expected_timeout_test!(test_monitor_default_ignores_dir, async move {
let dir = tempdir().unwrap();
let mut monitor = DirectoryMonitor::new(dir.path()).await?;

let sub_dir = dir.path().join("test");
dbg!(&sub_dir);
fs::create_dir(&sub_dir).await?;

monitor.next_file().await?;
anyhow::Ok(())
});

timed_test!(test_monitor_set_report_directories, async move {
use std::fs::canonicalize;

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

let sub_dir = dir.path().join("test");
dbg!(&sub_dir);
fs::create_dir(&sub_dir).await?;

assert_eq!(monitor.next_file().await?, Some(canonicalize(&sub_dir)?));

Ok(())
});

0 comments on commit 8fbe049

Please sign in to comment.