Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
e4f67d1
fix: getdents
Oct 10, 2024
5baf6cb
test: add test harness
Oct 10, 2024
e01d516
fix: openat directory flag
Oct 16, 2024
8a18da3
fix: open with absolute path
Oct 16, 2024
d99b6c9
refactor: define AbsPath and RelPath type
Oct 16, 2024
ec5095e
refactor: devfs use new path defs
Oct 16, 2024
86a14b3
refactor: ramfs use new path defs
Oct 24, 2024
fc064b8
fix: ruxfs api and posix api
Oct 28, 2024
90128f8
refactor: implement simple ruxfs operations
Oct 29, 2024
f90c2ea
refacotr: fs posix api
Oct 31, 2024
07e55ce
fix: root directory lookup path
Oct 31, 2024
ff00a8b
feat: rmdir mkdirat unlink unlinkat api
Oct 31, 2024
8265722
fix: unlinkat with removedir
Nov 6, 2024
50977bb
test: use specific test dir
Nov 7, 2024
f5d3a63
feat: support fatfs and ext4 fs
Nov 14, 2024
d51a242
feat: make ext4 fs scripts
Nov 14, 2024
ed5d31c
fix: path canonicalization
Nov 18, 2024
840dbdd
feat: vfs add link and setattr fn
Nov 18, 2024
6acc1c3
fix: ramfs and devfs support new vfs trait
Nov 18, 2024
c9c8ee6
refactor: ruxfs and posix_api compats with new vfs
Nov 21, 2024
c392acc
feat: add inode number in vfsattr
Nov 21, 2024
8af1c3e
refactor: remove fops with relative path
Nov 25, 2024
e3e07e3
fix: path lifetime
Nov 27, 2024
abf3a6c
refactor: filelike path method
Nov 27, 2024
0e806a0
refactor: fs posix api with absolute path
Nov 27, 2024
0b37197
fix: FileLike stat
Nov 28, 2024
8cf6918
feat: directory check empty
Nov 28, 2024
2bef77f
fix: unlinkat
Nov 28, 2024
41879f9
Update ruxfs dependencies
Nov 28, 2024
0e58f4e
fix: blkfs features
Nov 28, 2024
52247a2
feat: ruxfs high-level api
Nov 28, 2024
b80d765
feat: fs arceos api
Nov 28, 2024
c9c0963
feat: axstd fs api
Nov 28, 2024
a98c3e7
fix: devfs and ramfs lookup
Nov 28, 2024
454bb3d
doc: axfs_vfs and ruxfs doc comments
Nov 28, 2024
01e9eee
fix: remove harness from workspace
Nov 28, 2024
4a99179
refactor: remove test harness
Feb 15, 2025
18bc4a9
fix: incompatible interfaces
Feb 19, 2025
3e55f9e
style: ruxos_posix_api fs syscalls
Feb 20, 2025
c99c128
fix: make fs img
Feb 26, 2025
e8d90f6
fix: ruxruntime fs test
Feb 26, 2025
aa5895a
fix: unit test
Feb 27, 2025
790e423
fix: clippy
Feb 27, 2025
ee46070
fix: aarch64 & riscv64 openat
Feb 27, 2025
8c699bc
fix: build fs shell
Feb 27, 2025
9c9b775
fix: build iperf & redis
Feb 27, 2025
e3fdd09
fix: log dep version
Feb 27, 2025
e14b5fb
fix: temporarily remove lwext4_rust
Mar 4, 2025
287ed72
fix: update another_ext4 dep
Mar 4, 2025
c20a832
style: cargo format all
Mar 4, 2025
0f85878
fix: docs
Mar 4, 2025
f883f12
Merge branch 'dev'
Mar 4, 2025
b18325e
fix: getdents
Mar 5, 2025
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
Prev Previous commit
Next Next commit
refactor: implement simple ruxfs operations
  • Loading branch information
liujingx committed Feb 19, 2025
commit 90128f83013273a467c46603d12ad624e7ff7676
23 changes: 17 additions & 6 deletions modules/ruxfs/src/api/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
*/

use alloc::string::String;
use axfs_vfs::path::AbsPath;
use axerrno::ax_err;
use axfs_vfs::{path::AbsPath, VfsError};
use axio::Result;
use core::fmt;

use super::FileType;
use super::{FileType, OpenOptions};
use crate::fops;

/// Iterator over the entries in a directory.
Expand Down Expand Up @@ -40,9 +41,9 @@ pub struct DirBuilder {

impl<'a> ReadDir<'a> {
pub(super) fn new(path: &'a AbsPath<'a>) -> Result<Self> {
let mut opts = fops::OpenOptions::new();
let mut opts = OpenOptions::new();
opts.read(true);
let inner = crate::root::open_dir(path, &opts)?;
let inner = fops::open_dir(path, (&opts).into())?;
const EMPTY: fops::DirEntry = fops::DirEntry::default();
let dirent_buf = [EMPTY; 31];
Ok(ReadDir {
Expand Down Expand Up @@ -147,13 +148,23 @@ impl DirBuilder {
if self.recursive {
self.create_dir_all(path)
} else {
crate::root::create_dir(path)
match fops::lookup(path) {
Ok(_) => return ax_err!(AlreadyExists),
Err(VfsError::NotFound) => {}
Err(e) => return ax_err!(e),
}
fops::create_dir(path)
}
}

/// Recursively create a directory and all of its parent components if they
/// are missing.
pub fn create_dir_all(&self, path: &AbsPath) -> Result<()> {
crate::root::create_dir_all(path)
match fops::lookup(path) {
Ok(_) => return ax_err!(AlreadyExists),
Err(VfsError::NotFound) => {}
Err(e) => return ax_err!(e),
}
fops::create_dir_all(path)
}
}
148 changes: 132 additions & 16 deletions modules/ruxfs/src/api/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
* See the Mulan PSL v2 for more details.
*/

use axfs_vfs::path::AbsPath;
use crate::fops;
use axerrno::ax_err;
use axfs_vfs::{
path::{AbsPath, RelPath},
VfsError,
};
use axio::{prelude::*, Result, SeekFrom};
use capability::Cap;
use core::fmt;
use crate::fops::{self, Directory};

/// A structure representing a type of file with accessors for each file type.
/// It is returned by [`Metadata::file_type`] method.
Expand All @@ -28,59 +33,170 @@ pub struct File {
pub struct Metadata(fops::FileAttr);

/// Options and flags which can be used to configure how a file is opened.
#[derive(Clone, Debug)]
pub struct OpenOptions(fops::OpenOptions);
#[derive(Clone)]
pub struct OpenOptions {
// generic
read: bool,
write: bool,
append: bool,
truncate: bool,
create: bool,
create_new: bool,
// system-specific
_custom_flags: i32,
_mode: u32,
}

impl OpenOptions {
/// Creates a blank new set of options ready for configuration.
pub const fn new() -> Self {
OpenOptions(fops::OpenOptions::new())
Self {
// generic
read: false,
write: false,
append: false,
truncate: false,
create: false,
create_new: false,
// system-specific
_custom_flags: 0,
_mode: 0o666,
}
}

/// Sets the option for read access.
pub fn read(&mut self, read: bool) -> &mut Self {
self.0.read(read);
self.read = read;
self
}

/// Sets the option for write access.
pub fn write(&mut self, write: bool) -> &mut Self {
self.0.write(write);
self.write = write;
self
}

/// Sets the option for the append mode.
pub fn append(&mut self, append: bool) -> &mut Self {
self.0.append(append);
self.append = append;
self
}

/// Sets the option for truncating a previous file.
pub fn truncate(&mut self, truncate: bool) -> &mut Self {
self.0.truncate(truncate);
self.truncate = truncate;
self
}

/// Sets the option to create a new file, or open it if it already exists.
pub fn create(&mut self, create: bool) -> &mut Self {
self.0.create(create);
self.create = create;
self
}

/// Sets the option to create a new file, failing if it already exists.
pub fn create_new(&mut self, create_new: bool) -> &mut Self {
self.0.create_new(create_new);
self.create_new = create_new;
self
}

/// Check if the options are valid.
pub const fn is_valid(&self) -> bool {
if !self.read && !self.write && !self.append {
return false;
}
match (self.write, self.append) {
(true, false) => {}
(false, false) => {
if self.truncate || self.create || self.create_new {
return false;
}
}
(_, true) => {
if self.truncate && !self.create_new {
return false;
}
}
}
true
}

/// Opens a file at `path` with the options specified by `self`.
pub fn open(&self, path: &AbsPath) -> Result<File> {
crate::root::open_file(&path, &self.0).map(|inner| File { inner })
pub fn open(&self, path: &str) -> Result<File> {
// Check options
if !self.is_valid() {
return ax_err!(InvalidInput);
}
// Find node
let path = if path.starts_with("/") {
AbsPath::new_canonicalized(path)
} else {
fops::concat_path(&RelPath::new_canonicalized(path))
};
// Check flag and attr
let node = match fops::lookup(&path) {
Ok(node) => {
if self.create_new {
return ax_err!(AlreadyExists);
}
node
}
Err(VfsError::NotFound) => {
if !self.create {
return ax_err!(NotFound);
}
fops::create_file(&path)?;
fops::lookup(&path)?
}
Err(e) => return Err(e),
};
if node.get_attr()?.is_dir() {
return ax_err!(IsADirectory);
}
// Truncate
if self.truncate {
node.truncate(0)?;
}
// Open
fops::open_file(&path, self.into(), self.append).map(|inner| File { inner })
}
}

impl From<&OpenOptions> for Cap {
fn from(opts: &OpenOptions) -> Cap {
let mut cap = Cap::empty();
if opts.read {
cap |= Cap::READ;
}
if opts.write | opts.append {
cap |= Cap::WRITE;
}
cap
}
}

/// Opens a directory at `path` with the options specified by `self`.
pub fn open_dir(&self, path: &AbsPath) -> Result<Directory> {
crate::root::open_dir(&path, &self.0)
impl fmt::Debug for OpenOptions {
#[allow(unused_assignments)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut written = false;
macro_rules! fmt_opt {
($field: ident, $label: literal) => {
if self.$field {
if written {
write!(f, " | ")?;
}
write!(f, $label)?;
written = true;
}
};
}
fmt_opt!(read, "READ");
fmt_opt!(write, "WRITE");
fmt_opt!(append, "APPEND");
fmt_opt!(truncate, "TRUNC");
fmt_opt!(create, "CREATE");
fmt_opt!(create_new, "CREATE_NEW");
Ok(())
}
}

Expand Down
36 changes: 31 additions & 5 deletions modules/ruxfs/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,26 @@ pub use self::dir::{DirBuilder, DirEntry, ReadDir};
pub use self::file::{File, FileType, Metadata, OpenOptions, Permissions};

use alloc::{string::String, vec::Vec};
use axerrno::ax_err;
use axfs_vfs::path::AbsPath;
use axfs_vfs::VfsError;
use axio::{self as io, prelude::*};

use crate::fops;

/// Returns an iterator over the entries within a directory.
pub fn read_dir<'a>(path: &'a AbsPath<'a>) -> io::Result<ReadDir<'a>> {
ReadDir::new(path)
}

/// Returns the current working directory as a [`AbsPath`].
pub fn current_dir() -> io::Result<AbsPath<'static>> {
crate::root::current_dir()
Ok(fops::current_dir())
}

/// Changes the current working directory to the specified path.
pub fn set_current_dir(path: AbsPath<'static>) -> io::Result<()> {
crate::root::set_current_dir(path)
fops::set_current_dir(path)
}

/// Read the entire contents of a file into a bytes vector.
Expand Down Expand Up @@ -76,18 +80,40 @@ pub fn create_dir_all(path: &AbsPath) -> io::Result<()> {

/// Removes an empty directory.
pub fn remove_dir(path: &AbsPath) -> io::Result<()> {
crate::root::remove_dir(path)
let node = fops::lookup(path)?;
let attr = node.get_attr()?;
if !attr.is_dir() {
return ax_err!(NotADirectory);
}
if !attr.perm().owner_writable() {
return ax_err!(PermissionDenied);
}
// TODO: check empty
fops::remove_dir(path)
}

/// Removes a file from the filesystem.
pub fn remove_file(path: &AbsPath) -> io::Result<()> {
crate::root::remove_file(path)
let node = fops::lookup(path)?;
let attr = node.get_attr()?;
if !attr.is_dir() {
return ax_err!(NotADirectory);
}
if !attr.perm().owner_writable() {
return ax_err!(PermissionDenied);
}
fops::remove_file(path)
}

/// Rename a file or directory to a new name.
/// Delete the original file if `old` already exists.
///
/// This only works then the new path is in the same mounted fs.
pub fn rename(old: &AbsPath, new: &AbsPath) -> io::Result<()> {
crate::root::rename(old, new)
let old_node = fops::lookup(old)?;
match fops::lookup(new) {
Ok(node) => return ax_err!(AlreadyExists),
Err(VfsError::NotFound) => fops::rename(old, new),
Err(e) => return ax_err!(e),
}
}
Loading