From 9b9da43082d575a0b4f2261e9cabb9db5f8c6e6e Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 14 May 2017 19:35:50 -0400 Subject: SimpleFileSystem traversal. --- Cargo.lock | 1 + Cargo.toml | 1 + default.nix | 13 ++++++++++++- deps.nix | 5 ++--- src/data.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 92 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91497e5..cd5a59f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.1.0" dependencies = [ "fuse 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4e938a1..41918d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ authors = ["Kjetil Orbekk "] [dependencies] fuse = "0.3" libc = "0.2" +time = "0.1" diff --git a/default.nix b/default.nix index 66bb250..2355d10 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,18 @@ with import {}; +# deps generated with +# ~/.cargo/bin/generate-nix-pkg Cargo.lock -o deps2.nix let mkRustCrate = import ./rust-utils.nix { lib = pkgs.lib; inherit buildPlatform stdenv; }; in let deps = import ./deps.nix { inherit pkgs mkRustCrate; }; in -(deps.rafs_0_1_0 rustUnstable.rustc) +{ + rafs_0_1_0 = mkRustCrate { + crateName = "rafs"; + version = "0.1.0"; + dependencies = with deps; [ fuse_0_3_0 libc_0_2_22 time_0_1_37 ]; + buildInputs = [ fuse pkgconfig glibc ]; + src = ./.; + release = true; + } rustUnstable.rustc; +} diff --git a/deps.nix b/deps.nix index f07958a..671489f 100644 --- a/deps.nix +++ b/deps.nix @@ -12,7 +12,7 @@ rec { sha256 = "1n49fdna5vxygl5v8s8i2brirg4zn7psc65chga4crcs9fk49w8i"; name = "fuse-0.3.0.tar.gz"; }; - buildInputs = with pkgs; [fuse pkgconfig glibc]; + buildInputs = with pkgs; [ fuse pkgconfig glibc ]; libPath = "src/lib.rs"; libName = "fuse"; build = "build.rs"; @@ -67,8 +67,7 @@ rec { rafs_0_1_0 = mkRustCrate { crateName = "rafs"; version = "0.1.0"; - dependencies = [ fuse_0_3_0 libc_0_2_22 ]; - buildInputs = [ fuse pkgconfig glibc ]; + dependencies = [ fuse_0_3_0 libc_0_2_22 time_0_1_37 ]; src = ./.; inherit release; }; diff --git a/src/data.rs b/src/data.rs index f652a13..6976352 100644 --- a/src/data.rs +++ b/src/data.rs @@ -14,3 +14,29 @@ pub fn get_data1() -> SimpleFsData { 1, vec!(("hello".to_string(), SimpleFsData::File(2, "Hello, World!".to_string())))) } + +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); + }, + _ => (), + } +} + +#[test] +fn traverse() { + let mut result = String::new(); + { + let mut f = |ref file: &SimpleFsData| { + match *file { + &SimpleFsData::File(_, ref s) => result += &s, + _ => (), + }; + }; + traverse_fs(get_data1(), &mut f); + } + assert_eq!("Hello, World!", result); +} diff --git a/src/main.rs b/src/main.rs index 9ad2982..1fc8e53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,66 @@ extern crate fuse; extern crate libc; extern crate rafs; +extern crate time; use rafs::data::get_data1; use rafs::data::SimpleFsData; -use fuse::{Filesystem, Request, ReplyEntry, ReplyAttr, ReplyDirectory, ReplyData}; +use fuse::{Filesystem, Request, ReplyEntry, ReplyAttr, ReplyDirectory, ReplyData, FileAttr, FileType}; use libc::ENOENT; use std::ffi::OsStr; +use time::Timespec; struct MemFs { data: SimpleFsData, } +const CREATE_TIME: Timespec = Timespec { sec: 1494802762, nsec: 0 }; + +fn getAttr(file: SimpleFsData) -> FileAttr { + fn fileAttr(ino: u64, len: usize) -> FileAttr { + FileAttr { + ino: ino, + size: len as u64, + blocks: 1, + atime: CREATE_TIME, + mtime: CREATE_TIME, + ctime: CREATE_TIME, + crtime: CREATE_TIME, + kind: FileType::RegularFile, + perm: 0o755, + nlink: 1, + uid: 501, + gid: 20, + rdev: 0, + flags: 0, + } + } + + fn dirAttr(ino: u64, children: usize) -> FileAttr { + FileAttr { + ino: ino, + size: 0, + blocks: 1, + atime: CREATE_TIME, + mtime: CREATE_TIME, + ctime: CREATE_TIME, + crtime: CREATE_TIME, + kind: FileType::Directory, + perm: 0o755, + nlink: children as u32, + uid: 501, + gid: 20, + rdev: 0, + flags: 0, + } + } + + match file { + SimpleFsData::File(ino, name) => fileAttr(ino, name.len()), + SimpleFsData::Directory(ino, children) => dirAttr(ino, children.len()), + } +} + impl Filesystem for MemFs { fn lookup(&mut self, _req: &Request, _parent: u64, _name: &OsStr, reply: ReplyEntry) { -- cgit v1.2.3