Skip to content

Commit 234171c

Browse files
committed
fs: allow to define custom filesystem in apps
1 parent e7f1054 commit 234171c

File tree

15 files changed

+172
-42
lines changed

15 files changed

+172
-42
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ members = [
88
"apps/fs/shell",
99
"apps/net/echoserver",
1010
"apps/net/httpclient",
11-
"apps/net/udpserver",
1211
"apps/net/httpserver",
12+
"apps/net/udpserver",
1313
"apps/task/parallel",
1414
"apps/task/sleep",
1515
"apps/task/yield",

apps/fs/shell/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ authors = ["Yuekai Jia <equation618@gmail.com>"]
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

9+
[features]
10+
use_ramfs = ["axfs/myfs", "dep:axfs_vfs", "dep:axfs_ramfs", "dep:crate_interface"]
11+
default = []
12+
913
[dependencies]
14+
axfs = { path = "../../../modules/axfs", optional = true }
15+
axfs_vfs = { path = "../../../crates/axfs_vfs", optional = true }
16+
axfs_ramfs = { path = "../../../crates/axfs_ramfs", optional = true }
17+
crate_interface = { path = "../../../crates/crate_interface", optional = true }
1018
libax = { path = "../../../ulib/libax", features = ["fs"] }

apps/fs/shell/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
mod cmd;
55

6+
#[cfg(feature = "use_ramfs")]
7+
mod ramfs;
8+
69
use libax::io::prelude::*;
710

811
#[macro_use]

apps/fs/shell/src/ramfs.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern crate alloc;
2+
3+
use alloc::sync::Arc;
4+
use axfs::fops::{Disk, MyFileSystemIf};
5+
use axfs_ramfs::RamFileSystem;
6+
use axfs_vfs::VfsOps;
7+
8+
struct MyFileSystemIfImpl;
9+
10+
#[crate_interface::impl_interface]
11+
impl MyFileSystemIf for MyFileSystemIfImpl {
12+
fn new_myfs(_disk: Disk) -> Arc<dyn VfsOps> {
13+
Arc::new(RamFileSystem::new())
14+
}
15+
}

modules/axfs/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use-virtio-blk = ["axdriver/virtio-blk"]
1616
devfs = ["dep:axfs_devfs"]
1717
ramfs = ["dep:axfs_ramfs"]
1818
fatfs = ["dep:fatfs"]
19+
myfs = ["dep:crate_interface"]
1920

2021
default = ["use-ramdisk", "devfs", "ramfs", "fatfs"]
2122

@@ -32,6 +33,7 @@ axfs_devfs = { path = "../../crates/axfs_devfs", optional = true }
3233
axfs_ramfs = { path = "../../crates/axfs_ramfs", optional = true }
3334
axdriver = { path = "../axdriver", optional = true }
3435
axsync = { path = "../axsync", default-features = false }
36+
crate_interface = { path = "../../crates/crate_interface", optional = true }
3537

3638
[dependencies.fatfs]
3739
git = "https://github.com/rafalh/rust-fatfs"

modules/axfs/src/fops.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ use axio::SeekFrom;
66
use capability::{Cap, WithCap};
77
use core::fmt;
88

9+
#[cfg(feature = "myfs")]
10+
pub use crate::dev::Disk;
11+
#[cfg(feature = "myfs")]
12+
pub use crate::fs::myfs::MyFileSystemIf;
13+
914
/// Alias of [`axfs_vfs::VfsNodeType`].
1015
pub type FileType = axfs_vfs::VfsNodeType;
1116
/// Alias of [`axfs_vfs::VfsDirEntry`].

modules/axfs/src/fs/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
#[cfg(feature = "fatfs")]
2-
pub mod fatfs;
1+
cfg_if::cfg_if! {
2+
if #[cfg(feature = "myfs")] {
3+
pub mod myfs;
4+
} else if #[cfg(feature = "fatfs")] {
5+
pub mod fatfs;
6+
}
7+
}
38

49
#[cfg(feature = "devfs")]
510
pub use axfs_devfs as devfs;

modules/axfs/src/fs/myfs.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::dev::Disk;
2+
use alloc::sync::Arc;
3+
use axfs_vfs::VfsOps;
4+
5+
/// The interface to define custom filesystems in user apps.
6+
#[crate_interface::def_interface]
7+
pub trait MyFileSystemIf {
8+
/// Creates a new instance of the filesystem with initialization.
9+
///
10+
/// TODO: use generic disk type
11+
fn new_myfs(disk: Disk) -> Arc<dyn VfsOps>;
12+
}
13+
14+
pub(crate) fn new_myfs(disk: Disk) -> Arc<dyn VfsOps> {
15+
crate_interface::call_interface!(MyFileSystemIf::new_myfs(disk))
16+
}

modules/axfs/src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@
55
//! # Cargo Features
66
//!
77
//! - `use-ramdisk`: Use [`driver_block::ramdisk::RamDisk`] as the block device.
8-
//! This feature is enabled by default.
8+
//! This feature is **enabled** by default.
99
//! - `use-virtio-blk`: Use [`axdriver::VirtIoBlockDev`] as the block device.
10-
//! This feature is disabled by default, but it will override `use-ramdisk`
10+
//! This feature is **disabled** by default, but it will override `use-ramdisk`
1111
//! if both are enabled.
1212
//! - `fatfs`: Use [FAT] as the main filesystem and mount it on `/`. This feature
13-
//! is enabled by default.
13+
//! is **enabled** by default.
1414
//! - `devfs`: Mount [`axfs_devfs::DeviceFileSystem`] on `/dev`. This feature is
15-
//! enabled by default.
15+
//! **enabled** by default.
1616
//! - `ramfs`: Mount [`axfs_ramfs::RamFileSystem`] on `/tmp`. This feature is
17-
//! enabled by default.
17+
//! **enabled** by default.
18+
//! - `myfs`: Allow users to define their custom filesystems to override the
19+
//! default. In this case, [`MyFileSystemIf`] is required to be implemented
20+
//! to create and initialize other filesystems. This feature is **disabled** by
21+
//! by default, but it will override
22+
//! other filesystem selection features if both are enabled.
1823
//!
1924
//! [FAT]: https://en.wikipedia.org/wiki/File_Allocation_Table
25+
//! [`MyFileSystemIf`]: fops::MyFileSystemIf
2026
2127
#![cfg_attr(all(not(test), not(doc)), no_std)]
28+
#![feature(doc_auto_cfg)]
2229

2330
#[macro_use]
2431
extern crate log;

0 commit comments

Comments
 (0)