Handle updates of source files
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
pub trait FileWatcher {
|
||||
fn watch(&self);
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
use fuser::{
|
||||
Errno, FileAttr, FileType, Filesystem, Generation, INodeNo, ReplyAttr, ReplyDirectory, Request,
|
||||
};
|
||||
use serde_json::{Map, Value};
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
pub struct JsonFilesystem {
|
||||
tree: Map<String, Value>,
|
||||
attrs: BTreeMap<INodeNo, FileAttr>,
|
||||
inodes: BTreeMap<String, INodeNo>,
|
||||
}
|
||||
|
||||
impl JsonFilesystem {
|
||||
pub fn new(tree: &Map<String, Value>) -> JsonFilesystem {
|
||||
let mut attrs = BTreeMap::new();
|
||||
let mut inodes = BTreeMap::new();
|
||||
let ts = SystemTime::now();
|
||||
let attr = FileAttr {
|
||||
ino: INodeNo::ROOT,
|
||||
size: 0,
|
||||
blocks: 0,
|
||||
atime: ts,
|
||||
mtime: ts,
|
||||
ctime: ts,
|
||||
crtime: ts,
|
||||
kind: FileType::Directory,
|
||||
perm: 0o755,
|
||||
nlink: 0,
|
||||
uid: 0,
|
||||
gid: 0,
|
||||
rdev: 0,
|
||||
blksize: 0,
|
||||
flags: 0,
|
||||
};
|
||||
|
||||
attrs.insert(INodeNo(1), attr);
|
||||
inodes.insert("/".to_string(), INodeNo(1));
|
||||
|
||||
for (i, (key, value)) in tree.iter().enumerate() {
|
||||
let attr = FileAttr {
|
||||
ino: INodeNo(i as u64 + 2),
|
||||
size: value.to_string().len() as u64,
|
||||
blocks: 0,
|
||||
atime: ts,
|
||||
mtime: ts,
|
||||
ctime: ts,
|
||||
crtime: ts,
|
||||
kind: FileType::RegularFile,
|
||||
perm: 0o644,
|
||||
nlink: 0,
|
||||
uid: 0,
|
||||
gid: 0,
|
||||
rdev: 0,
|
||||
blksize: 0,
|
||||
flags: 0,
|
||||
};
|
||||
|
||||
attrs.insert(attr.ino, attr);
|
||||
inodes.insert(key.clone(), attr.ino);
|
||||
}
|
||||
|
||||
return JsonFilesystem {
|
||||
tree: tree.clone(),
|
||||
attrs: attrs,
|
||||
inodes: inodes,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Filesystem for JsonFilesystem {
|
||||
// For stat function like `stat /path/to/fuse`
|
||||
fn getattr(
|
||||
&self,
|
||||
_req: &Request,
|
||||
ino: fuser::INodeNo,
|
||||
fh: Option<fuser::FileHandle>,
|
||||
reply: ReplyAttr,
|
||||
) {
|
||||
println!("getattr(ino={})", ino);
|
||||
|
||||
match self.attrs.get(&ino) {
|
||||
Some(attr) => {
|
||||
let ttl = Duration::new(1, 0);
|
||||
reply.attr(&ttl, attr);
|
||||
}
|
||||
None => reply.error(Errno::ENOENT),
|
||||
};
|
||||
}
|
||||
|
||||
fn readdir(
|
||||
&self,
|
||||
_req: &Request,
|
||||
ino: INodeNo,
|
||||
fh: fuser::FileHandle,
|
||||
offset: u64,
|
||||
mut reply: ReplyDirectory,
|
||||
) {
|
||||
println!("readdir(ino={}, fh={}, offset={})", ino, fh, offset);
|
||||
|
||||
if ino == INodeNo::ROOT {
|
||||
if offset == 0 {
|
||||
let _ = reply.add(INodeNo::ROOT, 0, FileType::Directory, &Path::new("."));
|
||||
let _ = reply.add(INodeNo::ROOT, 1, FileType::Directory, &Path::new(".."));
|
||||
for (i, key) in self.tree.keys().enumerate() {
|
||||
let inode: u64 = 2 + i as u64;
|
||||
let offset: u64 = 2 + i as u64;
|
||||
let _ = reply.add(
|
||||
INodeNo(inode),
|
||||
offset,
|
||||
FileType::RegularFile,
|
||||
&Path::new(key),
|
||||
);
|
||||
}
|
||||
}
|
||||
reply.ok();
|
||||
} else {
|
||||
reply.error(Errno::ENOSYS);
|
||||
}
|
||||
}
|
||||
|
||||
fn lookup(
|
||||
&self,
|
||||
_req: &Request,
|
||||
parent: INodeNo,
|
||||
name: &std::ffi::OsStr,
|
||||
reply: fuser::ReplyEntry,
|
||||
) {
|
||||
println!("lookup(parent={}, name={})", parent, name.display());
|
||||
|
||||
let inode = match self.inodes.get(name.to_str().unwrap()) {
|
||||
Some(inode) => inode,
|
||||
None => {
|
||||
reply.error(Errno::ENOENT);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
match self.attrs.get(inode) {
|
||||
Some(attr) => {
|
||||
let ttl = Duration::new(1, 0);
|
||||
reply.entry(&ttl, attr, Generation(0));
|
||||
}
|
||||
None => reply.error(Errno::ENOENT),
|
||||
};
|
||||
}
|
||||
|
||||
fn read(
|
||||
&self,
|
||||
_req: &Request,
|
||||
ino: INodeNo,
|
||||
fh: fuser::FileHandle,
|
||||
offset: u64,
|
||||
size: u32,
|
||||
flags: fuser::OpenFlags,
|
||||
lock_owner: Option<fuser::LockOwner>,
|
||||
reply: fuser::ReplyData,
|
||||
) {
|
||||
println!(
|
||||
"read(ino={}, fh={}, offset={}, size={})",
|
||||
ino, fh, offset, size
|
||||
);
|
||||
|
||||
for (key, &inode) in self.inodes.iter() {
|
||||
if inode == ino {
|
||||
let value = self.tree.get(key).unwrap();
|
||||
reply.data(value.to_string().as_bytes());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
reply.error(Errno::ENOENT);
|
||||
}
|
||||
}
|
||||
+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),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+7
-4
@@ -1,11 +1,11 @@
|
||||
use clap::Parser;
|
||||
use serde_json::json;
|
||||
use std::env;
|
||||
use file_watcher::FileWatcher;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
mod json_filesystem;
|
||||
mod file_watcher;
|
||||
mod local;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
@@ -33,7 +33,10 @@ fn main() {
|
||||
|
||||
// TODO don't think clone is necessary
|
||||
// TODO unwrap might not be safe and better would be to handle it properly
|
||||
let fs = local::LocalOrigin::new(args.source, mountpoint.clone()).unwrap();
|
||||
let fs = local::LocalOrigin::new(args.source.clone(), mountpoint.clone()).unwrap();
|
||||
|
||||
// start watching for changes in source files
|
||||
fs.watch();
|
||||
|
||||
let cfg = fuser::Config::default();
|
||||
let session = fuser::spawn_mount2(fs, &mountpoint, &cfg);
|
||||
|
||||
Reference in New Issue
Block a user