diff --git a/.gitignore b/.gitignore index d3f1b89..a573d69 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ **/*.rs.bk Cargo.lock .idea -/sfs.img +/*.img diff --git a/src/sfs.rs b/src/sfs.rs index 3e47564..5982280 100644 --- a/src/sfs.rs +++ b/src/sfs.rs @@ -144,7 +144,7 @@ impl INode { self.set_disk_block_id(i as usize, disk_block_id).unwrap(); } // clean up - let old_size = self.disk_inode.size as usize; + let old_size = self._size(); self.disk_inode.size = len as u32; self._clean_at(old_size, len).unwrap(); } @@ -166,17 +166,22 @@ impl INode { self.disk_inode.size = len as u32; Ok(()) } + /// Get the actual size of this inode, + /// since size in inode for dir is not real size + fn _size(&self) -> usize { + match self.disk_inode.type_ { + FileType::Dir => self.disk_inode.blocks as usize * BLKSIZE, + FileType::File => self.disk_inode.size as usize, + _ => unimplemented!(), + } + } /// Read/Write content, no matter what type it is fn _io_at(&self, begin: usize, end: usize, mut f: F) -> vfs::Result where F: FnMut(RefMut>, &BlockRange, usize) { let fs = self.fs.upgrade().unwrap(); - let size = match self.disk_inode.type_ { - FileType::Dir => self.disk_inode.blocks as usize * BLKSIZE, - FileType::File => self.disk_inode.size as usize, - _ => unimplemented!(), - }; + let size = self._size(); let iter = BlockIter { begin: size.min(begin), end: size.min(end), @@ -232,7 +237,7 @@ impl vfs::INode for INode { } fn info(&self) -> vfs::Result { Ok(vfs::FileInfo { - size: self.disk_inode.size as usize, + size: self._size(), mode: 0, type_: vfs::FileType::from(self.disk_inode.type_.clone()), blocks: self.disk_inode.blocks as usize, diff --git a/src/tests.rs b/src/tests.rs index ce19b5a..f09e89f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,3 +1,4 @@ +use std::fs; use std::fs::{File, OpenOptions}; use std::io::{Read, Write, Seek, SeekFrom}; use std::boxed::Box; @@ -27,8 +28,10 @@ impl Device for File { } fn _open_sample_file() -> Rc { - let file = File::open("sfs.img") - .expect("failed to open sfs.img"); + fs::copy("sfs.img","test.img").expect("failed to open sfs.img"); + let file = OpenOptions::new() + .read(true).write(true).open("test.img") + .expect("failed to open test.img"); SimpleFileSystem::open(Box::new(file)) .expect("failed to open SFS") } @@ -149,4 +152,16 @@ fn rc_layout() { let start = unsafe{ (ptr as *const usize).offset(-2) }; let ns = unsafe{ &*(start as *const [usize; 2]) }; assert_eq!(ns, &[1usize, 1]); +} + +// #[test] +fn kernel_image_file_create(){ + let sfs = _open_sample_file(); + let root = sfs.root_inode(); + let files_count_before = root.borrow().list().unwrap(); + root.borrow_mut().create("hello2",FileType::File).unwrap(); + let files_count_after = root.borrow().list().unwrap(); + assert_eq!(files_count_before+1, files_count_after) + + sfs.sync().unwrap(); } \ No newline at end of file