Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace fuse-rs to fuser #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 151 additions & 99 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ clap = "2.29.0"
daemonize = "0.2"
env_logger = "0.4"
fd = { git = "https://github.com/stemjail/fd-rs.git", rev = "3bc3e3587f8904cce8bf29163a2021c2f5906557" }
fuse = "0.3.0"
fuser = "0.7"
generic-array = "0.8"
itertools = "0.6"
log = "0.3"
Expand All @@ -25,7 +25,7 @@ rand = "0.3"
sha2 = "0.6"
syslog = "3.3"
threadpool = "1.4"
time = "0.1"
chrono = "0.4"
twox-hash = "1.5.0"
xattr = "0.2"

Expand Down
61 changes: 25 additions & 36 deletions src/catfs/inode.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
extern crate fuse;
extern crate fuser;
extern crate libc;
extern crate threadpool;
extern crate time;

use self::threadpool::ThreadPool;
use self::time::{Duration, Timespec};

use std::ffi::OsStr;
use std::ffi::OsString;
use std::io;
use std::os::unix::io::RawFd;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::{Duration, SystemTime};

use catfs::dir;
use catfs::error;
Expand All @@ -27,22 +26,22 @@ pub struct Inode {
name: OsString,
path: PathBuf,

attr: fuse::FileAttr,
time: Timespec,
attr: fuser::FileAttr,
time: SystemTime,
cache_valid_if_present: bool,
flush_failed: bool,

refcnt: u64,
}

fn to_filetype(t: libc::mode_t) -> fuse::FileType {
fn to_filetype(t: libc::mode_t) -> fuser::FileType {
match t & libc::S_IFMT {
libc::S_IFLNK => fuse::FileType::Symlink,
libc::S_IFREG => fuse::FileType::RegularFile,
libc::S_IFBLK => fuse::FileType::BlockDevice,
libc::S_IFDIR => fuse::FileType::Directory,
libc::S_IFCHR => fuse::FileType::CharDevice,
libc::S_IFIFO => fuse::FileType::NamedPipe,
libc::S_IFLNK => fuser::FileType::Symlink,
libc::S_IFREG => fuser::FileType::RegularFile,
libc::S_IFBLK => fuser::FileType::BlockDevice,
libc::S_IFDIR => fuser::FileType::Directory,
libc::S_IFCHR => fuser::FileType::CharDevice,
libc::S_IFIFO => fuser::FileType::NamedPipe,
v => panic!("unknown type: {}", v),
}
}
Expand All @@ -54,15 +53,15 @@ impl Inode {
cache_dir: RawFd,
name: OsString,
path: PathBuf,
attr: fuse::FileAttr,
attr: fuser::FileAttr,
) -> Inode {
return Inode {
src_dir: src_dir,
cache_dir: cache_dir,
name: name,
path: path,
attr: attr,
time: time::get_time(),
time: SystemTime::now(),
cache_valid_if_present: false,
flush_failed: false,
refcnt: 1,
Expand All @@ -75,7 +74,7 @@ impl Inode {
}

pub fn not_expired(&self, ttl: &Duration) -> bool {
(time::get_time() - self.time) > *ttl
SystemTime::now() > self.time + *ttl
}

pub fn get_child_name(&self, name: &OsStr) -> PathBuf {
Expand All @@ -88,11 +87,11 @@ impl Inode {
return &self.path;
}

pub fn get_attr(&self) -> &fuse::FileAttr {
pub fn get_attr(&self) -> &fuser::FileAttr {
return &self.attr;
}

pub fn get_kind(&self) -> fuse::FileType {
pub fn get_kind(&self) -> fuser::FileType {
return self.attr.kind;
}

Expand All @@ -106,35 +105,25 @@ impl Inode {
}
}

pub fn lookup_path(dir: RawFd, path: &dyn AsRef<Path>) -> io::Result<fuse::FileAttr> {
pub fn lookup_path(dir: RawFd, path: &dyn AsRef<Path>) -> io::Result<fuser::FileAttr> {
let st = rlibc::fstatat(dir, path)?;
let attr = fuse::FileAttr {
let attr = fuser::FileAttr {
ino: st.st_ino,
size: st.st_size as u64,
blocks: st.st_blocks as u64,
atime: Timespec {
sec: st.st_atime as i64,
nsec: st.st_atime_nsec as i32,
},
mtime: Timespec {
sec: st.st_mtime as i64,
nsec: st.st_mtime_nsec as i32,
},
ctime: Timespec {
sec: st.st_ctime as i64,
nsec: st.st_ctime_nsec as i32,
},
crtime: Timespec {
sec: st.st_ctime as i64,
nsec: st.st_ctime_nsec as i32,
},
atime: SystemTime::UNIX_EPOCH + Duration::from_secs(st.st_atime as u64),
mtime: SystemTime::UNIX_EPOCH + Duration::from_secs(st.st_mtime as u64),
ctime: SystemTime::UNIX_EPOCH + Duration::from_secs(st.st_ctime as u64),
crtime: SystemTime::UNIX_EPOCH + Duration::from_secs(st.st_ctime as u64),
kind: to_filetype(st.st_mode),
perm: (st.st_mode & !libc::S_IFMT) as u16,
nlink: st.st_nlink as u32,
uid: st.st_uid,
gid: st.st_gid,
rdev: st.st_rdev as u32,
blksize: st.st_blksize as u32,
flags: 0,
padding: 0
};
return Ok(attr);
}
Expand Down Expand Up @@ -272,7 +261,7 @@ impl Inode {
return Ok(());
}

pub fn utimes(&self, atime: &Timespec, mtime: &Timespec, flags: u32) -> io::Result<()> {
pub fn utimes(&self, atime: &SystemTime, mtime: &SystemTime, flags: u32) -> io::Result<()> {
rlibc::utimensat(self.src_dir, &self.path, atime, mtime, flags)
}

Expand Down
90 changes: 62 additions & 28 deletions src/catfs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
extern crate fuse;
extern crate fuser;
extern crate libc;
extern crate threadpool;
extern crate time;

use self::fuse::{ReplyEntry, ReplyAttr, ReplyOpen, ReplyEmpty, ReplyDirectory, ReplyData,
ReplyWrite, ReplyCreate, ReplyStatfs};

use self::time::{Duration, Timespec};
use self::fuser::{
ReplyEntry,
ReplyAttr,
ReplyOpen,
ReplyEmpty,
ReplyDirectory,
ReplyData,ReplyWrite,
ReplyCreate,
ReplyStatfs,
TimeOrNow
};

use std::cmp;
use std::collections::HashMap;
Expand All @@ -16,6 +22,7 @@ use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::RawFd;
use std::sync::{Arc, Mutex, MutexGuard, RwLock, RwLockWriteGuard};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};

use self::threadpool::ThreadPool;

Expand Down Expand Up @@ -119,7 +126,7 @@ impl CatFS {
cache: to.as_ref().to_path_buf(),
src_dir: src_dir,
cache_dir: cache_dir,
ttl: Duration::zero(),
ttl: Duration::ZERO,
store: Mutex::new(Default::default()),
dh_store: Mutex::new(Default::default()),
fh_store: Mutex::new(Default::default()),
Expand All @@ -146,7 +153,7 @@ impl CatFS {
PathBuf::new(),
root_attr,
);
inode.use_ino(fuse::FUSE_ROOT_ID);
inode.use_ino(fuser::FUSE_ROOT_ID);

self.insert_inode(inode);

Expand Down Expand Up @@ -184,8 +191,8 @@ impl CatFS {
store.inodes_cache.remove(path);
}

fn ttl_now(&self) -> time::Timespec {
return time::get_time() + self.ttl;
fn ttl_now(&self) -> Duration {
return SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap() + self.ttl;
}

pub fn statfs(&mut self, _ino: u64, reply: ReplyStatfs) {
Expand Down Expand Up @@ -343,12 +350,13 @@ impl CatFS {
uid: Option<u32>,
gid: Option<u32>,
size: Option<u64>,
atime: Option<Timespec>,
mtime: Option<Timespec>,
atime: Option<TimeOrNow>,
mtime: Option<TimeOrNow>,
_ctime: Option<SystemTime>,
fh: Option<u64>,
crtime: Option<Timespec>,
chgtime: Option<Timespec>,
bkuptime: Option<Timespec>,
crtime: Option<SystemTime>,
chgtime: Option<SystemTime>,
bkuptime: Option<SystemTime>,
flags: Option<u32>,
reply: ReplyAttr,
) {
Expand Down Expand Up @@ -435,6 +443,22 @@ impl CatFS {
if mtime.is_some() || atime.is_some() {
let old_attr = inode.get_attr();

let mtime = match mtime {
Some(time_or_now) => match time_or_now {
TimeOrNow::SpecificTime(time) => Some(time),
TimeOrNow::Now => Some(SystemTime::now()),
},
None => None,
};

let atime = match atime {
Some(time_or_now) => match time_or_now {
TimeOrNow::SpecificTime(time) => Some(time),
TimeOrNow::Now => Some(SystemTime::now()),
},
None => None,
};

if let Err(e) = inode.utimes(
&atime.unwrap_or(old_attr.atime),
&mtime.unwrap_or(old_attr.mtime),
Expand Down Expand Up @@ -504,7 +528,7 @@ impl CatFS {
}
}

pub fn opendir(&mut self, ino: u64, flags: u32, reply: ReplyOpen) {
pub fn opendir(&mut self, ino: u64, flags: i32, reply: ReplyOpen) {
let inode: Arc<RwLock<Inode>>;
{
let store = self.store.lock().unwrap();
Expand All @@ -518,7 +542,7 @@ impl CatFS {
let dh = dh_store.next_id;
dh_store.next_id += 1;
dh_store.handles.insert(dh, dir);
reply.opened(dh, flags);
reply.opened(dh, flags as u32);
debug!("<-- opendir {:?} = {}", inode.get_path(), dh);
}
Err(e) => {
Expand Down Expand Up @@ -561,28 +585,28 @@ impl CatFS {
reply.ok();
}

pub fn releasedir(&mut self, _ino: u64, dh: u64, _flags: u32, reply: ReplyEmpty) {
pub fn releasedir(&mut self, _ino: u64, dh: u64, _flags: i32, reply: ReplyEmpty) {
let mut dh_store = self.dh_store.lock().unwrap();
// the handle will be destroyed and closed
dh_store.handles.remove(&dh);
reply.ok();
}

pub fn open(&mut self, ino: u64, flags: u32, reply: ReplyOpen) {
pub fn open(&mut self, ino: u64, flags: i32, reply: ReplyOpen) {
let inode: Arc<RwLock<Inode>>;
{
let store = self.store.lock().unwrap();
inode = store.get(ino);
}

let mut inode = inode.write().unwrap();
match inode.open(flags, &self.tp) {
match inode.open(flags as u32, &self.tp) {
Ok(file) => {
let mut fh_store = self.fh_store.lock().unwrap();
let fh = fh_store.next_id;
fh_store.next_id += 1;
fh_store.handles.insert(fh, Arc::new(Mutex::new(file)));
reply.opened(fh, flags);
reply.opened(fh, flags as u32);
debug!("<-- open {:?} = {}", inode.get_path(), fh);
}
Err(e) => {
Expand All @@ -592,7 +616,16 @@ impl CatFS {
}
}

pub fn read(&mut self, _ino: u64, fh: u64, offset: i64, size: u32, reply: ReplyData) {
pub fn read(
&mut self,
_ino: u64,
fh: u64,
offset: i64,
size: u32,
_flags: i32,
_lock_owner: Option<u64>,
reply: ReplyData
) {
let file: Arc<Mutex<file::Handle>>;
{
let fh_store = self.fh_store.lock().unwrap();
Expand All @@ -617,7 +650,8 @@ impl CatFS {
parent: u64,
name: OsString,
mode: u32,
flags: u32,
_umask: u32,
flags: i32,
reply: ReplyCreate,
) {
let parent_inode: Arc<RwLock<Inode>>;
Expand All @@ -640,7 +674,7 @@ impl CatFS {
let attr = *inode.get_attr();
debug!("<-- create {:?} = {}", inode.get_path(), fh);
self.insert_inode(inode);
reply.created(&self.ttl_now(), &attr, 0, fh, flags);
reply.created(&self.ttl_now(), &attr, 0, fh, flags as u32);
}
Err(e) => {
error!(
Expand All @@ -659,7 +693,7 @@ impl CatFS {
fh: u64,
offset: i64,
data: Vec<u8>,
_flags: u32,
_flags: i32,
reply: ReplyWrite,
) {
let nwritten: usize;
Expand Down Expand Up @@ -781,8 +815,8 @@ impl CatFS {
&mut self,
_ino: u64,
fh: u64,
_flags: u32,
_lock_owner: u64,
_flags: i32,
_lock_owner: Option<u64>,
_flush: bool,
reply: ReplyEmpty,
) {
Expand Down Expand Up @@ -834,7 +868,7 @@ impl CatFS {
}
}

pub fn mkdir(&mut self, parent: u64, name: OsString, mode: u32, reply: ReplyEntry) {
pub fn mkdir(&mut self, parent: u64, name: OsString, mode: u32, _umask: u32, reply: ReplyEntry) {
let parent_inode: Arc<RwLock<Inode>>;
{
let store = self.store.lock().unwrap();
Expand Down
Loading