Merge pull request #2 from benpigchu/dir-size-fix

Fix file creation bug caused by invalid size of dir
master
Wang Runji 6 years ago committed by GitHub
commit e26f14c55f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
.gitignore vendored

@ -2,4 +2,4 @@
**/*.rs.bk **/*.rs.bk
Cargo.lock Cargo.lock
.idea .idea
/sfs.img /*.img

@ -144,7 +144,7 @@ impl INode {
self.set_disk_block_id(i as usize, disk_block_id).unwrap(); self.set_disk_block_id(i as usize, disk_block_id).unwrap();
} }
// clean up // clean up
let old_size = self.disk_inode.size as usize; let old_size = self._size();
self.disk_inode.size = len as u32; self.disk_inode.size = len as u32;
self._clean_at(old_size, len).unwrap(); self._clean_at(old_size, len).unwrap();
} }
@ -166,17 +166,22 @@ impl INode {
self.disk_inode.size = len as u32; self.disk_inode.size = len as u32;
Ok(()) 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 /// Read/Write content, no matter what type it is
fn _io_at<F>(&self, begin: usize, end: usize, mut f: F) -> vfs::Result<usize> fn _io_at<F>(&self, begin: usize, end: usize, mut f: F) -> vfs::Result<usize>
where F: FnMut(RefMut<Box<Device>>, &BlockRange, usize) where F: FnMut(RefMut<Box<Device>>, &BlockRange, usize)
{ {
let fs = self.fs.upgrade().unwrap(); let fs = self.fs.upgrade().unwrap();
let size = match self.disk_inode.type_ { let size = self._size();
FileType::Dir => self.disk_inode.blocks as usize * BLKSIZE,
FileType::File => self.disk_inode.size as usize,
_ => unimplemented!(),
};
let iter = BlockIter { let iter = BlockIter {
begin: size.min(begin), begin: size.min(begin),
end: size.min(end), end: size.min(end),
@ -232,7 +237,7 @@ impl vfs::INode for INode {
} }
fn info(&self) -> vfs::Result<vfs::FileInfo> { fn info(&self) -> vfs::Result<vfs::FileInfo> {
Ok(vfs::FileInfo { Ok(vfs::FileInfo {
size: self.disk_inode.size as usize, size: self._size(),
mode: 0, mode: 0,
type_: vfs::FileType::from(self.disk_inode.type_.clone()), type_: vfs::FileType::from(self.disk_inode.type_.clone()),
blocks: self.disk_inode.blocks as usize, blocks: self.disk_inode.blocks as usize,

@ -1,3 +1,4 @@
use std::fs;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::{Read, Write, Seek, SeekFrom}; use std::io::{Read, Write, Seek, SeekFrom};
use std::boxed::Box; use std::boxed::Box;
@ -27,8 +28,10 @@ impl Device for File {
} }
fn _open_sample_file() -> Rc<SimpleFileSystem> { fn _open_sample_file() -> Rc<SimpleFileSystem> {
let file = File::open("sfs.img") fs::copy("sfs.img","test.img").expect("failed to open sfs.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)) SimpleFileSystem::open(Box::new(file))
.expect("failed to open SFS") .expect("failed to open SFS")
} }
@ -149,4 +152,16 @@ fn rc_layout() {
let start = unsafe{ (ptr as *const usize).offset(-2) }; let start = unsafe{ (ptr as *const usize).offset(-2) };
let ns = unsafe{ &*(start as *const [usize; 2]) }; let ns = unsafe{ &*(start as *const [usize; 2]) };
assert_eq!(ns, &[1usize, 1]); 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();
} }
Loading…
Cancel
Save