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