diff --git a/src/sfs.rs b/src/sfs.rs index adb4055..6e9bed3 100644 --- a/src/sfs.rs +++ b/src/sfs.rs @@ -120,12 +120,12 @@ impl INode { use vfs::INode; // Insert entries: '.' '..' self.resize(BLKSIZE * 2).unwrap(); - self.write_at(0, DiskEntry { + self.write_at(BLKSIZE * 1, DiskEntry { id: parent as u32, name: Str256::from(".."), }.as_buf()).unwrap(); let id = self.id as u32; - self.write_at(0, DiskEntry { + self.write_at(BLKSIZE * 0, DiskEntry { id, name: Str256::from("."), }.as_buf()).unwrap(); @@ -246,7 +246,7 @@ impl vfs::INode for INode { self.disk_inode.size = len as u32; Ok(()) } - fn create(&mut self, name: &'static str) -> vfs::Result> { + fn create(&mut self, name: &'static str, type_: vfs::FileType) -> vfs::Result> { let fs = self.fs.upgrade().unwrap(); let info = self.info().unwrap(); assert_eq!(info.type_, vfs::FileType::Dir); @@ -256,7 +256,10 @@ impl vfs::INode for INode { assert!(self.get_file_inode_id(name).is_none(), "file name exist"); // Create new INode - let inode = fs.new_inode_file().unwrap(); + let inode = match type_ { + vfs::FileType::File => fs.new_inode_file().unwrap(), + vfs::FileType::Dir => fs.new_inode_dir(self.id).unwrap(), + }; // Write new entry let entry = DiskEntry { @@ -287,7 +290,7 @@ impl vfs::INode for INode { let type_ = inode.borrow().disk_inode.type_; match type_ { FileType::File => if rest_path == "" {Ok(inode)} else {Err(())}, - FileType::Dir => inode.borrow_mut().lookup(rest_path), + FileType::Dir => if rest_path == "" {Ok(inode)} else {inode.borrow().lookup(rest_path)}, _ => unimplemented!(), } } @@ -424,6 +427,7 @@ impl SimpleFileSystem { } fs } + /// Allocate a block, return block id fn alloc_block(&self) -> Option { let id = self.free_map.borrow_mut().alloc(); @@ -481,8 +485,6 @@ impl SimpleFileSystem { } impl vfs::FileSystem for SimpleFileSystem { - type INode = INode; - /// Write back super block if dirty fn sync(&self) -> vfs::Result<()> { { @@ -506,7 +508,7 @@ impl vfs::FileSystem for SimpleFileSystem { Ok(()) } - fn root_inode(&self) -> Ptr { + fn root_inode(&self) -> Ptr { self.get_inode(BLKN_ROOT) } } diff --git a/src/tests.rs b/src/tests.rs index 39fff5c..a4dcc26 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -35,7 +35,7 @@ fn _create_new_sfs() -> Rc { let file = OpenOptions::new() .read(true).write(true).create(true).open("test.img") .expect("failed to create file"); - SimpleFileSystem::create(Box::new(file), 16 * 4096) + SimpleFileSystem::create(Box::new(file), 32 * 4096) } #[test] @@ -67,7 +67,7 @@ fn print_root() { fn create_file() { let sfs = _create_new_sfs(); let root = sfs.root_inode(); - let file1 = root.borrow_mut().create("file1").unwrap(); + let file1 = root.borrow_mut().create("file1", FileType::File).unwrap(); assert_eq!(file1.borrow().info().unwrap(), FileInfo { size: 0, @@ -82,10 +82,21 @@ fn create_file() { fn lookup() { let sfs = _create_new_sfs(); let root = sfs.root_inode(); - let file1 = root.borrow_mut().create("file1").unwrap(); - let found = root.borrow().lookup("file1").expect("lookup not found"); - println!("{:?}", found.borrow()); - println!("{:?}", file1.borrow()); - assert!(Rc::ptr_eq(&found, &file1), "found wrong INode"); + + assert!(Rc::ptr_eq(&root.borrow().lookup(".").unwrap(), &root), "failed to find ."); + assert!(Rc::ptr_eq(&root.borrow().lookup("..").unwrap(), &root), "failed to find .."); + + let file1 = root.borrow_mut().create("file1", FileType::File) + .expect("failed to create file1"); + assert!(Rc::ptr_eq(&root.borrow().lookup("file1").unwrap(), &file1), "failed to find file1"); + assert!(root.borrow().lookup("file2").is_err(), "found non-existent file"); + + let dir1 = root.borrow_mut().create("dir1", FileType::Dir) + .expect("failed to create dir1"); + let file2 = dir1.borrow_mut().create("file2", FileType::File) + .expect("failed to create /dir1/file2"); + assert!(Rc::ptr_eq(&root.borrow().lookup("dir1/file2").unwrap(), &file2), "failed to find dir1/file1"); + assert!(Rc::ptr_eq(&dir1.borrow().lookup("..").unwrap(), &root), "failed to find .. from dir1"); + sfs.sync().unwrap(); } \ No newline at end of file diff --git a/src/vfs.rs b/src/vfs.rs index d5cf004..1305016 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -16,7 +16,7 @@ pub trait INode: Debug { // fn reclaim(&mut self) -> Result<()>; // fn try_seek(&mut self, offset: u64) -> Result<()>; fn resize(&mut self, len: usize) -> Result<()>; - fn create(&mut self, name: &'static str) -> Result>>; + fn create(&mut self, name: &'static str, type_: FileType) -> Result>>; fn lookup(&self, path: &'static str) -> Result>>; // fn io_ctrl(&mut self, op: u32, data: &[u8]) -> Result<()>; } @@ -37,9 +37,8 @@ pub type Result = core::result::Result; /// Abstract filesystem pub trait FileSystem { - type INode: INode; fn sync(&self) -> Result<()>; - fn root_inode(&self) -> Rc>; + fn root_inode(&self) -> Rc>; // fn unmount(&self) -> Result<()>; // fn cleanup(&self); } \ No newline at end of file