This is the initial monitoring.
Here I am building up the list of directories and files to monitor.
This commit is contained in:
parent
a90917caea
commit
5c98c8a608
80
src/dir_monitor.rs
Normal file
80
src/dir_monitor.rs
Normal file
@ -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<PathBuf> = 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(())
|
||||
}
|
29
src/event.rs
Normal file
29
src/event.rs
Normal file
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
31
src/monitored_files.rs
Normal file
31
src/monitored_files.rs
Normal file
@ -0,0 +1,31 @@
|
||||
///
|
||||
pub struct MonitoredFiles
|
||||
{
|
||||
///
|
||||
mod_dates: Vec<Option<std::time::SystemTime>>,
|
||||
|
||||
///
|
||||
paths: Vec<Option<std::path::PathBuf>>
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user