diff --git a/easy-fs/src/bin/main.rs b/easy-fs/src/bin/main.rs index 4a91928a..6eb7d0e7 100644 --- a/easy-fs/src/bin/main.rs +++ b/easy-fs/src/bin/main.rs @@ -5,7 +5,7 @@ use easy_fs::{ BlockDevice, EasyFileSystem, }; -use std::fs::{File, OpenOptions}; +use std::fs::{File, OpenOptions, read_dir}; use std::io::{Read, Write, Seek, SeekFrom}; use std::sync::Mutex; use alloc::sync::Arc; @@ -37,7 +37,48 @@ fn main() { easy_fs_pack().expect("Error when packing easy-fs!"); } +static TARGET_PATH: &str = "../user/target/riscv64gc-unknown-none-elf/release/"; + fn easy_fs_pack() -> std::io::Result<()> { + let block_file = Arc::new(BlockFile(Mutex::new({ + let f = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open(format!("{}{}", TARGET_PATH, "fs.img"))?; + f.set_len(8192 * 512); + f + }))); + // 4MiB, at most 4095 files + let efs = EasyFileSystem::create( + block_file.clone(), + 8192, + 1, + ); + let mut root_inode = Arc::new(EasyFileSystem::root_inode(&efs)); + let apps: Vec<_> = read_dir("../user/src/bin") + .unwrap() + .into_iter() + .map(|dir_entry| { + let mut name_with_ext = dir_entry.unwrap().file_name().into_string().unwrap(); + name_with_ext.drain(name_with_ext.find('.').unwrap()..name_with_ext.len()); + name_with_ext + }) + .collect(); + for app in apps { + // load app data from host file system + let mut host_file = File::open(format!("{}{}", TARGET_PATH, app)).unwrap(); + let mut all_data: Vec = Vec::new(); + let app_size = 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 + inode.write_at(0, all_data.as_slice()); + } + // list apps + for app in root_inode.ls() { + println!("{}", app); + } Ok(()) } diff --git a/easy-fs/src/layout.rs b/easy-fs/src/layout.rs index 99e4abc6..3723d0f7 100644 --- a/easy-fs/src/layout.rs +++ b/easy-fs/src/layout.rs @@ -8,7 +8,7 @@ use alloc::sync::Arc; use alloc::vec::Vec; const EFS_MAGIC: u32 = 0x3b800001; -const INODE_DIRECT_COUNT: usize = 12; +const INODE_DIRECT_COUNT: usize = 60; const NAME_LENGTH_LIMIT: usize = 27; #[repr(C)] diff --git a/easy-fs/src/vfs.rs b/easy-fs/src/vfs.rs index fe42adff..b373172d 100644 --- a/easy-fs/src/vfs.rs +++ b/easy-fs/src/vfs.rs @@ -104,7 +104,7 @@ impl Inode { }); } - pub fn create(&mut self, name: &str) -> Option> { + pub fn create(&self, name: &str) -> Option> { let mut fs = self.fs.lock(); println!("creating name {}", name); let mut inode = self.get_disk_inode(&mut fs);