diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bd35ef2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,106 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "afs-fuse" +version = "0.1.0" +dependencies = [ + "fuse", + "libc", + "time", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "fuse" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e57070510966bfef93662a81cb8aa2b1c7db0964354fa9921434f04b9e8660" +dependencies = [ + "libc", + "log 0.3.9", + "pkg-config", + "thread-scoped", + "time", +] + +[[package]] +name = "libc" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" + +[[package]] +name = "log" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" +dependencies = [ + "log 0.4.14", +] + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "pkg-config" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12295df4f294471248581bc09bef3c38a5e46f1e36d6a37353621a0c6c357e1f" + +[[package]] +name = "thread-scoped" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcbb6aa301e5d3b0b5ef639c9a9c7e2f1c944f177b460c04dc24c69b1fa2bd99" + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2280216 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "afs-fuse" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +fuse = "0.3.1" +time="*" +libc="*" diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..388415b --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ + mkdir /tmp/rust-fuse + cargo run /tmp/rust-fuse + fusermount -u /tmp/rust-fuse diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..fbef732 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,118 @@ +#![feature(libc)] + +extern crate fuse; +extern crate libc; +extern crate time; +use std::ffi::OsStr; + +use fuse::{ + FileAttr, FileType, Filesystem, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry, Request, +}; +use libc::ENOENT; +use std::env; +use std::path::Path; +use time::Timespec; + +const TTL: Timespec = Timespec { sec: 1, nsec: 0 }; // 1 second + +const CREATE_TIME: Timespec = Timespec { + sec: 1381237736, + nsec: 0, +}; // 2013-10-08 08:56 + +const HELLO_DIR_ATTR: FileAttr = FileAttr { + ino: 1, + size: 0, + blocks: 0, + atime: CREATE_TIME, + mtime: CREATE_TIME, + ctime: CREATE_TIME, + crtime: CREATE_TIME, + kind: FileType::Directory, + perm: 0o755, + nlink: 2, + uid: 501, + gid: 20, + rdev: 0, + flags: 0, +}; + +const HELLO_TXT_CONTENT: &'static str = "Hello World!\n"; + +const HELLO_TXT_ATTR: FileAttr = FileAttr { + ino: 2, + size: 13, + blocks: 1, + atime: CREATE_TIME, + mtime: CREATE_TIME, + ctime: CREATE_TIME, + crtime: CREATE_TIME, + kind: FileType::RegularFile, + perm: 0o644, + nlink: 1, + uid: 501, + gid: 20, + rdev: 0, + flags: 0, +}; + +struct HelloFS; + +impl Filesystem for HelloFS { + fn lookup(&mut self, _req: &Request, parent: u64, name: &OsStr, reply: ReplyEntry) { + if parent == 1 && name.to_str() == Some("hello.txt") { + reply.entry(&TTL, &HELLO_TXT_ATTR, 0); + } else { + reply.error(ENOENT); + } + } + + fn getattr(&mut self, _req: &Request, ino: u64, reply: ReplyAttr) { + match ino { + 1 => reply.attr(&TTL, &HELLO_DIR_ATTR), + 2 => reply.attr(&TTL, &HELLO_TXT_ATTR), + _ => reply.error(ENOENT), + } + } + + fn read( + &mut self, + _req: &Request, + ino: u64, + _fh: u64, + offset: i64, + _size: u32, + reply: ReplyData, + ) { + if ino == 2 { + reply.data(&HELLO_TXT_CONTENT.as_bytes()[offset as usize..]); + } else { + reply.error(ENOENT); + } + } + + fn readdir( + &mut self, + _req: &Request, + ino: u64, + _fh: u64, + offset: i64, + mut reply: ReplyDirectory, + ) { + if ino == 1 { + if offset == 0 { + reply.add(1, 0, FileType::Directory, "."); + reply.add(1, 1, FileType::Directory, ".."); + reply.add(2, 2, FileType::RegularFile, "hello.txt"); + } + reply.ok(); + } else { + reply.error(ENOENT); + } + } +} + +fn main() { + let mountpoint = env::args_os().nth(1).unwrap(); + fuse::mount(HelloFS, &mountpoint, &[]); +}