diff --git a/src/lib.rs b/src/lib.rs index 94ae27b..e573e1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ pub use blocked_device::BlockedDevice; #[cfg(any(test, feature = "std"))] pub mod std_impl { - use std::fs::{File, OpenOptions}; + use std::fs::File; use std::io::{Read, Write, Seek, SeekFrom}; use super::Device; diff --git a/src/sfs.rs b/src/sfs.rs index 92c25be..f6060cc 100644 --- a/src/sfs.rs +++ b/src/sfs.rs @@ -6,7 +6,7 @@ use core::fmt::{Debug, Formatter, Error}; use core::any::Any; use dirty::Dirty; use structs::*; -use vfs::{self, Device, INode, FileSystem, FsError}; +use vfs::{self, Device, INode, FsError}; use util::*; use spin::{Mutex, RwLock}; @@ -134,7 +134,9 @@ impl INodeImpl { } /// Resize content size, no matter what type it is. fn _resize(&self, len: usize) -> vfs::Result<()> { - assert!(len <= MAX_FILE_SIZE, "file size exceed limit"); + if len > MAX_FILE_SIZE { + return Err(FsError::InvalidParam); + } let blocks = ((len + BLKSIZE - 1) / BLKSIZE) as u32; use core::cmp::{Ord, Ordering}; let old_blocks = self.disk_inode.read().blocks; @@ -249,13 +251,13 @@ impl INodeImpl { impl vfs::INode for INodeImpl { fn read_at(&self, offset: usize, buf: &mut [u8]) -> vfs::Result { - if(self.disk_inode.read().type_!=FileType::File){ + if self.disk_inode.read().type_!=FileType::File { return Err(FsError::NotFile); } self._read_at(offset, buf) } fn write_at(&self, offset: usize, buf: &[u8]) -> vfs::Result { - if(self.disk_inode.read().type_!=FileType::File){ + if self.disk_inode.read().type_!=FileType::File { return Err(FsError::NotFile); } self._write_at(offset, buf) @@ -284,20 +286,20 @@ impl vfs::INode for INodeImpl { Ok(()) } fn resize(&self, len: usize) -> vfs::Result<()> { - if(self.disk_inode.read().type_!=FileType::File){ + if self.disk_inode.read().type_!=FileType::File { return Err(FsError::NotFile); } self._resize(len) } fn create(&self, name: &str, type_: vfs::FileType) -> vfs::Result> { let info = self.info()?; - if(info.type_!=vfs::FileType::Dir){ + if info.type_!=vfs::FileType::Dir { return Err(FsError::NotDir); } assert!(info.nlinks > 0); // Ensure the name is not exist - if(!self.get_file_inode_id(name).is_none()){ + if !self.get_file_inode_id(name).is_none() { return Err(FsError::EntryExist); } @@ -325,11 +327,15 @@ impl vfs::INode for INodeImpl { } fn unlink(&self, name: &str) -> vfs::Result<()> { let info = self.info()?; - if(info.type_!=vfs::FileType::Dir){ + if info.type_!=vfs::FileType::Dir { return Err(FsError::NotDir) } - assert!(name != "."); - assert!(name != ".."); + if name != "." { + return Err(FsError::IsDir) + } + if name != ".." { + return Err(FsError::IsDir) + } let (inode_id, entry_id) = self.get_file_inode_and_entry_id(name).ok_or(FsError::EntryNotFound)?; let inode = self.fs.get_inode(inode_id); @@ -350,18 +356,18 @@ impl vfs::INode for INodeImpl { } fn link(&self, name: &str, other: &Arc) -> vfs::Result<()> { let info = self.info()?; - if(info.type_!=vfs::FileType::Dir){ + if info.type_!=vfs::FileType::Dir { return Err(FsError::NotDir) } assert!(info.nlinks > 0); - if(!self.get_file_inode_id(name).is_none()){ + if !self.get_file_inode_id(name).is_none() { return Err(FsError::EntryExist); } let child = other.downcast_ref::().ok_or(FsError::NotSameFs)?; - if(!Arc::ptr_eq(&self.fs, &child.fs)){ + if !Arc::ptr_eq(&self.fs, &child.fs) { return Err(FsError::NotSameFs); } - if(child.info()?.type_ == vfs::FileType::Dir){ + if child.info()?.type_ == vfs::FileType::Dir { return Err(FsError::IsDir); } let entry = DiskEntry { @@ -376,12 +382,12 @@ impl vfs::INode for INodeImpl { } fn rename(&self, old_name: &str, new_name: &str) -> vfs::Result<()> { let info = self.info()?; - if(info.type_!=vfs::FileType::Dir){ + if info.type_!=vfs::FileType::Dir { return Err(FsError::NotDir) } assert!(info.nlinks > 0); - if(!self.get_file_inode_id(new_name).is_none()){ + if !self.get_file_inode_id(new_name).is_none() { return Err(FsError::EntryExist); } @@ -398,20 +404,20 @@ impl vfs::INode for INodeImpl { } fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> vfs::Result<()> { let info = self.info()?; - if(info.type_!=vfs::FileType::Dir){ + if info.type_!=vfs::FileType::Dir { return Err(FsError::NotDir) } assert!(info.nlinks > 0); let dest = target.downcast_ref::().ok_or(FsError::NotSameFs)?; - if(!Arc::ptr_eq(&self.fs, &dest.fs)){ + if !Arc::ptr_eq(&self.fs, &dest.fs) { return Err(FsError::NotSameFs); } - if(dest.info()?.type_ != vfs::FileType::Dir){ + if dest.info()?.type_ != vfs::FileType::Dir { return Err(FsError::NotDir) } assert!(dest.info()?.nlinks > 0); - if(!self.get_file_inode_id(new_name).is_none()){ + if !self.get_file_inode_id(new_name).is_none() { return Err(FsError::EntryExist); } @@ -437,14 +443,14 @@ impl vfs::INode for INodeImpl { } fn find(&self, name: &str) -> vfs::Result> { let info = self.info()?; - if(info.type_!=vfs::FileType::Dir){ + if info.type_!=vfs::FileType::Dir { return Err(FsError::NotDir) } let inode_id = self.get_file_inode_id(name).ok_or(FsError::EntryNotFound)?; Ok(self.fs.get_inode(inode_id)) } fn get_entry(&self, id: usize) -> vfs::Result { - if(self.disk_inode.read().type_!=FileType::Dir){ + if self.disk_inode.read().type_!=FileType::Dir { return Err(FsError::NotDir) } assert!(id < self.disk_inode.read().blocks as usize); @@ -635,6 +641,7 @@ impl vfs::FileSystem for SimpleFileSystem { self.device.lock().write_at(BLKSIZE * BLKN_FREEMAP, free_map.as_buf()).unwrap(); free_map.sync(); } + self.flush_weak_inodes(); for inode in self.inodes.read().values() { if let Some(inode) = inode.upgrade() { inode.sync()?; diff --git a/src/tests.rs b/src/tests.rs index 682fa3a..6697ed3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,12 +1,10 @@ -use std::fs::{self, File, OpenOptions}; -use std::io::{Read, Write, Seek, SeekFrom}; +use std::fs::{self, OpenOptions}; use std::boxed::Box; use std::sync::Arc; use std::mem::uninitialized; use super::sfs::*; use super::vfs::*; -use super::vfs::INode; -use super::structs::{DiskEntry, AsBuf}; +use super::structs::AsBuf; fn _open_sample_file() -> Arc { fs::copy("sfs.img", "test.img").expect("failed to open sfs.img"); @@ -36,16 +34,17 @@ fn create_new_sfs() { } // #[test] -fn print_root() { +fn print_root() -> Result<()> { let sfs = _open_sample_file(); let root = sfs.root_inode(); println!("{:?}", root); - let files = root.list().unwrap(); + let files = root.list()?; println!("{:?}", files); - assert_eq!(files[3], root.get_entry(3).unwrap()); + assert_eq!(files[3], root.get_entry(3)?); - sfs.sync().unwrap(); + sfs.sync()?; + Ok(()) } #[test] @@ -148,7 +147,7 @@ fn kernel_image_file_create() -> Result<()> { let sfs = _open_sample_file(); let root = sfs.root_inode(); let files_count_before = root.list()?.len(); - root.create("hello2", FileType::File); + root.create("hello2", FileType::File)?; let files_count_after = root.list()?.len(); assert_eq!(files_count_before + 1, files_count_after); assert!(root.lookup("hello2").is_ok()); @@ -176,7 +175,7 @@ fn kernel_image_file_unlink() -> Result<()> { fn kernel_image_file_rename() -> Result<()> { let sfs = _open_sample_file(); let root = sfs.root_inode(); - let files_count_before = root.list().unwrap().len(); + let files_count_before = root.list()?.len(); root.rename("hello", "hello2")?; let files_count_after = root.list()?.len(); assert_eq!(files_count_before, files_count_after); @@ -213,7 +212,7 @@ fn hard_link() -> Result<()> { let file1 = root.create("file1", FileType::File)?; root.link("file2", &file1)?; let file2 = root.lookup("file2")?; - file1.resize(100); + file1.resize(100)?; assert_eq!(file2.info()?.size, 100); sfs.sync()?; diff --git a/src/vfs.rs b/src/vfs.rs index 9d3624a..3ebb849 100644 --- a/src/vfs.rs +++ b/src/vfs.rs @@ -113,8 +113,8 @@ pub enum FsError { EntryNotFound,//E_NOENT EntryExist,//E_EXIST NotSameFs,//E_XDEV + InvalidParam,//E_INVAL //and something else - } pub type Result = result::Result;