Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ members = [
"modules/axmm",
"modules/axdma",
"modules/axnet",
"modules/axns",
"modules/axruntime",
"modules/axsync",
"modules/axtask",
Expand Down Expand Up @@ -59,12 +58,13 @@ axhal = { path = "modules/axhal" }
axlog = { path = "modules/axlog" }
axmm = { path = "modules/axmm" }
axnet = { path = "modules/axnet" }
axns = { path = "modules/axns" }
axruntime = { path = "modules/axruntime" }
axsync = { path = "modules/axsync" }
axtask = { path = "modules/axtask" }
axdma = { path = "modules/axdma" }

axns = { git = "https://github.com/Starry-OS/axns.git", rev = "622a680" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we publish the axns to crates.io? Maybe this crate doesn't need to modified frequently?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's fine, but since it is an ArceOS related module, I was worried about the owner and permissions so I didn’t do it.


[profile.release]
lto = true

Expand Down
1 change: 0 additions & 1 deletion api/arceos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ flatten_objects = "0.2.3"
static_assertions = "1.1.0"
spin = { version = "0.9" }
lazy_static = { version = "1.5", features = ["spin_no_std"] }
ctor_bare = "0.2"

[build-dependencies]
bindgen = { version = "0.69" }
53 changes: 23 additions & 30 deletions api/arceos_posix_api/src/imp/fd_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::ffi::c_int;

use axerrno::{LinuxError, LinuxResult};
use axio::PollState;
use axns::{ResArc, def_resource};
use axns::def_resource;
use flatten_objects::FlattenObjects;
use spin::RwLock;

Expand All @@ -23,24 +23,26 @@ pub trait FileLike: Send + Sync {
}

def_resource! {
pub static FD_TABLE: ResArc<RwLock<FlattenObjects<Arc<dyn FileLike>, AX_FILE_LIMIT>>> = ResArc::new();
}

impl FD_TABLE {
/// Return a copy of the inner table.
pub fn copy_inner(&self) -> RwLock<FlattenObjects<Arc<dyn FileLike>, AX_FILE_LIMIT>> {
let table = self.read();
let mut new_table = FlattenObjects::new();
for id in table.ids() {
let _ = new_table.add_at(id, table.get(id).unwrap().clone());
}
RwLock::new(new_table)
}
/// File descriptor table.
pub static FD_TABLE: RwLock<FlattenObjects<Arc<dyn FileLike>, AX_FILE_LIMIT>> = RwLock::new({
let mut fd_table = FlattenObjects::new();
fd_table
.add_at(0, Arc::new(stdin()) as _)
.unwrap_or_else(|_| panic!()); // stdin
fd_table
.add_at(1, Arc::new(stdout()) as _)
.unwrap_or_else(|_| panic!()); // stdout
fd_table
.add_at(2, Arc::new(stdout()) as _)
.unwrap_or_else(|_| panic!()); // stderr
fd_table
});
}

/// Get a file by `fd`.
pub fn get_file_like(fd: c_int) -> LinuxResult<Arc<dyn FileLike>> {
FD_TABLE
.current()
.read()
.get(fd as usize)
.cloned()
Expand All @@ -49,12 +51,17 @@ pub fn get_file_like(fd: c_int) -> LinuxResult<Arc<dyn FileLike>> {

/// Add a file to the file descriptor table.
pub fn add_file_like(f: Arc<dyn FileLike>) -> LinuxResult<c_int> {
Ok(FD_TABLE.write().add(f).map_err(|_| LinuxError::EMFILE)? as c_int)
Ok(FD_TABLE
.current()
.write()
.add(f)
.map_err(|_| LinuxError::EMFILE)? as c_int)
}

/// Close a file by `fd`.
pub fn close_file_like(fd: c_int) -> LinuxResult {
let f = FD_TABLE
.current()
.write()
.remove(fd as usize)
.ok_or(LinuxError::EBADF)?;
Expand Down Expand Up @@ -103,6 +110,7 @@ pub fn sys_dup2(old_fd: c_int, new_fd: c_int) -> c_int {

let f = get_file_like(old_fd)?;
FD_TABLE
.current()
.write()
.add_at(new_fd as usize, f)
.map_err(|_| LinuxError::EMFILE)?;
Expand Down Expand Up @@ -137,18 +145,3 @@ pub fn sys_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int {
}
})
}

#[ctor_bare::register_ctor]
fn init_stdio() {
let mut fd_table = flatten_objects::FlattenObjects::new();
fd_table
.add_at(0, Arc::new(stdin()) as _)
.unwrap_or_else(|_| panic!()); // stdin
fd_table
.add_at(1, Arc::new(stdout()) as _)
.unwrap_or_else(|_| panic!()); // stdout
fd_table
.add_at(2, Arc::new(stdout()) as _)
.unwrap_or_else(|_| panic!()); // stderr
FD_TABLE.init_new(spin::RwLock::new(fd_table));
}
2 changes: 1 addition & 1 deletion modules/axfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod root;

pub mod api;
pub mod fops;
pub use root::{CURRENT_DIR, CURRENT_DIR_PATH};
pub use root::CURRENT_DIR;

use axdriver::{AxDeviceContainer, prelude::*};

Expand Down
47 changes: 17 additions & 30 deletions modules/axfs/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use alloc::{string::String, sync::Arc, vec::Vec};
use axerrno::{AxError, AxResult, ax_err};
use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType, VfsOps, VfsResult};
use axns::{ResArc, def_resource};
use axns::def_resource;
use axsync::Mutex;
use lazyinit::LazyInit;
use spin::RwLock;
Expand All @@ -16,25 +16,6 @@ use crate::{
mounts,
};

def_resource! {
pub static CURRENT_DIR_PATH: ResArc<Mutex<String>> = ResArc::new();
pub static CURRENT_DIR: ResArc<Mutex<VfsNodeRef>> = ResArc::new();
}

impl CURRENT_DIR_PATH {
/// Return a copy of the inner path.
pub fn copy_inner(&self) -> Mutex<String> {
Mutex::new(self.lock().clone())
}
}

impl CURRENT_DIR {
/// Return a copy of the CURRENT_DIR_NODE.
pub fn copy_inner(&self) -> Mutex<VfsNodeRef> {
Mutex::new(self.lock().clone())
}
}

struct MountPoint {
path: &'static str,
fs: Arc<dyn VfsOps>,
Expand Down Expand Up @@ -207,24 +188,32 @@ pub(crate) fn init_rootfs(disk: crate::dev::Disk) {

ROOT_DIR.init_once(Arc::new(root_dir));
info!("rootfs initialized");
CURRENT_DIR.init_new(Mutex::new(ROOT_DIR.clone()));
info!("test");
CURRENT_DIR_PATH.init_new(Mutex::new("/".into()));
}

def_resource! {
/// The current working directory node and path
pub static CURRENT_DIR: Mutex<(VfsNodeRef, String)> = Mutex::new((
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why combine CURRENT_DIR_PATH and CURRENT_DIR into one variable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should always be modified at the same time, and not doing so would be a semantic error. Using a single variable facilitates management and avoids errors when writing code.

// Note: we can write this since resources are lazily initialized,
// but this requires that no resource can be accessed before `init_rootfs` finished.
ROOT_DIR.clone(),
"/".into(),
));
}

fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef {
if path.starts_with('/') {
ROOT_DIR.clone()
} else {
dir.cloned().unwrap_or_else(|| CURRENT_DIR.lock().clone())
dir.cloned()
.unwrap_or_else(|| CURRENT_DIR.current().lock().0.clone())
}
}

pub(crate) fn absolute_path(path: &str) -> AxResult<String> {
if path.starts_with('/') {
Ok(axfs_vfs::path::canonicalize(path))
} else {
let path = CURRENT_DIR_PATH.lock().clone() + path;
let path = CURRENT_DIR.current().lock().1.clone() + path;
Ok(axfs_vfs::path::canonicalize(&path))
}
}
Expand Down Expand Up @@ -302,7 +291,7 @@ pub(crate) fn remove_dir(dir: Option<&VfsNodeRef>, path: &str) -> AxResult {
}

pub(crate) fn current_dir() -> AxResult<String> {
Ok(CURRENT_DIR_PATH.lock().clone())
Ok(CURRENT_DIR.current().lock().1.clone())
}

pub(crate) fn set_current_dir(path: &str) -> AxResult {
Expand All @@ -311,8 +300,7 @@ pub(crate) fn set_current_dir(path: &str) -> AxResult {
abs_path += "/";
}
if abs_path == "/" {
*CURRENT_DIR.lock() = ROOT_DIR.clone();
*CURRENT_DIR_PATH.lock() = "/".into();
*CURRENT_DIR.current().lock() = (ROOT_DIR.clone(), "/".into());
return Ok(());
}

Expand All @@ -323,8 +311,7 @@ pub(crate) fn set_current_dir(path: &str) -> AxResult {
} else if !attr.perm().owner_executable() {
ax_err!(PermissionDenied)
} else {
*CURRENT_DIR.lock() = node;
*CURRENT_DIR_PATH.lock() = abs_path;
*CURRENT_DIR.current().lock() = (node, abs_path);
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/linker.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ SECTIONS {
linkm2_SYSCALL : { *(linkm2_SYSCALL) }
linkme_POST_TRAP : { *(linkme_POST_TRAP) }
linkm2_POST_TRAP : { *(linkm2_POST_TRAP) }
axns_resource : { *(axns_resource) }
axns_resources : { *(axns_resources) }
}
INSERT AFTER .tbss;
24 changes: 0 additions & 24 deletions modules/axns/Cargo.toml

This file was deleted.

Loading
Loading