Skip to content

Commit ef708dc

Browse files
committed
test3:ramfs-rename try1
1 parent 6884c15 commit ef708dc

File tree

13 files changed

+142
-25
lines changed

13 files changed

+142
-25
lines changed

arceos/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ axfs_ramfs = { path = "./axfs_ramfs" }
9494

9595
[patch.crates-io]
9696
kernel_guard = { path = "../crates/kernel_guard"}
97+
axfs_ramfs = { path = "./axfs_ramfs" }
9798

9899
[profile.release]
99100
lto = true

arceos/api/arceos_api/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ pub mod fs {
192192
pub type MyFileSystemIf;
193193
}
194194

195-
define_api! {
196-
@cfg "fs";
195+
define_api! {//定义了api后 用户程序、开启了fs特性的上层crate都可以直接用函数
196+
@cfg "fs";//// 条件编译:只有在启用 "fs" 特性时才包含此API
197197

198198
/// Opens a file at the path relative to the current directory with the
199199
/// options specified by `opts`.

arceos/axfs_ramfs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[package]
1313
edition = "2021"
1414
name = "axfs_ramfs"
15-
version = "0.1.1"
15+
version = "0.1.2" #原来0.1.1
1616
authors = ["Yuekai Jia <equation618@gmail.com>"]
1717
build = false
1818
autobins = false

arceos/axfs_ramfs/src/dir.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::collections::BTreeMap;
22
use alloc::sync::{Arc, Weak};
33
use alloc::{string::String, vec::Vec};
4-
4+
use crate::alloc::string::ToString;
55
use axfs_vfs::{VfsDirEntry, VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType};
66
use axfs_vfs::{VfsError, VfsResult};
77
use spin::RwLock;
@@ -14,7 +14,9 @@ use crate::file::FileNode;
1414
pub struct DirNode {
1515
this: Weak<DirNode>,
1616
parent: RwLock<Weak<dyn VfsNodeOps>>,
17-
children: RwLock<BTreeMap<String, VfsNodeRef>>,
17+
children: RwLock<BTreeMap<String, VfsNodeRef>>,//目录项表
18+
// ↑文件名 ↑文件实体(相当于inode的抽象
19+
//BTreeMap数据结构相当于一个map 存键值对 但是将键值对插到平衡二叉树 内部是有序的并且迭代的时候按照键的顺序
1820
}
1921

2022
impl DirNode {
@@ -67,6 +69,27 @@ impl DirNode {
6769
children.remove(name);
6870
Ok(())
6971
}
72+
fn rename_in_same_dir(&self, old_name: &str, new_name: &str) -> VfsResult {
73+
log::error!("[dir] call rename_in_same_dir oldname:{},newname:{}",old_name,new_name);
74+
let mut children = self.children.write(); // Rwlock是读写锁 通过.write() 获取写锁
75+
//可以有多个读锁(共享) 或者有一个写锁(独占) 但不能同时有读锁和写锁
76+
// 现在可以安全地修改 children...
77+
log::error!("[dir] getting node");
78+
let node=children.get(old_name).ok_or(VfsError::NotFound)?.clone();//找到old_name对应inode
79+
//get是通过键查找值 返回option ok_or(E)将option<T>转化为Result<T,E>
80+
// ?操作符可以跟在option或者result后面 成功了就解包 失败了就提前返回
81+
// 检查目标是否已存在
82+
if children.contains_key(new_name) {
83+
return Err(VfsError::AlreadyExists);
84+
}
85+
// 关键操作:修改名称映射,不复制数据!
86+
children.remove(old_name);
87+
children.insert(new_name.to_string(),node);// 同一个 node 对象!
88+
89+
log::error!("[dir] complete rename in same dir!");
90+
// VfsResult的定义 等价于:type VfsResult<T> = Result<T, VfsError>;
91+
Ok(())
92+
}
7093
}
7194

7295
impl VfsNodeOps for DirNode {
@@ -164,11 +187,22 @@ impl VfsNodeOps for DirNode {
164187
self.remove_node(name)
165188
}
166189
}
167-
168-
axfs_vfs::impl_vfs_dir_default! {}
190+
191+
fn rename(&self,src_path:&str,dst_path:&str)->VfsResult{
192+
log::error!("[dir] call rename");
193+
//现在先实现只支持在同一目录下重命名
194+
let (src_name, src_rest) = split_path(src_path);
195+
let (dst_dir,dst_rest)=split_path(dst_path);
196+
let dst_rest=dst_rest.unwrap();
197+
//
198+
199+
self.rename_in_same_dir(src_name, dst_rest)
200+
}
201+
axfs_vfs::impl_vfs_dir_default! {}//提供了其他方法的默认实现
202+
//经过查cargo.lock 发现axfs_vfs是外部依赖
169203
}
170204

171-
fn split_path(path: &str) -> (&str, Option<&str>) {
205+
fn split_path(path: &str) -> (&str, Option<&str>) {//去掉开头的/ 再以中间的第一个/进行切分
172206
let trimmed_path = path.trim_start_matches('/');
173207
trimmed_path.find('/').map_or((trimmed_path, None), |n| {
174208
(&trimmed_path[..n], Some(&trimmed_path[n + 1..]))

arceos/axfs_ramfs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct RamFileSystem {
2828
impl RamFileSystem {
2929
/// Create a new instance.
3030
pub fn new() -> Self {
31+
log::error!("[lib.rs] call ramfs new()");
3132
Self {
3233
parent: Once::new(),
3334
root: DirNode::new(None),
@@ -51,6 +52,7 @@ impl VfsOps for RamFileSystem {
5152
}
5253

5354
fn root_dir(&self) -> VfsNodeRef {
55+
log::error!("[lib] call root_dir");
5456
self.root.clone()
5557
}
5658
}

arceos/exercises/alt_alloc/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ use alloc::vec::Vec;
1313
fn main() {
1414
println!("Running bump tests...");
1515

16+
#[cfg(feature = "axstd")]
17+
println!("axstd feature is ENABLED");
18+
19+
#[cfg(not(feature = "axstd"))]
20+
println!("axstd feature is DISABLED");
1621
const N: usize = 3_000_000;
1722
let mut v = Vec::with_capacity(N);
1823
for i in 0..N {

arceos/exercises/ramfs_rename/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ mod ramfs;
99

1010
use std::io::{self, prelude::*};
1111
use std::fs::{self, File};
12+
//self相当于模块本身 相当于:
13+
//use std::fs;
14+
//use std::fs::File;
1215

1316
fn create_file(fname: &str, text: &str) -> io::Result<()> {
1417
println!("Create '{}' and write [{}] ...", fname, text);
@@ -47,6 +50,13 @@ fn process() -> io::Result<()> {
4750

4851
#[cfg_attr(feature = "axstd", no_mangle)]
4952
fn main() {
53+
//测试axstd是否启用---start---
54+
#[cfg(feature = "axstd")]
55+
println!("axstd feature is ENABLED");
56+
57+
#[cfg(not(feature = "axstd"))]
58+
println!("axstd feature is DISABLED");
59+
//----end------
5060
if let Err(e) = process() {
5161
panic!("Error: {}", e);
5262
}

arceos/modules/axfs/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ fatfs = ["dep:fatfs"]
1818
myfs = ["dep:crate_interface"]
1919
use-ramdisk = []
2020

21-
default = ["devfs", "ramfs", "fatfs", "procfs", "sysfs"]
21+
# default = ["devfs", "ramfs", "fatfs", "procfs", "sysfs"]
22+
default = ["devfs", "ramfs", "procfs", "sysfs"] # 不使用fatfs
2223

2324
[dependencies]
2425
log = "0.4.21"

arceos/modules/axfs/src/api/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ pub fn remove_file(path: &str) -> io::Result<()> {
8585
///
8686
/// This only works then the new path is in the same mounted fs.
8787
pub fn rename(old: &str, new: &str) -> io::Result<()> {
88-
crate::root::rename(old, new)
88+
crate::root::rename(old, new)//crate代表本crate axfs
8989
}

arceos/modules/axfs/src/lib.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,26 @@ use axdriver::{prelude::*, AxDeviceContainer};
3838

3939
/// Initializes filesystems by block devices.
4040
pub fn init_filesystems(mut blk_devs: AxDeviceContainer<AxBlockDevice>) {
41+
// info!("Initialize filesystems...");
42+
43+
// let dev = blk_devs.take_one().expect("No block device found!");
44+
// info!(" use block device 0: {:?}", dev.device_name());
45+
// self::root::init_rootfs(self::dev::Disk::new(dev));
4146
info!("Initialize filesystems...");
4247

43-
let dev = blk_devs.take_one().expect("No block device found!");
44-
info!(" use block device 0: {:?}", dev.device_name());
45-
self::root::init_rootfs(self::dev::Disk::new(dev));
48+
#[cfg(feature = "fatfs")]
49+
{
50+
if let Some(dev) = blk_devs.take_one() {
51+
let disk = self::dev::Disk::new(dev);
52+
self::root::init_rootfs(Some(disk)); // 传递 Some(disk)
53+
} else {
54+
warn!("No block device for FATFS, but RAMFS will be used");
55+
self::root::init_rootfs(None); // 传递 None
56+
}
57+
}
58+
59+
#[cfg(not(feature = "fatfs"))]
60+
{
61+
self::root::init_rootfs(None); // 不需要磁盘
62+
}
4663
}

0 commit comments

Comments
 (0)