implemented read

This commit is contained in:
Moritz Gmeiner 2025-08-01 01:08:48 +02:00
commit 2b01b9ff0e
6 changed files with 149 additions and 23 deletions

View file

@ -1,11 +1,12 @@
use std::ffi::c_int;
use std::io::Read;
use std::rc::Rc;
use std::time::Duration;
use fat_bits::dir::DirEntry;
use fuser::{FileType, Filesystem};
use libc::{EINVAL, EIO, ENOENT, ENOSYS, ENOTDIR};
use log::{debug, warn};
use libc::{EBADF, EINVAL, EIO, EISDIR, ENOENT, ENOSYS, ENOTDIR};
use log::{debug, error};
use crate::{FatFuse, Inode};
@ -281,6 +282,8 @@ impl Filesystem for FatFuse {
debug!("fh {} was associated with ino {}, now with ino {}", fh, old_ino, ino);
}
debug!("opened inode {}: fh {}", ino, fh);
reply.opened(fh, 0);
}
@ -291,16 +294,74 @@ impl Filesystem for FatFuse {
fh: u64,
offset: i64,
size: u32,
flags: i32,
lock_owner: Option<u64>,
_flags: i32,
_lock_owner: Option<u64>,
reply: fuser::ReplyData,
) {
warn!(
"[Not Implemented] read(ino: {:#x?}, fh: {}, offset: {}, size: {}, \
flags: {:#x?}, lock_owner: {:?})",
ino, fh, offset, size, flags, lock_owner
);
reply.error(ENOSYS);
// warn!(
// "[Not Implemented] read(ino: {:#x?}, fh: {}, offset: {}, size: {}, \
// flags: {:#x?}, lock_owner: {:?})",
// ino, fh, offset, size, flags, lock_owner
// );
// reply.error(ENOSYS);
if offset < 0 {
debug!("tried to read with negative offset {offset}");
reply.error(EINVAL);
return;
}
let offset = offset as u64;
let Some(inode) = self.get_inode_by_fh(fh) else {
debug!("fh {fh} is not associated by any inode");
reply.error(EBADF);
return;
};
if inode.ino() != ino {
debug!("fh {fh} is associated with inode {} instead of {ino}", inode.ino());
reply.error(EIO);
return;
}
if !inode.is_file() {
debug!("tried to use read on directory {ino}");
reply.error(EISDIR);
return;
}
let mut reader = match inode.file_reader(&self.fat_fs) {
Ok(reader) => reader,
Err(err) => {
reply.error(err);
return;
}
};
if reader.skip(offset) != offset {
// reader is exhausted, bail
reply.data(&[]);
return;
}
let mut buf = vec![0; size as usize];
let n = match reader.read(&mut buf) {
Ok(n) => n,
Err(err) => {
error!("error while reading: {err}");
reply.error(EIO);
return;
}
};
reply.data(&buf[..n]);
}
fn write(
@ -347,13 +408,27 @@ impl Filesystem for FatFuse {
fn release(
&mut self,
_req: &fuser::Request<'_>,
_ino: u64,
_fh: u64,
ino: u64,
fh: u64,
_flags: i32,
_lock_owner: Option<u64>,
_flush: bool,
reply: fuser::ReplyEmpty,
) {
let Some(found_ino) = self.ino_by_fh.remove(&fh) else {
debug!("tried to release fh {fh} with ino {ino}, but no ino was found in mapping");
reply.error(EINVAL);
return;
};
if found_ino != ino {
debug!("tried to release fh {fh} with ino {ino}, but found ino is {found_ino} instead");
reply.error(EIO);
return;
}
reply.ok();
}

View file

@ -5,8 +5,9 @@ use std::time::SystemTime;
use chrono::{NaiveDateTime, NaiveTime};
use fat_bits::FatFs;
use fat_bits::dir::DirEntry;
use fat_bits::iter::ClusterChainReader;
use fuser::FileAttr;
use libc::ENOTDIR;
use libc::{EISDIR, ENOTDIR};
use log::debug;
use rand::{Rng, SeedableRng as _};
@ -232,6 +233,14 @@ impl Inode {
self.kind
}
pub fn is_file(&self) -> bool {
self.kind == Kind::File
}
pub fn is_dir(&self) -> bool {
self.kind == Kind::Dir
}
pub fn first_cluster(&self) -> u32 {
self.first_cluster
}
@ -281,4 +290,12 @@ impl Inode {
Ok(fat_fs.dir_iter(self.first_cluster))
}
pub fn file_reader<'a>(&'a self, fat_fs: &'a FatFs) -> Result<ClusterChainReader<'a>, i32> {
if self.is_dir() {
return Err(EISDIR);
}
Ok(fat_fs.file_reader(self.first_cluster()))
}
}