Skip to content

Commit

Permalink
move fd table to crates and update rust-fatfs
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed May 31, 2024
1 parent b52dad1 commit 63c82c7
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 62 deletions.
20 changes: 18 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ members = [
"modules/ruxconfig",
"modules/ruxdisplay",
"modules/ruxdriver",
"modules/ruxfdtable",
"modules/ruxfs",
"modules/ruxhal",
"modules/ruxruntime",
Expand Down
1 change: 1 addition & 0 deletions api/ruxos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ruxconfig = { path = "../../modules/ruxconfig" }
axlog = { path = "../../modules/axlog" }
ruxhal = { path = "../../modules/ruxhal" }
axsync = { path = "../../modules/axsync" }
ruxfdtable = { path = "../../modules/ruxfdtable" }
ruxfutex = { path = "../../modules/ruxfutex", optional = true }
axalloc = { path = "../../modules/axalloc", optional = true }
ruxtask = { path = "../../modules/ruxtask", optional = true }
Expand Down
132 changes: 114 additions & 18 deletions api/ruxos_posix_api/src/imp/fd_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,130 @@ use alloc::sync::Arc;
use core::ffi::c_int;

use axerrno::{LinuxError, LinuxResult};
use axio::PollState;
use flatten_objects::FlattenObjects;
use spin::RwLock;
use ruxfdtable::{FileLike, RuxStat, RuxTimeSpec, FD_TABLE, RUX_FILE_LIMIT};

use super::stdio::{stdin, stdout};
use crate::ctypes;

/// Maximum number of files per process
pub const RUX_FILE_LIMIT: usize = 1024;
impl From<ctypes::timespec> for RuxTimeSpec {
fn from(ctimespec: ctypes::timespec) -> Self {
RuxTimeSpec {
tv_sec: ctimespec.tv_sec,
tv_nsec: ctimespec.tv_nsec,
}
}
}

impl From<ctypes::stat> for RuxStat {
#[cfg(target_arch = "aarch64")]
fn from(cstat: ctypes::stat) -> Self {
RuxStat {
st_dev: cstat.st_dev,
st_ino: cstat.st_ino,
st_mode: cstat.st_mode,
st_nlink: cstat.st_nlink,
st_uid: cstat.st_uid,
st_gid: cstat.st_gid,
st_rdev: cstat.st_rdev,
__pad: cstat.__pad,
st_size: cstat.st_size,
st_blksize: cstat.st_blksize,
__pad2: cstat.__pad2,
st_blocks: cstat.st_blocks,
st_atime: RuxTimeSpec::from(cstat.st_atime),
st_mtime: RuxTimeSpec::from(cstat.st_mtime),
st_ctime: RuxTimeSpec::from(cstat.st_ctime),
__unused: cstat.__unused,
}
}

#[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
fn from(cstat: ctypes::stat) -> Self {
RuxStat {
st_dev: cstat.st_dev,
st_ino: cstat.st_ino,
st_nlink: cstat.st_nlink,
st_mode: cstat.st_mode,
st_uid: cstat.st_uid,
st_gid: cstat.st_gid,
__pad0: cstat.__pad0,
st_rdev: cstat.st_rdev,
st_size: cstat.st_size,
st_blksize: cstat.st_blksize,
st_blocks: cstat.st_blocks,
st_atime: RuxTimeSpec::from(cstat.st_atime),
st_mtime: RuxTimeSpec::from(cstat.st_mtime),
st_ctime: RuxTimeSpec::from(cstat.st_ctime),
__unused: cstat.__unused,
}
}
}

pub trait FileLike: Send + Sync {
fn read(&self, buf: &mut [u8]) -> LinuxResult<usize>;
fn write(&self, buf: &[u8]) -> LinuxResult<usize>;
fn stat(&self) -> LinuxResult<ctypes::stat>;
fn into_any(self: Arc<Self>) -> Arc<dyn core::any::Any + Send + Sync>;
fn poll(&self) -> LinuxResult<PollState>;
fn set_nonblocking(&self, nonblocking: bool) -> LinuxResult;
impl From<RuxTimeSpec> for ctypes::timespec {
fn from(rtimespec: RuxTimeSpec) -> Self {
ctypes::timespec {
tv_sec: rtimespec.tv_sec,
tv_nsec: rtimespec.tv_nsec,
}
}
}

impl From<RuxStat> for ctypes::stat {
#[cfg(target_arch = "aarch64")]
fn from(rstat: RuxStat) -> Self {
ctypes::stat {
st_dev: rstat.st_dev,
st_ino: rstat.st_ino,
st_mode: rstat.st_mode,
st_nlink: rstat.st_nlink,
st_uid: rstat.st_uid,
st_gid: rstat.st_gid,
st_rdev: rstat.st_rdev,
__pad: rstat.__pad,
st_size: rstat.st_size,
st_blksize: rstat.st_blksize,
__pad2: rstat.__pad2,
st_blocks: rstat.st_blocks,
st_atime: rstat.st_atime.into(),
st_mtime: rstat.st_mtime.into(),
st_ctime: rstat.st_ctime.into(),
__unused: rstat.__unused,
}
}

#[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
fn from(rstat: RuxStat) -> Self {
ctypes::stat {
st_dev: rstat.st_dev,
st_ino: rstat.st_ino,
st_nlink: rstat.st_nlink,
st_mode: rstat.st_mode,
st_uid: rstat.st_uid,
st_gid: rstat.st_gid,
__pad0: rstat.__pad0,
st_rdev: rstat.st_rdev,
st_size: rstat.st_size,
st_blksize: rstat.st_blksize,
st_blocks: rstat.st_blocks,
st_atime: rstat.st_atime.into(),
st_mtime: rstat.st_mtime.into(),
st_ctime: rstat.st_ctime.into(),
__unused: rstat.__unused,
}
}
}

lazy_static::lazy_static! {
static ref FD_TABLE: RwLock<FlattenObjects<Arc<dyn FileLike>, RUX_FILE_LIMIT>> = {
let mut fd_table = FlattenObjects::new();
fd_table.add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin
fd_table.add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout
fd_table.add_at(2, Arc::new(stdout()) as _).unwrap(); // stderr
RwLock::new(fd_table)
static ref MUST_EXEC: usize = {
FD_TABLE.write().add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin
FD_TABLE.write().add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout
FD_TABLE.write().add_at(2, Arc::new(stdout()) as _).unwrap(); // stderr
0
};
}

pub fn get_file_like(fd: c_int) -> LinuxResult<Arc<dyn FileLike>> {
let _exec = *MUST_EXEC;
FD_TABLE
.read()
.get(fd as usize)
Expand All @@ -49,10 +143,12 @@ pub fn get_file_like(fd: c_int) -> LinuxResult<Arc<dyn FileLike>> {
}

pub fn add_file_like(f: Arc<dyn FileLike>) -> LinuxResult<c_int> {
let _exec = *MUST_EXEC;
Ok(FD_TABLE.write().add(f).ok_or(LinuxError::EMFILE)? as c_int)
}

pub fn close_file_like(fd: c_int) -> LinuxResult {
let _exec = *MUST_EXEC;
let f = FD_TABLE
.write()
.remove(fd as usize)
Expand Down
29 changes: 20 additions & 9 deletions api/ruxos_posix_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ use core::ffi::{c_char, c_int, c_long, c_void};
use axerrno::{LinuxError, LinuxResult};
use axio::{PollState, SeekFrom};
use axsync::Mutex;
use ruxfdtable::{FileLike, RuxStat};
use ruxfs::{
api::set_current_dir,
fops::{DirEntry, OpenOptions},
};

use super::fd_ops::{get_file_like, FileLike};
use super::fd_ops::get_file_like;
use crate::{ctypes, utils::char_ptr_to_str};
use alloc::vec::Vec;

Expand Down Expand Up @@ -54,7 +55,11 @@ impl FileLike for File {
Ok(self.inner.lock().write(buf)?)
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
fn flush(&self) -> LinuxResult {
Ok(self.inner.lock().flush()?)
}

fn stat(&self) -> LinuxResult<RuxStat> {
let metadata = self.inner.lock().get_attr()?;
let ty = metadata.file_type() as u8;
let perm = metadata.perm().bits() as u32;
Expand All @@ -65,7 +70,7 @@ impl FileLike for File {
// TODO: implement real inode.
let st_ino = metadata.size() + st_mode as u64;

Ok(ctypes::stat {
let res = RuxStat::from(ctypes::stat {
st_ino,
st_nlink: 1,
st_mode,
Expand All @@ -75,7 +80,9 @@ impl FileLike for File {
st_blocks: metadata.blocks() as _,
st_blksize: 512,
..Default::default()
})
});

Ok(res)
}

fn into_any(self: Arc<Self>) -> Arc<dyn core::any::Any + Send + Sync> {
Expand Down Expand Up @@ -126,12 +133,16 @@ impl FileLike for Directory {
Err(LinuxError::EACCES)
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<RuxStat> {
let metadata = self.inner.lock().get_attr()?;
let ty = metadata.file_type() as u8;
let perm = metadata.perm().bits() as u32;
let st_mode = ((ty as u32) << 12) | perm;
Ok(ctypes::stat {
Ok(RuxStat::from(ctypes::stat {
st_ino: 1,
st_nlink: 1,
st_mode,
Expand All @@ -141,7 +152,7 @@ impl FileLike for Directory {
st_blocks: metadata.blocks() as _,
st_blksize: 512,
..Default::default()
})
}))
}

fn into_any(self: Arc<Self>) -> Arc<dyn core::any::Any + Send + Sync> {
Expand Down Expand Up @@ -318,7 +329,7 @@ pub unsafe fn sys_stat(path: *const c_char, buf: *mut core::ffi::c_void) -> c_in
let mut options = OpenOptions::new();
options.read(true);
let file = ruxfs::fops::File::open(path?, &options)?;
let st = File::new(file).stat()?;
let st: ctypes::stat = File::new(file).stat()?.into();

#[cfg(not(feature = "musl"))]
{
Expand Down Expand Up @@ -356,7 +367,7 @@ pub fn sys_fstat(fd: c_int, kst: *mut core::ffi::c_void) -> c_int {
#[cfg(not(feature = "musl"))]
{
let buf = kst as *mut ctypes::stat;
unsafe { *buf = get_file_like(fd)?.stat()? };
unsafe { *buf = get_file_like(fd)?.stat()?.into() };
Ok(0)
}
#[cfg(feature = "musl")]
Expand Down
13 changes: 9 additions & 4 deletions api/ruxos_posix_api/src/imp/io_mpx/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ use core::{ffi::c_int, time::Duration};

use axerrno::{LinuxError, LinuxResult};
use axsync::Mutex;
use ruxfdtable::{FileLike, RuxStat};
use ruxhal::time::current_time;

use crate::ctypes;
use crate::imp::fd_ops::{add_file_like, get_file_like, FileLike};
use crate::imp::fd_ops::{add_file_like, get_file_like};

pub struct EpollInstance {
events: Mutex<BTreeMap<usize, ctypes::epoll_event>>,
Expand Down Expand Up @@ -123,14 +124,18 @@ impl FileLike for EpollInstance {
Err(LinuxError::ENOSYS)
}

fn stat(&self) -> LinuxResult<ctypes::stat> {
fn flush(&self) -> LinuxResult {
Ok(())
}

fn stat(&self) -> LinuxResult<RuxStat> {
let st_mode = 0o600u32; // rw-------
Ok(ctypes::stat {
Ok(RuxStat::from(ctypes::stat {
st_ino: 1,
st_nlink: 1,
st_mode,
..Default::default()
})
}))
}

fn into_any(self: Arc<Self>) -> alloc::sync::Arc<dyn core::any::Any + Send + Sync> {
Expand Down
Loading

0 comments on commit 63c82c7

Please sign in to comment.