Start implementing LocalSource

This commit is contained in:
Alexander
2026-06-20 13:36:15 +02:00
parent 872580602d
commit 9349d29200
5 changed files with 507 additions and 184 deletions
+175
View File
@@ -0,0 +1,175 @@
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);
}
}
+186
View File
@@ -0,0 +1,186 @@
use std::{
collections::BTreeMap,
fs, io,
os::unix::fs::{MetadataExt, PermissionsExt},
path::{Path, PathBuf},
time::{Duration, SystemTime},
};
use fuser::{Errno, FileAttr, Filesystem, INodeNo};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
enum FileType {
Directory,
File,
}
#[derive(Debug)]
struct LocalItem {
inode: INodeNo,
name: String,
original_path: PathBuf,
local_path: PathBuf,
file_type: FileType,
metadata: fs::Metadata,
}
#[derive(Debug)]
pub struct LocalOrigin {
source: PathBuf,
destination: PathBuf,
files: 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 local_root = LocalItem {
inode: INodeNo::ROOT,
name: "/".to_string(),
original_path: PathBuf::from(&source),
local_path: PathBuf::from(&destination),
file_type: FileType::Directory,
metadata: fs::metadata(&source).unwrap(),
};
map.insert(INodeNo::ROOT, local_root);
match LocalOrigin::read_source(PathBuf::from(&source), Path::new(&destination), &mut map) {
Ok(_) => {
let local_origin = LocalOrigin {
source: source.into(),
destination: destination.into(),
files: map,
};
dbg!(&local_origin);
return Ok(local_origin);
}
Err(err) => return Err(err),
}
}
fn read_source(
source: PathBuf,
destination: &Path,
map: &mut BTreeMap<INodeNo, LocalItem>,
) -> Result<(), io::Error> {
match fs::read_dir(source) {
Ok(dir) => {
for item in dir {
match item {
Ok(entry) => {
let name = entry.file_name().to_string_lossy().into_owned();
let item_path: PathBuf = entry.path();
let metadata: fs::Metadata = entry.metadata().unwrap();
let file_type = if entry.file_type().unwrap().is_dir() {
FileType::Directory
} else {
FileType::File
};
let mut local_path: PathBuf = PathBuf::new();
local_path.push(destination);
local_path.push(name.clone());
let local_item = LocalItem {
inode: INodeNo(metadata.ino()),
name: name,
original_path: item_path.clone(),
local_path: local_path,
file_type: file_type,
metadata,
};
let inode = local_item.inode;
map.insert(inode, local_item);
if file_type == FileType::Directory {
match LocalOrigin::read_source(item_path, destination, map) {
Ok(_) => {}
Err(err) => return Err(err),
};
}
}
Err(err) => return Err(err),
}
}
}
Err(err) => return Err(err),
};
return Ok(());
}
fn local_file_to_file_attr(local_file: &LocalItem) -> FileAttr {
let metadata = &local_file.metadata;
let ctime = std::time::UNIX_EPOCH + Duration::from_secs(metadata.ctime() as u64);
let atime = metadata
.accessed()
.map_err(|err| {
println!("Failed to get atime for: {}", &local_file.name);
err
})
.unwrap();
let mtime = metadata
.modified()
.map_err(|err| {
println!("Failed to get atime for: {}", &local_file.name);
err
})
.unwrap();
let crtime = metadata
.created()
.map_err(|err| {
println!("Failed to get atime for: {}", &local_file.name);
err
})
.unwrap();
let kind = match local_file.file_type {
FileType::Directory => fuser::FileType::Directory,
FileType::File => fuser::FileType::RegularFile,
};
return FileAttr {
ino: local_file.inode,
size: metadata.size(),
blocks: metadata.blocks(),
atime: atime,
mtime: mtime,
ctime: ctime,
crtime: crtime,
kind: kind,
perm: metadata.permissions().mode() as u16,
nlink: metadata.nlink() as u32,
uid: metadata.uid(),
gid: metadata.gid(),
rdev: metadata.rdev() as u32,
blksize: metadata.blksize() as u32,
flags: 0, // TODO this is for macos only but perhaps we could get it from metadata still?
};
}
}
impl Filesystem for LocalOrigin {
fn getattr(
&self,
_req: &fuser::Request,
ino: INodeNo,
fh: Option<fuser::FileHandle>,
reply: fuser::ReplyAttr,
) {
println!("getattr(ino={})", ino);
match self.files.get(&ino) {
Some(file) => {
let ttl = Duration::new(1, 0);
let attr = LocalOrigin::local_file_to_file_attr(file);
reply.attr(&ttl, &attr);
}
None => reply.error(Errno::ENOENT),
}
}
}
+18 -184
View File
@@ -1,196 +1,26 @@
use fuser::{
Errno, FileAttr, FileType, Filesystem, Generation, INodeNo, ReplyAttr, ReplyDirectory, Request,
};
use serde_json::{Map, Value, json};
use std::collections::BTreeMap;
use clap::Parser;
use serde_json::json;
use std::env;
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, SystemTime};
use std::time::Duration;
struct JsonFilesystem {
tree: Map<String, Value>,
attrs: BTreeMap<INodeNo, FileAttr>,
inodes: BTreeMap<String, INodeNo>,
}
mod json_filesystem;
mod local;
impl JsonFilesystem {
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,
};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long, required = true)]
mountpoint: String,
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);
}
#[arg(short, long, required = true)]
source: String,
}
fn main() {
let data = json!({
"foo": "bar",
"answer": 42,
});
let tree = data.as_object().unwrap();
let fs = JsonFilesystem::new(tree);
let mountpoint = match env::args().nth(1) {
Some(path) => path,
None => {
println!("Usage: {} <MOUNTPOINT>", env::args().nth(0).unwrap());
return;
}
};
let args = Args::parse();
let mountpoint = args.mountpoint;
let running = Arc::new(AtomicBool::new(true));
let r_clone = running.clone();
@@ -201,6 +31,10 @@ fn main() {
})
.expect("Error setting Ctrl+C handler");
// 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 cfg = fuser::Config::default();
let session = fuser::spawn_mount2(fs, &mountpoint, &cfg);