From 05a4df20bf4ecd8ab90a430e945daebc41661af7 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Wed, 27 Mar 2019 23:33:34 +0800 Subject: [PATCH] replace chmod by set_metadata in INode --- rcore-fs-fuse/src/fuse.rs | 75 +++++++++++++++++++++++++++++++-------- rcore-fs-sefs/src/lib.rs | 18 +++++----- rcore-fs-sfs/src/lib.rs | 4 +-- rcore-fs/src/vfs.rs | 2 +- 4 files changed, 72 insertions(+), 27 deletions(-) diff --git a/rcore-fs-fuse/src/fuse.rs b/rcore-fs-fuse/src/fuse.rs index 28ce051..6ba5f51 100644 --- a/rcore-fs-fuse/src/fuse.rs +++ b/rcore-fs-fuse/src/fuse.rs @@ -28,6 +28,12 @@ impl VfsFuse { nsec: time.nsec, } } + fn trans_time_r(time: Timespec) -> vfs::Timespec { + vfs::Timespec { + sec: time.sec, + nsec: time.nsec, + } + } fn trans_attr(info: vfs::Metadata) -> FileAttr { FileAttr { ino: info.inode as u64, @@ -40,8 +46,8 @@ impl VfsFuse { kind: Self::trans_type(info.type_), perm: info.mode, nlink: info.nlinks as u32, - uid: info.uid as u32, - gid: info.gid as u32, + uid: 501, // info.uid as u32, + gid: 20, // info.gid as u32, rdev: 0, flags: 0, } @@ -59,20 +65,19 @@ impl VfsFuse { } fn trans_error(err: vfs::FsError) -> i32 { use libc::*; - use vfs::FsError::*; match err { - NotSupported => ENOSYS, - EntryNotFound => ENOENT, - EntryExist => EEXIST, - IsDir => EISDIR, - NotFile => EISDIR, - NotDir => ENOTDIR, - NotSameFs => EXDEV, - InvalidParam => EINVAL, - NoDeviceSpace => ENOSPC, - DirRemoved => ENOENT, - DirNotEmpty => ENOTEMPTY, - WrongFs => EINVAL, + vfs::FsError::NotSupported => ENOSYS, + vfs::FsError::EntryNotFound => ENOENT, + vfs::FsError::EntryExist => EEXIST, + vfs::FsError::IsDir => EISDIR, + vfs::FsError::NotFile => EISDIR, + vfs::FsError::NotDir => ENOTDIR, + vfs::FsError::NotSameFs => EXDEV, + vfs::FsError::InvalidParam => EINVAL, + vfs::FsError::NoDeviceSpace => ENOSPC, + vfs::FsError::DirRemoved => ENOENT, + vfs::FsError::DirNotEmpty => ENOTEMPTY, + vfs::FsError::WrongFs => EINVAL, _ => EINVAL, } } @@ -118,6 +123,45 @@ impl Filesystem for VfsFuse { reply.attr(&TTL, &attr); } + fn setattr( + &mut self, + _req: &Request, + ino: u64, + mode: Option, + uid: Option, + gid: Option, + _size: Option, + atime: Option, + mtime: Option, + _fh: Option, + _crtime: Option, + _chgtime: Option, + _bkuptime: Option, + _flags: Option, + reply: ReplyAttr, + ) { + let inode = try_vfs!(reply, self.get_inode(ino)); + let mut info = try_vfs!(reply, inode.metadata()); + if let Some(mode) = mode { + info.mode = mode as u16; + } + if let Some(uid) = uid { + info.uid = uid as usize; + } + if let Some(gid) = gid { + info.gid = gid as usize; + } + if let Some(atime) = atime { + info.atime = Self::trans_time_r(atime); + } + if let Some(mtime) = mtime { + info.mtime = Self::trans_time_r(mtime); + } + try_vfs!(reply, inode.set_metadata(&info)); + let attr = Self::trans_attr(info); + reply.attr(&TTL, &attr); + } + fn mknod( &mut self, _req: &Request, @@ -141,6 +185,7 @@ impl Filesystem for VfsFuse { let inode = try_vfs!(reply, self.get_inode(parent)); let target = try_vfs!(reply, inode.create(name, vfs::FileType::Dir, mode)); let info = try_vfs!(reply, target.metadata()); + self.inodes.insert(info.inode, target); let attr = Self::trans_attr(info); reply.entry(&TTL, &attr, 0); } diff --git a/rcore-fs-sefs/src/lib.rs b/rcore-fs-sefs/src/lib.rs index c67768e..cea64f4 100644 --- a/rcore-fs-sefs/src/lib.rs +++ b/rcore-fs-sefs/src/lib.rs @@ -2,9 +2,6 @@ #![feature(alloc)] extern crate alloc; -#[cfg(feature = "sgx")] -#[macro_use] -extern crate sgx_tstd as std; use alloc::{ boxed::Box, @@ -191,13 +188,16 @@ impl vfs::INode for INodeImpl { blk_size: 0x1000, }) } - - fn chmod(&self, mode: u16) -> vfs::Result<()> { + fn set_metadata(&self, metadata: &vfs::Metadata) -> vfs::Result<()> { let mut disk_inode = self.disk_inode.write(); - disk_inode.mode = mode; + disk_inode.mode = metadata.mode; + disk_inode.uid = metadata.uid as u16; + disk_inode.gid = metadata.gid as u8; + disk_inode.atime = metadata.atime.sec as u32; + disk_inode.mtime = metadata.mtime.sec as u32; + disk_inode.ctime = metadata.ctime.sec as u32; Ok(()) } - fn sync_all(&self) -> vfs::Result<()> { let mut disk_inode = self.disk_inode.write(); if disk_inode.dirty() { @@ -206,10 +206,12 @@ impl vfs::INode for INodeImpl { .write_block(self.id, disk_inode.as_buf())?; disk_inode.sync(); } + self.sync_data()?; Ok(()) } fn sync_data(&self) -> vfs::Result<()> { - self.sync_all() + self.file.flush()?; + Ok(()) } fn resize(&self, len: usize) -> vfs::Result<()> { if self.disk_inode.read().type_ != FileType::File { diff --git a/rcore-fs-sfs/src/lib.rs b/rcore-fs-sfs/src/lib.rs index 791eef5..bc7c3df 100644 --- a/rcore-fs-sfs/src/lib.rs +++ b/rcore-fs-sfs/src/lib.rs @@ -421,12 +421,10 @@ impl vfs::INode for INodeImpl { blk_size: BLKSIZE, }) } - - fn chmod(&self, _mode: u16) -> vfs::Result<()> { + fn set_metadata(&self, _metadata: &vfs::Metadata) -> vfs::Result<()> { // No-op for sfs Ok(()) } - fn sync_all(&self) -> vfs::Result<()> { let mut disk_inode = self.disk_inode.write(); if disk_inode.dirty() { diff --git a/rcore-fs/src/vfs.rs b/rcore-fs/src/vfs.rs index 4b23d4f..1a44053 100644 --- a/rcore-fs/src/vfs.rs +++ b/rcore-fs/src/vfs.rs @@ -9,7 +9,7 @@ pub trait INode: Any + Sync + Send { fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result; fn write_at(&self, offset: usize, buf: &[u8]) -> Result; fn metadata(&self) -> Result; - fn chmod(&self, mode: u16) -> Result<()>; + fn set_metadata(&self, metadata: &Metadata) -> Result<()>; /// Sync all data and metadata fn sync_all(&self) -> Result<()>; /// Sync data (not include metadata)