remove warning+add FsError, pt3

master
Ben Pig Chu 6 years ago
parent 0a3f4218fe
commit c194e34582

@ -55,7 +55,7 @@ impl INodeImpl {
let disk_inode = self.disk_inode.read(); let disk_inode = self.disk_inode.read();
match file_block_id { match file_block_id {
id if id >= disk_inode.blocks as BlockId => id if id >= disk_inode.blocks as BlockId =>
panic!(), Err(FsError::InvalidParam),
id if id < NDIRECT => id if id < NDIRECT =>
Ok(disk_inode.direct[id] as BlockId), Ok(disk_inode.direct[id] as BlockId),
id if id < NDIRECT + BLK_NENTRY => { id if id < NDIRECT + BLK_NENTRY => {
@ -73,7 +73,7 @@ impl INodeImpl {
fn set_disk_block_id(&self, file_block_id: BlockId, disk_block_id: BlockId) -> vfs::Result<()> { fn set_disk_block_id(&self, file_block_id: BlockId, disk_block_id: BlockId) -> vfs::Result<()> {
match file_block_id { match file_block_id {
id if id >= self.disk_inode.read().blocks as BlockId => id if id >= self.disk_inode.read().blocks as BlockId =>
panic!(), Err(FsError::InvalidParam),
id if id < NDIRECT => { id if id < NDIRECT => {
self.disk_inode.write().direct[id] = disk_block_id as u32; self.disk_inode.write().direct[id] = disk_block_id as u32;
Ok(()) Ok(())
@ -112,11 +112,11 @@ impl INodeImpl {
self._write_at(BLKSIZE * 1, DiskEntry { self._write_at(BLKSIZE * 1, DiskEntry {
id: parent as u32, id: parent as u32,
name: Str256::from(".."), name: Str256::from(".."),
}.as_buf())?; }.as_buf()).unwrap();
self._write_at(BLKSIZE * 0, DiskEntry { self._write_at(BLKSIZE * 0, DiskEntry {
id: self.id as u32, id: self.id as u32,
name: Str256::from("."), name: Str256::from("."),
}.as_buf())?; }.as_buf()).unwrap();
Ok(()) Ok(())
} }
/// remove a page in middle of file and insert the last page here, useful for dirent remove /// remove a page in middle of file and insert the last page here, useful for dirent remove
@ -159,7 +159,7 @@ impl INodeImpl {
// clean up // clean up
let old_size = self._size(); let old_size = self._size();
self._set_size(len); self._set_size(len);
self._clean_at(old_size, len)?; self._clean_at(old_size, len).unwrap();
} }
Ordering::Less => { Ordering::Less => {
// free extra blocks // free extra blocks
@ -186,7 +186,7 @@ impl INodeImpl {
match disk_inode.type_ { match disk_inode.type_ {
FileType::Dir => disk_inode.blocks as usize * BLKSIZE, FileType::Dir => disk_inode.blocks as usize * BLKSIZE,
FileType::File => disk_inode.size as usize, FileType::File => disk_inode.size as usize,
_ => unimplemented!(), _ => panic!("Unknown file type"),
} }
} }
/// Set the ucore compat size of this inode, /// Set the ucore compat size of this inode,
@ -196,7 +196,7 @@ impl INodeImpl {
disk_inode.size = match disk_inode.type_ { disk_inode.size = match disk_inode.type_ {
FileType::Dir => disk_inode.blocks as usize * DIRENT_SIZE, FileType::Dir => disk_inode.blocks as usize * DIRENT_SIZE,
FileType::File => len, FileType::File => len,
_ => unimplemented!(), _ => panic!("Unknown file type"),
} as u32 } as u32
} }
/// Read/Write content, no matter what type it is /// Read/Write content, no matter what type it is
@ -234,9 +234,10 @@ impl INodeImpl {
/// Clean content, no matter what type it is /// Clean content, no matter what type it is
fn _clean_at(&self, begin: usize, end: usize) -> vfs::Result<()> { fn _clean_at(&self, begin: usize, end: usize) -> vfs::Result<()> {
static ZEROS: [u8; BLKSIZE] = [0; BLKSIZE]; static ZEROS: [u8; BLKSIZE] = [0; BLKSIZE];
self._io_at(begin, end, |device, range, _| { let size = self._io_at(begin, end, |device, range, _| {
device.write_block(range.block, range.begin, &ZEROS[..range.len()]) device.write_block(range.block, range.begin, &ZEROS[..range.len()])
})?; })?;
assert_eq!(size, end - begin);
Ok(()) Ok(())
} }
fn nlinks_inc(&self) { fn nlinks_inc(&self) {
@ -269,7 +270,7 @@ impl vfs::INode for INodeImpl {
size: match disk_inode.type_ { size: match disk_inode.type_ {
FileType::File => disk_inode.size as usize, FileType::File => disk_inode.size as usize,
FileType::Dir => disk_inode.blocks as usize, FileType::Dir => disk_inode.blocks as usize,
_ => unimplemented!(), _ => panic!("Unknown file type"),
}, },
mode: 0, mode: 0,
type_: vfs::FileType::from(disk_inode.type_.clone()), type_: vfs::FileType::from(disk_inode.type_.clone()),
@ -316,7 +317,7 @@ impl vfs::INode for INodeImpl {
}; };
let old_size = self._size(); let old_size = self._size();
self._resize(old_size + BLKSIZE)?; self._resize(old_size + BLKSIZE)?;
self._write_at(old_size, entry.as_buf())?; self._write_at(old_size, entry.as_buf()).unwrap();
inode.nlinks_inc(); inode.nlinks_inc();
if type_ == vfs::FileType::Dir { if type_ == vfs::FileType::Dir {
inode.nlinks_inc(); //for . inode.nlinks_inc(); //for .
@ -376,7 +377,7 @@ impl vfs::INode for INodeImpl {
}; };
let old_size = self._size(); let old_size = self._size();
self._resize(old_size + BLKSIZE)?; self._resize(old_size + BLKSIZE)?;
self._write_at(old_size, entry.as_buf())?; self._write_at(old_size, entry.as_buf()).unwrap();
child.nlinks_inc(); child.nlinks_inc();
Ok(()) Ok(())
} }
@ -396,9 +397,9 @@ impl vfs::INode for INodeImpl {
// in place modify name // in place modify name
let mut entry: DiskEntry = unsafe { uninitialized() }; let mut entry: DiskEntry = unsafe { uninitialized() };
let entry_pos = entry_id as usize * BLKSIZE; let entry_pos = entry_id as usize * BLKSIZE;
self._read_at(entry_pos, entry.as_buf_mut())?; self._read_at(entry_pos, entry.as_buf_mut()).unwrap();
entry.name = Str256::from(new_name); entry.name = Str256::from(new_name);
self._write_at(entry_pos, entry.as_buf())?; self._write_at(entry_pos, entry.as_buf()).unwrap();
Ok(()) Ok(())
} }
@ -430,7 +431,7 @@ impl vfs::INode for INodeImpl {
}; };
let old_size = dest._size(); let old_size = dest._size();
dest._resize(old_size + BLKSIZE)?; dest._resize(old_size + BLKSIZE)?;
dest._write_at(old_size, entry.as_buf())?; dest._write_at(old_size, entry.as_buf()).unwrap();
self.remove_dirent_page(entry_id)?; self.remove_dirent_page(entry_id)?;
@ -453,9 +454,11 @@ impl vfs::INode for INodeImpl {
if self.disk_inode.read().type_!=FileType::Dir { if self.disk_inode.read().type_!=FileType::Dir {
return Err(FsError::NotDir) return Err(FsError::NotDir)
} }
assert!(id < self.disk_inode.read().blocks as usize); if id >= self.disk_inode.read().blocks as usize {
return Err(FsError::EntryNotFound)
};
let mut entry: DiskEntry = unsafe { uninitialized() }; let mut entry: DiskEntry = unsafe { uninitialized() };
self._read_at(id as usize * BLKSIZE, entry.as_buf_mut())?; self._read_at(id as usize * BLKSIZE, entry.as_buf_mut()).unwrap();
Ok(String::from(entry.name.as_ref())) Ok(String::from(entry.name.as_ref()))
} }
fn fs(&self) -> Arc<vfs::FileSystem> { fn fs(&self) -> Arc<vfs::FileSystem> {
@ -563,9 +566,15 @@ impl SimpleFileSystem {
/// Allocate a block, return block id /// Allocate a block, return block id
fn alloc_block(&self) -> Option<usize> { fn alloc_block(&self) -> Option<usize> {
let id = self.free_map.write().alloc(); let mut free_map = self.free_map.write();
if id.is_some() { let id = free_map.alloc();
self.super_block.write().unused_blocks -= 1; // will panic if underflow if let Some(block_id) = id {
let mut super_block = self.super_block.write();
if super_block.unused_blocks==0 {
free_map.set(block_id, true);
return None
}
super_block.unused_blocks -= 1; // will panic if underflow
} }
id id
} }
@ -605,13 +614,13 @@ impl SimpleFileSystem {
} }
/// Create a new INode file /// Create a new INode file
fn new_inode_file(&self) -> vfs::Result<Arc<INodeImpl>> { fn new_inode_file(&self) -> vfs::Result<Arc<INodeImpl>> {
let id = self.alloc_block().expect("no space"); let id = self.alloc_block().ok_or(FsError::NoDeviceSpace)?;
let disk_inode = Dirty::new_dirty(DiskINode::new_file()); let disk_inode = Dirty::new_dirty(DiskINode::new_file());
Ok(self._new_inode(id, disk_inode)) Ok(self._new_inode(id, disk_inode))
} }
/// Create a new INode dir /// Create a new INode dir
fn new_inode_dir(&self, parent: INodeId) -> vfs::Result<Arc<INodeImpl>> { fn new_inode_dir(&self, parent: INodeId) -> vfs::Result<Arc<INodeImpl>> {
let id = self.alloc_block().expect("no space"); let id = self.alloc_block().ok_or(FsError::NoDeviceSpace)?;
let disk_inode = Dirty::new_dirty(DiskINode::new_dir()); let disk_inode = Dirty::new_dirty(DiskINode::new_dir());
let inode = self._new_inode(id, disk_inode); let inode = self._new_inode(id, disk_inode);
inode.init_dir_entry(parent)?; inode.init_dir_entry(parent)?;

@ -114,6 +114,7 @@ pub enum FsError {
EntryExist,//E_EXIST EntryExist,//E_EXIST
NotSameFs,//E_XDEV NotSameFs,//E_XDEV
InvalidParam,//E_INVAL InvalidParam,//E_INVAL
NoDeviceSpace,//E_NOSPC, but is defined and not used in the original ucore, which uses E_NO_MEM
//and something else //and something else
} }

Loading…
Cancel
Save