mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2025-01-18 21:17:14 +04:00
Import easy-fs in os && change easy-fs to no_std mode.
This commit is contained in:
parent
ecccea65a0
commit
ea515323d3
@ -9,7 +9,6 @@ use std::fs::{File, OpenOptions, read_dir};
|
||||
use std::io::{Read, Write, Seek, SeekFrom};
|
||||
use std::sync::Mutex;
|
||||
use alloc::sync::Arc;
|
||||
use rand;
|
||||
|
||||
const BLOCK_SZ: usize = 512;
|
||||
|
||||
@ -17,7 +16,6 @@ struct BlockFile(Mutex<File>);
|
||||
|
||||
impl BlockDevice for BlockFile {
|
||||
fn read_block(&self, block_id: usize, buf: &mut [u8]) {
|
||||
println!("reading block {}", block_id);
|
||||
let mut file = self.0.lock().unwrap();
|
||||
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
|
||||
.expect("Error when seeking!");
|
||||
@ -25,7 +23,6 @@ impl BlockDevice for BlockFile {
|
||||
}
|
||||
|
||||
fn write_block(&self, block_id: usize, buf: &[u8]) {
|
||||
println!("writing block {}", block_id);
|
||||
let mut file = self.0.lock().unwrap();
|
||||
file.seek(SeekFrom::Start((block_id * BLOCK_SZ) as u64))
|
||||
.expect("Error when seeking!");
|
||||
@ -46,7 +43,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(format!("{}{}", TARGET_PATH, "fs.img"))?;
|
||||
f.set_len(8192 * 512);
|
||||
f.set_len(8192 * 512).unwrap();
|
||||
f
|
||||
})));
|
||||
// 4MiB, at most 4095 files
|
||||
@ -55,7 +52,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||
8192,
|
||||
1,
|
||||
);
|
||||
let mut root_inode = Arc::new(EasyFileSystem::root_inode(&efs));
|
||||
let root_inode = Arc::new(EasyFileSystem::root_inode(&efs));
|
||||
let apps: Vec<_> = read_dir("../user/src/bin")
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
@ -69,7 +66,7 @@ fn easy_fs_pack() -> std::io::Result<()> {
|
||||
// load app data from host file system
|
||||
let mut host_file = File::open(format!("{}{}", TARGET_PATH, app)).unwrap();
|
||||
let mut all_data: Vec<u8> = Vec::new();
|
||||
let app_size = host_file.read_to_end(&mut all_data).unwrap();
|
||||
host_file.read_to_end(&mut all_data).unwrap();
|
||||
// create a file in easy-fs
|
||||
let inode = root_inode.create(app.as_str()).unwrap();
|
||||
// write data to easy-fs
|
||||
@ -119,6 +116,7 @@ fn efs_test() -> std::io::Result<()> {
|
||||
0,
|
||||
);
|
||||
let mut str = String::new();
|
||||
use rand;
|
||||
// random digit
|
||||
for _ in 0..len {
|
||||
str.push(char::from('0' as u8 + rand::random::<u8>() % 10));
|
||||
|
@ -82,7 +82,6 @@ impl EasyFileSystem {
|
||||
let super_block_dirty: Dirty<SuperBlock> = Dirty::new(0, 0, block_device.clone());
|
||||
let super_block = super_block_dirty.get_ref();
|
||||
assert!(super_block.is_valid(), "Error loading EFS!");
|
||||
println!("{:?}", super_block);
|
||||
let inode_total_blocks =
|
||||
super_block.inode_bitmap_blocks + super_block.inode_area_blocks;
|
||||
let efs = Self {
|
||||
|
@ -122,7 +122,6 @@ impl DiskInode {
|
||||
new_blocks: Vec<u32>,
|
||||
block_device: &Arc<dyn BlockDevice>,
|
||||
) {
|
||||
println!("increase_size new_size={}, new_blocks={:?}", new_size, new_blocks);
|
||||
assert_eq!(new_blocks.len() as u32, self.blocks_num_needed(new_size));
|
||||
let last_blocks = self.blocks();
|
||||
self.size = new_size;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![no_std]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
mod block_dev;
|
||||
|
@ -41,13 +41,11 @@ impl Inode {
|
||||
name: &str,
|
||||
inode: &Dirty<DiskInode>,
|
||||
) -> Option<u32> {
|
||||
println!("into find_inode_id");
|
||||
// assert it is a directory
|
||||
assert!(inode.read(|inode| inode.is_dir()));
|
||||
let file_count = inode.read(|inode| {
|
||||
inode.size as usize
|
||||
}) / DIRENT_SZ;
|
||||
println!("file_count = {}", file_count);
|
||||
let mut dirent_space: DirentBytes = Default::default();
|
||||
for i in 0..file_count {
|
||||
assert_eq!(
|
||||
@ -94,7 +92,6 @@ impl Inode {
|
||||
let blocks_needed = inode.read(|inode| {
|
||||
inode.blocks_num_needed(new_size)
|
||||
});
|
||||
println!("blocks_num_needed = {}", blocks_needed);
|
||||
let mut v: Vec<u32> = Vec::new();
|
||||
for _ in 0..blocks_needed {
|
||||
v.push(fs.alloc_data());
|
||||
@ -106,7 +103,6 @@ impl Inode {
|
||||
|
||||
pub fn create(&self, name: &str) -> Option<Arc<Inode>> {
|
||||
let mut fs = self.fs.lock();
|
||||
println!("creating name {}", name);
|
||||
let mut inode = self.get_disk_inode(&mut fs);
|
||||
// assert it is a directory
|
||||
assert!(inode.read(|inode| inode.is_dir()));
|
||||
@ -114,13 +110,11 @@ impl Inode {
|
||||
if let Some(_) = self.find_inode_id(name, &inode) {
|
||||
return None;
|
||||
}
|
||||
println!("stop1");
|
||||
|
||||
// create a new file
|
||||
// alloc a inode with an indirect block
|
||||
let new_inode_id = fs.alloc_inode();
|
||||
let indirect1 = fs.alloc_data();
|
||||
println!("creating new file, new_inode_id={}, indirect={}", new_inode_id, indirect1);
|
||||
// initialize inode
|
||||
fs.get_disk_inode(new_inode_id).modify(|inode| {
|
||||
inode.initialize(
|
||||
@ -133,7 +127,6 @@ impl Inode {
|
||||
let file_count =
|
||||
inode.read(|inode| inode.size as usize) / DIRENT_SZ;
|
||||
let new_size = (file_count + 1) * DIRENT_SZ;
|
||||
println!("expected new_size={}", new_size);
|
||||
// increase size
|
||||
self.increase_size(new_size as u32, &mut inode, &mut fs);
|
||||
// write dirent
|
||||
@ -155,13 +148,11 @@ impl Inode {
|
||||
}
|
||||
|
||||
pub fn ls(&self) -> Vec<String> {
|
||||
println!("into ls!");
|
||||
let mut fs = self.fs.lock();
|
||||
let inode = self.get_disk_inode(&mut fs);
|
||||
let file_count = inode.read(|inode| {
|
||||
(inode.size as usize) / DIRENT_SZ
|
||||
});
|
||||
println!("file_count = {}", file_count);
|
||||
let mut v: Vec<String> = Vec::new();
|
||||
for i in 0..file_count {
|
||||
let mut dirent_bytes: DirentBytes = Default::default();
|
||||
|
@ -17,6 +17,7 @@ virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers" }
|
||||
k210-pac = { git = "https://github.com/wyfcyx/k210-pac" }
|
||||
k210-hal = { git = "https://github.com/wyfcyx/k210-hal" }
|
||||
k210-soc = { git = "https://github.com/wyfcyx/k210-soc" }
|
||||
easy-fs = { path = "../easy-fs" }
|
||||
|
||||
[features]
|
||||
board_qemu = []
|
||||
|
Loading…
Reference in New Issue
Block a user