Add creat syscall

This commit is contained in:
LI Qing 2021-08-06 09:35:22 +08:00 committed by Zongmin.Gu
parent c5c18ccd6d
commit b390ecaae9
3 changed files with 29 additions and 8 deletions

@ -97,6 +97,12 @@ pub fn do_timerfd_gettime(fd: FileDesc, curr_value_ptr: *mut itimerspec_t) -> Re
Ok(0) Ok(0)
} }
pub fn do_creat(path: *const i8, mode: u32) -> Result<isize> {
let flags =
AccessMode::O_WRONLY as u32 | (CreationFlags::O_CREAT | CreationFlags::O_TRUNC).bits();
self::do_open(path, flags, mode)
}
pub fn do_open(path: *const i8, flags: u32, mode: u32) -> Result<isize> { pub fn do_open(path: *const i8, flags: u32, mode: u32) -> Result<isize> {
self::do_openat(AT_FDCWD, path, flags, mode) self::do_openat(AT_FDCWD, path, flags, mode)
} }

@ -22,13 +22,13 @@ use util::mem_util::from_user::*;
use crate::exception::do_handle_exception; use crate::exception::do_handle_exception;
use crate::fs::{ use crate::fs::{
do_access, do_chdir, do_chmod, do_chown, do_close, do_dup, do_dup2, do_dup3, do_eventfd, do_access, do_chdir, do_chmod, do_chown, do_close, do_creat, do_dup, do_dup2, do_dup3,
do_eventfd2, do_faccessat, do_fallocate, do_fchdir, do_fchmod, do_fchmodat, do_fchown, do_eventfd, do_eventfd2, do_faccessat, do_fallocate, do_fchdir, do_fchmod, do_fchmodat,
do_fchownat, do_fcntl, do_fdatasync, do_fstat, do_fstatat, do_fstatfs, do_fsync, do_ftruncate, do_fchown, do_fchownat, do_fcntl, do_fdatasync, do_fstat, do_fstatat, do_fstatfs, do_fsync,
do_getcwd, do_getdents, do_getdents64, do_ioctl, do_lchown, do_link, do_linkat, do_lseek, do_ftruncate, do_getcwd, do_getdents, do_getdents64, do_ioctl, do_lchown, do_link, do_linkat,
do_lstat, do_mkdir, do_mkdirat, do_mount_rootfs, do_open, do_openat, do_pipe, do_pipe2, do_lseek, do_lstat, do_mkdir, do_mkdirat, do_mount_rootfs, do_open, do_openat, do_pipe,
do_pread, do_pwrite, do_read, do_readlink, do_readlinkat, do_readv, do_rename, do_renameat, do_pipe2, do_pread, do_pwrite, do_read, do_readlink, do_readlinkat, do_readv, do_rename,
do_rmdir, do_sendfile, do_stat, do_statfs, do_symlink, do_symlinkat, do_sync, do_renameat, do_rmdir, do_sendfile, do_stat, do_statfs, do_symlink, do_symlinkat, do_sync,
do_timerfd_create, do_timerfd_gettime, do_timerfd_settime, do_truncate, do_unlink, do_unlinkat, do_timerfd_create, do_timerfd_gettime, do_timerfd_settime, do_truncate, do_unlink, do_unlinkat,
do_write, do_writev, iovec_t, AsTimer, File, FileDesc, FileRef, HostStdioFds, Stat, Statfs, do_write, do_writev, iovec_t, AsTimer, File, FileDesc, FileRef, HostStdioFds, Stat, Statfs,
}; };
@ -172,7 +172,7 @@ macro_rules! process_syscall_table_with_callback {
(Rename = 82) => do_rename(oldpath: *const i8, newpath: *const i8), (Rename = 82) => do_rename(oldpath: *const i8, newpath: *const i8),
(Mkdir = 83) => do_mkdir(path: *const i8, mode: usize), (Mkdir = 83) => do_mkdir(path: *const i8, mode: usize),
(Rmdir = 84) => do_rmdir(path: *const i8), (Rmdir = 84) => do_rmdir(path: *const i8),
(Creat = 85) => handle_unsupported(), (Creat = 85) => do_creat(path: *const i8, mode: u32),
(Link = 86) => do_link(oldpath: *const i8, newpath: *const i8), (Link = 86) => do_link(oldpath: *const i8, newpath: *const i8),
(Unlink = 87) => do_unlink(path: *const i8), (Unlink = 87) => do_unlink(path: *const i8),
(Symlink = 88) => do_symlink(target: *const i8, link_path: *const i8), (Symlink = 88) => do_symlink(target: *const i8, link_path: *const i8),

@ -1,3 +1,4 @@
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include "test_fs.h" #include "test_fs.h"
@ -102,6 +103,15 @@ static int __test_openat_with_dirfd(const char *file_path, int flags, int mode)
return 0; return 0;
} }
static int __test_creat(const char *file_path, int flags, int mode) {
int fd = creat(file_path, mode);
if (fd < 0) {
THROW_ERROR("failed to creat a file");
}
close(fd);
return 0;
}
typedef int(*test_open_func_t)(const char *, int, int); typedef int(*test_open_func_t)(const char *, int, int);
static int test_open_framework(test_open_func_t fn) { static int test_open_framework(test_open_func_t fn) {
@ -138,6 +148,10 @@ static int test_openat_with_dirfd() {
return test_open_framework(__test_openat_with_dirfd); return test_open_framework(__test_openat_with_dirfd);
} }
static int test_creat() {
return test_open_framework(__test_creat);
}
// ============================================================================ // ============================================================================
// Test suite main // Test suite main
// ============================================================================ // ============================================================================
@ -148,6 +162,7 @@ static test_case_t test_cases[] = {
TEST_CASE(test_open_dir_with_write_flags), TEST_CASE(test_open_dir_with_write_flags),
TEST_CASE(test_openat_with_abs_path), TEST_CASE(test_openat_with_abs_path),
TEST_CASE(test_openat_with_dirfd), TEST_CASE(test_openat_with_dirfd),
TEST_CASE(test_creat),
}; };
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {