Skip to content

Commit

Permalink
ramfs: Add stat method
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel committed Oct 21, 2023
1 parent ddcd3f4 commit 8c48844
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
16 changes: 0 additions & 16 deletions cykusz-rs/src/drivers/tty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,22 +432,6 @@ impl INode for Tty {
Ok(self.dev_id)
}

fn stat(&self) -> Result<syscall_defs::stat::Stat, FsError> {
let mut stat = syscall_defs::stat::Stat::default();

logln!("CALLING STAT ON TTY");

stat.st_ino = self.inode().id()? as u64;
//stat.st_dev = self.device().unwrap().id() as u64;
stat.st_mode.insert(syscall_defs::stat::Mode::IFCHR);

stat.st_mode.insert(syscall_defs::stat::Mode::IRWXU);
stat.st_mode.insert(syscall_defs::stat::Mode::IRWXG);
stat.st_mode.insert(syscall_defs::stat::Mode::IRWXO);

Ok(stat)
}

fn read_at(&self, _offset: usize, buf: &mut [u8]) -> Result<usize, FsError> {
logln2!("try tty read");
let r = self.read(buf.as_mut_ptr(), buf.len());
Expand Down
4 changes: 0 additions & 4 deletions cykusz-rs/src/kernel/fs/devnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ impl INode for DevNode {
self.dev.inode().metadata()
}

fn stat(&self) -> Result<Stat> {
self.dev.inode().stat()
}

fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> {
self.dev.inode().read_at(offset, buf)
}
Expand Down
36 changes: 32 additions & 4 deletions cykusz-rs/src/kernel/fs/ramfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use crate::kernel::fs::pcache::MappedAccess;
use crate::kernel::fs::poll::PollTable;
use crate::kernel::fs::vfs::Result;
use crate::kernel::fs::vfs::{FsError, Metadata};
use crate::kernel::mm::PAGE_SIZE;
use crate::kernel::sync::{RwSpin, Spin};
use crate::kernel::utils::types::CeilDiv;

struct LockedRamINode(RwSpin<RamINode>);

Expand Down Expand Up @@ -252,12 +254,38 @@ impl INode for LockedRamINode {
}
}

fn stat(&self) -> Result<Stat> {
if let Content::DevNode(Some(d)) = &self.0.read().content {
d.stat()
fn stat(&self) -> Result<syscall_defs::stat::Stat> {
let mut stat = syscall_defs::stat::Stat::default();

stat.st_ino = self.id()? as u64;
stat.st_dev = 0;

let content = self.0.read();
if let Content::Bytes(b) = &content.content {
let bytes = b.lock();

stat.st_nlink = 1;
stat.st_blksize = PAGE_SIZE as u64;
stat.st_blocks = bytes.len().ceil_div(PAGE_SIZE) as u64;
stat.st_size = bytes.len() as i64;
}

let ftype = content.typ;
if ftype == FileType::File {
stat.st_mode.insert(syscall_defs::stat::Mode::IFREG);
} else if ftype == FileType::Dir {
stat.st_mode.insert(syscall_defs::stat::Mode::IFDIR);
} else if ftype == FileType::Symlink {
stat.st_mode.insert(syscall_defs::stat::Mode::IFLNK);
} else {
Err(FsError::NotSupported)
stat.st_mode.insert(syscall_defs::stat::Mode::IFCHR);
}

stat.st_mode.insert(syscall_defs::stat::Mode::IRWXU);
stat.st_mode.insert(syscall_defs::stat::Mode::IRWXG);
stat.st_mode.insert(syscall_defs::stat::Mode::IRWXO);

Ok(stat)
}

fn as_mappable(&self) -> Option<Arc<dyn MappedAccess>> {
Expand Down

0 comments on commit 8c48844

Please sign in to comment.