12dbef785SGnoCiYeH use core::ffi::CStr; 22dbef785SGnoCiYeH 31496ba7bSLoGin use alloc::{ 41496ba7bSLoGin string::{String, ToString}, 51496ba7bSLoGin sync::Arc, 61496ba7bSLoGin vec::Vec, 71496ba7bSLoGin }; 8004e86ffSlogin 9004e86ffSlogin use crate::{ 102dbef785SGnoCiYeH driver::base::{block::SeekFrom, device::DeviceNumber}, 112b771e32SGou Ngai filesystem::vfs::file::FileDescriptorVec, 122dbef785SGnoCiYeH include::bindings::bindings::{verify_area, AT_REMOVEDIR, PROC_MAX_FD_NUM}, 132b771e32SGou Ngai kerror, 141496ba7bSLoGin libs::rwlock::RwLockWriteGuard, 151496ba7bSLoGin mm::VirtAddr, 161496ba7bSLoGin process::ProcessManager, 172dbef785SGnoCiYeH syscall::{user_access::UserBufferReader, Syscall, SystemError}, 1867b48188Shoumkh time::TimeSpec, 19004e86ffSlogin }; 20004e86ffSlogin 21004e86ffSlogin use super::{ 22ab5c8ca4Slogin core::{do_mkdir, do_remove_dir, do_unlink_at}, 236d81180bSLoGin fcntl::{FcntlCommand, FD_CLOEXEC}, 24004e86ffSlogin file::{File, FileMode}, 25ab5c8ca4Slogin utils::rsplit_path, 26*a03c4f9dSLoGin Dirent, FileType, IndexNode, MAX_PATHLEN, ROOT_INODE, VFS_MAX_FOLLOW_SYMLINK_TIMES, 27004e86ffSlogin }; 28004e86ffSlogin 29ab5c8ca4Slogin pub const SEEK_SET: u32 = 0; 30ab5c8ca4Slogin pub const SEEK_CUR: u32 = 1; 31ab5c8ca4Slogin pub const SEEK_END: u32 = 2; 32ab5c8ca4Slogin pub const SEEK_MAX: u32 = 3; 33004e86ffSlogin 3467b48188Shoumkh bitflags! { 3567b48188Shoumkh /// 文件类型和权限 366b4e7a29SLoGin #[repr(C)] 3767b48188Shoumkh pub struct ModeType: u32 { 3867b48188Shoumkh /// 掩码 3967b48188Shoumkh const S_IFMT = 0o0_170_000; 4067b48188Shoumkh /// 文件类型 4167b48188Shoumkh const S_IFSOCK = 0o140000; 4267b48188Shoumkh const S_IFLNK = 0o120000; 4367b48188Shoumkh const S_IFREG = 0o100000; 4467b48188Shoumkh const S_IFBLK = 0o060000; 4567b48188Shoumkh const S_IFDIR = 0o040000; 4667b48188Shoumkh const S_IFCHR = 0o020000; 4767b48188Shoumkh const S_IFIFO = 0o010000; 4867b48188Shoumkh 4967b48188Shoumkh const S_ISUID = 0o004000; 5067b48188Shoumkh const S_ISGID = 0o002000; 5167b48188Shoumkh const S_ISVTX = 0o001000; 5267b48188Shoumkh /// 文件用户权限 5367b48188Shoumkh const S_IRWXU = 0o0700; 5467b48188Shoumkh const S_IRUSR = 0o0400; 5567b48188Shoumkh const S_IWUSR = 0o0200; 5667b48188Shoumkh const S_IXUSR = 0o0100; 5767b48188Shoumkh /// 文件组权限 5867b48188Shoumkh const S_IRWXG = 0o0070; 5967b48188Shoumkh const S_IRGRP = 0o0040; 6067b48188Shoumkh const S_IWGRP = 0o0020; 6167b48188Shoumkh const S_IXGRP = 0o0010; 6267b48188Shoumkh /// 文件其他用户权限 6367b48188Shoumkh const S_IRWXO = 0o0007; 6467b48188Shoumkh const S_IROTH = 0o0004; 6567b48188Shoumkh const S_IWOTH = 0o0002; 6667b48188Shoumkh const S_IXOTH = 0o0001; 6706d5e247SLoGin 6806d5e247SLoGin /// 0o777 6906d5e247SLoGin const S_IRWXUGO = Self::S_IRWXU.bits | Self::S_IRWXG.bits | Self::S_IRWXO.bits; 7006d5e247SLoGin /// 0o7777 7106d5e247SLoGin const S_IALLUGO = Self::S_ISUID.bits | Self::S_ISGID.bits | Self::S_ISVTX.bits| Self::S_IRWXUGO.bits; 7206d5e247SLoGin /// 0o444 7306d5e247SLoGin const S_IRUGO = Self::S_IRUSR.bits | Self::S_IRGRP.bits | Self::S_IROTH.bits; 7406d5e247SLoGin /// 0o222 7506d5e247SLoGin const S_IWUGO = Self::S_IWUSR.bits | Self::S_IWGRP.bits | Self::S_IWOTH.bits; 7606d5e247SLoGin /// 0o111 7706d5e247SLoGin const S_IXUGO = Self::S_IXUSR.bits | Self::S_IXGRP.bits | Self::S_IXOTH.bits; 7806d5e247SLoGin 7906d5e247SLoGin 8067b48188Shoumkh } 8167b48188Shoumkh } 8267b48188Shoumkh 8367b48188Shoumkh #[repr(C)] 8467b48188Shoumkh /// # 文件信息结构体 8567b48188Shoumkh pub struct PosixKstat { 8667b48188Shoumkh /// 硬件设备ID 8767b48188Shoumkh dev_id: u64, 8867b48188Shoumkh /// inode号 8967b48188Shoumkh inode: u64, 9067b48188Shoumkh /// 硬链接数 9167b48188Shoumkh nlink: u64, 9267b48188Shoumkh /// 文件权限 9367b48188Shoumkh mode: ModeType, 9467b48188Shoumkh /// 所有者用户ID 9567b48188Shoumkh uid: i32, 9667b48188Shoumkh /// 所有者组ID 9767b48188Shoumkh gid: i32, 9867b48188Shoumkh /// 设备ID 9967b48188Shoumkh rdev: i64, 10067b48188Shoumkh /// 文件大小 10167b48188Shoumkh size: i64, 10267b48188Shoumkh /// 文件系统块大小 10367b48188Shoumkh blcok_size: i64, 10467b48188Shoumkh /// 分配的512B块数 10567b48188Shoumkh blocks: u64, 10667b48188Shoumkh /// 最后访问时间 10767b48188Shoumkh atime: TimeSpec, 10867b48188Shoumkh /// 最后修改时间 10967b48188Shoumkh mtime: TimeSpec, 11067b48188Shoumkh /// 最后状态变化时间 11167b48188Shoumkh ctime: TimeSpec, 11267b48188Shoumkh /// 用于填充结构体大小的空白数据 11367b48188Shoumkh pub _pad: [i8; 24], 11467b48188Shoumkh } 11567b48188Shoumkh impl PosixKstat { 11667b48188Shoumkh fn new() -> Self { 11767b48188Shoumkh Self { 11867b48188Shoumkh inode: 0, 11967b48188Shoumkh dev_id: 0, 12067b48188Shoumkh mode: ModeType { bits: 0 }, 12167b48188Shoumkh nlink: 0, 12267b48188Shoumkh uid: 0, 12367b48188Shoumkh gid: 0, 12467b48188Shoumkh rdev: 0, 12567b48188Shoumkh size: 0, 12667b48188Shoumkh atime: TimeSpec { 12767b48188Shoumkh tv_sec: 0, 12867b48188Shoumkh tv_nsec: 0, 12967b48188Shoumkh }, 13067b48188Shoumkh mtime: TimeSpec { 13167b48188Shoumkh tv_sec: 0, 13267b48188Shoumkh tv_nsec: 0, 13367b48188Shoumkh }, 13467b48188Shoumkh ctime: TimeSpec { 13567b48188Shoumkh tv_sec: 0, 13667b48188Shoumkh tv_nsec: 0, 13767b48188Shoumkh }, 13867b48188Shoumkh blcok_size: 0, 13967b48188Shoumkh blocks: 0, 14067b48188Shoumkh _pad: Default::default(), 14167b48188Shoumkh } 14267b48188Shoumkh } 14367b48188Shoumkh } 144ab5c8ca4Slogin impl Syscall { 145ab5c8ca4Slogin /// @brief 为当前进程打开一个文件 146ab5c8ca4Slogin /// 147ab5c8ca4Slogin /// @param path 文件路径 148ab5c8ca4Slogin /// @param o_flags 打开文件的标志位 149ab5c8ca4Slogin /// 150ab5c8ca4Slogin /// @return 文件描述符编号,或者是错误码 151ab5c8ca4Slogin pub fn open(path: &str, mode: FileMode) -> Result<usize, SystemError> { 1526d81180bSLoGin // kdebug!("open: path: {}, mode: {:?}", path, mode); 1531496ba7bSLoGin 154ab5c8ca4Slogin // 文件名过长 1552dbef785SGnoCiYeH if path.len() > MAX_PATHLEN as usize { 156ab5c8ca4Slogin return Err(SystemError::ENAMETOOLONG); 157ab5c8ca4Slogin } 158ab5c8ca4Slogin 159*a03c4f9dSLoGin let inode: Result<Arc<dyn IndexNode>, SystemError> = 160*a03c4f9dSLoGin ROOT_INODE().lookup_follow_symlink(path, VFS_MAX_FOLLOW_SYMLINK_TIMES); 161ab5c8ca4Slogin 162ab5c8ca4Slogin let inode: Arc<dyn IndexNode> = if inode.is_err() { 163ab5c8ca4Slogin let errno = inode.unwrap_err(); 164ab5c8ca4Slogin // 文件不存在,且需要创建 165ab5c8ca4Slogin if mode.contains(FileMode::O_CREAT) 166ab5c8ca4Slogin && !mode.contains(FileMode::O_DIRECTORY) 167ab5c8ca4Slogin && errno == SystemError::ENOENT 168ab5c8ca4Slogin { 169ab5c8ca4Slogin let (filename, parent_path) = rsplit_path(path); 170ab5c8ca4Slogin // 查找父目录 171ab5c8ca4Slogin let parent_inode: Arc<dyn IndexNode> = 172ab5c8ca4Slogin ROOT_INODE().lookup(parent_path.unwrap_or("/"))?; 173ab5c8ca4Slogin // 创建文件 1746b4e7a29SLoGin let inode: Arc<dyn IndexNode> = parent_inode.create( 1756b4e7a29SLoGin filename, 1766b4e7a29SLoGin FileType::File, 1776b4e7a29SLoGin ModeType::from_bits_truncate(0o755), 1786b4e7a29SLoGin )?; 179ab5c8ca4Slogin inode 180004e86ffSlogin } else { 181ab5c8ca4Slogin // 不需要创建文件,因此返回错误码 182ab5c8ca4Slogin return Err(errno); 183004e86ffSlogin } 184004e86ffSlogin } else { 185ab5c8ca4Slogin inode.unwrap() 186004e86ffSlogin }; 187004e86ffSlogin 188ab5c8ca4Slogin let file_type: FileType = inode.metadata()?.file_type; 189ab5c8ca4Slogin // 如果要打开的是文件夹,而目标不是文件夹 190ab5c8ca4Slogin if mode.contains(FileMode::O_DIRECTORY) && file_type != FileType::Dir { 191ab5c8ca4Slogin return Err(SystemError::ENOTDIR); 192004e86ffSlogin } 193ab5c8ca4Slogin 194ab5c8ca4Slogin // 如果O_TRUNC,并且,打开模式包含O_RDWR或O_WRONLY,清空文件 195ab5c8ca4Slogin if mode.contains(FileMode::O_TRUNC) 196ab5c8ca4Slogin && (mode.contains(FileMode::O_RDWR) || mode.contains(FileMode::O_WRONLY)) 197ab5c8ca4Slogin && file_type == FileType::File 198ab5c8ca4Slogin { 199ab5c8ca4Slogin inode.truncate(0)?; 200ab5c8ca4Slogin } 201ab5c8ca4Slogin 202ab5c8ca4Slogin // 创建文件对象 20334e6d6c8Syuyi2439 204ab5c8ca4Slogin let mut file: File = File::new(inode, mode)?; 205ab5c8ca4Slogin 206ab5c8ca4Slogin // 打开模式为“追加” 207ab5c8ca4Slogin if mode.contains(FileMode::O_APPEND) { 208ab5c8ca4Slogin file.lseek(SeekFrom::SeekEnd(0))?; 209ab5c8ca4Slogin } 210ab5c8ca4Slogin 211ab5c8ca4Slogin // 把文件对象存入pcb 2121496ba7bSLoGin let r = ProcessManager::current_pcb() 2131496ba7bSLoGin .fd_table() 2141496ba7bSLoGin .write() 2151496ba7bSLoGin .alloc_fd(file, None) 2161496ba7bSLoGin .map(|fd| fd as usize); 2171496ba7bSLoGin 2186d81180bSLoGin return r; 219ab5c8ca4Slogin } 220ab5c8ca4Slogin 221ab5c8ca4Slogin /// @brief 关闭文件 222ab5c8ca4Slogin /// 223ab5c8ca4Slogin /// @param fd 文件描述符编号 224ab5c8ca4Slogin /// 225ab5c8ca4Slogin /// @return 成功返回0,失败返回错误码 226ab5c8ca4Slogin pub fn close(fd: usize) -> Result<usize, SystemError> { 2271496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 2281496ba7bSLoGin let mut fd_table_guard = binding.write(); 2291496ba7bSLoGin 23034e6d6c8Syuyi2439 let res = fd_table_guard.drop_fd(fd as i32).map(|_| 0); 23134e6d6c8Syuyi2439 23234e6d6c8Syuyi2439 return res; 233ab5c8ca4Slogin } 234ab5c8ca4Slogin 235ab5c8ca4Slogin /// @brief 根据文件描述符,读取文件数据。尝试读取的数据长度与buf的长度相同。 236ab5c8ca4Slogin /// 237ab5c8ca4Slogin /// @param fd 文件描述符编号 238ab5c8ca4Slogin /// @param buf 输出缓冲区。 239ab5c8ca4Slogin /// 240ab5c8ca4Slogin /// @return Ok(usize) 成功读取的数据的字节数 241ab5c8ca4Slogin /// @return Err(SystemError) 读取失败,返回posix错误码 242ab5c8ca4Slogin pub fn read(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 2431496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 2441496ba7bSLoGin let fd_table_guard = binding.read(); 2451496ba7bSLoGin 2461496ba7bSLoGin let file = fd_table_guard.get_file_by_fd(fd); 247ab5c8ca4Slogin if file.is_none() { 248ab5c8ca4Slogin return Err(SystemError::EBADF); 249ab5c8ca4Slogin } 2501496ba7bSLoGin // drop guard 以避免无法调度的问题 2511496ba7bSLoGin drop(fd_table_guard); 2521496ba7bSLoGin let file = file.unwrap(); 253ab5c8ca4Slogin 2541496ba7bSLoGin return file.lock_no_preempt().read(buf.len(), buf); 255ab5c8ca4Slogin } 256ab5c8ca4Slogin 257ab5c8ca4Slogin /// @brief 根据文件描述符,向文件写入数据。尝试写入的数据长度与buf的长度相同。 258ab5c8ca4Slogin /// 259ab5c8ca4Slogin /// @param fd 文件描述符编号 260ab5c8ca4Slogin /// @param buf 输入缓冲区。 261ab5c8ca4Slogin /// 262ab5c8ca4Slogin /// @return Ok(usize) 成功写入的数据的字节数 263ab5c8ca4Slogin /// @return Err(SystemError) 写入失败,返回posix错误码 264ab5c8ca4Slogin pub fn write(fd: i32, buf: &[u8]) -> Result<usize, SystemError> { 2651496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 2661496ba7bSLoGin let fd_table_guard = binding.read(); 267ab5c8ca4Slogin 2681496ba7bSLoGin let file = fd_table_guard 2691496ba7bSLoGin .get_file_by_fd(fd) 2701496ba7bSLoGin .ok_or(SystemError::EBADF)?; 2711496ba7bSLoGin 2721496ba7bSLoGin // drop guard 以避免无法调度的问题 2731496ba7bSLoGin drop(fd_table_guard); 2741496ba7bSLoGin return file.lock_no_preempt().write(buf.len(), buf); 275ab5c8ca4Slogin } 276ab5c8ca4Slogin 277ab5c8ca4Slogin /// @brief 调整文件操作指针的位置 278ab5c8ca4Slogin /// 279ab5c8ca4Slogin /// @param fd 文件描述符编号 280ab5c8ca4Slogin /// @param seek 调整的方式 281ab5c8ca4Slogin /// 282ab5c8ca4Slogin /// @return Ok(usize) 调整后,文件访问指针相对于文件头部的偏移量 283ab5c8ca4Slogin /// @return Err(SystemError) 调整失败,返回posix错误码 284ab5c8ca4Slogin pub fn lseek(fd: i32, seek: SeekFrom) -> Result<usize, SystemError> { 2851496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 2861496ba7bSLoGin let fd_table_guard = binding.read(); 2871496ba7bSLoGin let file = fd_table_guard 2881496ba7bSLoGin .get_file_by_fd(fd) 2891496ba7bSLoGin .ok_or(SystemError::EBADF)?; 2901496ba7bSLoGin 2911496ba7bSLoGin // drop guard 以避免无法调度的问题 2921496ba7bSLoGin drop(fd_table_guard); 2931496ba7bSLoGin return file.lock_no_preempt().lseek(seek); 294004e86ffSlogin } 295004e86ffSlogin 296004e86ffSlogin /// @brief 切换工作目录 297004e86ffSlogin /// 298004e86ffSlogin /// @param dest_path 目标路径 299004e86ffSlogin /// 300004e86ffSlogin /// @return 返回码 描述 301004e86ffSlogin /// 0 | 成功 302004e86ffSlogin /// 303004e86ffSlogin /// EACCESS | 权限不足 304004e86ffSlogin /// 305004e86ffSlogin /// ELOOP | 解析path时遇到路径循环 306004e86ffSlogin /// 307004e86ffSlogin /// ENAMETOOLONG | 路径名过长 308004e86ffSlogin /// 309004e86ffSlogin /// ENOENT | 目标文件或目录不存在 310004e86ffSlogin /// 311004e86ffSlogin /// ENODIR | 检索期间发现非目录项 312004e86ffSlogin /// 313004e86ffSlogin /// ENOMEM | 系统内存不足 314004e86ffSlogin /// 315004e86ffSlogin /// EFAULT | 错误的地址 316004e86ffSlogin /// 317004e86ffSlogin /// ENAMETOOLONG | 路径过长 318ab5c8ca4Slogin pub fn chdir(dest_path: &str) -> Result<usize, SystemError> { 3191496ba7bSLoGin let proc = ProcessManager::current_pcb(); 320ab5c8ca4Slogin // Copy path to kernel space to avoid some security issues 3211496ba7bSLoGin let path = dest_path.to_string(); 3221496ba7bSLoGin let mut new_path = String::from(""); 3231496ba7bSLoGin if path.len() > 0 { 3241496ba7bSLoGin let cwd = match path.as_bytes()[0] { 3251496ba7bSLoGin b'/' => String::from("/"), 3261496ba7bSLoGin _ => proc.basic().cwd(), 3271496ba7bSLoGin }; 3281496ba7bSLoGin let mut cwd_vec: Vec<_> = cwd.split("/").filter(|&x| x != "").collect(); 3291496ba7bSLoGin let path_split = path.split("/").filter(|&x| x != ""); 3301496ba7bSLoGin for seg in path_split { 3311496ba7bSLoGin if seg == ".." { 3321496ba7bSLoGin cwd_vec.pop(); 3331496ba7bSLoGin } else if seg == "." { 3341496ba7bSLoGin // 当前目录 3351496ba7bSLoGin } else { 3361496ba7bSLoGin cwd_vec.push(seg); 3371496ba7bSLoGin } 3381496ba7bSLoGin } 3391496ba7bSLoGin //proc.basic().set_path(String::from("")); 3401496ba7bSLoGin for seg in cwd_vec { 3411496ba7bSLoGin new_path.push_str("/"); 3421496ba7bSLoGin new_path.push_str(seg); 3431496ba7bSLoGin } 3441496ba7bSLoGin if new_path == "" { 3451496ba7bSLoGin new_path = String::from("/"); 3461496ba7bSLoGin } 3471496ba7bSLoGin } 348*a03c4f9dSLoGin let inode = 349*a03c4f9dSLoGin match ROOT_INODE().lookup_follow_symlink(&new_path, VFS_MAX_FOLLOW_SYMLINK_TIMES) { 350004e86ffSlogin Err(e) => { 351676b8ef6SMork kerror!("Change Directory Failed, Error = {:?}", e); 352ab5c8ca4Slogin return Err(SystemError::ENOENT); 353004e86ffSlogin } 354004e86ffSlogin Ok(i) => i, 355004e86ffSlogin }; 3561496ba7bSLoGin let metadata = inode.metadata()?; 3571496ba7bSLoGin if metadata.file_type == FileType::Dir { 3581496ba7bSLoGin proc.basic_mut().set_cwd(String::from(new_path)); 359ab5c8ca4Slogin return Ok(0); 360004e86ffSlogin } else { 361ab5c8ca4Slogin return Err(SystemError::ENOTDIR); 362004e86ffSlogin } 363004e86ffSlogin } 3641496ba7bSLoGin 3651496ba7bSLoGin /// @brief 获取当前进程的工作目录路径 3661496ba7bSLoGin /// 3671496ba7bSLoGin /// @param buf 指向缓冲区的指针 3681496ba7bSLoGin /// @param size 缓冲区的大小 3691496ba7bSLoGin /// 3701496ba7bSLoGin /// @return 成功,返回的指针指向包含工作目录路径的字符串 3711496ba7bSLoGin /// @return 错误,没有足够的空间 3721496ba7bSLoGin pub fn getcwd(buf: &mut [u8]) -> Result<VirtAddr, SystemError> { 3731496ba7bSLoGin let proc = ProcessManager::current_pcb(); 3741496ba7bSLoGin let cwd = proc.basic().cwd(); 3751496ba7bSLoGin 3761496ba7bSLoGin let cwd_bytes = cwd.as_bytes(); 3771496ba7bSLoGin let cwd_len = cwd_bytes.len(); 3781496ba7bSLoGin if cwd_len + 1 > buf.len() { 3791496ba7bSLoGin return Err(SystemError::ENOMEM); 380004e86ffSlogin } 3811496ba7bSLoGin buf[..cwd_len].copy_from_slice(cwd_bytes); 3821496ba7bSLoGin buf[cwd_len] = 0; 3831496ba7bSLoGin 3841496ba7bSLoGin return Ok(VirtAddr::new(buf.as_ptr() as usize)); 385004e86ffSlogin } 386004e86ffSlogin 387004e86ffSlogin /// @brief 获取目录中的数据 388004e86ffSlogin /// 389ab5c8ca4Slogin /// TODO: 这个函数的语义与Linux不一致,需要修改!!! 390ab5c8ca4Slogin /// 391004e86ffSlogin /// @param fd 文件描述符号 392ab5c8ca4Slogin /// @param buf 输出缓冲区 393ab5c8ca4Slogin /// 394ab5c8ca4Slogin /// @return 成功返回读取的字节数,失败返回错误码 395ab5c8ca4Slogin pub fn getdents(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 396ab5c8ca4Slogin let dirent = 397ab5c8ca4Slogin unsafe { (buf.as_mut_ptr() as *mut Dirent).as_mut() }.ok_or(SystemError::EFAULT)?; 398004e86ffSlogin 399004e86ffSlogin if fd < 0 || fd as u32 > PROC_MAX_FD_NUM { 400ab5c8ca4Slogin return Err(SystemError::EBADF); 401004e86ffSlogin } 402004e86ffSlogin 403004e86ffSlogin // 获取fd 4041496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 4051496ba7bSLoGin let fd_table_guard = binding.read(); 4061496ba7bSLoGin let file = fd_table_guard 4071496ba7bSLoGin .get_file_by_fd(fd) 4081496ba7bSLoGin .ok_or(SystemError::EBADF)?; 409004e86ffSlogin 4101496ba7bSLoGin // drop guard 以避免无法调度的问题 4111496ba7bSLoGin drop(fd_table_guard); 41234e6d6c8Syuyi2439 41334e6d6c8Syuyi2439 let res = file.lock_no_preempt().readdir(dirent).map(|x| x as usize); 41434e6d6c8Syuyi2439 41534e6d6c8Syuyi2439 return res; 416004e86ffSlogin } 417004e86ffSlogin 418004e86ffSlogin /// @brief 创建文件夹 419004e86ffSlogin /// 420004e86ffSlogin /// @param path(r8) 路径 / mode(r9) 模式 421004e86ffSlogin /// 422004e86ffSlogin /// @return uint64_t 负数错误码 / 0表示成功 423ab5c8ca4Slogin pub fn mkdir(path: &str, mode: usize) -> Result<usize, SystemError> { 424ab5c8ca4Slogin return do_mkdir(path, FileMode::from_bits_truncate(mode as u32)).map(|x| x as usize); 425004e86ffSlogin } 426004e86ffSlogin 427ab5c8ca4Slogin /// **删除文件夹、取消文件的链接、删除文件的系统调用** 428004e86ffSlogin /// 429ab5c8ca4Slogin /// ## 参数 430004e86ffSlogin /// 431ab5c8ca4Slogin /// - `dirfd`:文件夹的文件描述符.目前暂未实现 432ab5c8ca4Slogin /// - `pathname`:文件夹的路径 433ab5c8ca4Slogin /// - `flags`:标志位 434004e86ffSlogin /// 435004e86ffSlogin /// 436ab5c8ca4Slogin pub fn unlinkat(_dirfd: i32, pathname: &str, flags: u32) -> Result<usize, SystemError> { 437004e86ffSlogin // kdebug!("sys_unlink_at={path:?}"); 438ab5c8ca4Slogin if (flags & (!AT_REMOVEDIR)) != 0 { 439ab5c8ca4Slogin return Err(SystemError::EINVAL); 440004e86ffSlogin } 441004e86ffSlogin 442ab5c8ca4Slogin if (flags & AT_REMOVEDIR) > 0 { 443004e86ffSlogin // kdebug!("rmdir"); 444ab5c8ca4Slogin match do_remove_dir(&pathname) { 445004e86ffSlogin Err(err) => { 446676b8ef6SMork kerror!("Failed to Remove Directory, Error Code = {:?}", err); 447ab5c8ca4Slogin return Err(err); 448004e86ffSlogin } 449004e86ffSlogin Ok(_) => { 450ab5c8ca4Slogin return Ok(0); 451004e86ffSlogin } 452004e86ffSlogin } 453004e86ffSlogin } 454004e86ffSlogin 455ab5c8ca4Slogin match do_unlink_at(&pathname, FileMode::from_bits_truncate(flags as u32)) { 456004e86ffSlogin Err(err) => { 457676b8ef6SMork kerror!("Failed to Remove Directory, Error Code = {:?}", err); 458ab5c8ca4Slogin return Err(err); 459004e86ffSlogin } 460004e86ffSlogin Ok(_) => { 461ab5c8ca4Slogin return Ok(0); 462004e86ffSlogin } 463004e86ffSlogin } 464004e86ffSlogin } 4652b771e32SGou Ngai 466ab5c8ca4Slogin /// @brief 根据提供的文件描述符的fd,复制对应的文件结构体,并返回新复制的文件结构体对应的fd 467ab5c8ca4Slogin pub fn dup(oldfd: i32) -> Result<usize, SystemError> { 4681496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 4691496ba7bSLoGin let mut fd_table_guard = binding.write(); 470ab5c8ca4Slogin 4711496ba7bSLoGin let old_file = fd_table_guard 4721496ba7bSLoGin .get_file_by_fd(oldfd) 4731496ba7bSLoGin .ok_or(SystemError::EBADF)?; 4741496ba7bSLoGin 4751496ba7bSLoGin let new_file = old_file 4761496ba7bSLoGin .lock_no_preempt() 4771496ba7bSLoGin .try_clone() 4781496ba7bSLoGin .ok_or(SystemError::EBADF)?; 4792b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 4801496ba7bSLoGin let res = fd_table_guard.alloc_fd(new_file, None).map(|x| x as usize); 4812b771e32SGou Ngai return res; 4822b771e32SGou Ngai } 4832b771e32SGou Ngai 484ab5c8ca4Slogin /// 根据提供的文件描述符的fd,和指定新fd,复制对应的文件结构体, 485ab5c8ca4Slogin /// 并返回新复制的文件结构体对应的fd. 486ab5c8ca4Slogin /// 如果新fd已经打开,则会先关闭新fd. 487ab5c8ca4Slogin /// 488ab5c8ca4Slogin /// ## 参数 489ab5c8ca4Slogin /// 490ab5c8ca4Slogin /// - `oldfd`:旧文件描述符 491ab5c8ca4Slogin /// - `newfd`:新文件描述符 492ab5c8ca4Slogin /// 493ab5c8ca4Slogin /// ## 返回值 494ab5c8ca4Slogin /// 495ab5c8ca4Slogin /// - 成功:新文件描述符 496ab5c8ca4Slogin /// - 失败:错误码 497ab5c8ca4Slogin pub fn dup2(oldfd: i32, newfd: i32) -> Result<usize, SystemError> { 4981496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 4991496ba7bSLoGin let mut fd_table_guard = binding.write(); 5001496ba7bSLoGin return Self::do_dup2(oldfd, newfd, &mut fd_table_guard); 5011496ba7bSLoGin } 5021496ba7bSLoGin 5031496ba7bSLoGin fn do_dup2( 5041496ba7bSLoGin oldfd: i32, 5051496ba7bSLoGin newfd: i32, 5061496ba7bSLoGin fd_table_guard: &mut RwLockWriteGuard<'_, FileDescriptorVec>, 5071496ba7bSLoGin ) -> Result<usize, SystemError> { 5082b771e32SGou Ngai // 确认oldfd, newid是否有效 5091496ba7bSLoGin if !(FileDescriptorVec::validate_fd(oldfd) && FileDescriptorVec::validate_fd(newfd)) { 5101496ba7bSLoGin return Err(SystemError::EBADF); 5111496ba7bSLoGin } 5121496ba7bSLoGin 5132b771e32SGou Ngai if oldfd == newfd { 5142b771e32SGou Ngai // 若oldfd与newfd相等 515ab5c8ca4Slogin return Ok(newfd as usize); 5162b771e32SGou Ngai } 5171496ba7bSLoGin let new_exists = fd_table_guard.get_file_by_fd(newfd).is_some(); 5181496ba7bSLoGin if new_exists { 5192b771e32SGou Ngai // close newfd 5201496ba7bSLoGin if let Err(_) = fd_table_guard.drop_fd(newfd) { 5212b771e32SGou Ngai // An I/O error occurred while attempting to close fildes2. 5222b771e32SGou Ngai return Err(SystemError::EIO); 5232b771e32SGou Ngai } 5242b771e32SGou Ngai } 5252b771e32SGou Ngai 5261496ba7bSLoGin let old_file = fd_table_guard 5271496ba7bSLoGin .get_file_by_fd(oldfd) 5281496ba7bSLoGin .ok_or(SystemError::EBADF)?; 5291496ba7bSLoGin let new_file = old_file 5301496ba7bSLoGin .lock_no_preempt() 5311496ba7bSLoGin .try_clone() 5321496ba7bSLoGin .ok_or(SystemError::EBADF)?; 5332b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 5341496ba7bSLoGin let res = fd_table_guard 5351496ba7bSLoGin .alloc_fd(new_file, Some(newfd)) 536ab5c8ca4Slogin .map(|x| x as usize); 5372b771e32SGou Ngai return res; 5382b771e32SGou Ngai } 5396d81180bSLoGin 5406d81180bSLoGin /// # fcntl 5416d81180bSLoGin /// 5426d81180bSLoGin /// ## 参数 5436d81180bSLoGin /// 5446d81180bSLoGin /// - `fd`:文件描述符 5456d81180bSLoGin /// - `cmd`:命令 5466d81180bSLoGin /// - `arg`:参数 5476d81180bSLoGin pub fn fcntl(fd: i32, cmd: FcntlCommand, arg: i32) -> Result<usize, SystemError> { 5486d81180bSLoGin match cmd { 5496d81180bSLoGin FcntlCommand::DupFd => { 5506d81180bSLoGin if arg < 0 || arg as usize >= FileDescriptorVec::PROCESS_MAX_FD { 5516d81180bSLoGin return Err(SystemError::EBADF); 5526d81180bSLoGin } 5536d81180bSLoGin let arg = arg as usize; 5546d81180bSLoGin for i in arg..FileDescriptorVec::PROCESS_MAX_FD { 5551496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5561496ba7bSLoGin let mut fd_table_guard = binding.write(); 5571496ba7bSLoGin if fd_table_guard.get_file_by_fd(fd).is_none() { 5581496ba7bSLoGin return Self::do_dup2(fd, i as i32, &mut fd_table_guard); 5596d81180bSLoGin } 5606d81180bSLoGin } 5616d81180bSLoGin return Err(SystemError::EMFILE); 5626d81180bSLoGin } 5636d81180bSLoGin FcntlCommand::GetFd => { 5646d81180bSLoGin // Get file descriptor flags. 5651496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5661496ba7bSLoGin let fd_table_guard = binding.read(); 5671496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 5681496ba7bSLoGin // drop guard 以避免无法调度的问题 5691496ba7bSLoGin drop(fd_table_guard); 5706d81180bSLoGin 5711496ba7bSLoGin if file.lock().close_on_exec() { 5726d81180bSLoGin return Ok(FD_CLOEXEC as usize); 5736d81180bSLoGin } 5746d81180bSLoGin } 5756d81180bSLoGin return Err(SystemError::EBADF); 5766d81180bSLoGin } 5776d81180bSLoGin FcntlCommand::SetFd => { 5786d81180bSLoGin // Set file descriptor flags. 5791496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5801496ba7bSLoGin let fd_table_guard = binding.write(); 5811496ba7bSLoGin 5821496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 5831496ba7bSLoGin // drop guard 以避免无法调度的问题 5841496ba7bSLoGin drop(fd_table_guard); 5856d81180bSLoGin let arg = arg as u32; 5866d81180bSLoGin if arg & FD_CLOEXEC != 0 { 5871496ba7bSLoGin file.lock().set_close_on_exec(true); 5886d81180bSLoGin } else { 5891496ba7bSLoGin file.lock().set_close_on_exec(false); 5906d81180bSLoGin } 5916d81180bSLoGin return Ok(0); 5926d81180bSLoGin } 5936d81180bSLoGin return Err(SystemError::EBADF); 5946d81180bSLoGin } 5956d81180bSLoGin 5966d81180bSLoGin FcntlCommand::GetFlags => { 5976d81180bSLoGin // Get file status flags. 5981496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5991496ba7bSLoGin let fd_table_guard = binding.read(); 6001496ba7bSLoGin 6011496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 6021496ba7bSLoGin // drop guard 以避免无法调度的问题 6031496ba7bSLoGin drop(fd_table_guard); 6041496ba7bSLoGin return Ok(file.lock_no_preempt().mode().bits() as usize); 6056d81180bSLoGin } 6061496ba7bSLoGin 6076d81180bSLoGin return Err(SystemError::EBADF); 6086d81180bSLoGin } 6096d81180bSLoGin FcntlCommand::SetFlags => { 6106d81180bSLoGin // Set file status flags. 6111496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 6121496ba7bSLoGin let fd_table_guard = binding.write(); 6131496ba7bSLoGin 6141496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 6156d81180bSLoGin let arg = arg as u32; 6166d81180bSLoGin let mode = FileMode::from_bits(arg).ok_or(SystemError::EINVAL)?; 6171496ba7bSLoGin // drop guard 以避免无法调度的问题 6181496ba7bSLoGin drop(fd_table_guard); 6191496ba7bSLoGin file.lock_no_preempt().set_mode(mode)?; 6206d81180bSLoGin return Ok(0); 6216d81180bSLoGin } 6221496ba7bSLoGin 6236d81180bSLoGin return Err(SystemError::EBADF); 6246d81180bSLoGin } 6256d81180bSLoGin _ => { 6266d81180bSLoGin // TODO: unimplemented 6276d81180bSLoGin // 未实现的命令,返回0,不报错。 6286d81180bSLoGin 6296d81180bSLoGin // kwarn!("fcntl: unimplemented command: {:?}, defaults to 0.", cmd); 6306d81180bSLoGin return Ok(0); 6316d81180bSLoGin } 6326d81180bSLoGin } 6336d81180bSLoGin } 6346d81180bSLoGin 6356d81180bSLoGin /// # ftruncate 6366d81180bSLoGin /// 6376d81180bSLoGin /// ## 描述 6386d81180bSLoGin /// 6396d81180bSLoGin /// 改变文件大小. 6406d81180bSLoGin /// 如果文件大小大于原来的大小,那么文件的内容将会被扩展到指定的大小,新的空间将会用0填充. 6416d81180bSLoGin /// 如果文件大小小于原来的大小,那么文件的内容将会被截断到指定的大小. 6426d81180bSLoGin /// 6436d81180bSLoGin /// ## 参数 6446d81180bSLoGin /// 6456d81180bSLoGin /// - `fd`:文件描述符 6466d81180bSLoGin /// - `len`:文件大小 6476d81180bSLoGin /// 6486d81180bSLoGin /// ## 返回值 6496d81180bSLoGin /// 6506d81180bSLoGin /// 如果成功,返回0,否则返回错误码. 6516d81180bSLoGin pub fn ftruncate(fd: i32, len: usize) -> Result<usize, SystemError> { 6521496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 6531496ba7bSLoGin let fd_table_guard = binding.read(); 6541496ba7bSLoGin 6551496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 6561496ba7bSLoGin // drop guard 以避免无法调度的问题 6571496ba7bSLoGin drop(fd_table_guard); 6581496ba7bSLoGin let r = file.lock_no_preempt().ftruncate(len).map(|_| 0); 6596d81180bSLoGin return r; 6606d81180bSLoGin } 6611496ba7bSLoGin 6626d81180bSLoGin return Err(SystemError::EBADF); 6636d81180bSLoGin } 6641496ba7bSLoGin 66567b48188Shoumkh fn do_fstat(fd: i32) -> Result<PosixKstat, SystemError> { 6661496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 6671496ba7bSLoGin let fd_table_guard = binding.read(); 6681496ba7bSLoGin let file = fd_table_guard 6691496ba7bSLoGin .get_file_by_fd(fd) 6701496ba7bSLoGin .ok_or(SystemError::EBADF)?; 6711496ba7bSLoGin // drop guard 以避免无法调度的问题 6721496ba7bSLoGin drop(fd_table_guard); 6731496ba7bSLoGin 67467b48188Shoumkh let mut kstat = PosixKstat::new(); 67567b48188Shoumkh // 获取文件信息 6761496ba7bSLoGin let metadata = file.lock().metadata()?; 67767b48188Shoumkh kstat.size = metadata.size as i64; 67867b48188Shoumkh kstat.dev_id = metadata.dev_id as u64; 6796b4e7a29SLoGin kstat.inode = metadata.inode_id.into() as u64; 68067b48188Shoumkh kstat.blcok_size = metadata.blk_size as i64; 68167b48188Shoumkh kstat.blocks = metadata.blocks as u64; 68267b48188Shoumkh 68367b48188Shoumkh kstat.atime.tv_sec = metadata.atime.tv_sec; 68467b48188Shoumkh kstat.atime.tv_nsec = metadata.atime.tv_nsec; 68567b48188Shoumkh kstat.mtime.tv_sec = metadata.mtime.tv_sec; 68667b48188Shoumkh kstat.mtime.tv_nsec = metadata.mtime.tv_nsec; 68767b48188Shoumkh kstat.ctime.tv_sec = metadata.ctime.tv_sec; 68867b48188Shoumkh kstat.ctime.tv_nsec = metadata.ctime.tv_nsec; 68967b48188Shoumkh 69067b48188Shoumkh kstat.nlink = metadata.nlinks as u64; 69167b48188Shoumkh kstat.uid = metadata.uid as i32; 69267b48188Shoumkh kstat.gid = metadata.gid as i32; 69367b48188Shoumkh kstat.rdev = metadata.raw_dev as i64; 6946b4e7a29SLoGin kstat.mode = metadata.mode; 6951496ba7bSLoGin match file.lock().file_type() { 69667b48188Shoumkh FileType::File => kstat.mode.insert(ModeType::S_IFMT), 69767b48188Shoumkh FileType::Dir => kstat.mode.insert(ModeType::S_IFDIR), 69867b48188Shoumkh FileType::BlockDevice => kstat.mode.insert(ModeType::S_IFBLK), 69967b48188Shoumkh FileType::CharDevice => kstat.mode.insert(ModeType::S_IFCHR), 70067b48188Shoumkh FileType::SymLink => kstat.mode.insert(ModeType::S_IFLNK), 70167b48188Shoumkh FileType::Socket => kstat.mode.insert(ModeType::S_IFSOCK), 70267b48188Shoumkh FileType::Pipe => kstat.mode.insert(ModeType::S_IFIFO), 70367b48188Shoumkh } 70467b48188Shoumkh 70567b48188Shoumkh return Ok(kstat); 70667b48188Shoumkh } 7071496ba7bSLoGin 70867b48188Shoumkh pub fn fstat(fd: i32, usr_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 7091496ba7bSLoGin let kstat = Self::do_fstat(fd)?; 71067b48188Shoumkh if usr_kstat.is_null() { 71167b48188Shoumkh return Err(SystemError::EFAULT); 71267b48188Shoumkh } 71367b48188Shoumkh unsafe { 71467b48188Shoumkh *usr_kstat = kstat; 71567b48188Shoumkh } 71667b48188Shoumkh return Ok(0); 71767b48188Shoumkh } 7182dbef785SGnoCiYeH 7192dbef785SGnoCiYeH pub fn mknod( 7202dbef785SGnoCiYeH path_ptr: *const i8, 7212dbef785SGnoCiYeH mode: ModeType, 7222dbef785SGnoCiYeH dev_t: DeviceNumber, 7232dbef785SGnoCiYeH ) -> Result<usize, SystemError> { 7242dbef785SGnoCiYeH // 安全检验 7252dbef785SGnoCiYeH let len = unsafe { CStr::from_ptr(path_ptr).to_bytes().len() }; 7262dbef785SGnoCiYeH let user_buffer = UserBufferReader::new(path_ptr, len, true)?; 7272dbef785SGnoCiYeH let buf = user_buffer.read_from_user::<u8>(0)?; 7282dbef785SGnoCiYeH let path = core::str::from_utf8(buf).map_err(|_| SystemError::EINVAL)?; 7292dbef785SGnoCiYeH 7302dbef785SGnoCiYeH // 文件名过长 7312dbef785SGnoCiYeH if path.len() > MAX_PATHLEN as usize { 7322dbef785SGnoCiYeH return Err(SystemError::ENAMETOOLONG); 7332dbef785SGnoCiYeH } 7342dbef785SGnoCiYeH 735*a03c4f9dSLoGin let inode: Result<Arc<dyn IndexNode>, SystemError> = 736*a03c4f9dSLoGin ROOT_INODE().lookup_follow_symlink(path, VFS_MAX_FOLLOW_SYMLINK_TIMES); 7372dbef785SGnoCiYeH 7382dbef785SGnoCiYeH if inode.is_ok() { 7392dbef785SGnoCiYeH return Err(SystemError::EEXIST); 7402dbef785SGnoCiYeH } 7412dbef785SGnoCiYeH 7422dbef785SGnoCiYeH let (filename, parent_path) = rsplit_path(path); 7432dbef785SGnoCiYeH 7442dbef785SGnoCiYeH // 查找父目录 745*a03c4f9dSLoGin let parent_inode: Arc<dyn IndexNode> = ROOT_INODE() 746*a03c4f9dSLoGin .lookup_follow_symlink(parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 7472dbef785SGnoCiYeH // 创建nod 7482dbef785SGnoCiYeH parent_inode.mknod(filename, mode, dev_t)?; 7492dbef785SGnoCiYeH 7502dbef785SGnoCiYeH return Ok(0); 7512dbef785SGnoCiYeH } 75267b48188Shoumkh } 753cde5492fSlogin #[repr(C)] 754cde5492fSlogin #[derive(Debug, Clone, Copy)] 755cde5492fSlogin pub struct IoVec { 756cde5492fSlogin /// 缓冲区的起始地址 757cde5492fSlogin pub iov_base: *mut u8, 758cde5492fSlogin /// 缓冲区的长度 759cde5492fSlogin pub iov_len: usize, 760cde5492fSlogin } 761cde5492fSlogin 762cde5492fSlogin /// 用于存储多个来自用户空间的IoVec 763cde5492fSlogin /// 764cde5492fSlogin /// 由于目前内核中的文件系统还不支持分散读写,所以暂时只支持将用户空间的IoVec聚合成一个缓冲区,然后进行操作。 765cde5492fSlogin /// TODO:支持分散读写 766cde5492fSlogin #[derive(Debug)] 767cde5492fSlogin pub struct IoVecs(Vec<&'static mut [u8]>); 768cde5492fSlogin 769cde5492fSlogin impl IoVecs { 770cde5492fSlogin /// 从用户空间的IoVec中构造IoVecs 771cde5492fSlogin /// 772cde5492fSlogin /// @param iov 用户空间的IoVec 773cde5492fSlogin /// @param iovcnt 用户空间的IoVec的数量 774cde5492fSlogin /// @param readv 是否为readv系统调用 775cde5492fSlogin /// 776cde5492fSlogin /// @return 构造成功返回IoVecs,否则返回错误码 777cde5492fSlogin pub unsafe fn from_user( 778cde5492fSlogin iov: *const IoVec, 779cde5492fSlogin iovcnt: usize, 780cde5492fSlogin _readv: bool, 781cde5492fSlogin ) -> Result<Self, SystemError> { 782cde5492fSlogin // 检查iov指针所在空间是否合法 783cde5492fSlogin if !verify_area( 784cde5492fSlogin iov as usize as u64, 785cde5492fSlogin (iovcnt * core::mem::size_of::<IoVec>()) as u64, 786cde5492fSlogin ) { 787cde5492fSlogin return Err(SystemError::EFAULT); 788cde5492fSlogin } 789cde5492fSlogin 790cde5492fSlogin // 将用户空间的IoVec转换为引用(注意:这里的引用是静态的,因为用户空间的IoVec不会被释放) 791cde5492fSlogin let iovs: &[IoVec] = core::slice::from_raw_parts(iov, iovcnt); 792cde5492fSlogin 793cde5492fSlogin let mut slices: Vec<&mut [u8]> = vec![]; 794cde5492fSlogin slices.reserve(iovs.len()); 795cde5492fSlogin 796cde5492fSlogin for iov in iovs.iter() { 797cde5492fSlogin if iov.iov_len == 0 { 798cde5492fSlogin continue; 799cde5492fSlogin } 800cde5492fSlogin 801cde5492fSlogin if !verify_area(iov.iov_base as usize as u64, iov.iov_len as u64) { 802cde5492fSlogin return Err(SystemError::EFAULT); 803cde5492fSlogin } 804cde5492fSlogin 805cde5492fSlogin slices.push(core::slice::from_raw_parts_mut(iov.iov_base, iov.iov_len)); 806cde5492fSlogin } 807cde5492fSlogin 808cde5492fSlogin return Ok(Self(slices)); 809cde5492fSlogin } 810cde5492fSlogin 811cde5492fSlogin /// @brief 将IoVecs中的数据聚合到一个缓冲区中 812cde5492fSlogin /// 813cde5492fSlogin /// @return 返回聚合后的缓冲区 814cde5492fSlogin pub fn gather(&self) -> Vec<u8> { 815cde5492fSlogin let mut buf = Vec::new(); 816cde5492fSlogin for slice in self.0.iter() { 817cde5492fSlogin buf.extend_from_slice(slice); 818cde5492fSlogin } 819cde5492fSlogin return buf; 820cde5492fSlogin } 821cde5492fSlogin 822cde5492fSlogin /// @brief 将给定的数据分散写入到IoVecs中 823cde5492fSlogin pub fn scatter(&mut self, data: &[u8]) { 824cde5492fSlogin let mut data: &[u8] = data; 825cde5492fSlogin for slice in self.0.iter_mut() { 826cde5492fSlogin let len = core::cmp::min(slice.len(), data.len()); 827cde5492fSlogin if len == 0 { 828cde5492fSlogin continue; 829cde5492fSlogin } 830cde5492fSlogin 831cde5492fSlogin slice[..len].copy_from_slice(&data[..len]); 832cde5492fSlogin data = &data[len..]; 833cde5492fSlogin } 834cde5492fSlogin } 835cde5492fSlogin 836cde5492fSlogin /// @brief 创建与IoVecs等长的缓冲区 837cde5492fSlogin /// 838cde5492fSlogin /// @param set_len 是否设置返回的Vec的len。 839cde5492fSlogin /// 如果为true,则返回的Vec的len为所有IoVec的长度之和; 840cde5492fSlogin /// 否则返回的Vec的len为0,capacity为所有IoVec的长度之和. 841cde5492fSlogin /// 842cde5492fSlogin /// @return 返回创建的缓冲区 843cde5492fSlogin pub fn new_buf(&self, set_len: bool) -> Vec<u8> { 844cde5492fSlogin let total_len: usize = self.0.iter().map(|slice| slice.len()).sum(); 845cde5492fSlogin let mut buf: Vec<u8> = Vec::with_capacity(total_len); 846cde5492fSlogin 847cde5492fSlogin if set_len { 8487ae679ddSLoGin buf.resize(total_len, 0); 849cde5492fSlogin } 850cde5492fSlogin return buf; 851cde5492fSlogin } 852cde5492fSlogin } 853