diff options
author | Chuyan Zhang <me@zcy.moe> | 2023-11-27 13:50:18 -0800 |
---|---|---|
committer | Chuyan Zhang <me@zcy.moe> | 2023-11-27 13:50:18 -0800 |
commit | ceb83a7214000ccd048857b40cc0ebfc54290731 (patch) | |
tree | 9511c0bcba3cc5d22c58fa300071b64953a57f07 /src/filesystem | |
parent | dfe7a2b658d31f6032f86ce225a467530860bf77 (diff) | |
download | myfs-ceb83a7214000ccd048857b40cc0ebfc54290731.tar.gz myfs-ceb83a7214000ccd048857b40cc0ebfc54290731.zip |
fix write & fix setattr
Diffstat (limited to 'src/filesystem')
-rw-r--r-- | src/filesystem/trait_impl.rs | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/src/filesystem/trait_impl.rs b/src/filesystem/trait_impl.rs index 0b73c9b..78916ed 100644 --- a/src/filesystem/trait_impl.rs +++ b/src/filesystem/trait_impl.rs @@ -5,8 +5,7 @@ use crate::utils::{from_systime, time_now, to_fileattr, to_filetype}; use crate::{AyaFS, TTL}; use fuser::TimeOrNow::{Now, SpecificTime}; use fuser::{ - Filesystem, KernelConfig, ReplyAttr, ReplyData, ReplyDirectory, ReplyEmpty, ReplyEntry, - ReplyLseek, ReplyOpen, ReplyWrite, Request, TimeOrNow, + Filesystem, KernelConfig, ReplyAttr, ReplyData, ReplyDirectory, ReplyEmpty, ReplyEntry, ReplyOpen, ReplyWrite, Request, TimeOrNow, }; use libc::{c_int, EACCES, EBADF, EEXIST, EINVAL, EIO, EISDIR, ENAMETOOLONG, ENOENT, ENOSPC, ENOSYS, ENOTDIR, ENOTEMPTY, EPERM, O_ACCMODE, O_RDONLY, O_RDWR, O_WRONLY, R_OK, S_ISGID, S_ISUID, S_IXGRP, S_IXOTH, S_IXUSR, W_OK}; use log::debug; @@ -75,11 +74,11 @@ impl Filesystem for AyaFS { mode: Option<u32>, uid: Option<u32>, gid: Option<u32>, - size: Option<u64>, // TODO 当 setattr 被 ftruncate invoke 时会设置 size + size: Option<u64>, // 当 setattr 被 ftruncate invoke 时会设置 size atime: Option<TimeOrNow>, mtime: Option<TimeOrNow>, _ctime: Option<SystemTime>, - fh: Option<u64>, // TODO 当 setattr 被 ftruncate invoke 时会提供 fh + fh: Option<u64>, // 当 setattr 被 ftruncate invoke 时会提供 fh _crtime: Option<SystemTime>, // 忽略 _chgtime: Option<SystemTime>, // 忽略 _bkuptime: Option<SystemTime>, // 忽略 @@ -158,12 +157,32 @@ impl Filesystem for AyaFS { if let Some(size) = size { debug!("ftruncate on inode {:#x?} size {:?}", ino, size); if let Some(file_handle) = fh { - // 有 file handle 的 - todo!() + let mut inode = inode.clone(); + let (inode_index, _read, write) = self.file_handle_map.get(&file_handle).unwrap(); + assert_eq!(ino as usize, *inode_index); + if !write { + reply.error(EACCES); + } else { + inode.size = size as u32; + reply.attr(&TTL, &to_fileattr(*inode_index, &inode)); + self.update_inode(*inode_index, inode); + } } else { - // 没有 file handle 基于 inode 的 - todo!() + if !check_access( + req.uid(), + req.gid(), + inode.uid, + inode.gid, + inode.mode, + W_OK, + ) { + reply.error(EACCES); + } else { + inode.size = size as u32; + reply.attr(&TTL, &to_fileattr(ino as usize, &inode)); + } } + return; } // time 相关 @@ -541,7 +560,7 @@ impl Filesystem for AyaFS { O_RDONLY => { // Behavior is undefined, but most filesystems return EACCES if flags & libc::O_TRUNC != 0 { - reply.error(libc::EACCES); + reply.error(EACCES); return; } (R_OK, true, false) @@ -655,8 +674,8 @@ impl Filesystem for AyaFS { fh: u64, offset: i64, data: &[u8], - write_flags: u32, - flags: i32, + _write_flags: u32, + _flags: i32, _lock_owner: Option<u64>, reply: ReplyWrite, ) { @@ -689,16 +708,16 @@ impl Filesystem for AyaFS { // 当前块已分配, 直接往里写 if let Some(block) = self.access_block_mut::<DataBlock>(&inode, current_block_index) { - debug!("writing in block {} within inode", current_block_index); - (block.block.0[ .. current_offset + write_length_within_block]) + debug!("writing {} bytes in block {} within inode", write_length_within_block, current_block_index); + (block.block.0[current_offset .. current_offset + write_length_within_block]) .copy_from_slice(&data[write_ptr .. write_ptr + write_length_within_block]); write_ptr += write_length_within_block; } else { // 当前块未分配,尝试分配 if let Some((block_index, block_index_within_inode)) = self.allocate_block_for(&mut inode) { - debug!("allocating block {} within inode, block index is {}", block_index_within_inode, block_index); + debug!("writing {} bytes in allocated block {} within inode, block index is {}", write_length_within_block, block_index_within_inode, block_index); // 能分配, 往里写 let block = self.access_block_mut::<DataBlock>(&inode, block_index_within_inode).unwrap(); - (block.block.0[ .. current_offset + write_length_within_block]) + (block.block.0[current_offset .. current_offset + write_length_within_block]) .copy_from_slice(&data[write_ptr .. write_ptr + write_length_within_block]); write_ptr += write_length_within_block; } else { @@ -741,7 +760,7 @@ impl Filesystem for AyaFS { O_RDONLY => { // Behavior is undefined, but most filesystems return EACCES if flags & libc::O_TRUNC != 0 { - reply.error(libc::EACCES); + reply.error(EACCES); return; } (R_OK, true, false) |