From 43e1a12b5bce11b4a28a53acca243e35c2be6d3e Mon Sep 17 00:00:00 2001 From: evuez Date: Wed, 3 Apr 2024 22:43:16 +0200 Subject: Initial commit --- src/client/fs/file.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/client/fs/file.rs (limited to 'src/client/fs/file.rs') diff --git a/src/client/fs/file.rs b/src/client/fs/file.rs new file mode 100644 index 0000000..5d51913 --- /dev/null +++ b/src/client/fs/file.rs @@ -0,0 +1,78 @@ +use super::ino::Ino; +use crate::server::blobref::BlobRef; +use fuser::{FileAttr, FileType}; +use std::{ + ffi::{OsStr, OsString}, + time::SystemTime, +}; + +const DEFAULT_PERMISSIONS: u16 = 0o644; + +#[derive(Debug)] +pub struct File { + // Files only have a blobref if they were written to. No blob is created if the + // files is only `touch`ed. This means empty files will disappear on `umount`. + pub blobref: Option, + parent: Option, + pub attr: FileAttr, + name: OsString, +} + +impl File { + fn new(ino: Ino, name: &OsStr, kind: FileType) -> Self { + let now = SystemTime::now(); + + let attr = FileAttr { + ino: ino.into(), + size: 0, + blocks: 0, + atime: now, + mtime: now, + ctime: now, + crtime: now, + kind, + perm: DEFAULT_PERMISSIONS, + nlink: 0, + uid: 0, + gid: 0, + rdev: 0, + flags: 0, + blksize: 0, + }; + + File { + blobref: None, + parent: None, + attr, + name: name.into(), + } + } + + pub fn new_regular_file>(ino: Ino, name: T) -> Self { + Self::new(ino, &name.into(), FileType::RegularFile) + } + + pub fn new_directory>(ino: Ino, name: T) -> Self { + Self::new(ino, &name.into(), FileType::Directory) + } + + pub fn set_parent(&mut self, ino: Ino) { + self.parent = Some(ino); + } + + pub fn name(&self) -> OsString { + self.name.clone() + } + + pub fn parent(&self) -> Option { + self.parent + } + + pub fn ino(&self) -> Ino { + self.attr.ino.into() + } + + pub fn size(&self) -> usize { + self.attr.size as usize + } +} -- cgit v1.2.3