From 89411a5c6f5e3c2a7be5287438b20f75ef0044e9 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Wed, 17 May 2017 07:08:08 -0400 Subject: lookup: Using traverse_fn --- src/data.rs | 9 +++++---- src/main.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/data.rs b/src/data.rs index 6976352..0db499c 100644 --- a/src/data.rs +++ b/src/data.rs @@ -15,13 +15,14 @@ pub fn get_data1() -> SimpleFsData { SimpleFsData::File(2, "Hello, World!".to_string())))) } -pub fn traverse_fs(file: SimpleFsData, f: &mut F) +pub fn traverse_fs(file: &SimpleFsData, f: &mut F) where F: FnMut(& SimpleFsData) { f(&file); match file { - SimpleFsData::Directory(_, children) => for (_, file) in children { - traverse_fs(file, f); - }, + &SimpleFsData::Directory(_, ref children) => + for &(_, ref file) in children { + traverse_fs(&file, f); + }, _ => (), } } diff --git a/src/main.rs b/src/main.rs index 1fc8e53..89b1f4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ extern crate rafs; extern crate time; use rafs::data::get_data1; -use rafs::data::SimpleFsData; +use rafs::data::{SimpleFsData, traverse_fs}; use fuse::{Filesystem, Request, ReplyEntry, ReplyAttr, ReplyDirectory, ReplyData, FileAttr, FileType}; use libc::ENOENT; use std::ffi::OsStr; @@ -15,8 +15,9 @@ struct MemFs { } const CREATE_TIME: Timespec = Timespec { sec: 1494802762, nsec: 0 }; +const TTL: Timespec = Timespec { sec: 1, nsec: 0 }; -fn getAttr(file: SimpleFsData) -> FileAttr { +fn getAttr(file: &SimpleFsData) -> FileAttr { fn fileAttr(ino: u64, len: usize) -> FileAttr { FileAttr { ino: ino, @@ -56,15 +57,35 @@ fn getAttr(file: SimpleFsData) -> FileAttr { } match file { - SimpleFsData::File(ino, name) => fileAttr(ino, name.len()), - SimpleFsData::Directory(ino, children) => dirAttr(ino, children.len()), + &SimpleFsData::File(ino, ref name) => fileAttr(ino, name.len()), + &SimpleFsData::Directory(ino, ref children) => dirAttr(ino, children.len()), } } impl Filesystem for MemFs { fn lookup(&mut self, _req: &Request, _parent: u64, _name: &OsStr, reply: ReplyEntry) { - reply.error(ENOENT); + let mut result: Option = None; + { + let mut f = |ref file: &SimpleFsData| { + match *file { + &SimpleFsData::Directory(i, ref children) => + if i == _parent { + for &(ref name, ref file) in children { + if Some(name.as_str()) == _name.to_str() { + result = Some(getAttr(file)); + } + }; + }, + _ => () + }; + }; + traverse_fs(&self.data, &mut f); + } + match result { + Some(r) => reply.entry(&TTL, &r, 0), + None => reply.error(ENOENT) + } } fn getattr(&mut self, _req: &Request, _ino: u64, reply: ReplyAttr) { -- cgit v1.2.3