10fb515b0SLoGin use alloc::sync::Arc; 2*2eab6dd7S曾俊 use log::warn; 391e9d4abSLoGin use system_error::SystemError; 40fb515b0SLoGin 5bf4a4899SLoGin use crate::{ 691e9d4abSLoGin driver::base::block::SeekFrom, process::ProcessManager, 791e9d4abSLoGin syscall::user_access::check_and_clone_cstr, 8bf4a4899SLoGin }; 99b0abe6dSLoGin 10bf4a4899SLoGin use super::{ 110fb515b0SLoGin fcntl::AtFlags, 120fb515b0SLoGin file::{File, FileMode}, 130fb515b0SLoGin syscall::{ModeType, OpenHow, OpenHowResolve}, 140fb515b0SLoGin utils::{rsplit_path, user_path_at}, 150fb515b0SLoGin FileType, IndexNode, MAX_PATHLEN, ROOT_INODE, VFS_MAX_FOLLOW_SYMLINK_TIMES, 16bf4a4899SLoGin }; 179b0abe6dSLoGin 189b0abe6dSLoGin pub(super) fn do_faccessat( 19bf4a4899SLoGin dirfd: i32, 20bf4a4899SLoGin path: *const u8, 219b0abe6dSLoGin mode: ModeType, 229b0abe6dSLoGin flags: u32, 239b0abe6dSLoGin ) -> Result<usize, SystemError> { 24bf4a4899SLoGin if (mode.bits() & (!ModeType::S_IRWXO.bits())) != 0 { 259b0abe6dSLoGin return Err(SystemError::EINVAL); 269b0abe6dSLoGin } 279b0abe6dSLoGin 289b0abe6dSLoGin if (flags 299b0abe6dSLoGin & (!((AtFlags::AT_EACCESS | AtFlags::AT_SYMLINK_NOFOLLOW | AtFlags::AT_EMPTY_PATH).bits() 309b0abe6dSLoGin as u32))) 319b0abe6dSLoGin != 0 329b0abe6dSLoGin { 339b0abe6dSLoGin return Err(SystemError::EINVAL); 349b0abe6dSLoGin } 359b0abe6dSLoGin 369b0abe6dSLoGin // let follow_symlink = flags & AtFlags::AT_SYMLINK_NOFOLLOW.bits() as u32 == 0; 379b0abe6dSLoGin 38bf4a4899SLoGin let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 39bf4a4899SLoGin 400fb515b0SLoGin let (inode, path) = user_path_at(&ProcessManager::current_pcb(), dirfd, &path)?; 41bf4a4899SLoGin 42bf4a4899SLoGin // 如果找不到文件,则返回错误码ENOENT 43bf4a4899SLoGin let _inode = inode.lookup_follow_symlink(path.as_str(), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 44bf4a4899SLoGin 459b0abe6dSLoGin // todo: 接着完善(可以借鉴linux 6.1.9的do_faccessat) 469b0abe6dSLoGin return Ok(0); 479b0abe6dSLoGin } 48bf4a4899SLoGin 49bf4a4899SLoGin pub fn do_fchmodat(dirfd: i32, path: *const u8, _mode: ModeType) -> Result<usize, SystemError> { 50bf4a4899SLoGin let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?; 51bf4a4899SLoGin 520fb515b0SLoGin let (inode, path) = user_path_at(&ProcessManager::current_pcb(), dirfd, &path)?; 53bf4a4899SLoGin 54bf4a4899SLoGin // 如果找不到文件,则返回错误码ENOENT 55bf4a4899SLoGin let _inode = inode.lookup_follow_symlink(path.as_str(), VFS_MAX_FOLLOW_SYMLINK_TIMES)?; 56bf4a4899SLoGin 57*2eab6dd7S曾俊 warn!("do_fchmodat: not implemented yet\n"); 58bf4a4899SLoGin // todo: 真正去改变文件的权限 59bf4a4899SLoGin 60bf4a4899SLoGin return Ok(0); 61bf4a4899SLoGin } 620fb515b0SLoGin 630fb515b0SLoGin pub(super) fn do_sys_open( 640fb515b0SLoGin dfd: i32, 650fb515b0SLoGin path: &str, 660fb515b0SLoGin o_flags: FileMode, 670fb515b0SLoGin mode: ModeType, 680fb515b0SLoGin follow_symlink: bool, 690fb515b0SLoGin ) -> Result<usize, SystemError> { 700fb515b0SLoGin let how = OpenHow::new(o_flags, mode, OpenHowResolve::empty()); 710fb515b0SLoGin return do_sys_openat2(dfd, path, how, follow_symlink); 720fb515b0SLoGin } 730fb515b0SLoGin 740fb515b0SLoGin fn do_sys_openat2( 750fb515b0SLoGin dirfd: i32, 760fb515b0SLoGin path: &str, 770fb515b0SLoGin how: OpenHow, 780fb515b0SLoGin follow_symlink: bool, 790fb515b0SLoGin ) -> Result<usize, SystemError> { 80*2eab6dd7S曾俊 // debug!("open path: {}, how: {:?}", path, how); 8182df0a13Shmt let path = path.trim(); 8282df0a13Shmt 830fb515b0SLoGin let (inode_begin, path) = user_path_at(&ProcessManager::current_pcb(), dirfd, path)?; 840fb515b0SLoGin let inode: Result<Arc<dyn IndexNode>, SystemError> = inode_begin.lookup_follow_symlink( 850fb515b0SLoGin &path, 860fb515b0SLoGin if follow_symlink { 870fb515b0SLoGin VFS_MAX_FOLLOW_SYMLINK_TIMES 880fb515b0SLoGin } else { 890fb515b0SLoGin 0 900fb515b0SLoGin }, 910fb515b0SLoGin ); 920fb515b0SLoGin 93b5b571e0SLoGin let inode: Arc<dyn IndexNode> = match inode { 94b5b571e0SLoGin Ok(inode) => inode, 95b5b571e0SLoGin Err(errno) => { 960fb515b0SLoGin // 文件不存在,且需要创建 970fb515b0SLoGin if how.o_flags.contains(FileMode::O_CREAT) 980fb515b0SLoGin && !how.o_flags.contains(FileMode::O_DIRECTORY) 990fb515b0SLoGin && errno == SystemError::ENOENT 1000fb515b0SLoGin { 1010fb515b0SLoGin let (filename, parent_path) = rsplit_path(&path); 1020fb515b0SLoGin // 查找父目录 1030fb515b0SLoGin let parent_inode: Arc<dyn IndexNode> = 1040fb515b0SLoGin ROOT_INODE().lookup(parent_path.unwrap_or("/"))?; 1050fb515b0SLoGin // 创建文件 1060fb515b0SLoGin let inode: Arc<dyn IndexNode> = parent_inode.create( 1070fb515b0SLoGin filename, 1080fb515b0SLoGin FileType::File, 1090fb515b0SLoGin ModeType::from_bits_truncate(0o755), 1100fb515b0SLoGin )?; 1110fb515b0SLoGin inode 1120fb515b0SLoGin } else { 1130fb515b0SLoGin // 不需要创建文件,因此返回错误码 1140fb515b0SLoGin return Err(errno); 1150fb515b0SLoGin } 116b5b571e0SLoGin } 1170fb515b0SLoGin }; 1180fb515b0SLoGin 1190fb515b0SLoGin let file_type: FileType = inode.metadata()?.file_type; 1200fb515b0SLoGin // 如果要打开的是文件夹,而目标不是文件夹 1210fb515b0SLoGin if how.o_flags.contains(FileMode::O_DIRECTORY) && file_type != FileType::Dir { 1220fb515b0SLoGin return Err(SystemError::ENOTDIR); 1230fb515b0SLoGin } 1240fb515b0SLoGin 1250fb515b0SLoGin // 创建文件对象 1260fb515b0SLoGin 127dfe53cf0SGnoCiYeH let file: File = File::new(inode, how.o_flags)?; 1280fb515b0SLoGin 1290fb515b0SLoGin // 打开模式为“追加” 1300fb515b0SLoGin if how.o_flags.contains(FileMode::O_APPEND) { 1310fb515b0SLoGin file.lseek(SeekFrom::SeekEnd(0))?; 1320fb515b0SLoGin } 1330fb515b0SLoGin 1340fb515b0SLoGin // 如果O_TRUNC,并且,打开模式包含O_RDWR或O_WRONLY,清空文件 1350fb515b0SLoGin if how.o_flags.contains(FileMode::O_TRUNC) 1360fb515b0SLoGin && (how.o_flags.contains(FileMode::O_RDWR) || how.o_flags.contains(FileMode::O_WRONLY)) 1370fb515b0SLoGin && file_type == FileType::File 1380fb515b0SLoGin { 1390fb515b0SLoGin file.ftruncate(0)?; 1400fb515b0SLoGin } 1410fb515b0SLoGin // 把文件对象存入pcb 1420fb515b0SLoGin let r = ProcessManager::current_pcb() 1430fb515b0SLoGin .fd_table() 1440fb515b0SLoGin .write() 1450fb515b0SLoGin .alloc_fd(file, None) 1460fb515b0SLoGin .map(|fd| fd as usize); 1470fb515b0SLoGin 1480fb515b0SLoGin return r; 1490fb515b0SLoGin } 150