Handle updates of source files
This commit is contained in:
+98
-12
@@ -3,10 +3,15 @@ use std::{
|
||||
fs, io,
|
||||
os::unix::fs::{MetadataExt, PermissionsExt},
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex, mpsc},
|
||||
thread,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use fuser::{Errno, FileAttr, Filesystem, Generation, INodeNo, Request};
|
||||
use notify::{Event, EventKind, RecursiveMode, Watcher};
|
||||
|
||||
use crate::file_watcher::FileWatcher;
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
enum FileType {
|
||||
@@ -28,14 +33,14 @@ struct LocalItem {
|
||||
pub struct LocalOrigin {
|
||||
source: PathBuf,
|
||||
destination: PathBuf,
|
||||
files: BTreeMap<INodeNo, LocalItem>,
|
||||
files: Arc<Mutex<BTreeMap<INodeNo, LocalItem>>>,
|
||||
}
|
||||
|
||||
impl LocalOrigin {
|
||||
pub fn new(source: String, destination: String) -> Result<LocalOrigin, io::Error> {
|
||||
println!("Initializing the LocalOrigin");
|
||||
|
||||
let mut map: BTreeMap<INodeNo, LocalItem> = BTreeMap::new();
|
||||
let map: Arc<Mutex<BTreeMap<INodeNo, LocalItem>>> = Arc::new(Mutex::new(BTreeMap::new()));
|
||||
|
||||
let local_root = LocalItem {
|
||||
inode: INodeNo::ROOT,
|
||||
@@ -45,9 +50,9 @@ impl LocalOrigin {
|
||||
file_type: FileType::Directory,
|
||||
metadata: fs::metadata(&source).unwrap(),
|
||||
};
|
||||
map.insert(INodeNo::ROOT, local_root);
|
||||
map.lock().unwrap().insert(INodeNo::ROOT, local_root);
|
||||
|
||||
match LocalOrigin::read_source(PathBuf::from(&source), Path::new(&destination), &mut map) {
|
||||
match LocalOrigin::read_source(Path::new(&source), Path::new(&destination), &map) {
|
||||
Ok(_) => {
|
||||
let local_origin = LocalOrigin {
|
||||
source: source.into(),
|
||||
@@ -62,10 +67,29 @@ impl LocalOrigin {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_source(
|
||||
source: PathBuf,
|
||||
fn fill_fileset(
|
||||
map: &Arc<Mutex<BTreeMap<INodeNo, LocalItem>>>,
|
||||
source: &Path,
|
||||
destination: &Path,
|
||||
map: &mut BTreeMap<INodeNo, LocalItem>,
|
||||
) {
|
||||
let local_root = LocalItem {
|
||||
inode: INodeNo::ROOT,
|
||||
name: "/".to_string(),
|
||||
original_path: source.to_path_buf(),
|
||||
local_path: destination.to_path_buf(),
|
||||
file_type: FileType::Directory,
|
||||
metadata: fs::metadata(&source).unwrap(),
|
||||
};
|
||||
map.lock().unwrap().insert(INodeNo::ROOT, local_root);
|
||||
|
||||
let _ = LocalOrigin::read_source(&source, &destination, &map)
|
||||
.inspect_err(|e| eprintln!("Error while reading source: {e}"));
|
||||
}
|
||||
|
||||
fn read_source(
|
||||
source: &Path,
|
||||
destination: &Path,
|
||||
map: &Arc<Mutex<BTreeMap<INodeNo, LocalItem>>>,
|
||||
) -> Result<(), io::Error> {
|
||||
match fs::read_dir(source) {
|
||||
Ok(dir) => {
|
||||
@@ -95,10 +119,10 @@ impl LocalOrigin {
|
||||
};
|
||||
let inode = local_item.inode;
|
||||
|
||||
map.insert(inode, local_item);
|
||||
map.lock().unwrap().insert(inode, local_item);
|
||||
|
||||
if file_type == FileType::Directory {
|
||||
match LocalOrigin::read_source(item_path, destination, map) {
|
||||
match LocalOrigin::read_source(&item_path, destination, map) {
|
||||
Ok(_) => {}
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
@@ -155,11 +179,12 @@ impl Filesystem for LocalOrigin {
|
||||
&self,
|
||||
_req: &fuser::Request,
|
||||
ino: INodeNo,
|
||||
fh: Option<fuser::FileHandle>,
|
||||
_fh: Option<fuser::FileHandle>,
|
||||
reply: fuser::ReplyAttr,
|
||||
) {
|
||||
println!("getattr(ino={})", ino);
|
||||
match self.files.get(&ino) {
|
||||
// TODO fix unwrap
|
||||
match self.files.lock().unwrap().get(&ino) {
|
||||
Some(file) => {
|
||||
let ttl = Duration::new(1, 0);
|
||||
let attr = LocalOrigin::local_file_to_file_attr(file);
|
||||
@@ -195,7 +220,15 @@ impl Filesystem for LocalOrigin {
|
||||
);
|
||||
}
|
||||
|
||||
for (i, (key, value)) in self.files.iter().skip(offset as usize).enumerate() {
|
||||
// TODO fix unwrap
|
||||
for (i, (key, value)) in self
|
||||
.files
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.skip(offset as usize)
|
||||
.enumerate()
|
||||
{
|
||||
let offset: u64 = 2 + i as u64;
|
||||
let file_type: fuser::FileType = LocalOrigin::file_type(value.file_type);
|
||||
let path = &value.local_path;
|
||||
@@ -223,6 +256,8 @@ impl Filesystem for LocalOrigin {
|
||||
|
||||
return match self
|
||||
.files
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|item| item.1.name == name.to_str().unwrap())
|
||||
{
|
||||
@@ -236,3 +271,54 @@ impl Filesystem for LocalOrigin {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl FileWatcher for LocalOrigin {
|
||||
fn watch(&self) {
|
||||
let files = self.files.clone();
|
||||
let source = self.source.clone();
|
||||
let destination = self.destination.clone();
|
||||
|
||||
println!("Starting to watch the source files in another thread");
|
||||
thread::spawn(move || {
|
||||
let (tx, rx): (
|
||||
mpsc::Sender<Result<Event, notify::Error>>,
|
||||
mpsc::Receiver<Result<Event, notify::Error>>,
|
||||
) = mpsc::channel();
|
||||
|
||||
// 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::INotifyWatcher = notify::recommended_watcher(tx).unwrap();
|
||||
|
||||
// Add a path to be watched. All files and directories at that path and
|
||||
// below will be monitored for changes.
|
||||
watcher.watch(&source, RecursiveMode::Recursive).unwrap();
|
||||
// Block forever, printing out events as they come in
|
||||
for res in rx {
|
||||
match res {
|
||||
Ok(event) => {
|
||||
println!("event: {:?}", event);
|
||||
|
||||
match event.kind {
|
||||
EventKind::Any => {
|
||||
println!("Something happened to item, ignoring");
|
||||
}
|
||||
EventKind::Access(_access_kind) => {
|
||||
println!("Item was read");
|
||||
}
|
||||
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) => {
|
||||
println!("Item was removed");
|
||||
LocalOrigin::fill_fileset(&files, &source, &destination);
|
||||
}
|
||||
EventKind::Other => {
|
||||
println!("Some other action happened to item, ignoring");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => println!("watch error: {:?}", e),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user