-
Notifications
You must be signed in to change notification settings - Fork 10
feat(fs): add layered VFS framework with RAM and device filesystem implementations #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dayuxiaoshui
wants to merge
7
commits into
arceos-org:main
Choose a base branch
from
Dayuxiaoshui:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3293e15
feat(ci): upgrade rust toolchain to nightly-2025-05-20
89a358e
test: Add comprehensive integration tests for axfs_devfs
13f7e94
style(devfs): apply automated formatting
19ac932
test: Add comprehensive integration tests for axfs_ramfs and axfs_vfs
82f9b62
fix warning
bb18eb1
fix warnings
14707c0
complete the merge test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dayuxiaoshui marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try to improve upon existing tests to minimize code changes.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, trying it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // tests/test_axfs_dexfs.rs | ||
|
|
||
| // 1. Import the crate itself. | ||
|
|
||
| // 2. Import other dependencies required for testing. | ||
| use axfs_vfs::{VfsError, VfsNodeType, VfsOps}; | ||
| use std::sync::Arc; | ||
|
|
||
| // Test Scene 1: Basic filesystem construction and operations on device nodes in the root directory. | ||
| #[test] | ||
| fn test_root_device_operations() { | ||
| // === Setup Phase === | ||
| let devfs = axfs_devfs::DeviceFileSystem::new(); | ||
| devfs.add("null", Arc::new(axfs_devfs::NullDev)); | ||
| devfs.add("zero", Arc::new(axfs_devfs::ZeroDev)); | ||
|
|
||
| let root = devfs.root_dir(); | ||
|
|
||
| // === Test Phase === | ||
|
|
||
| // Verify root directory attributes. | ||
| assert!(root.get_attr().unwrap().is_dir()); | ||
| assert_eq!(root.get_attr().unwrap().file_type(), VfsNodeType::Dir); | ||
|
|
||
| // Verify lookup of a non-existent node. | ||
| // Use clone() to move a copy, not root itself. | ||
| assert_eq!( | ||
| root.clone().lookup("non_existent_file").err(), | ||
| Some(VfsError::NotFound) | ||
| ); | ||
|
|
||
| // --- Test /null device --- | ||
| // Clone again. | ||
| let null_dev = root.clone().lookup("null").expect("Failed to lookup /null"); | ||
| assert_eq!( | ||
| null_dev.get_attr().unwrap().file_type(), | ||
| VfsNodeType::CharDevice | ||
| ); | ||
|
|
||
| let mut buffer = [42u8; 16]; | ||
| assert_eq!(null_dev.read_at(0, &mut buffer).unwrap(), 0); | ||
| assert_eq!(buffer, [42u8; 16]); | ||
| assert_eq!(null_dev.write_at(0, &[1, 2, 3]).unwrap(), 3); | ||
|
|
||
| // --- Test /zero device --- | ||
| // This is the last use of root, so we can move it directly without cloning. | ||
| let zero_dev = root.lookup("zero").expect("Failed to lookup /zero"); | ||
| assert_eq!( | ||
| zero_dev.get_attr().unwrap().file_type(), | ||
| VfsNodeType::CharDevice | ||
| ); | ||
| assert_eq!(zero_dev.read_at(0, &mut buffer).unwrap(), buffer.len()); | ||
| assert_eq!(buffer, [0u8; 16]); | ||
| } | ||
|
|
||
| // Test Scene 2: Subdirectory creation, nested lookups, and path traversal. | ||
| #[test] | ||
| fn test_subdirectory_and_path_traversal() { | ||
| // === Setup Phase === | ||
| let devfs = axfs_devfs::DeviceFileSystem::new(); | ||
| devfs.add("null", Arc::new(axfs_devfs::NullDev)); | ||
| let dir_sub = devfs.mkdir("sub"); | ||
| dir_sub.add("zero", Arc::new(axfs_devfs::ZeroDev)); | ||
| let dir_sub_nested = dir_sub.mkdir("nested"); | ||
| dir_sub_nested.add("another_null", Arc::new(axfs_devfs::NullDev)); | ||
|
|
||
| let root = devfs.root_dir(); | ||
|
|
||
| // === Test Phase === | ||
|
|
||
| // --- Basic path lookup --- | ||
| let sub_dir = root.clone().lookup("sub").unwrap(); | ||
| assert!(sub_dir.get_attr().unwrap().is_dir()); | ||
|
|
||
| // Clone sub_dir to use it for lookup. | ||
| let zero_in_sub = sub_dir.clone().lookup("zero").unwrap(); | ||
| assert_eq!( | ||
| zero_in_sub.get_attr().unwrap().file_type(), | ||
| VfsNodeType::CharDevice | ||
| ); | ||
|
|
||
| // --- Test complex and redundant paths --- | ||
| let same_zero_in_sub = root.clone().lookup("sub/./zero").unwrap(); | ||
| assert!(Arc::ptr_eq(&zero_in_sub, &same_zero_in_sub)); | ||
|
|
||
| // --- Test '..' parent directory lookup --- | ||
| let sub_from_nested = root.clone().lookup("sub/nested/..").unwrap(); | ||
| assert!(Arc::ptr_eq(&sub_dir, &sub_from_nested)); | ||
|
|
||
| // --- Test deep lookup from the root directory --- | ||
| let deep_null = root.clone().lookup("sub/nested/another_null").unwrap(); | ||
| assert_eq!( | ||
| deep_null.get_attr().unwrap().file_type(), | ||
| VfsNodeType::CharDevice | ||
| ); | ||
|
|
||
| // --- Verify parent node relationship --- | ||
| let nested_dir = root.clone().lookup("sub/nested").unwrap(); | ||
| let parent_of_nested = nested_dir | ||
| .parent() | ||
| .expect("nested dir should have a parent"); | ||
| assert!(Arc::ptr_eq(&parent_of_nested, &sub_dir)); | ||
| } | ||
|
|
||
| // Test Scene 3: Error handling. | ||
| #[test] | ||
| fn test_error_conditions() { | ||
| let devfs = axfs_devfs::DeviceFileSystem::new(); | ||
| devfs.add("null", Arc::new(axfs_devfs::NullDev)); | ||
| let root = devfs.root_dir(); | ||
|
|
||
| // Perform directory operations on a file node. | ||
| assert_eq!( | ||
| root.clone().lookup("null/some_file").err(), | ||
| Some(VfsError::NotADirectory) | ||
| ); | ||
|
|
||
| // Perform file operations on a directory node. | ||
| assert_eq!( | ||
| root.read_at(0, &mut [0; 1]).err(), | ||
| Some(VfsError::IsADirectory) | ||
| ); | ||
| assert_eq!( | ||
| root.write_at(0, &[0; 1]).err(), | ||
| Some(VfsError::IsADirectory) | ||
| ); | ||
|
|
||
| // devfs does not support runtime creation/deletion; verify it returns a permission error. | ||
| assert_eq!( | ||
| root.clone().create("new_file", VfsNodeType::File).err(), | ||
| Some(VfsError::PermissionDenied) | ||
| ); | ||
| // This is the last use of root, so we can move it directly without cloning. | ||
| assert_eq!(root.remove("null").err(), Some(VfsError::PermissionDenied)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dayuxiaoshui marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| // tests/test_axfs_ramfs.rs | ||
|
|
||
| use axfs_ramfs::*; | ||
| use axfs_vfs::{VfsError, VfsNodeType, VfsOps}; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Helper function: Creates a standard filesystem structure for multiple tests. | ||
| fn setup_test_fs() -> RamFileSystem { | ||
| let ramfs = RamFileSystem::new(); | ||
| let root = ramfs.root_dir(); | ||
|
|
||
| // .clone() before calling methods that consume ownership. | ||
| root.clone().create("file.txt", VfsNodeType::File).unwrap(); | ||
| root.clone().create("dir1", VfsNodeType::Dir).unwrap(); | ||
| let dir1 = root.clone().lookup("dir1").unwrap(); | ||
| dir1.clone().create("dir2", VfsNodeType::Dir).unwrap(); | ||
| let dir2 = dir1.clone().lookup("dir2").unwrap(); | ||
| dir2.create("nested_file.txt", VfsNodeType::File).unwrap(); | ||
| ramfs | ||
| } | ||
|
|
||
| // ================================================================= | ||
| // Test Case 1: Edge cases for path resolution and lookup. | ||
| // ================================================================= | ||
| #[test] | ||
| fn test_path_manipulation_and_lookup() { | ||
| println!("\n--- Test: Path Resolution and Lookup ---"); | ||
| let ramfs = setup_test_fs(); | ||
| let root = ramfs.root_dir(); | ||
|
|
||
| // Clone before each call to .lookup() or other methods that consume self. | ||
| let dir1 = root.clone().lookup("dir1").unwrap(); | ||
| let dir2 = root.clone().lookup("dir1/dir2").unwrap(); | ||
|
|
||
| // 1. Test with redundant slashes. | ||
| println!("Testing: Redundant slashes"); | ||
| let found_dir2 = root.clone().lookup("///dir1//dir2/").unwrap(); | ||
| assert!( | ||
| Arc::ptr_eq(&dir2, &found_dir2), | ||
| "Path resolution with extra slashes should be correct" | ||
| ); | ||
|
|
||
| // 2. Test with current directory indicator '.'. | ||
| println!("Testing: Current directory indicator '.'"); | ||
| let found_dir2_with_dots = root.clone().lookup("./dir1/./dir2").unwrap(); | ||
| assert!( | ||
| Arc::ptr_eq(&dir2, &found_dir2_with_dots), | ||
| "Path resolution with '.' should be correct" | ||
| ); | ||
|
|
||
| // 3. Test with parent directory indicator '..'. | ||
| println!("Testing: Parent directory indicator '..'"); | ||
| let found_dir1 = dir2.clone().lookup("..").unwrap(); | ||
| assert!( | ||
| Arc::ptr_eq(&dir1, &found_dir1), | ||
| "'..' should correctly return the parent directory" | ||
| ); | ||
|
|
||
| // 4. Test complex '..' combination paths. | ||
| let found_root = dir2.clone().lookup("../..").unwrap(); | ||
| assert!( | ||
| Arc::ptr_eq(&root, &found_root), | ||
| "'../..' should return the root directory" | ||
| ); | ||
|
|
||
| // 5. Test lookup from a file node (should fail). | ||
| println!("Testing: Lookup on a file node"); | ||
| let file = root.clone().lookup("file.txt").unwrap(); | ||
| assert_eq!( | ||
| file.lookup("any").err(), // 'file' is moved here, but it's okay as it's not used afterward. | ||
| Some(VfsError::NotADirectory), | ||
| "Performing lookup on a file should return NotADirectory" | ||
| ); | ||
|
|
||
| println!("--- Path resolution tests passed ---"); | ||
| } | ||
|
|
||
| // ================================================================= | ||
| // Test Case 2: Edge cases for file I/O. | ||
| // ================================================================= | ||
| #[test] | ||
| fn test_io_edge_cases() { | ||
| println!("\n--- Test: File I/O Edge Cases ---"); | ||
| let ramfs = RamFileSystem::new(); | ||
| let root = ramfs.root_dir(); | ||
| root.clone().create("data.dat", VfsNodeType::File).unwrap(); | ||
| let file = root.clone().lookup("data.dat").unwrap(); | ||
|
|
||
| let mut buf = [0u8; 32]; | ||
|
|
||
| // 1. Write past the end of the file. | ||
| println!("Testing: Writing past the end of the file"); | ||
| file.clone().write_at(10, b"world").unwrap(); | ||
| assert_eq!(file.get_attr().unwrap().size(), 15); | ||
|
|
||
| let bytes_read = file.clone().read_at(0, &mut buf).unwrap(); | ||
| assert_eq!(bytes_read, 15); | ||
| assert_eq!(&buf[..bytes_read], b"\0\0\0\0\0\0\0\0\0\0world"); | ||
|
|
||
| // 2. Read past the end of the file. | ||
| println!("Testing: Reading past the end of the file"); | ||
| let mut small_buf = [0u8; 8]; | ||
| let bytes_read = file.clone().read_at(10, &mut small_buf).unwrap(); | ||
| assert_eq!(bytes_read, 5); | ||
| assert_eq!(&small_buf[..bytes_read], b"world"); | ||
|
|
||
| // 3. Truncate file to make it larger. | ||
| println!("Testing: Truncating file to make it larger"); | ||
| file.clone().truncate(20).unwrap(); | ||
| assert_eq!(file.get_attr().unwrap().size(), 20); | ||
| let bytes_read = file.clone().read_at(0, &mut buf).unwrap(); | ||
| assert_eq!(bytes_read, 20); | ||
| assert_eq!(&buf[..15], b"\0\0\0\0\0\0\0\0\0\0world"); | ||
| assert_eq!(&buf[15..20], &[0, 0, 0, 0, 0]); | ||
|
|
||
| // 4. Truncate file to zero. | ||
| println!("Testing: Truncating file to zero"); | ||
| file.clone().truncate(0).unwrap(); | ||
| assert_eq!(file.get_attr().unwrap().size(), 0); | ||
| let bytes_read = file.read_at(0, &mut buf).unwrap(); // Last use, no clone needed. | ||
| assert_eq!( | ||
| bytes_read, 0, | ||
| "Reading from an empty file should return 0 bytes" | ||
| ); | ||
|
|
||
| println!("--- File I/O edge case tests passed ---"); | ||
| } | ||
|
|
||
| // ================================================================= | ||
| // Test Case 3: Verification of various error conditions. | ||
| // ================================================================= | ||
| #[test] | ||
| fn test_error_conditions() { | ||
| println!("\n--- Test: Error Conditions ---"); | ||
| let ramfs = setup_test_fs(); | ||
| let root = ramfs.root_dir(); | ||
| let dir1 = root.clone().lookup("dir1").unwrap(); | ||
| let file = root.clone().lookup("file.txt").unwrap(); | ||
|
|
||
| // 1. AlreadyExists error. | ||
| println!("Testing: AlreadyExists error"); | ||
| assert_eq!( | ||
| root.clone().create("file.txt", VfsNodeType::File).err(), | ||
| Some(VfsError::AlreadyExists) | ||
| ); | ||
| assert_eq!( | ||
| root.clone().create("dir1", VfsNodeType::Dir).err(), | ||
| Some(VfsError::AlreadyExists) | ||
| ); | ||
|
|
||
| // 2. DirectoryNotEmpty error. | ||
| println!("Testing: DirectoryNotEmpty error"); | ||
| assert_eq!( | ||
| root.clone().remove("dir1").err(), | ||
| Some(VfsError::DirectoryNotEmpty) | ||
| ); | ||
|
|
||
| // 3. NotADirectory error. | ||
| println!("Testing: NotADirectory error"); | ||
| assert_eq!( | ||
| file.clone().create("anything", VfsNodeType::File).err(), | ||
| Some(VfsError::NotADirectory) | ||
| ); | ||
|
|
||
| // 4. IsADirectory error. | ||
| println!("Testing: IsADirectory error"); | ||
| let mut buf = [0u8; 1]; | ||
| assert_eq!( | ||
| dir1.clone().read_at(0, &mut buf).err(), | ||
| Some(VfsError::IsADirectory) | ||
| ); | ||
| assert_eq!( | ||
| dir1.write_at(0, &buf).err(), // Last use of dir1, no clone needed. | ||
| Some(VfsError::IsADirectory) | ||
| ); | ||
|
|
||
| // 5. InvalidInput error. | ||
| println!("Testing: InvalidInput error"); | ||
| assert_eq!(root.clone().remove(".").err(), Some(VfsError::InvalidInput)); | ||
| assert_eq!( | ||
| root.clone().remove("..").err(), | ||
| Some(VfsError::InvalidInput) | ||
| ); | ||
|
|
||
| // 6. NotFound error. | ||
| println!("Testing: NotFound error"); | ||
| assert_eq!( | ||
| root.clone().lookup("no-such-file").err(), | ||
| Some(VfsError::NotFound) | ||
| ); | ||
| assert_eq!(root.remove("no-such-file").err(), Some(VfsError::NotFound)); // Last use of root. | ||
|
|
||
| println!("--- Error condition tests passed ---"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.