Skip to content

Commit

Permalink
add comments and extend support of meta information
Browse files Browse the repository at this point in the history
- add functions to determine the file type and the file size
  • Loading branch information
stlankes committed Jan 6, 2024
1 parent 3999dbf commit 1a9cb35
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bitflags! {
#[derive(Debug, Copy, Clone)]
pub struct AccessPermission: u32 {
const S_IFMT = 0o170000;
const S_IFSOCK = 0140000;
const S_IFSOCK = 0o140000;
const S_IFLNK = 0o120000;
const S_IFREG = 0o100000;
const S_IFBLK = 0o060000;
Expand Down
12 changes: 12 additions & 0 deletions src/fs/fuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,32 @@ unsafe impl FuseOut for fuse_entry_out {}
#[repr(C)]
#[derive(Default, Debug)]
struct fuse_attr {
/// inode number
pub ino: u64,
/// size in bytes
pub size: u64,
/// size in blocks
pub blocks: u64,
/// time of last access
pub atime: u64,
/// time of last modification
pub mtime: u64,
/// time of last status change
pub ctime: u64,
pub atimensec: u32,
pub mtimensec: u32,
pub ctimensec: u32,
/// access permissions
pub mode: u32,
/// number of hard links
pub nlink: u32,
/// user id
pub uid: u32,
/// group id
pub gid: u32,
/// device id
pub rdev: u32,
/// block size
pub blksize: u32,
pub padding: u32,
}
Expand Down
20 changes: 10 additions & 10 deletions src/fs/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Clone for RamFileInterface {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub(crate) struct RomFile {
data: Arc<RwSpinLock<&'static [u8]>>,
attr: FileAttr,
Expand All @@ -171,15 +171,15 @@ impl VfsNode for RomFile {

fn traverse_lstat(&self, components: &mut Vec<&str>) -> Result<FileAttr, IoError> {
if components.is_empty() {
Ok(self.attr)
self.get_file_attributes()
} else {
Err(IoError::EBADF)
}
}

fn traverse_stat(&self, components: &mut Vec<&str>) -> Result<FileAttr, IoError> {
if components.is_empty() {
Ok(self.attr)
self.get_file_attributes()
} else {
Err(IoError::EBADF)
}
Expand All @@ -193,7 +193,7 @@ impl RomFile {
slice::from_raw_parts(ptr, length)
})),
attr: FileAttr {
st_mode: mode,
st_mode: mode | AccessPermission::S_IFREG,
..Default::default()
},
}
Expand Down Expand Up @@ -223,15 +223,15 @@ impl VfsNode for RamFile {

fn traverse_lstat(&self, components: &mut Vec<&str>) -> Result<FileAttr, IoError> {
if components.is_empty() {
Ok(self.attr)
self.get_file_attributes()
} else {
Err(IoError::EBADF)
}
}

fn traverse_stat(&self, components: &mut Vec<&str>) -> Result<FileAttr, IoError> {
if components.is_empty() {
Ok(self.attr)
self.get_file_attributes()
} else {
Err(IoError::EBADF)
}
Expand All @@ -243,7 +243,7 @@ impl RamFile {
Self {
data: Arc::new(RwSpinLock::new(Vec::new())),
attr: FileAttr {
st_mode: mode,
st_mode: mode | AccessPermission::S_IFREG,
..Default::default()
},
}
Expand All @@ -263,7 +263,7 @@ impl MemDirectory {
Self {
inner: Arc::new(RwSpinLock::new(BTreeMap::new())),
attr: FileAttr {
st_mode: mode,
st_mode: mode | AccessPermission::S_IFDIR,
..Default::default()
},
}
Expand Down Expand Up @@ -392,7 +392,7 @@ impl VfsNode for MemDirectory {

if components.is_empty() {
if let Some(node) = self.inner.read().get(&node_name) {
node.get_file_attributes()?;
return node.get_file_attributes();
}
}

Expand All @@ -412,7 +412,7 @@ impl VfsNode for MemDirectory {

if components.is_empty() {
if let Some(node) = self.inner.read().get(&node_name) {
node.get_file_attributes()?;
return node.get_file_attributes();
}
}

Expand Down
32 changes: 27 additions & 5 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,27 @@ pub struct FileAttr {
pub st_dev: u64,
pub st_ino: u64,
pub st_nlink: u64,
/// access permissions
pub st_mode: AccessPermission,
/// user id
pub st_uid: u32,
/// group id
pub st_gid: u32,
/// device id
pub st_rdev: u64,
/// size in bytes
pub st_size: i64,
/// block size
pub st_blksize: i64,
/// size in blocks
pub st_blocks: i64,
/// time of last access
pub st_atime: i64,
pub st_atime_nsec: i64,
/// time of last modification
pub st_mtime: i64,
pub st_mtime_nsec: i64,
/// time of last status change
pub st_ctime: i64,
pub st_ctime_nsec: i64,
}
Expand Down Expand Up @@ -410,18 +420,30 @@ pub fn file_attributes(path: &str) -> Result<FileAttr, IoError> {
FILESYSTEM.get().unwrap().lstat(path)
}

#[allow(clippy::len_without_is_empty)]
#[derive(Debug, Copy, Clone)]
pub struct Metadata(FileAttr);

impl Metadata {
pub fn new(attr: FileAttr) -> Self {
Self(attr)
}

/// Returns the size of the file, in bytes
pub fn len(&self) -> usize {
self.0.st_size.try_into().unwrap()
}

/// Returns true if this metadata is for a file.
pub fn is_file(&self) -> bool {
self.0.st_mode.contains(AccessPermission::S_IFREG)
}

/// Returns true if this metadata is for a directory.
pub fn is_dir(&self) -> bool {
self.0.st_mode.contains(AccessPermission::S_IFDIR)
}
}

/// Given a path, query the file system to get information about a file, directory, etc.
pub fn metadata(path: &str) -> Result<Metadata, IoError> {
Ok(Metadata(file_attributes(path)?))
}

#[derive(Debug)]
Expand Down Expand Up @@ -464,7 +486,7 @@ impl File {
}

pub fn metadata(&self) -> Result<Metadata, IoError> {
Ok(Metadata::new(file_attributes(&self.path)?))
metadata(&self.path)
}
}

Expand Down

0 comments on commit 1a9cb35

Please sign in to comment.