I added threading (I don't really know what I'm doing)
The bytes update whenever a tracked file is edited/deleted.
This commit is contained in:
59
src/files.rs
59
src/files.rs
@@ -1,27 +1,66 @@
|
||||
extern crate notify;
|
||||
use notify::{
|
||||
DebouncedEvent,
|
||||
RecommendedWatcher,
|
||||
RecursiveMode,
|
||||
Watcher
|
||||
Watcher,
|
||||
Result,
|
||||
Event,
|
||||
};
|
||||
use std::
|
||||
{
|
||||
fs,
|
||||
time::Duration,
|
||||
path::Path,
|
||||
sync::mpsc::channel,
|
||||
path::PathBuf,
|
||||
sync::mpsc,
|
||||
};
|
||||
use crate::
|
||||
{
|
||||
ROOT_DIR,
|
||||
};
|
||||
|
||||
pub fn watch_files(file_list: &Vec<PathBuf>) -> u8
|
||||
{
|
||||
let (tx, rx) = mpsc::channel::<Result<Event>>(); // Use recommended_watcher() to automatically select the best implementation
|
||||
|
||||
// for your platform. The `EventHandler` passed to this constructor can be a
|
||||
// closure, a `std::sync::mpsc::Sender`, a `crossbeam_channel::Sender`, or
|
||||
// another type the trait is implemented for.
|
||||
let mut watcher = notify::recommended_watcher(tx).expect("Watcher start failed"); // Add a path to be watched. All files and directories at that path and
|
||||
|
||||
// below will be monitored for changes.
|
||||
watcher.watch(Path::new("root"), RecursiveMode::Recursive).expect("Path watching failed"); // Block forever, printing out events as they come in
|
||||
for res in rx
|
||||
{
|
||||
match res
|
||||
{
|
||||
Ok(event) =>
|
||||
{
|
||||
// Ignore access event kinds as they're irrelevant
|
||||
if ! notify::EventKind::is_access(&event.kind)
|
||||
&& file_list.contains(&event.paths[0])
|
||||
{
|
||||
return 0
|
||||
}
|
||||
},
|
||||
Err(e) =>
|
||||
{
|
||||
println!("watch error: {:?}", e);
|
||||
return 2
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("Watching files...");
|
||||
return 1
|
||||
}
|
||||
|
||||
pub fn read_file(file_name: &str, depth: u8) ->
|
||||
(
|
||||
Vec<u8>,
|
||||
Vec<String>,
|
||||
Vec<PathBuf>,
|
||||
)
|
||||
{
|
||||
if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name))
|
||||
{
|
||||
let (bytes, mut file_list) = encode_npon(&file_contents, depth);
|
||||
let (bytes, file_list) = encode_npon(&file_contents, depth);
|
||||
return (bytes, file_list)
|
||||
}
|
||||
return (vec![], vec![])
|
||||
@@ -30,11 +69,11 @@ pub fn read_file(file_name: &str, depth: u8) ->
|
||||
fn encode_npon(file_contents: &String, depth: u8) ->
|
||||
(
|
||||
Vec<u8>,
|
||||
Vec<String>,
|
||||
Vec<PathBuf>,
|
||||
)
|
||||
{
|
||||
let mut bytes: Vec<u8> = vec![];
|
||||
let mut file_list: Vec<String> = vec![];
|
||||
let mut file_list: Vec<PathBuf> = vec![];
|
||||
if depth > 63 {
|
||||
println!("Depth level safety reached (63), recursion error suspected");
|
||||
return (vec![], file_list);
|
||||
@@ -139,7 +178,7 @@ fn encode_npon(file_contents: &String, depth: u8) ->
|
||||
{
|
||||
58 =>
|
||||
{
|
||||
file_list.push((&file_contents[index_flag..index.clone()]).to_string());
|
||||
file_list.push(ROOT_DIR.join(&file_contents[index_flag..index.clone()]));
|
||||
let (new_bytes, mut new_file_list) = read_file(&file_contents[index_flag..index], depth+1);
|
||||
file_list.append(&mut new_file_list);
|
||||
for byte in new_bytes.iter()
|
||||
|
||||
Reference in New Issue
Block a user