11d37ca6dSDonkey Kane use core::ffi::c_void; 282df0a13Shmt use core::mem::size_of; 32dbef785SGnoCiYeH 482df0a13Shmt use alloc::{string::String, sync::Arc, vec::Vec}; 591e9d4abSLoGin use system_error::SystemError; 6004e86ffSlogin 71d37ca6dSDonkey Kane use crate::producefs; 8004e86ffSlogin use crate::{ 9c566df45SLoGin driver::base::{block::SeekFrom, device::device_number::DeviceNumber}, 101d37ca6dSDonkey Kane filesystem::vfs::{core as Vcore, file::FileDescriptorVec}, 111d37ca6dSDonkey Kane kerror, 121496ba7bSLoGin libs::rwlock::RwLockWriteGuard, 134fda81ceSLoGin mm::{verify_area, VirtAddr}, 141496ba7bSLoGin process::ProcessManager, 15709498caSLoGin syscall::{ 161d37ca6dSDonkey Kane user_access::{self, check_and_clone_cstr, UserBufferWriter}, 1791e9d4abSLoGin Syscall, 18709498caSLoGin }, 1967b48188Shoumkh time::TimeSpec, 20004e86ffSlogin }; 21004e86ffSlogin 22004e86ffSlogin use super::{ 23ab5c8ca4Slogin core::{do_mkdir, do_remove_dir, do_unlink_at}, 24709498caSLoGin fcntl::{AtFlags, FcntlCommand, FD_CLOEXEC}, 25004e86ffSlogin file::{File, FileMode}, 260fb515b0SLoGin open::{do_faccessat, do_fchmodat, do_sys_open}, 279b0abe6dSLoGin utils::{rsplit_path, user_path_at}, 281d37ca6dSDonkey Kane Dirent, FileType, IndexNode, FSMAKER, MAX_PATHLEN, ROOT_INODE, VFS_MAX_FOLLOW_SYMLINK_TIMES, 29004e86ffSlogin }; 3040314b30SXiaoye Zheng // use crate::kdebug; 31004e86ffSlogin 32ab5c8ca4Slogin pub const SEEK_SET: u32 = 0; 33ab5c8ca4Slogin pub const SEEK_CUR: u32 = 1; 34ab5c8ca4Slogin pub const SEEK_END: u32 = 2; 35ab5c8ca4Slogin pub const SEEK_MAX: u32 = 3; 36004e86ffSlogin 3767b48188Shoumkh bitflags! { 3867b48188Shoumkh /// 文件类型和权限 396b4e7a29SLoGin #[repr(C)] 4067b48188Shoumkh pub struct ModeType: u32 { 4167b48188Shoumkh /// 掩码 4267b48188Shoumkh const S_IFMT = 0o0_170_000; 4367b48188Shoumkh /// 文件类型 4467b48188Shoumkh const S_IFSOCK = 0o140000; 4567b48188Shoumkh const S_IFLNK = 0o120000; 4667b48188Shoumkh const S_IFREG = 0o100000; 4767b48188Shoumkh const S_IFBLK = 0o060000; 4867b48188Shoumkh const S_IFDIR = 0o040000; 4967b48188Shoumkh const S_IFCHR = 0o020000; 5067b48188Shoumkh const S_IFIFO = 0o010000; 5167b48188Shoumkh 5267b48188Shoumkh const S_ISUID = 0o004000; 5367b48188Shoumkh const S_ISGID = 0o002000; 5467b48188Shoumkh const S_ISVTX = 0o001000; 5567b48188Shoumkh /// 文件用户权限 5667b48188Shoumkh const S_IRWXU = 0o0700; 5767b48188Shoumkh const S_IRUSR = 0o0400; 5867b48188Shoumkh const S_IWUSR = 0o0200; 5967b48188Shoumkh const S_IXUSR = 0o0100; 6067b48188Shoumkh /// 文件组权限 6167b48188Shoumkh const S_IRWXG = 0o0070; 6267b48188Shoumkh const S_IRGRP = 0o0040; 6367b48188Shoumkh const S_IWGRP = 0o0020; 6467b48188Shoumkh const S_IXGRP = 0o0010; 6567b48188Shoumkh /// 文件其他用户权限 6667b48188Shoumkh const S_IRWXO = 0o0007; 6767b48188Shoumkh const S_IROTH = 0o0004; 6867b48188Shoumkh const S_IWOTH = 0o0002; 6967b48188Shoumkh const S_IXOTH = 0o0001; 7006d5e247SLoGin 7106d5e247SLoGin /// 0o777 7206d5e247SLoGin const S_IRWXUGO = Self::S_IRWXU.bits | Self::S_IRWXG.bits | Self::S_IRWXO.bits; 7306d5e247SLoGin /// 0o7777 7406d5e247SLoGin const S_IALLUGO = Self::S_ISUID.bits | Self::S_ISGID.bits | Self::S_ISVTX.bits| Self::S_IRWXUGO.bits; 7506d5e247SLoGin /// 0o444 7606d5e247SLoGin const S_IRUGO = Self::S_IRUSR.bits | Self::S_IRGRP.bits | Self::S_IROTH.bits; 7706d5e247SLoGin /// 0o222 7806d5e247SLoGin const S_IWUGO = Self::S_IWUSR.bits | Self::S_IWGRP.bits | Self::S_IWOTH.bits; 7906d5e247SLoGin /// 0o111 8006d5e247SLoGin const S_IXUGO = Self::S_IXUSR.bits | Self::S_IXGRP.bits | Self::S_IXOTH.bits; 8106d5e247SLoGin 8206d5e247SLoGin 8367b48188Shoumkh } 8467b48188Shoumkh } 8567b48188Shoumkh 8667b48188Shoumkh #[repr(C)] 8782df0a13Shmt #[derive(Clone, Copy)] 8867b48188Shoumkh /// # 文件信息结构体 8967b48188Shoumkh pub struct PosixKstat { 9067b48188Shoumkh /// 硬件设备ID 9167b48188Shoumkh dev_id: u64, 9267b48188Shoumkh /// inode号 9367b48188Shoumkh inode: u64, 9467b48188Shoumkh /// 硬链接数 9567b48188Shoumkh nlink: u64, 9667b48188Shoumkh /// 文件权限 9767b48188Shoumkh mode: ModeType, 9867b48188Shoumkh /// 所有者用户ID 9967b48188Shoumkh uid: i32, 10067b48188Shoumkh /// 所有者组ID 10167b48188Shoumkh gid: i32, 10267b48188Shoumkh /// 设备ID 10367b48188Shoumkh rdev: i64, 10467b48188Shoumkh /// 文件大小 10567b48188Shoumkh size: i64, 10667b48188Shoumkh /// 文件系统块大小 10767b48188Shoumkh blcok_size: i64, 10867b48188Shoumkh /// 分配的512B块数 10967b48188Shoumkh blocks: u64, 11067b48188Shoumkh /// 最后访问时间 11167b48188Shoumkh atime: TimeSpec, 11267b48188Shoumkh /// 最后修改时间 11367b48188Shoumkh mtime: TimeSpec, 11467b48188Shoumkh /// 最后状态变化时间 11567b48188Shoumkh ctime: TimeSpec, 11667b48188Shoumkh /// 用于填充结构体大小的空白数据 11767b48188Shoumkh pub _pad: [i8; 24], 11867b48188Shoumkh } 11967b48188Shoumkh impl PosixKstat { 12067b48188Shoumkh fn new() -> Self { 12167b48188Shoumkh Self { 12267b48188Shoumkh inode: 0, 12367b48188Shoumkh dev_id: 0, 12467b48188Shoumkh mode: ModeType { bits: 0 }, 12567b48188Shoumkh nlink: 0, 12667b48188Shoumkh uid: 0, 12767b48188Shoumkh gid: 0, 12867b48188Shoumkh rdev: 0, 12967b48188Shoumkh size: 0, 13067b48188Shoumkh atime: TimeSpec { 13167b48188Shoumkh tv_sec: 0, 13267b48188Shoumkh tv_nsec: 0, 13367b48188Shoumkh }, 13467b48188Shoumkh mtime: TimeSpec { 13567b48188Shoumkh tv_sec: 0, 13667b48188Shoumkh tv_nsec: 0, 13767b48188Shoumkh }, 13867b48188Shoumkh ctime: TimeSpec { 13967b48188Shoumkh tv_sec: 0, 14067b48188Shoumkh tv_nsec: 0, 14167b48188Shoumkh }, 14267b48188Shoumkh blcok_size: 0, 14367b48188Shoumkh blocks: 0, 14467b48188Shoumkh _pad: Default::default(), 14567b48188Shoumkh } 14667b48188Shoumkh } 14767b48188Shoumkh } 1480fb515b0SLoGin 149b4eb05a1STTaq #[repr(C)] 150b4eb05a1STTaq #[derive(Clone, Copy)] 151b4eb05a1STTaq /// # 文件信息结构体X 152b4eb05a1STTaq pub struct PosixStatx { 153b4eb05a1STTaq /* 0x00 */ 154b4eb05a1STTaq stx_mask: PosixStatxMask, 155b4eb05a1STTaq /// 文件系统块大小 156b4eb05a1STTaq stx_blksize: u32, 157b4eb05a1STTaq /// Flags conveying information about the file [uncond] 158b4eb05a1STTaq stx_attributes: StxAttributes, 159b4eb05a1STTaq /* 0x10 */ 160b4eb05a1STTaq /// 硬链接数 161b4eb05a1STTaq stx_nlink: u32, 162b4eb05a1STTaq /// 所有者用户ID 163b4eb05a1STTaq stx_uid: u32, 164b4eb05a1STTaq /// 所有者组ID 165b4eb05a1STTaq stx_gid: u32, 166b4eb05a1STTaq /// 文件权限 167b4eb05a1STTaq stx_mode: ModeType, 168b4eb05a1STTaq 169b4eb05a1STTaq /* 0x20 */ 170b4eb05a1STTaq /// inode号 171b4eb05a1STTaq stx_inode: u64, 172b4eb05a1STTaq /// 文件大小 173b4eb05a1STTaq stx_size: i64, 174b4eb05a1STTaq /// 分配的512B块数 175b4eb05a1STTaq stx_blocks: u64, 176b4eb05a1STTaq /// Mask to show what's supported in stx_attributes 177b4eb05a1STTaq stx_attributes_mask: StxAttributes, 178b4eb05a1STTaq 179b4eb05a1STTaq /* 0x40 */ 180b4eb05a1STTaq /// 最后访问时间 181b4eb05a1STTaq stx_atime: TimeSpec, 182b4eb05a1STTaq /// 文件创建时间 183b4eb05a1STTaq stx_btime: TimeSpec, 184b4eb05a1STTaq /// 最后状态变化时间 185b4eb05a1STTaq stx_ctime: TimeSpec, 186b4eb05a1STTaq /// 最后修改时间 187b4eb05a1STTaq stx_mtime: TimeSpec, 188b4eb05a1STTaq 189b4eb05a1STTaq /* 0x80 */ 190b4eb05a1STTaq /// 主设备ID 191b4eb05a1STTaq stx_rdev_major: u32, 192b4eb05a1STTaq /// 次设备ID 193b4eb05a1STTaq stx_rdev_minor: u32, 194b4eb05a1STTaq /// 主硬件设备ID 195b4eb05a1STTaq stx_dev_major: u32, 196b4eb05a1STTaq /// 次硬件设备ID 197b4eb05a1STTaq stx_dev_minor: u32, 198b4eb05a1STTaq 199b4eb05a1STTaq /* 0x90 */ 200b4eb05a1STTaq stx_mnt_id: u64, 201b4eb05a1STTaq stx_dio_mem_align: u32, 202b4eb05a1STTaq stx_dio_offset_align: u32, 203b4eb05a1STTaq } 204b4eb05a1STTaq impl PosixStatx { 205b4eb05a1STTaq fn new() -> Self { 206b4eb05a1STTaq Self { 207b4eb05a1STTaq stx_mask: PosixStatxMask::STATX_BASIC_STATS, 208b4eb05a1STTaq stx_blksize: 0, 209b4eb05a1STTaq stx_attributes: StxAttributes::STATX_ATTR_APPEND, 210b4eb05a1STTaq stx_nlink: 0, 211b4eb05a1STTaq stx_uid: 0, 212b4eb05a1STTaq stx_gid: 0, 213b4eb05a1STTaq stx_mode: ModeType { bits: 0 }, 214b4eb05a1STTaq stx_inode: 0, 215b4eb05a1STTaq stx_size: 0, 216b4eb05a1STTaq stx_blocks: 0, 217b4eb05a1STTaq stx_attributes_mask: StxAttributes::STATX_ATTR_APPEND, 218b4eb05a1STTaq stx_atime: TimeSpec { 219b4eb05a1STTaq tv_sec: 0, 220b4eb05a1STTaq tv_nsec: 0, 221b4eb05a1STTaq }, 222b4eb05a1STTaq stx_btime: TimeSpec { 223b4eb05a1STTaq tv_sec: 0, 224b4eb05a1STTaq tv_nsec: 0, 225b4eb05a1STTaq }, 226b4eb05a1STTaq stx_ctime: TimeSpec { 227b4eb05a1STTaq tv_sec: 0, 228b4eb05a1STTaq tv_nsec: 0, 229b4eb05a1STTaq }, 230b4eb05a1STTaq stx_mtime: TimeSpec { 231b4eb05a1STTaq tv_sec: 0, 232b4eb05a1STTaq tv_nsec: 0, 233b4eb05a1STTaq }, 234b4eb05a1STTaq stx_rdev_major: 0, 235b4eb05a1STTaq stx_rdev_minor: 0, 236b4eb05a1STTaq stx_dev_major: 0, 237b4eb05a1STTaq stx_dev_minor: 0, 238b4eb05a1STTaq stx_mnt_id: 0, 239b4eb05a1STTaq stx_dio_mem_align: 0, 240b4eb05a1STTaq stx_dio_offset_align: 0, 241b4eb05a1STTaq } 242b4eb05a1STTaq } 243b4eb05a1STTaq } 244b4eb05a1STTaq 245b4eb05a1STTaq bitflags! { 246b4eb05a1STTaq pub struct PosixStatxMask: u32{ 247b4eb05a1STTaq /// Want stx_mode & S_IFMT 248b4eb05a1STTaq const STATX_TYPE = 0x00000001; 249b4eb05a1STTaq 250b4eb05a1STTaq /// Want stx_mode & ~S_IFMT 251b4eb05a1STTaq const STATX_MODE = 0x00000002; 252b4eb05a1STTaq 253b4eb05a1STTaq /// Want stx_nlink 254b4eb05a1STTaq const STATX_NLINK = 0x00000004; 255b4eb05a1STTaq 256b4eb05a1STTaq /// Want stx_uid 257b4eb05a1STTaq const STATX_UID = 0x00000008; 258b4eb05a1STTaq 259b4eb05a1STTaq /// Want stx_gid 260b4eb05a1STTaq const STATX_GID = 0x00000010; 261b4eb05a1STTaq 262b4eb05a1STTaq /// Want stx_atime 263b4eb05a1STTaq const STATX_ATIME = 0x00000020; 264b4eb05a1STTaq 265b4eb05a1STTaq /// Want stx_mtime 266b4eb05a1STTaq const STATX_MTIME = 0x00000040; 267b4eb05a1STTaq 268b4eb05a1STTaq /// Want stx_ctime 269b4eb05a1STTaq const STATX_CTIME = 0x00000080; 270b4eb05a1STTaq 271b4eb05a1STTaq /// Want stx_ino 272b4eb05a1STTaq const STATX_INO = 0x00000100; 273b4eb05a1STTaq 274b4eb05a1STTaq /// Want stx_size 275b4eb05a1STTaq const STATX_SIZE = 0x00000200; 276b4eb05a1STTaq 277b4eb05a1STTaq /// Want stx_blocks 278b4eb05a1STTaq const STATX_BLOCKS = 0x00000400; 279b4eb05a1STTaq 280b4eb05a1STTaq /// [All of the above] 281b4eb05a1STTaq const STATX_BASIC_STATS = 0x000007ff; 282b4eb05a1STTaq 283b4eb05a1STTaq /// Want stx_btime 284b4eb05a1STTaq const STATX_BTIME = 0x00000800; 285b4eb05a1STTaq 286b4eb05a1STTaq /// The same as STATX_BASIC_STATS | STATX_BTIME. 287b4eb05a1STTaq /// It is deprecated and should not be used. 288b4eb05a1STTaq const STATX_ALL = 0x00000fff; 289b4eb05a1STTaq 290b4eb05a1STTaq /// Want stx_mnt_id (since Linux 5.8) 291b4eb05a1STTaq const STATX_MNT_ID = 0x00001000; 292b4eb05a1STTaq 293b4eb05a1STTaq /// Want stx_dio_mem_align and stx_dio_offset_align 294b4eb05a1STTaq /// (since Linux 6.1; support varies by filesystem) 295b4eb05a1STTaq const STATX_DIOALIGN = 0x00002000; 296b4eb05a1STTaq 297b4eb05a1STTaq /// Reserved for future struct statx expansion 298b4eb05a1STTaq const STATX_RESERVED = 0x80000000; 299b4eb05a1STTaq } 300b4eb05a1STTaq } 301b4eb05a1STTaq 302b4eb05a1STTaq bitflags! { 303b4eb05a1STTaq pub struct StxAttributes: u64 { 304b4eb05a1STTaq /// 文件被文件系统压缩 305b4eb05a1STTaq const STATX_ATTR_COMPRESSED = 0x00000004; 306b4eb05a1STTaq /// 文件被标记为不可修改 307b4eb05a1STTaq const STATX_ATTR_IMMUTABLE = 0x00000010; 308b4eb05a1STTaq /// 文件是只追加写入的 309b4eb05a1STTaq const STATX_ATTR_APPEND = 0x00000020; 310b4eb05a1STTaq /// 文件不会被备份 311b4eb05a1STTaq const STATX_ATTR_NODUMP = 0x00000040; 312b4eb05a1STTaq /// 文件需要密钥才能在文件系统中解密 313b4eb05a1STTaq const STATX_ATTR_ENCRYPTED = 0x00000800; 314b4eb05a1STTaq /// 目录是自动挂载触发器 315b4eb05a1STTaq const STATX_ATTR_AUTOMOUNT = 0x00001000; 316b4eb05a1STTaq /// 目录是挂载点的根目录 317b4eb05a1STTaq const STATX_ATTR_MOUNT_ROOT = 0x00002000; 318b4eb05a1STTaq /// 文件受到 Verity 保护 319b4eb05a1STTaq const STATX_ATTR_VERITY = 0x00100000; 320b4eb05a1STTaq /// 文件当前处于 DAX 状态 CPU直接访问 321b4eb05a1STTaq const STATX_ATTR_DAX = 0x00200000; 322b4eb05a1STTaq } 323b4eb05a1STTaq } 324b4eb05a1STTaq 3250fb515b0SLoGin /// 3260fb515b0SLoGin /// Arguments for how openat2(2) should open the target path. If only @flags and 3270fb515b0SLoGin /// @mode are non-zero, then openat2(2) operates very similarly to openat(2). 3280fb515b0SLoGin /// 3290fb515b0SLoGin /// However, unlike openat(2), unknown or invalid bits in @flags result in 3300fb515b0SLoGin /// -EINVAL rather than being silently ignored. @mode must be zero unless one of 3310fb515b0SLoGin /// {O_CREAT, O_TMPFILE} are set. 3320fb515b0SLoGin /// 3330fb515b0SLoGin /// ## 成员变量 3340fb515b0SLoGin /// 3350fb515b0SLoGin /// - flags: O_* flags. 3360fb515b0SLoGin /// - mode: O_CREAT/O_TMPFILE file mode. 3370fb515b0SLoGin /// - resolve: RESOLVE_* flags. 3380fb515b0SLoGin #[derive(Debug, Clone, Copy)] 3390fb515b0SLoGin #[repr(C)] 3400fb515b0SLoGin pub struct PosixOpenHow { 3410fb515b0SLoGin pub flags: u64, 3420fb515b0SLoGin pub mode: u64, 3430fb515b0SLoGin pub resolve: u64, 3440fb515b0SLoGin } 3450fb515b0SLoGin 3460fb515b0SLoGin impl PosixOpenHow { 3470fb515b0SLoGin #[allow(dead_code)] 3480fb515b0SLoGin pub fn new(flags: u64, mode: u64, resolve: u64) -> Self { 3490fb515b0SLoGin Self { 3500fb515b0SLoGin flags, 3510fb515b0SLoGin mode, 3520fb515b0SLoGin resolve, 3530fb515b0SLoGin } 3540fb515b0SLoGin } 3550fb515b0SLoGin } 3560fb515b0SLoGin 3570fb515b0SLoGin #[derive(Debug, Clone, Copy)] 3580fb515b0SLoGin pub struct OpenHow { 3590fb515b0SLoGin pub o_flags: FileMode, 3600fb515b0SLoGin pub mode: ModeType, 3610fb515b0SLoGin pub resolve: OpenHowResolve, 3620fb515b0SLoGin } 3630fb515b0SLoGin 3640fb515b0SLoGin impl OpenHow { 3650fb515b0SLoGin pub fn new(mut o_flags: FileMode, mut mode: ModeType, resolve: OpenHowResolve) -> Self { 3660fb515b0SLoGin if !o_flags.contains(FileMode::O_CREAT) { 3670fb515b0SLoGin mode = ModeType::empty(); 3680fb515b0SLoGin } 3690fb515b0SLoGin 3700fb515b0SLoGin if o_flags.contains(FileMode::O_PATH) { 3710fb515b0SLoGin o_flags = o_flags.intersection(FileMode::O_PATH_FLAGS); 3720fb515b0SLoGin } 3730fb515b0SLoGin 3740fb515b0SLoGin Self { 3750fb515b0SLoGin o_flags, 3760fb515b0SLoGin mode, 3770fb515b0SLoGin resolve, 3780fb515b0SLoGin } 3790fb515b0SLoGin } 3800fb515b0SLoGin } 3810fb515b0SLoGin 3820fb515b0SLoGin impl From<PosixOpenHow> for OpenHow { 3830fb515b0SLoGin fn from(posix_open_how: PosixOpenHow) -> Self { 3840fb515b0SLoGin let o_flags = FileMode::from_bits_truncate(posix_open_how.flags as u32); 3850fb515b0SLoGin let mode = ModeType::from_bits_truncate(posix_open_how.mode as u32); 3860fb515b0SLoGin let resolve = OpenHowResolve::from_bits_truncate(posix_open_how.resolve as u64); 3870fb515b0SLoGin return Self::new(o_flags, mode, resolve); 3880fb515b0SLoGin } 3890fb515b0SLoGin } 3900fb515b0SLoGin 3910fb515b0SLoGin bitflags! { 3920fb515b0SLoGin pub struct OpenHowResolve: u64{ 3930fb515b0SLoGin /// Block mount-point crossings 3940fb515b0SLoGin /// (including bind-mounts). 3950fb515b0SLoGin const RESOLVE_NO_XDEV = 0x01; 3960fb515b0SLoGin 3970fb515b0SLoGin /// Block traversal through procfs-style 3980fb515b0SLoGin /// "magic-links" 3990fb515b0SLoGin const RESOLVE_NO_MAGICLINKS = 0x02; 4000fb515b0SLoGin 4010fb515b0SLoGin /// Block traversal through all symlinks 4020fb515b0SLoGin /// (implies OEXT_NO_MAGICLINKS) 4030fb515b0SLoGin const RESOLVE_NO_SYMLINKS = 0x04; 4040fb515b0SLoGin /// Block "lexical" trickery like 4050fb515b0SLoGin /// "..", symlinks, and absolute 4060fb515b0SLoGin const RESOLVE_BENEATH = 0x08; 4070fb515b0SLoGin /// Make all jumps to "/" and ".." 4080fb515b0SLoGin /// be scoped inside the dirfd 4090fb515b0SLoGin /// (similar to chroot(2)). 4100fb515b0SLoGin const RESOLVE_IN_ROOT = 0x10; 4110fb515b0SLoGin // Only complete if resolution can be 4120fb515b0SLoGin // completed through cached lookup. May 4130fb515b0SLoGin // return -EAGAIN if that's not 4140fb515b0SLoGin // possible. 4150fb515b0SLoGin const RESOLVE_CACHED = 0x20; 4160fb515b0SLoGin } 4170fb515b0SLoGin } 418ab5c8ca4Slogin impl Syscall { 419ab5c8ca4Slogin /// @brief 为当前进程打开一个文件 420ab5c8ca4Slogin /// 421ab5c8ca4Slogin /// @param path 文件路径 422ab5c8ca4Slogin /// @param o_flags 打开文件的标志位 423ab5c8ca4Slogin /// 424ab5c8ca4Slogin /// @return 文件描述符编号,或者是错误码 4250fb515b0SLoGin pub fn open( 42682df0a13Shmt path: *const u8, 42782df0a13Shmt o_flags: u32, 42882df0a13Shmt mode: u32, 4290fb515b0SLoGin follow_symlink: bool, 4300fb515b0SLoGin ) -> Result<usize, SystemError> { 43182df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 43282df0a13Shmt let open_flags: FileMode = FileMode::from_bits(o_flags).ok_or(SystemError::EINVAL)?; 43382df0a13Shmt let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?; 43482df0a13Shmt return do_sys_open( 43582df0a13Shmt AtFlags::AT_FDCWD.bits(), 43682df0a13Shmt &path, 43782df0a13Shmt open_flags, 43882df0a13Shmt mode, 43982df0a13Shmt follow_symlink, 44082df0a13Shmt ); 441ab5c8ca4Slogin } 442ab5c8ca4Slogin 4430fb515b0SLoGin pub fn openat( 4440fb515b0SLoGin dirfd: i32, 44582df0a13Shmt path: *const u8, 44682df0a13Shmt o_flags: u32, 44782df0a13Shmt mode: u32, 4480fb515b0SLoGin follow_symlink: bool, 4490fb515b0SLoGin ) -> Result<usize, SystemError> { 45082df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 45182df0a13Shmt let open_flags: FileMode = FileMode::from_bits(o_flags).ok_or(SystemError::EINVAL)?; 45282df0a13Shmt let mode = ModeType::from_bits(mode as u32).ok_or(SystemError::EINVAL)?; 45382df0a13Shmt return do_sys_open(dirfd, &path, open_flags, mode, follow_symlink); 454ab5c8ca4Slogin } 455ab5c8ca4Slogin 456ab5c8ca4Slogin /// @brief 关闭文件 457ab5c8ca4Slogin /// 458ab5c8ca4Slogin /// @param fd 文件描述符编号 459ab5c8ca4Slogin /// 460ab5c8ca4Slogin /// @return 成功返回0,失败返回错误码 461ab5c8ca4Slogin pub fn close(fd: usize) -> Result<usize, SystemError> { 4621496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 4631496ba7bSLoGin let mut fd_table_guard = binding.write(); 4641496ba7bSLoGin 46534e6d6c8Syuyi2439 let res = fd_table_guard.drop_fd(fd as i32).map(|_| 0); 46634e6d6c8Syuyi2439 46734e6d6c8Syuyi2439 return res; 468ab5c8ca4Slogin } 469ab5c8ca4Slogin 47040314b30SXiaoye Zheng /// @brief 发送命令到文件描述符对应的设备, 47140314b30SXiaoye Zheng /// 47240314b30SXiaoye Zheng /// @param fd 文件描述符编号 47340314b30SXiaoye Zheng /// @param cmd 设备相关的请求类型 47440314b30SXiaoye Zheng /// 47540314b30SXiaoye Zheng /// @return Ok(usize) 成功返回0 47640314b30SXiaoye Zheng /// @return Err(SystemError) 读取失败,返回posix错误码 47740314b30SXiaoye Zheng pub fn ioctl(fd: usize, cmd: u32, data: usize) -> Result<usize, SystemError> { 47840314b30SXiaoye Zheng let binding = ProcessManager::current_pcb().fd_table(); 47940314b30SXiaoye Zheng let fd_table_guard = binding.read(); 48040314b30SXiaoye Zheng 48140314b30SXiaoye Zheng let file = fd_table_guard 48240314b30SXiaoye Zheng .get_file_by_fd(fd as i32) 48340314b30SXiaoye Zheng .ok_or(SystemError::EBADF)?; 48440314b30SXiaoye Zheng 48540314b30SXiaoye Zheng // drop guard 以避免无法调度的问题 48640314b30SXiaoye Zheng drop(fd_table_guard); 48752da9a59SGnoCiYeH let file = file.lock_no_preempt(); 48852da9a59SGnoCiYeH let r = file.inode().ioctl(cmd, data, &file.private_data); 48940314b30SXiaoye Zheng return r; 49040314b30SXiaoye Zheng } 49140314b30SXiaoye Zheng 492ab5c8ca4Slogin /// @brief 根据文件描述符,读取文件数据。尝试读取的数据长度与buf的长度相同。 493ab5c8ca4Slogin /// 494ab5c8ca4Slogin /// @param fd 文件描述符编号 49527b967a3S裕依 /// @param buf 输出缓冲区 496ab5c8ca4Slogin /// 497ab5c8ca4Slogin /// @return Ok(usize) 成功读取的数据的字节数 498ab5c8ca4Slogin /// @return Err(SystemError) 读取失败,返回posix错误码 499ab5c8ca4Slogin pub fn read(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 5001496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5011496ba7bSLoGin let fd_table_guard = binding.read(); 5021496ba7bSLoGin 5031496ba7bSLoGin let file = fd_table_guard.get_file_by_fd(fd); 504ab5c8ca4Slogin if file.is_none() { 505ab5c8ca4Slogin return Err(SystemError::EBADF); 506ab5c8ca4Slogin } 5071496ba7bSLoGin // drop guard 以避免无法调度的问题 5081496ba7bSLoGin drop(fd_table_guard); 5091496ba7bSLoGin let file = file.unwrap(); 510ab5c8ca4Slogin 5111496ba7bSLoGin return file.lock_no_preempt().read(buf.len(), buf); 512ab5c8ca4Slogin } 513ab5c8ca4Slogin 514ab5c8ca4Slogin /// @brief 根据文件描述符,向文件写入数据。尝试写入的数据长度与buf的长度相同。 515ab5c8ca4Slogin /// 516ab5c8ca4Slogin /// @param fd 文件描述符编号 51727b967a3S裕依 /// @param buf 输入缓冲区 518ab5c8ca4Slogin /// 519ab5c8ca4Slogin /// @return Ok(usize) 成功写入的数据的字节数 520ab5c8ca4Slogin /// @return Err(SystemError) 写入失败,返回posix错误码 521ab5c8ca4Slogin pub fn write(fd: i32, buf: &[u8]) -> Result<usize, SystemError> { 5221496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5231496ba7bSLoGin let fd_table_guard = binding.read(); 524ab5c8ca4Slogin 5251496ba7bSLoGin let file = fd_table_guard 5261496ba7bSLoGin .get_file_by_fd(fd) 5271496ba7bSLoGin .ok_or(SystemError::EBADF)?; 5281496ba7bSLoGin 5291496ba7bSLoGin // drop guard 以避免无法调度的问题 5301496ba7bSLoGin drop(fd_table_guard); 5311496ba7bSLoGin return file.lock_no_preempt().write(buf.len(), buf); 532ab5c8ca4Slogin } 533ab5c8ca4Slogin 534ab5c8ca4Slogin /// @brief 调整文件操作指针的位置 535ab5c8ca4Slogin /// 536ab5c8ca4Slogin /// @param fd 文件描述符编号 537ab5c8ca4Slogin /// @param seek 调整的方式 538ab5c8ca4Slogin /// 539ab5c8ca4Slogin /// @return Ok(usize) 调整后,文件访问指针相对于文件头部的偏移量 540ab5c8ca4Slogin /// @return Err(SystemError) 调整失败,返回posix错误码 54182df0a13Shmt pub fn lseek(fd: i32, offset: i64, seek: u32) -> Result<usize, SystemError> { 54282df0a13Shmt let seek = match seek { 54382df0a13Shmt SEEK_SET => Ok(SeekFrom::SeekSet(offset)), 54482df0a13Shmt SEEK_CUR => Ok(SeekFrom::SeekCurrent(offset)), 54582df0a13Shmt SEEK_END => Ok(SeekFrom::SeekEnd(offset)), 54682df0a13Shmt SEEK_MAX => Ok(SeekFrom::SeekEnd(0)), 54782df0a13Shmt _ => Err(SystemError::EINVAL), 54882df0a13Shmt }?; 54982df0a13Shmt 5501496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 5511496ba7bSLoGin let fd_table_guard = binding.read(); 5521496ba7bSLoGin let file = fd_table_guard 5531496ba7bSLoGin .get_file_by_fd(fd) 5541496ba7bSLoGin .ok_or(SystemError::EBADF)?; 5551496ba7bSLoGin 5561496ba7bSLoGin // drop guard 以避免无法调度的问题 5571496ba7bSLoGin drop(fd_table_guard); 5581496ba7bSLoGin return file.lock_no_preempt().lseek(seek); 559004e86ffSlogin } 560004e86ffSlogin 56127b967a3S裕依 /// # sys_pread64 系统调用的实际执行函数 56227b967a3S裕依 /// 56327b967a3S裕依 /// ## 参数 56427b967a3S裕依 /// - `fd`: 文件描述符 56527b967a3S裕依 /// - `buf`: 读出缓冲区 56627b967a3S裕依 /// - `len`: 要读取的字节数 56727b967a3S裕依 /// - `offset`: 文件偏移量 56827b967a3S裕依 pub fn pread(fd: i32, buf: &mut [u8], len: usize, offset: usize) -> Result<usize, SystemError> { 56927b967a3S裕依 let binding = ProcessManager::current_pcb().fd_table(); 57027b967a3S裕依 let fd_table_guard = binding.read(); 57127b967a3S裕依 57227b967a3S裕依 let file = fd_table_guard.get_file_by_fd(fd); 57327b967a3S裕依 if file.is_none() { 57427b967a3S裕依 return Err(SystemError::EBADF); 57527b967a3S裕依 } 57627b967a3S裕依 // drop guard 以避免无法调度的问题 57727b967a3S裕依 drop(fd_table_guard); 57827b967a3S裕依 let file = file.unwrap(); 57927b967a3S裕依 58027b967a3S裕依 return file.lock_no_preempt().pread(offset, len, buf); 58127b967a3S裕依 } 58227b967a3S裕依 58327b967a3S裕依 /// # sys_pwrite64 系统调用的实际执行函数 58427b967a3S裕依 /// 58527b967a3S裕依 /// ## 参数 58627b967a3S裕依 /// - `fd`: 文件描述符 58727b967a3S裕依 /// - `buf`: 写入缓冲区 58827b967a3S裕依 /// - `len`: 要写入的字节数 58927b967a3S裕依 /// - `offset`: 文件偏移量 59027b967a3S裕依 pub fn pwrite(fd: i32, buf: &[u8], len: usize, offset: usize) -> Result<usize, SystemError> { 59127b967a3S裕依 let binding = ProcessManager::current_pcb().fd_table(); 59227b967a3S裕依 let fd_table_guard = binding.read(); 59327b967a3S裕依 59427b967a3S裕依 let file = fd_table_guard.get_file_by_fd(fd); 59527b967a3S裕依 if file.is_none() { 59627b967a3S裕依 return Err(SystemError::EBADF); 59727b967a3S裕依 } 59827b967a3S裕依 // drop guard 以避免无法调度的问题 59927b967a3S裕依 drop(fd_table_guard); 60027b967a3S裕依 let file = file.unwrap(); 60127b967a3S裕依 60227b967a3S裕依 return file.lock_no_preempt().pwrite(offset, len, buf); 60327b967a3S裕依 } 60427b967a3S裕依 605004e86ffSlogin /// @brief 切换工作目录 606004e86ffSlogin /// 607004e86ffSlogin /// @param dest_path 目标路径 608004e86ffSlogin /// 609004e86ffSlogin /// @return 返回码 描述 610004e86ffSlogin /// 0 | 成功 611004e86ffSlogin /// 612004e86ffSlogin /// EACCESS | 权限不足 613004e86ffSlogin /// 614004e86ffSlogin /// ELOOP | 解析path时遇到路径循环 615004e86ffSlogin /// 616004e86ffSlogin /// ENAMETOOLONG | 路径名过长 617004e86ffSlogin /// 618004e86ffSlogin /// ENOENT | 目标文件或目录不存在 619004e86ffSlogin /// 620004e86ffSlogin /// ENODIR | 检索期间发现非目录项 621004e86ffSlogin /// 622004e86ffSlogin /// ENOMEM | 系统内存不足 623004e86ffSlogin /// 624004e86ffSlogin /// EFAULT | 错误的地址 625004e86ffSlogin /// 626004e86ffSlogin /// ENAMETOOLONG | 路径过长 62782df0a13Shmt pub fn chdir(path: *const u8) -> Result<usize, SystemError> { 62882df0a13Shmt if path.is_null() { 62982df0a13Shmt return Err(SystemError::EFAULT); 63082df0a13Shmt } 63182df0a13Shmt 63282df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 6331496ba7bSLoGin let proc = ProcessManager::current_pcb(); 634ab5c8ca4Slogin // Copy path to kernel space to avoid some security issues 6351496ba7bSLoGin let mut new_path = String::from(""); 6361496ba7bSLoGin if path.len() > 0 { 6371496ba7bSLoGin let cwd = match path.as_bytes()[0] { 6381496ba7bSLoGin b'/' => String::from("/"), 6391496ba7bSLoGin _ => proc.basic().cwd(), 6401496ba7bSLoGin }; 6411496ba7bSLoGin let mut cwd_vec: Vec<_> = cwd.split("/").filter(|&x| x != "").collect(); 6421496ba7bSLoGin let path_split = path.split("/").filter(|&x| x != ""); 6431496ba7bSLoGin for seg in path_split { 6441496ba7bSLoGin if seg == ".." { 6451496ba7bSLoGin cwd_vec.pop(); 6461496ba7bSLoGin } else if seg == "." { 6471496ba7bSLoGin // 当前目录 6481496ba7bSLoGin } else { 6491496ba7bSLoGin cwd_vec.push(seg); 6501496ba7bSLoGin } 6511496ba7bSLoGin } 6521496ba7bSLoGin //proc.basic().set_path(String::from("")); 6531496ba7bSLoGin for seg in cwd_vec { 6541496ba7bSLoGin new_path.push_str("/"); 6551496ba7bSLoGin new_path.push_str(seg); 6561496ba7bSLoGin } 6571496ba7bSLoGin if new_path == "" { 6581496ba7bSLoGin new_path = String::from("/"); 6591496ba7bSLoGin } 6601496ba7bSLoGin } 661a03c4f9dSLoGin let inode = 662a03c4f9dSLoGin match ROOT_INODE().lookup_follow_symlink(&new_path, VFS_MAX_FOLLOW_SYMLINK_TIMES) { 66382df0a13Shmt Err(_) => { 664ab5c8ca4Slogin return Err(SystemError::ENOENT); 665004e86ffSlogin } 666004e86ffSlogin Ok(i) => i, 667004e86ffSlogin }; 6681496ba7bSLoGin let metadata = inode.metadata()?; 6691496ba7bSLoGin if metadata.file_type == FileType::Dir { 6701496ba7bSLoGin proc.basic_mut().set_cwd(String::from(new_path)); 671ab5c8ca4Slogin return Ok(0); 672004e86ffSlogin } else { 673ab5c8ca4Slogin return Err(SystemError::ENOTDIR); 674004e86ffSlogin } 675004e86ffSlogin } 6761496ba7bSLoGin 6771496ba7bSLoGin /// @brief 获取当前进程的工作目录路径 6781496ba7bSLoGin /// 6791496ba7bSLoGin /// @param buf 指向缓冲区的指针 6801496ba7bSLoGin /// @param size 缓冲区的大小 6811496ba7bSLoGin /// 6821496ba7bSLoGin /// @return 成功,返回的指针指向包含工作目录路径的字符串 6831496ba7bSLoGin /// @return 错误,没有足够的空间 6841496ba7bSLoGin pub fn getcwd(buf: &mut [u8]) -> Result<VirtAddr, SystemError> { 6851496ba7bSLoGin let proc = ProcessManager::current_pcb(); 6861496ba7bSLoGin let cwd = proc.basic().cwd(); 6871496ba7bSLoGin 6881496ba7bSLoGin let cwd_bytes = cwd.as_bytes(); 6891496ba7bSLoGin let cwd_len = cwd_bytes.len(); 6901496ba7bSLoGin if cwd_len + 1 > buf.len() { 6911496ba7bSLoGin return Err(SystemError::ENOMEM); 692004e86ffSlogin } 6931496ba7bSLoGin buf[..cwd_len].copy_from_slice(cwd_bytes); 6941496ba7bSLoGin buf[cwd_len] = 0; 6951496ba7bSLoGin 6961496ba7bSLoGin return Ok(VirtAddr::new(buf.as_ptr() as usize)); 697004e86ffSlogin } 698004e86ffSlogin 699004e86ffSlogin /// @brief 获取目录中的数据 700004e86ffSlogin /// 701ab5c8ca4Slogin /// TODO: 这个函数的语义与Linux不一致,需要修改!!! 702ab5c8ca4Slogin /// 703004e86ffSlogin /// @param fd 文件描述符号 704ab5c8ca4Slogin /// @param buf 输出缓冲区 705ab5c8ca4Slogin /// 706ab5c8ca4Slogin /// @return 成功返回读取的字节数,失败返回错误码 707ab5c8ca4Slogin pub fn getdents(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> { 708ab5c8ca4Slogin let dirent = 709ab5c8ca4Slogin unsafe { (buf.as_mut_ptr() as *mut Dirent).as_mut() }.ok_or(SystemError::EFAULT)?; 710004e86ffSlogin 71146e234aeSLoGin if fd < 0 || fd as usize > FileDescriptorVec::PROCESS_MAX_FD { 712ab5c8ca4Slogin return Err(SystemError::EBADF); 713004e86ffSlogin } 714004e86ffSlogin 715004e86ffSlogin // 获取fd 7161496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 7171496ba7bSLoGin let fd_table_guard = binding.read(); 7181496ba7bSLoGin let file = fd_table_guard 7191496ba7bSLoGin .get_file_by_fd(fd) 7201496ba7bSLoGin .ok_or(SystemError::EBADF)?; 721004e86ffSlogin 7221496ba7bSLoGin // drop guard 以避免无法调度的问题 7231496ba7bSLoGin drop(fd_table_guard); 72434e6d6c8Syuyi2439 72534e6d6c8Syuyi2439 let res = file.lock_no_preempt().readdir(dirent).map(|x| x as usize); 72634e6d6c8Syuyi2439 72734e6d6c8Syuyi2439 return res; 728004e86ffSlogin } 729004e86ffSlogin 730004e86ffSlogin /// @brief 创建文件夹 731004e86ffSlogin /// 732004e86ffSlogin /// @param path(r8) 路径 / mode(r9) 模式 733004e86ffSlogin /// 734004e86ffSlogin /// @return uint64_t 负数错误码 / 0表示成功 73582df0a13Shmt pub fn mkdir(path: *const u8, mode: usize) -> Result<usize, SystemError> { 73682df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 73782df0a13Shmt return do_mkdir(&path, FileMode::from_bits_truncate(mode as u32)).map(|x| x as usize); 738004e86ffSlogin } 739004e86ffSlogin 740*4695947eSChenzx /// **创建硬连接的系统调用** 741*4695947eSChenzx /// 742*4695947eSChenzx /// ## 参数 743*4695947eSChenzx /// 744*4695947eSChenzx /// - 'oldfd': 用于解析源文件路径的文件描述符 745*4695947eSChenzx /// - 'old': 源文件路径 746*4695947eSChenzx /// - 'newfd': 用于解析新文件路径的文件描述符 747*4695947eSChenzx /// - 'new': 新文件将创建的路径 748*4695947eSChenzx /// - 'flags': 标志位,仅以位或方式包含AT_EMPTY_PATH和AT_SYMLINK_FOLLOW 749*4695947eSChenzx /// 750*4695947eSChenzx /// 751*4695947eSChenzx pub fn do_linkat( 752*4695947eSChenzx oldfd: i32, 753*4695947eSChenzx old: &str, 754*4695947eSChenzx newfd: i32, 755*4695947eSChenzx new: &str, 756*4695947eSChenzx flags: AtFlags, 757*4695947eSChenzx ) -> Result<usize, SystemError> { 758*4695947eSChenzx // flag包含其他未规定值时返回EINVAL 759*4695947eSChenzx if !(AtFlags::AT_EMPTY_PATH | AtFlags::AT_SYMLINK_FOLLOW).contains(flags) { 760*4695947eSChenzx return Err(SystemError::EINVAL); 761*4695947eSChenzx } 762*4695947eSChenzx // TODO AT_EMPTY_PATH标志启用时,进行调用者CAP_DAC_READ_SEARCH或相似的检查 763*4695947eSChenzx let symlink_times = if flags.contains(AtFlags::AT_SYMLINK_FOLLOW) { 764*4695947eSChenzx 0 as usize 765*4695947eSChenzx } else { 766*4695947eSChenzx VFS_MAX_FOLLOW_SYMLINK_TIMES 767*4695947eSChenzx }; 768*4695947eSChenzx let pcb = ProcessManager::current_pcb(); 769*4695947eSChenzx 770*4695947eSChenzx // 得到源路径的inode 771*4695947eSChenzx let old_inode: Arc<dyn IndexNode> = if old.is_empty() { 772*4695947eSChenzx if flags.contains(AtFlags::AT_EMPTY_PATH) { 773*4695947eSChenzx // 在AT_EMPTY_PATH启用时,old可以为空,old_inode实际为oldfd所指文件,但该文件不能为目录。 774*4695947eSChenzx let binding = pcb.fd_table(); 775*4695947eSChenzx let fd_table_guard = binding.read(); 776*4695947eSChenzx let file = fd_table_guard 777*4695947eSChenzx .get_file_by_fd(oldfd) 778*4695947eSChenzx .ok_or(SystemError::EBADF)?; 779*4695947eSChenzx let old_inode = file.lock().inode(); 780*4695947eSChenzx old_inode 781*4695947eSChenzx } else { 782*4695947eSChenzx return Err(SystemError::ENONET); 783*4695947eSChenzx } 784*4695947eSChenzx } else { 785*4695947eSChenzx let (old_begin_inode, old_remain_path) = user_path_at(&pcb, oldfd, old)?; 786*4695947eSChenzx old_begin_inode.lookup_follow_symlink(&old_remain_path, symlink_times)? 787*4695947eSChenzx }; 788*4695947eSChenzx 789*4695947eSChenzx // old_inode为目录时返回EPERM 790*4695947eSChenzx if old_inode.metadata().unwrap().file_type == FileType::Dir { 791*4695947eSChenzx return Err(SystemError::EPERM); 792*4695947eSChenzx } 793*4695947eSChenzx 794*4695947eSChenzx // 得到新创建节点的父节点 795*4695947eSChenzx let (new_begin_inode, new_remain_path) = user_path_at(&pcb, newfd, new)?; 796*4695947eSChenzx let (new_name, new_parent_path) = rsplit_path(&new_remain_path); 797*4695947eSChenzx let new_parent = new_begin_inode 798*4695947eSChenzx .lookup_follow_symlink(&new_parent_path.unwrap_or("/"), symlink_times)?; 799*4695947eSChenzx 800*4695947eSChenzx // 被调用者利用downcast_ref判断两inode是否为同一文件系统 801*4695947eSChenzx return new_parent.link(&new_name, &old_inode).map(|_| 0); 802*4695947eSChenzx } 803*4695947eSChenzx 804*4695947eSChenzx pub fn link(old: *const u8, new: *const u8) -> Result<usize, SystemError> { 805*4695947eSChenzx let get_path = |cstr: *const u8| -> Result<String, SystemError> { 806*4695947eSChenzx let res = check_and_clone_cstr(cstr, Some(MAX_PATHLEN))?; 807*4695947eSChenzx if res.len() >= MAX_PATHLEN { 808*4695947eSChenzx return Err(SystemError::ENAMETOOLONG); 809*4695947eSChenzx } 810*4695947eSChenzx if res.is_empty() { 811*4695947eSChenzx return Err(SystemError::ENOENT); 812*4695947eSChenzx } 813*4695947eSChenzx Ok(res) 814*4695947eSChenzx }; 815*4695947eSChenzx let old = get_path(old)?; 816*4695947eSChenzx let new = get_path(new)?; 817*4695947eSChenzx return Self::do_linkat( 818*4695947eSChenzx AtFlags::AT_FDCWD.bits() as i32, 819*4695947eSChenzx &old, 820*4695947eSChenzx AtFlags::AT_FDCWD.bits() as i32, 821*4695947eSChenzx &new, 822*4695947eSChenzx AtFlags::empty(), 823*4695947eSChenzx ); 824*4695947eSChenzx } 825*4695947eSChenzx 826*4695947eSChenzx pub fn linkat( 827*4695947eSChenzx oldfd: i32, 828*4695947eSChenzx old: *const u8, 829*4695947eSChenzx newfd: i32, 830*4695947eSChenzx new: *const u8, 831*4695947eSChenzx flags: i32, 832*4695947eSChenzx ) -> Result<usize, SystemError> { 833*4695947eSChenzx let old = check_and_clone_cstr(old, Some(MAX_PATHLEN))?; 834*4695947eSChenzx let new = check_and_clone_cstr(new, Some(MAX_PATHLEN))?; 835*4695947eSChenzx if old.len() >= MAX_PATHLEN || new.len() >= MAX_PATHLEN { 836*4695947eSChenzx return Err(SystemError::ENAMETOOLONG); 837*4695947eSChenzx } 838*4695947eSChenzx // old 根据flags & AtFlags::AT_EMPTY_PATH判空 839*4695947eSChenzx if new.is_empty() { 840*4695947eSChenzx return Err(SystemError::ENOENT); 841*4695947eSChenzx } 842*4695947eSChenzx let flags = AtFlags::from_bits(flags).ok_or(SystemError::EINVAL)?; 843*4695947eSChenzx Self::do_linkat(oldfd, &old, newfd, &new, flags) 844*4695947eSChenzx } 845*4695947eSChenzx 846ab5c8ca4Slogin /// **删除文件夹、取消文件的链接、删除文件的系统调用** 847004e86ffSlogin /// 848ab5c8ca4Slogin /// ## 参数 849004e86ffSlogin /// 850ab5c8ca4Slogin /// - `dirfd`:文件夹的文件描述符.目前暂未实现 851ab5c8ca4Slogin /// - `pathname`:文件夹的路径 852ab5c8ca4Slogin /// - `flags`:标志位 853004e86ffSlogin /// 854004e86ffSlogin /// 85582df0a13Shmt pub fn unlinkat(dirfd: i32, path: *const u8, flags: u32) -> Result<usize, SystemError> { 856bf4a4899SLoGin let flags = AtFlags::from_bits(flags as i32).ok_or(SystemError::EINVAL)?; 857004e86ffSlogin 85882df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 85982df0a13Shmt 860bf4a4899SLoGin if flags.contains(AtFlags::AT_REMOVEDIR) { 861004e86ffSlogin // kdebug!("rmdir"); 86282df0a13Shmt match do_remove_dir(dirfd, &path) { 863004e86ffSlogin Err(err) => { 864ab5c8ca4Slogin return Err(err); 865004e86ffSlogin } 866004e86ffSlogin Ok(_) => { 867ab5c8ca4Slogin return Ok(0); 868004e86ffSlogin } 869004e86ffSlogin } 870004e86ffSlogin } 871004e86ffSlogin 87282df0a13Shmt match do_unlink_at(dirfd, &path) { 873004e86ffSlogin Err(err) => { 874ab5c8ca4Slogin return Err(err); 875004e86ffSlogin } 876004e86ffSlogin Ok(_) => { 877ab5c8ca4Slogin return Ok(0); 878004e86ffSlogin } 879004e86ffSlogin } 880004e86ffSlogin } 8812b771e32SGou Ngai 88282df0a13Shmt pub fn rmdir(path: *const u8) -> Result<usize, SystemError> { 88382df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 88482df0a13Shmt return do_remove_dir(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize); 8855eeefb8cSChenzx } 8865eeefb8cSChenzx 88782df0a13Shmt pub fn unlink(path: *const u8) -> Result<usize, SystemError> { 88882df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 88982df0a13Shmt return do_unlink_at(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize); 890bf4a4899SLoGin } 891bf4a4899SLoGin 8929e481b3bSTTaq /// # 修改文件名 8939e481b3bSTTaq /// 8949e481b3bSTTaq /// 8959e481b3bSTTaq /// ## 参数 8969e481b3bSTTaq /// 8979e481b3bSTTaq /// - oldfd: 源文件描述符 8989e481b3bSTTaq /// - filename_from: 源文件路径 8999e481b3bSTTaq /// - newfd: 目标文件描述符 9009e481b3bSTTaq /// - filename_to: 目标文件路径 9019e481b3bSTTaq /// - flags: 标志位 9029e481b3bSTTaq /// 9039e481b3bSTTaq /// 9049e481b3bSTTaq /// ## 返回值 9059e481b3bSTTaq /// - Ok(返回值类型): 返回值的说明 9069e481b3bSTTaq /// - Err(错误值类型): 错误的说明 9079e481b3bSTTaq /// 9089e481b3bSTTaq pub fn do_renameat2( 9099e481b3bSTTaq oldfd: i32, 9109e481b3bSTTaq filename_from: *const u8, 9119e481b3bSTTaq newfd: i32, 9129e481b3bSTTaq filename_to: *const u8, 9139e481b3bSTTaq _flags: u32, 9149e481b3bSTTaq ) -> Result<usize, SystemError> { 9159e481b3bSTTaq let filename_from = check_and_clone_cstr(filename_from, Some(MAX_PATHLEN)).unwrap(); 9169e481b3bSTTaq let filename_to = check_and_clone_cstr(filename_to, Some(MAX_PATHLEN)).unwrap(); 9179e481b3bSTTaq // 文件名过长 9189e481b3bSTTaq if filename_from.len() > MAX_PATHLEN as usize || filename_to.len() > MAX_PATHLEN as usize { 9199e481b3bSTTaq return Err(SystemError::ENAMETOOLONG); 9209e481b3bSTTaq } 9219e481b3bSTTaq 9229e481b3bSTTaq //获取pcb,文件节点 9239e481b3bSTTaq let pcb = ProcessManager::current_pcb(); 9249e481b3bSTTaq let (_old_inode_begin, old_remain_path) = user_path_at(&pcb, oldfd, &filename_from)?; 9259e481b3bSTTaq let (_new_inode_begin, new_remain_path) = user_path_at(&pcb, newfd, &filename_to)?; 9269e481b3bSTTaq //获取父目录 9279e481b3bSTTaq let (old_filename, old_parent_path) = rsplit_path(&old_remain_path); 9289e481b3bSTTaq let old_parent_inode = ROOT_INODE() 9299e481b3bSTTaq .lookup_follow_symlink(old_parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 9309e481b3bSTTaq let (new_filename, new_parent_path) = rsplit_path(&new_remain_path); 9319e481b3bSTTaq let new_parent_inode = ROOT_INODE() 9329e481b3bSTTaq .lookup_follow_symlink(new_parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 9339e481b3bSTTaq old_parent_inode.move_to(old_filename, &new_parent_inode, new_filename)?; 9349e481b3bSTTaq return Ok(0); 9359e481b3bSTTaq } 9369e481b3bSTTaq 937ab5c8ca4Slogin /// @brief 根据提供的文件描述符的fd,复制对应的文件结构体,并返回新复制的文件结构体对应的fd 938ab5c8ca4Slogin pub fn dup(oldfd: i32) -> Result<usize, SystemError> { 9391496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 9401496ba7bSLoGin let mut fd_table_guard = binding.write(); 941ab5c8ca4Slogin 9421496ba7bSLoGin let old_file = fd_table_guard 9431496ba7bSLoGin .get_file_by_fd(oldfd) 9441496ba7bSLoGin .ok_or(SystemError::EBADF)?; 9451496ba7bSLoGin 9461496ba7bSLoGin let new_file = old_file 9471496ba7bSLoGin .lock_no_preempt() 9481496ba7bSLoGin .try_clone() 9491496ba7bSLoGin .ok_or(SystemError::EBADF)?; 9502b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 9511496ba7bSLoGin let res = fd_table_guard.alloc_fd(new_file, None).map(|x| x as usize); 9522b771e32SGou Ngai return res; 9532b771e32SGou Ngai } 9542b771e32SGou Ngai 955ab5c8ca4Slogin /// 根据提供的文件描述符的fd,和指定新fd,复制对应的文件结构体, 956ab5c8ca4Slogin /// 并返回新复制的文件结构体对应的fd. 957ab5c8ca4Slogin /// 如果新fd已经打开,则会先关闭新fd. 958ab5c8ca4Slogin /// 959ab5c8ca4Slogin /// ## 参数 960ab5c8ca4Slogin /// 961ab5c8ca4Slogin /// - `oldfd`:旧文件描述符 962ab5c8ca4Slogin /// - `newfd`:新文件描述符 963ab5c8ca4Slogin /// 964ab5c8ca4Slogin /// ## 返回值 965ab5c8ca4Slogin /// 966ab5c8ca4Slogin /// - 成功:新文件描述符 967ab5c8ca4Slogin /// - 失败:错误码 968ab5c8ca4Slogin pub fn dup2(oldfd: i32, newfd: i32) -> Result<usize, SystemError> { 9691496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 9701496ba7bSLoGin let mut fd_table_guard = binding.write(); 9711496ba7bSLoGin return Self::do_dup2(oldfd, newfd, &mut fd_table_guard); 9721496ba7bSLoGin } 9731496ba7bSLoGin 9741496ba7bSLoGin fn do_dup2( 9751496ba7bSLoGin oldfd: i32, 9761496ba7bSLoGin newfd: i32, 9771496ba7bSLoGin fd_table_guard: &mut RwLockWriteGuard<'_, FileDescriptorVec>, 9781496ba7bSLoGin ) -> Result<usize, SystemError> { 9792b771e32SGou Ngai // 确认oldfd, newid是否有效 9801496ba7bSLoGin if !(FileDescriptorVec::validate_fd(oldfd) && FileDescriptorVec::validate_fd(newfd)) { 9811496ba7bSLoGin return Err(SystemError::EBADF); 9821496ba7bSLoGin } 9831496ba7bSLoGin 9842b771e32SGou Ngai if oldfd == newfd { 9852b771e32SGou Ngai // 若oldfd与newfd相等 986ab5c8ca4Slogin return Ok(newfd as usize); 9872b771e32SGou Ngai } 9881496ba7bSLoGin let new_exists = fd_table_guard.get_file_by_fd(newfd).is_some(); 9891496ba7bSLoGin if new_exists { 9902b771e32SGou Ngai // close newfd 9911496ba7bSLoGin if let Err(_) = fd_table_guard.drop_fd(newfd) { 9922b771e32SGou Ngai // An I/O error occurred while attempting to close fildes2. 9932b771e32SGou Ngai return Err(SystemError::EIO); 9942b771e32SGou Ngai } 9952b771e32SGou Ngai } 9962b771e32SGou Ngai 9971496ba7bSLoGin let old_file = fd_table_guard 9981496ba7bSLoGin .get_file_by_fd(oldfd) 9991496ba7bSLoGin .ok_or(SystemError::EBADF)?; 10001496ba7bSLoGin let new_file = old_file 10011496ba7bSLoGin .lock_no_preempt() 10021496ba7bSLoGin .try_clone() 10031496ba7bSLoGin .ok_or(SystemError::EBADF)?; 10042b771e32SGou Ngai // 申请文件描述符,并把文件对象存入其中 10051496ba7bSLoGin let res = fd_table_guard 10061496ba7bSLoGin .alloc_fd(new_file, Some(newfd)) 1007ab5c8ca4Slogin .map(|x| x as usize); 10082b771e32SGou Ngai return res; 10092b771e32SGou Ngai } 10106d81180bSLoGin 10116d81180bSLoGin /// # fcntl 10126d81180bSLoGin /// 10136d81180bSLoGin /// ## 参数 10146d81180bSLoGin /// 10156d81180bSLoGin /// - `fd`:文件描述符 10166d81180bSLoGin /// - `cmd`:命令 10176d81180bSLoGin /// - `arg`:参数 10186d81180bSLoGin pub fn fcntl(fd: i32, cmd: FcntlCommand, arg: i32) -> Result<usize, SystemError> { 10196d81180bSLoGin match cmd { 10206d81180bSLoGin FcntlCommand::DupFd => { 10216d81180bSLoGin if arg < 0 || arg as usize >= FileDescriptorVec::PROCESS_MAX_FD { 10226d81180bSLoGin return Err(SystemError::EBADF); 10236d81180bSLoGin } 10246d81180bSLoGin let arg = arg as usize; 10256d81180bSLoGin for i in arg..FileDescriptorVec::PROCESS_MAX_FD { 10261496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10271496ba7bSLoGin let mut fd_table_guard = binding.write(); 102852da9a59SGnoCiYeH if fd_table_guard.get_file_by_fd(i as i32).is_none() { 10291496ba7bSLoGin return Self::do_dup2(fd, i as i32, &mut fd_table_guard); 10306d81180bSLoGin } 10316d81180bSLoGin } 10326d81180bSLoGin return Err(SystemError::EMFILE); 10336d81180bSLoGin } 10346d81180bSLoGin FcntlCommand::GetFd => { 10356d81180bSLoGin // Get file descriptor flags. 10361496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10371496ba7bSLoGin let fd_table_guard = binding.read(); 10381496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 10391496ba7bSLoGin // drop guard 以避免无法调度的问题 10401496ba7bSLoGin drop(fd_table_guard); 10416d81180bSLoGin 10421496ba7bSLoGin if file.lock().close_on_exec() { 10436d81180bSLoGin return Ok(FD_CLOEXEC as usize); 10446d81180bSLoGin } 10456d81180bSLoGin } 10466d81180bSLoGin return Err(SystemError::EBADF); 10476d81180bSLoGin } 10486d81180bSLoGin FcntlCommand::SetFd => { 10496d81180bSLoGin // Set file descriptor flags. 10501496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10511496ba7bSLoGin let fd_table_guard = binding.write(); 10521496ba7bSLoGin 10531496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 10541496ba7bSLoGin // drop guard 以避免无法调度的问题 10551496ba7bSLoGin drop(fd_table_guard); 10566d81180bSLoGin let arg = arg as u32; 10576d81180bSLoGin if arg & FD_CLOEXEC != 0 { 10581496ba7bSLoGin file.lock().set_close_on_exec(true); 10596d81180bSLoGin } else { 10601496ba7bSLoGin file.lock().set_close_on_exec(false); 10616d81180bSLoGin } 10626d81180bSLoGin return Ok(0); 10636d81180bSLoGin } 10646d81180bSLoGin return Err(SystemError::EBADF); 10656d81180bSLoGin } 10666d81180bSLoGin 10676d81180bSLoGin FcntlCommand::GetFlags => { 10686d81180bSLoGin // Get file status flags. 10691496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10701496ba7bSLoGin let fd_table_guard = binding.read(); 10711496ba7bSLoGin 10721496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 10731496ba7bSLoGin // drop guard 以避免无法调度的问题 10741496ba7bSLoGin drop(fd_table_guard); 10751496ba7bSLoGin return Ok(file.lock_no_preempt().mode().bits() as usize); 10766d81180bSLoGin } 10771496ba7bSLoGin 10786d81180bSLoGin return Err(SystemError::EBADF); 10796d81180bSLoGin } 10806d81180bSLoGin FcntlCommand::SetFlags => { 10816d81180bSLoGin // Set file status flags. 10821496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 10831496ba7bSLoGin let fd_table_guard = binding.write(); 10841496ba7bSLoGin 10851496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 10866d81180bSLoGin let arg = arg as u32; 10876d81180bSLoGin let mode = FileMode::from_bits(arg).ok_or(SystemError::EINVAL)?; 10881496ba7bSLoGin // drop guard 以避免无法调度的问题 10891496ba7bSLoGin drop(fd_table_guard); 10901496ba7bSLoGin file.lock_no_preempt().set_mode(mode)?; 10916d81180bSLoGin return Ok(0); 10926d81180bSLoGin } 10931496ba7bSLoGin 10946d81180bSLoGin return Err(SystemError::EBADF); 10956d81180bSLoGin } 10966d81180bSLoGin _ => { 10976d81180bSLoGin // TODO: unimplemented 10986d81180bSLoGin // 未实现的命令,返回0,不报错。 10996d81180bSLoGin 11006d81180bSLoGin // kwarn!("fcntl: unimplemented command: {:?}, defaults to 0.", cmd); 11016d81180bSLoGin return Ok(0); 11026d81180bSLoGin } 11036d81180bSLoGin } 11046d81180bSLoGin } 11056d81180bSLoGin 11066d81180bSLoGin /// # ftruncate 11076d81180bSLoGin /// 11086d81180bSLoGin /// ## 描述 11096d81180bSLoGin /// 11106d81180bSLoGin /// 改变文件大小. 11116d81180bSLoGin /// 如果文件大小大于原来的大小,那么文件的内容将会被扩展到指定的大小,新的空间将会用0填充. 11126d81180bSLoGin /// 如果文件大小小于原来的大小,那么文件的内容将会被截断到指定的大小. 11136d81180bSLoGin /// 11146d81180bSLoGin /// ## 参数 11156d81180bSLoGin /// 11166d81180bSLoGin /// - `fd`:文件描述符 11176d81180bSLoGin /// - `len`:文件大小 11186d81180bSLoGin /// 11196d81180bSLoGin /// ## 返回值 11206d81180bSLoGin /// 11216d81180bSLoGin /// 如果成功,返回0,否则返回错误码. 11226d81180bSLoGin pub fn ftruncate(fd: i32, len: usize) -> Result<usize, SystemError> { 11231496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11241496ba7bSLoGin let fd_table_guard = binding.read(); 11251496ba7bSLoGin 11261496ba7bSLoGin if let Some(file) = fd_table_guard.get_file_by_fd(fd) { 11271496ba7bSLoGin // drop guard 以避免无法调度的问题 11281496ba7bSLoGin drop(fd_table_guard); 11291496ba7bSLoGin let r = file.lock_no_preempt().ftruncate(len).map(|_| 0); 11306d81180bSLoGin return r; 11316d81180bSLoGin } 11321496ba7bSLoGin 11336d81180bSLoGin return Err(SystemError::EBADF); 11346d81180bSLoGin } 11351496ba7bSLoGin 113667b48188Shoumkh fn do_fstat(fd: i32) -> Result<PosixKstat, SystemError> { 11371496ba7bSLoGin let binding = ProcessManager::current_pcb().fd_table(); 11381496ba7bSLoGin let fd_table_guard = binding.read(); 11391496ba7bSLoGin let file = fd_table_guard 11401496ba7bSLoGin .get_file_by_fd(fd) 11411496ba7bSLoGin .ok_or(SystemError::EBADF)?; 11421496ba7bSLoGin // drop guard 以避免无法调度的问题 11431496ba7bSLoGin drop(fd_table_guard); 11441496ba7bSLoGin 114567b48188Shoumkh let mut kstat = PosixKstat::new(); 114667b48188Shoumkh // 获取文件信息 11471496ba7bSLoGin let metadata = file.lock().metadata()?; 114867b48188Shoumkh kstat.size = metadata.size as i64; 114967b48188Shoumkh kstat.dev_id = metadata.dev_id as u64; 11506b4e7a29SLoGin kstat.inode = metadata.inode_id.into() as u64; 115167b48188Shoumkh kstat.blcok_size = metadata.blk_size as i64; 115267b48188Shoumkh kstat.blocks = metadata.blocks as u64; 115367b48188Shoumkh 115467b48188Shoumkh kstat.atime.tv_sec = metadata.atime.tv_sec; 115567b48188Shoumkh kstat.atime.tv_nsec = metadata.atime.tv_nsec; 115667b48188Shoumkh kstat.mtime.tv_sec = metadata.mtime.tv_sec; 115767b48188Shoumkh kstat.mtime.tv_nsec = metadata.mtime.tv_nsec; 115867b48188Shoumkh kstat.ctime.tv_sec = metadata.ctime.tv_sec; 115967b48188Shoumkh kstat.ctime.tv_nsec = metadata.ctime.tv_nsec; 116067b48188Shoumkh 116167b48188Shoumkh kstat.nlink = metadata.nlinks as u64; 116267b48188Shoumkh kstat.uid = metadata.uid as i32; 116367b48188Shoumkh kstat.gid = metadata.gid as i32; 116402343d0bSLoGin kstat.rdev = metadata.raw_dev.data() as i64; 11656b4e7a29SLoGin kstat.mode = metadata.mode; 11661496ba7bSLoGin match file.lock().file_type() { 11677eda31b2SLoGin FileType::File => kstat.mode.insert(ModeType::S_IFREG), 116867b48188Shoumkh FileType::Dir => kstat.mode.insert(ModeType::S_IFDIR), 116967b48188Shoumkh FileType::BlockDevice => kstat.mode.insert(ModeType::S_IFBLK), 117067b48188Shoumkh FileType::CharDevice => kstat.mode.insert(ModeType::S_IFCHR), 117167b48188Shoumkh FileType::SymLink => kstat.mode.insert(ModeType::S_IFLNK), 117267b48188Shoumkh FileType::Socket => kstat.mode.insert(ModeType::S_IFSOCK), 117367b48188Shoumkh FileType::Pipe => kstat.mode.insert(ModeType::S_IFIFO), 117440314b30SXiaoye Zheng FileType::KvmDevice => kstat.mode.insert(ModeType::S_IFCHR), 117502343d0bSLoGin FileType::FramebufferDevice => kstat.mode.insert(ModeType::S_IFCHR), 117667b48188Shoumkh } 117767b48188Shoumkh 117867b48188Shoumkh return Ok(kstat); 117967b48188Shoumkh } 11801496ba7bSLoGin 118167b48188Shoumkh pub fn fstat(fd: i32, usr_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 118282df0a13Shmt let mut writer = UserBufferWriter::new(usr_kstat, size_of::<PosixKstat>(), true)?; 11831496ba7bSLoGin let kstat = Self::do_fstat(fd)?; 118482df0a13Shmt 118582df0a13Shmt writer.copy_one_to_user(&kstat, 0)?; 118667b48188Shoumkh return Ok(0); 118767b48188Shoumkh } 11882dbef785SGnoCiYeH 118982df0a13Shmt pub fn stat(path: *const u8, user_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 119082df0a13Shmt let fd = Self::open( 119182df0a13Shmt path, 119282df0a13Shmt FileMode::O_RDONLY.bits(), 119382df0a13Shmt ModeType::empty().bits(), 119482df0a13Shmt true, 119582df0a13Shmt )?; 1196bf4a4899SLoGin let r = Self::fstat(fd as i32, user_kstat); 1197971462beSGnoCiYeH Self::close(fd).ok(); 1198bf4a4899SLoGin return r; 1199bf4a4899SLoGin } 1200bf4a4899SLoGin 120182df0a13Shmt pub fn lstat(path: *const u8, user_kstat: *mut PosixKstat) -> Result<usize, SystemError> { 120282df0a13Shmt let fd = Self::open( 120382df0a13Shmt path, 120482df0a13Shmt FileMode::O_RDONLY.bits(), 120582df0a13Shmt ModeType::empty().bits(), 120682df0a13Shmt false, 120782df0a13Shmt )?; 1208bf4a4899SLoGin let r = Self::fstat(fd as i32, user_kstat); 1209bf4a4899SLoGin Self::close(fd).ok(); 1210bf4a4899SLoGin return r; 1211971462beSGnoCiYeH } 1212971462beSGnoCiYeH 1213b4eb05a1STTaq pub fn do_statx( 1214b4eb05a1STTaq fd: i32, 1215b4eb05a1STTaq path: *const u8, 1216b4eb05a1STTaq flags: u32, 1217b4eb05a1STTaq mask: u32, 1218b4eb05a1STTaq usr_kstat: *mut PosixStatx, 1219b4eb05a1STTaq ) -> Result<usize, SystemError> { 1220b4eb05a1STTaq if usr_kstat.is_null() { 1221b4eb05a1STTaq return Err(SystemError::EFAULT); 1222b4eb05a1STTaq } 1223b4eb05a1STTaq 1224b4eb05a1STTaq let mask = PosixStatxMask::from_bits_truncate(mask); 1225b4eb05a1STTaq 1226b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_RESERVED) { 1227b4eb05a1STTaq return Err(SystemError::ENAVAIL); 1228b4eb05a1STTaq } 1229b4eb05a1STTaq 1230b4eb05a1STTaq let flags = FileMode::from_bits_truncate(flags); 1231b4eb05a1STTaq let ofd = Self::open(path, flags.bits(), ModeType::empty().bits, true)?; 1232b4eb05a1STTaq 1233b4eb05a1STTaq let binding = ProcessManager::current_pcb().fd_table(); 1234b4eb05a1STTaq let fd_table_guard = binding.read(); 1235b4eb05a1STTaq let file = fd_table_guard 1236b4eb05a1STTaq .get_file_by_fd(ofd as i32) 1237b4eb05a1STTaq .ok_or(SystemError::EBADF)?; 1238b4eb05a1STTaq // drop guard 以避免无法调度的问题 1239b4eb05a1STTaq drop(fd_table_guard); 1240b4eb05a1STTaq let mut writer = UserBufferWriter::new(usr_kstat, size_of::<PosixStatx>(), true)?; 1241b4eb05a1STTaq let mut tmp: PosixStatx = PosixStatx::new(); 1242b4eb05a1STTaq // 获取文件信息 1243b4eb05a1STTaq let metadata = file.lock().metadata()?; 1244b4eb05a1STTaq 1245b4eb05a1STTaq tmp.stx_mask |= PosixStatxMask::STATX_BASIC_STATS; 1246b4eb05a1STTaq tmp.stx_blksize = metadata.blk_size as u32; 1247b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MODE) || mask.contains(PosixStatxMask::STATX_TYPE) { 1248b4eb05a1STTaq tmp.stx_mode = metadata.mode; 1249b4eb05a1STTaq } 1250b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_NLINK) { 1251b4eb05a1STTaq tmp.stx_nlink = metadata.nlinks as u32; 1252b4eb05a1STTaq } 1253b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_UID) { 1254b4eb05a1STTaq tmp.stx_uid = metadata.uid as u32; 1255b4eb05a1STTaq } 1256b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_GID) { 1257b4eb05a1STTaq tmp.stx_gid = metadata.gid as u32; 1258b4eb05a1STTaq } 1259b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_ATIME) { 1260b4eb05a1STTaq tmp.stx_atime.tv_sec = metadata.atime.tv_sec; 1261b4eb05a1STTaq tmp.stx_atime.tv_nsec = metadata.atime.tv_nsec; 1262b4eb05a1STTaq } 1263b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MTIME) { 1264b4eb05a1STTaq tmp.stx_mtime.tv_sec = metadata.ctime.tv_sec; 1265b4eb05a1STTaq tmp.stx_mtime.tv_nsec = metadata.ctime.tv_nsec; 1266b4eb05a1STTaq } 1267b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_CTIME) { 1268b4eb05a1STTaq // ctime是文件上次修改状态的时间 1269b4eb05a1STTaq tmp.stx_ctime.tv_sec = metadata.mtime.tv_sec; 1270b4eb05a1STTaq tmp.stx_ctime.tv_nsec = metadata.mtime.tv_nsec; 1271b4eb05a1STTaq } 1272b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_INO) { 1273b4eb05a1STTaq tmp.stx_inode = metadata.inode_id.into() as u64; 1274b4eb05a1STTaq } 1275b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_SIZE) { 1276b4eb05a1STTaq tmp.stx_size = metadata.size; 1277b4eb05a1STTaq } 1278b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_BLOCKS) { 1279b4eb05a1STTaq tmp.stx_blocks = metadata.blocks as u64; 1280b4eb05a1STTaq } 1281b4eb05a1STTaq 1282b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_BTIME) { 1283b4eb05a1STTaq // btime是文件创建时间 1284b4eb05a1STTaq tmp.stx_btime.tv_sec = metadata.ctime.tv_sec; 1285b4eb05a1STTaq tmp.stx_btime.tv_nsec = metadata.ctime.tv_nsec; 1286b4eb05a1STTaq } 1287b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_ALL) { 1288b4eb05a1STTaq tmp.stx_attributes = StxAttributes::STATX_ATTR_APPEND; 1289b4eb05a1STTaq tmp.stx_attributes_mask |= 1290b4eb05a1STTaq StxAttributes::STATX_ATTR_AUTOMOUNT | StxAttributes::STATX_ATTR_DAX; 1291b4eb05a1STTaq tmp.stx_dev_major = metadata.dev_id as u32; 1292b4eb05a1STTaq tmp.stx_dev_minor = metadata.dev_id as u32; // 1293b4eb05a1STTaq tmp.stx_rdev_major = metadata.raw_dev.data() as u32; 1294b4eb05a1STTaq tmp.stx_rdev_minor = metadata.raw_dev.data() as u32; 1295b4eb05a1STTaq } 1296b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_MNT_ID) { 1297b4eb05a1STTaq tmp.stx_mnt_id = 0; 1298b4eb05a1STTaq } 1299b4eb05a1STTaq if mask.contains(PosixStatxMask::STATX_DIOALIGN) { 1300b4eb05a1STTaq tmp.stx_dio_mem_align = 0; 1301b4eb05a1STTaq tmp.stx_dio_offset_align = 0; 1302b4eb05a1STTaq } 1303b4eb05a1STTaq 1304b4eb05a1STTaq match file.lock().file_type() { 1305b4eb05a1STTaq FileType::File => tmp.stx_mode.insert(ModeType::S_IFREG), 1306b4eb05a1STTaq FileType::Dir => tmp.stx_mode.insert(ModeType::S_IFDIR), 1307b4eb05a1STTaq FileType::BlockDevice => tmp.stx_mode.insert(ModeType::S_IFBLK), 1308b4eb05a1STTaq FileType::CharDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1309b4eb05a1STTaq FileType::SymLink => tmp.stx_mode.insert(ModeType::S_IFLNK), 1310b4eb05a1STTaq FileType::Socket => tmp.stx_mode.insert(ModeType::S_IFSOCK), 1311b4eb05a1STTaq FileType::Pipe => tmp.stx_mode.insert(ModeType::S_IFIFO), 1312b4eb05a1STTaq FileType::KvmDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1313b4eb05a1STTaq FileType::FramebufferDevice => tmp.stx_mode.insert(ModeType::S_IFCHR), 1314b4eb05a1STTaq } 1315b4eb05a1STTaq 1316b4eb05a1STTaq writer.copy_one_to_user(&tmp, 0)?; 1317b4eb05a1STTaq Self::close(fd as usize).ok(); 1318b4eb05a1STTaq return Ok(0); 1319b4eb05a1STTaq } 1320b4eb05a1STTaq 13212dbef785SGnoCiYeH pub fn mknod( 132282df0a13Shmt path: *const u8, 13232dbef785SGnoCiYeH mode: ModeType, 13242dbef785SGnoCiYeH dev_t: DeviceNumber, 13252dbef785SGnoCiYeH ) -> Result<usize, SystemError> { 132682df0a13Shmt let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 132782df0a13Shmt let path = path.as_str().trim(); 13282dbef785SGnoCiYeH 1329a03c4f9dSLoGin let inode: Result<Arc<dyn IndexNode>, SystemError> = 1330a03c4f9dSLoGin ROOT_INODE().lookup_follow_symlink(path, VFS_MAX_FOLLOW_SYMLINK_TIMES); 13312dbef785SGnoCiYeH 13322dbef785SGnoCiYeH if inode.is_ok() { 13332dbef785SGnoCiYeH return Err(SystemError::EEXIST); 13342dbef785SGnoCiYeH } 13352dbef785SGnoCiYeH 13362dbef785SGnoCiYeH let (filename, parent_path) = rsplit_path(path); 13372dbef785SGnoCiYeH 13382dbef785SGnoCiYeH // 查找父目录 1339a03c4f9dSLoGin let parent_inode: Arc<dyn IndexNode> = ROOT_INODE() 1340a03c4f9dSLoGin .lookup_follow_symlink(parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 13412dbef785SGnoCiYeH // 创建nod 13422dbef785SGnoCiYeH parent_inode.mknod(filename, mode, dev_t)?; 13432dbef785SGnoCiYeH 13442dbef785SGnoCiYeH return Ok(0); 13452dbef785SGnoCiYeH } 1346971462beSGnoCiYeH 1347971462beSGnoCiYeH pub fn writev(fd: i32, iov: usize, count: usize) -> Result<usize, SystemError> { 1348971462beSGnoCiYeH // IoVecs会进行用户态检验 1349971462beSGnoCiYeH let iovecs = unsafe { IoVecs::from_user(iov as *const IoVec, count, false) }?; 1350971462beSGnoCiYeH 1351971462beSGnoCiYeH let data = iovecs.gather(); 1352971462beSGnoCiYeH 1353971462beSGnoCiYeH Self::write(fd, &data) 1354971462beSGnoCiYeH } 1355709498caSLoGin 1356bf4a4899SLoGin pub fn readv(fd: i32, iov: usize, count: usize) -> Result<usize, SystemError> { 1357bf4a4899SLoGin // IoVecs会进行用户态检验 1358bf4a4899SLoGin let mut iovecs = unsafe { IoVecs::from_user(iov as *const IoVec, count, true) }?; 1359bf4a4899SLoGin 1360bf4a4899SLoGin let mut data = Vec::new(); 1361bf4a4899SLoGin data.resize(iovecs.0.iter().map(|x| x.len()).sum(), 0); 1362bf4a4899SLoGin 1363bf4a4899SLoGin let len = Self::read(fd, &mut data)?; 1364bf4a4899SLoGin 1365bf4a4899SLoGin iovecs.scatter(&data[..len]); 1366bf4a4899SLoGin 1367bf4a4899SLoGin return Ok(len); 1368bf4a4899SLoGin } 1369bf4a4899SLoGin 1370709498caSLoGin pub fn readlink_at( 1371709498caSLoGin dirfd: i32, 1372709498caSLoGin path: *const u8, 1373709498caSLoGin user_buf: *mut u8, 1374709498caSLoGin buf_size: usize, 1375709498caSLoGin ) -> Result<usize, SystemError> { 13769b0abe6dSLoGin let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 137782df0a13Shmt let path = path.as_str().trim(); 1378709498caSLoGin let mut user_buf = UserBufferWriter::new(user_buf, buf_size, true)?; 1379709498caSLoGin 13800fb515b0SLoGin let (inode, path) = user_path_at(&ProcessManager::current_pcb(), dirfd, &path)?; 1381709498caSLoGin 1382709498caSLoGin let inode = inode.lookup(path.as_str())?; 1383709498caSLoGin if inode.metadata()?.file_type != FileType::SymLink { 1384709498caSLoGin return Err(SystemError::EINVAL); 1385709498caSLoGin } 1386709498caSLoGin 1387709498caSLoGin let ubuf = user_buf.buffer::<u8>(0).unwrap(); 1388709498caSLoGin 1389709498caSLoGin let mut file = File::new(inode, FileMode::O_RDONLY)?; 1390709498caSLoGin 1391709498caSLoGin let len = file.read(buf_size, ubuf)?; 1392709498caSLoGin 1393709498caSLoGin return Ok(len); 1394709498caSLoGin } 1395709498caSLoGin 1396709498caSLoGin pub fn readlink( 1397709498caSLoGin path: *const u8, 1398709498caSLoGin user_buf: *mut u8, 1399709498caSLoGin buf_size: usize, 1400709498caSLoGin ) -> Result<usize, SystemError> { 14019b0abe6dSLoGin return Self::readlink_at(AtFlags::AT_FDCWD.bits(), path, user_buf, buf_size); 14029b0abe6dSLoGin } 14039b0abe6dSLoGin 14049b0abe6dSLoGin pub fn access(pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 14059b0abe6dSLoGin return do_faccessat( 14069b0abe6dSLoGin AtFlags::AT_FDCWD.bits(), 14079b0abe6dSLoGin pathname, 1408bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 14099b0abe6dSLoGin 0, 14109b0abe6dSLoGin ); 14119b0abe6dSLoGin } 14129b0abe6dSLoGin 14139b0abe6dSLoGin pub fn faccessat2( 14149b0abe6dSLoGin dirfd: i32, 14159b0abe6dSLoGin pathname: *const u8, 14169b0abe6dSLoGin mode: u32, 14179b0abe6dSLoGin flags: u32, 14189b0abe6dSLoGin ) -> Result<usize, SystemError> { 1419bf4a4899SLoGin return do_faccessat( 1420bf4a4899SLoGin dirfd, 1421bf4a4899SLoGin pathname, 1422bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1423bf4a4899SLoGin flags, 1424bf4a4899SLoGin ); 1425bf4a4899SLoGin } 1426bf4a4899SLoGin 1427bf4a4899SLoGin pub fn chmod(pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 1428bf4a4899SLoGin return do_fchmodat( 1429bf4a4899SLoGin AtFlags::AT_FDCWD.bits(), 1430bf4a4899SLoGin pathname, 1431bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1432bf4a4899SLoGin ); 1433bf4a4899SLoGin } 1434bf4a4899SLoGin 1435bf4a4899SLoGin pub fn fchmodat(dirfd: i32, pathname: *const u8, mode: u32) -> Result<usize, SystemError> { 1436bf4a4899SLoGin return do_fchmodat( 1437bf4a4899SLoGin dirfd, 1438bf4a4899SLoGin pathname, 1439bf4a4899SLoGin ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?, 1440bf4a4899SLoGin ); 1441bf4a4899SLoGin } 1442bf4a4899SLoGin 1443bf4a4899SLoGin pub fn fchmod(fd: i32, mode: u32) -> Result<usize, SystemError> { 1444bf4a4899SLoGin let _mode = ModeType::from_bits(mode).ok_or(SystemError::EINVAL)?; 1445bf4a4899SLoGin let binding = ProcessManager::current_pcb().fd_table(); 1446bf4a4899SLoGin let fd_table_guard = binding.read(); 1447bf4a4899SLoGin let _file = fd_table_guard 1448bf4a4899SLoGin .get_file_by_fd(fd) 1449bf4a4899SLoGin .ok_or(SystemError::EBADF)?; 1450bf4a4899SLoGin 1451bf4a4899SLoGin // fchmod没完全实现,因此不修改文件的权限 1452bf4a4899SLoGin // todo: 实现fchmod 1453bf4a4899SLoGin kwarn!("fchmod not fully implemented"); 1454bf4a4899SLoGin return Ok(0); 1455709498caSLoGin } 14561d37ca6dSDonkey Kane /// #挂载文件系统 14571d37ca6dSDonkey Kane /// 14581d37ca6dSDonkey Kane /// 用于挂载文件系统,目前仅支持ramfs挂载 14591d37ca6dSDonkey Kane /// 14601d37ca6dSDonkey Kane /// ## 参数: 14611d37ca6dSDonkey Kane /// 14621d37ca6dSDonkey Kane /// - source 挂载设备(暂时不支持) 14631d37ca6dSDonkey Kane /// - target 挂载目录 14641d37ca6dSDonkey Kane /// - filesystemtype 文件系统 14651d37ca6dSDonkey Kane /// - mountflags 挂载选项(暂未实现) 14661d37ca6dSDonkey Kane /// - data 带数据挂载 14671d37ca6dSDonkey Kane /// 14681d37ca6dSDonkey Kane /// ## 返回值 14691d37ca6dSDonkey Kane /// - Ok(0): 挂载成功 14701d37ca6dSDonkey Kane /// - Err(SystemError) :挂载过程中出错 14711d37ca6dSDonkey Kane pub fn mount( 14721d37ca6dSDonkey Kane _source: *const u8, 14731d37ca6dSDonkey Kane target: *const u8, 14741d37ca6dSDonkey Kane filesystemtype: *const u8, 14751d37ca6dSDonkey Kane _mountflags: usize, 14761d37ca6dSDonkey Kane _data: *const c_void, 14771d37ca6dSDonkey Kane ) -> Result<usize, SystemError> { 14781d37ca6dSDonkey Kane let target = user_access::check_and_clone_cstr(target, Some(MAX_PATHLEN))?; 14791d37ca6dSDonkey Kane 14801d37ca6dSDonkey Kane let filesystemtype = user_access::check_and_clone_cstr(filesystemtype, Some(MAX_PATHLEN))?; 14811d37ca6dSDonkey Kane 14821d37ca6dSDonkey Kane let filesystemtype = producefs!(FSMAKER, filesystemtype)?; 14831d37ca6dSDonkey Kane 14841d37ca6dSDonkey Kane return Vcore::do_mount(filesystemtype, (format!("{target}")).as_str()); 14851d37ca6dSDonkey Kane } 14861d37ca6dSDonkey Kane 14871d37ca6dSDonkey Kane // 想法:可以在VFS中实现一个文件系统分发器,流程如下: 14881d37ca6dSDonkey Kane // 1. 接受从上方传来的文件类型字符串 14891d37ca6dSDonkey Kane // 2. 将传入值与启动时准备好的字符串数组逐个比较(probe) 14901d37ca6dSDonkey Kane // 3. 直接在函数内调用构造方法并直接返回文件系统对象 1491709498caSLoGin } 1492709498caSLoGin 1493cde5492fSlogin #[repr(C)] 1494cde5492fSlogin #[derive(Debug, Clone, Copy)] 1495cde5492fSlogin pub struct IoVec { 1496cde5492fSlogin /// 缓冲区的起始地址 1497cde5492fSlogin pub iov_base: *mut u8, 1498cde5492fSlogin /// 缓冲区的长度 1499cde5492fSlogin pub iov_len: usize, 1500cde5492fSlogin } 1501cde5492fSlogin 1502cde5492fSlogin /// 用于存储多个来自用户空间的IoVec 1503cde5492fSlogin /// 1504cde5492fSlogin /// 由于目前内核中的文件系统还不支持分散读写,所以暂时只支持将用户空间的IoVec聚合成一个缓冲区,然后进行操作。 1505cde5492fSlogin /// TODO:支持分散读写 1506cde5492fSlogin #[derive(Debug)] 1507cde5492fSlogin pub struct IoVecs(Vec<&'static mut [u8]>); 1508cde5492fSlogin 1509cde5492fSlogin impl IoVecs { 1510cde5492fSlogin /// 从用户空间的IoVec中构造IoVecs 1511cde5492fSlogin /// 1512cde5492fSlogin /// @param iov 用户空间的IoVec 1513cde5492fSlogin /// @param iovcnt 用户空间的IoVec的数量 1514cde5492fSlogin /// @param readv 是否为readv系统调用 1515cde5492fSlogin /// 1516cde5492fSlogin /// @return 构造成功返回IoVecs,否则返回错误码 1517cde5492fSlogin pub unsafe fn from_user( 1518cde5492fSlogin iov: *const IoVec, 1519cde5492fSlogin iovcnt: usize, 1520cde5492fSlogin _readv: bool, 1521cde5492fSlogin ) -> Result<Self, SystemError> { 1522cde5492fSlogin // 检查iov指针所在空间是否合法 15234fda81ceSLoGin verify_area( 15244fda81ceSLoGin VirtAddr::new(iov as usize), 15254fda81ceSLoGin iovcnt * core::mem::size_of::<IoVec>(), 15264fda81ceSLoGin ) 15274fda81ceSLoGin .map_err(|_| SystemError::EFAULT)?; 1528cde5492fSlogin 1529cde5492fSlogin // 将用户空间的IoVec转换为引用(注意:这里的引用是静态的,因为用户空间的IoVec不会被释放) 1530cde5492fSlogin let iovs: &[IoVec] = core::slice::from_raw_parts(iov, iovcnt); 1531cde5492fSlogin 1532cde5492fSlogin let mut slices: Vec<&mut [u8]> = vec![]; 1533cde5492fSlogin slices.reserve(iovs.len()); 1534cde5492fSlogin 1535cde5492fSlogin for iov in iovs.iter() { 1536cde5492fSlogin if iov.iov_len == 0 { 1537cde5492fSlogin continue; 1538cde5492fSlogin } 1539cde5492fSlogin 15404fda81ceSLoGin verify_area( 15414fda81ceSLoGin VirtAddr::new(iov.iov_base as usize), 15424fda81ceSLoGin iovcnt * core::mem::size_of::<IoVec>(), 15434fda81ceSLoGin ) 15444fda81ceSLoGin .map_err(|_| SystemError::EFAULT)?; 1545cde5492fSlogin 1546cde5492fSlogin slices.push(core::slice::from_raw_parts_mut(iov.iov_base, iov.iov_len)); 1547cde5492fSlogin } 1548cde5492fSlogin 1549cde5492fSlogin return Ok(Self(slices)); 1550cde5492fSlogin } 1551cde5492fSlogin 1552cde5492fSlogin /// @brief 将IoVecs中的数据聚合到一个缓冲区中 1553cde5492fSlogin /// 1554cde5492fSlogin /// @return 返回聚合后的缓冲区 1555cde5492fSlogin pub fn gather(&self) -> Vec<u8> { 1556cde5492fSlogin let mut buf = Vec::new(); 1557cde5492fSlogin for slice in self.0.iter() { 1558cde5492fSlogin buf.extend_from_slice(slice); 1559cde5492fSlogin } 1560cde5492fSlogin return buf; 1561cde5492fSlogin } 1562cde5492fSlogin 1563cde5492fSlogin /// @brief 将给定的数据分散写入到IoVecs中 1564cde5492fSlogin pub fn scatter(&mut self, data: &[u8]) { 1565cde5492fSlogin let mut data: &[u8] = data; 1566cde5492fSlogin for slice in self.0.iter_mut() { 1567cde5492fSlogin let len = core::cmp::min(slice.len(), data.len()); 1568cde5492fSlogin if len == 0 { 1569cde5492fSlogin continue; 1570cde5492fSlogin } 1571cde5492fSlogin 1572cde5492fSlogin slice[..len].copy_from_slice(&data[..len]); 1573cde5492fSlogin data = &data[len..]; 1574cde5492fSlogin } 1575cde5492fSlogin } 1576cde5492fSlogin 1577cde5492fSlogin /// @brief 创建与IoVecs等长的缓冲区 1578cde5492fSlogin /// 1579cde5492fSlogin /// @param set_len 是否设置返回的Vec的len。 1580cde5492fSlogin /// 如果为true,则返回的Vec的len为所有IoVec的长度之和; 1581cde5492fSlogin /// 否则返回的Vec的len为0,capacity为所有IoVec的长度之和. 1582cde5492fSlogin /// 1583cde5492fSlogin /// @return 返回创建的缓冲区 1584cde5492fSlogin pub fn new_buf(&self, set_len: bool) -> Vec<u8> { 1585cde5492fSlogin let total_len: usize = self.0.iter().map(|slice| slice.len()).sum(); 1586cde5492fSlogin let mut buf: Vec<u8> = Vec::with_capacity(total_len); 1587cde5492fSlogin 1588cde5492fSlogin if set_len { 15897ae679ddSLoGin buf.resize(total_len, 0); 1590cde5492fSlogin } 1591cde5492fSlogin return buf; 1592cde5492fSlogin } 1593cde5492fSlogin } 1594