get_entry(), as a different design from list()

master
Ben Pig Chu 6 years ago
parent 4a97d89b8f
commit c744a3aea2

@ -396,6 +396,14 @@ impl vfs::INode for INode {
_ => unimplemented!(), _ => unimplemented!(),
} }
} }
fn get_entry(&self,id: usize) -> vfs::Result<String> {
assert_eq!(self.disk_inode.type_, FileType::Dir);
assert!(id<self.disk_inode.blocks as usize);
use vfs::INode;
let mut entry: DiskEntry = unsafe { uninitialized() };
self._read_at(id as usize * BLKSIZE, entry.as_buf_mut()).unwrap();
Ok(String::from(entry.name.as_ref()))
}
fn list(&self) -> vfs::Result<Vec<String>> { fn list(&self) -> vfs::Result<Vec<String>> {
assert_eq!(self.disk_inode.type_, FileType::Dir); assert_eq!(self.disk_inode.type_, FileType::Dir);

@ -54,7 +54,7 @@ fn create_new_sfs() {
let root = sfs.root_inode(); let root = sfs.root_inode();
} }
//#[test] // #[test]
fn print_root() { fn print_root() {
let sfs = _open_sample_file(); let sfs = _open_sample_file();
let root = sfs.root_inode(); let root = sfs.root_inode();
@ -62,6 +62,9 @@ fn print_root() {
let files = root.borrow().list().unwrap(); let files = root.borrow().list().unwrap();
println!("{:?}", files); println!("{:?}", files);
assert_eq!(files[3],root.borrow().get_entry(3).unwrap());
sfs.sync().unwrap();
} }
#[test] #[test]

@ -29,6 +29,9 @@ pub trait INode: Debug + Any {
fn link(&mut self, name: &str, other:&mut INode) -> Result<()>; fn link(&mut self, name: &str, other:&mut INode) -> Result<()>;
fn lookup(&self, path: &str) -> Result<INodePtr>; fn lookup(&self, path: &str) -> Result<INodePtr>;
fn list(&self) -> Result<Vec<String>>; fn list(&self) -> Result<Vec<String>>;
/// like list()[id]
/// only get one item in list, often faster than list
fn get_entry(&self,id: usize) -> Result<String>;
// fn io_ctrl(&mut self, op: u32, data: &[u8]) -> Result<()>; // fn io_ctrl(&mut self, op: u32, data: &[u8]) -> Result<()>;
fn fs(&self) -> Weak<FileSystem>; fn fs(&self) -> Weak<FileSystem>;
/// this is used to implement dynamics cast /// this is used to implement dynamics cast

Loading…
Cancel
Save