diff --git a/src/dir_monitor.rs b/src/dir_monitor.rs new file mode 100644 index 0000000..458ea19 --- /dev/null +++ b/src/dir_monitor.rs @@ -0,0 +1,80 @@ +use std::path::{Path, PathBuf}; + +use crate::monitored_files::MonitoredFiles; + + + +/// A directory monitor. +/// +/// This will setup the monitoring of all the sub directories and files +/// of the given directory. +pub struct DirMonitor +{ + /// The directory to monitor. + dir: std::path::PathBuf, + + monitored_files: MonitoredFiles +} + + +impl DirMonitor +{ + /// Create a new directory monitor for the desired directory. + /// + /// monitor_path: A string representation of directory to monitor. + pub fn new(monitor_path: &str) -> Self + { + DirMonitor + { + dir: PathBuf::from(monitor_path), + monitored_files: MonitoredFiles::new() + } + } + + pub fn monitor(&mut self) + { + self.build_file_list(); + } + + fn build_file_list(&mut self) + { + // Start from the directory and add each file to our list, + // then recurse through all sub directories and add them to + // the list and repeat. + match scan_dir(&mut self.monitored_files, &self.dir) + { + Ok(_) => + { + } + + Err(e) => + { + println!("There was an issue during directory scanning."); + println!("{}", e); + } + } + } +} + + + +fn scan_dir(monitored_files: &mut MonitoredFiles, dir: &Path) -> std::io::Result<()> +{ + let mut dir_list: Vec = Vec::new(); + + for file in std::fs::read_dir(dir)? + { + let file = file?; + if file.file_type()?.is_dir() + { + dir_list.push(file.path().clone()); + } + } + + for sub_dir in dir_list + { + scan_dir(monitored_files, &sub_dir)?; + } + + Ok(()) +} diff --git a/src/event.rs b/src/event.rs new file mode 100644 index 0000000..340574a --- /dev/null +++ b/src/event.rs @@ -0,0 +1,29 @@ +enum Event +{ + New, + Modify, + Delete +} + + +impl std::fmt::Display for Event +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + match self + { + Event::New => + { + write!(f, "NEW") + } + Event::Modify => + { + write!(f, "MOD") + } + Event::Delete => + { + write!(f, "DEL") + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 0b10276..97d3380 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,4 +36,7 @@ fn main() let options: Options = Options::parse(); println!("Inbox: `{}`", options.inbox_dir); + + let mut directory_monitor: DirMonitor = DirMonitor::new(&options.inbox_dir); + directory_monitor.monitor(); } diff --git a/src/monitored_files.rs b/src/monitored_files.rs new file mode 100644 index 0000000..07d1cfc --- /dev/null +++ b/src/monitored_files.rs @@ -0,0 +1,31 @@ +/// +pub struct MonitoredFiles +{ + /// + mod_dates: Vec>, + + /// + paths: Vec> +} + + +impl MonitoredFiles +{ + pub fn new() -> Self + { + MonitoredFiles + { + mod_dates: Vec::new(), + paths: Vec::new() + } + } +} + + +impl std::fmt::Display for Point +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + write!(f, "({}, {})", self.x, self.y) + } +}