81 lines
1.7 KiB
Rust
81 lines
1.7 KiB
Rust
|
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(())
|
||
|
}
|