From bb9945af61e2f47509672794ba8a8b6bc8b8e658 Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Sat, 17 Aug 2019 16:47:35 +0800 Subject: [PATCH] remove `FileCreationFlags` Signed-off-by: yjhmelody <465402634@qq.com> --- vm/src/stdlib/io.rs | 20 ++++++++-------- vm/src/stdlib/os.rs | 58 ++++++--------------------------------------- 2 files changed, 17 insertions(+), 61 deletions(-) diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index bdbd2f272..1bbb4fbae 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -299,21 +299,21 @@ fn buffered_reader_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult } fn compute_c_flag(mode: &str) -> u32 { - let flags = match mode.chars().next() { + let flag = match mode.chars().next() { Some(mode) => match mode { - 'w' => os::FileCreationFlags::O_WRONLY | os::FileCreationFlags::O_CREAT, + 'w' => libc::O_WRONLY | libc::O_CREAT, 'x' => { - os::FileCreationFlags::O_WRONLY - | os::FileCreationFlags::O_CREAT - | os::FileCreationFlags::O_EXCL + libc::O_WRONLY + | libc::O_CREAT + | libc::O_EXCL } - 'a' => os::FileCreationFlags::O_APPEND, - '+' => os::FileCreationFlags::O_RDWR, - _ => os::FileCreationFlags::O_RDONLY, + 'a' => libc::O_APPEND, + '+' => libc::O_RDWR, + _ => libc::O_RDONLY, }, - None => os::FileCreationFlags::O_RDONLY, + None => libc::O_RDONLY, }; - flags.bits() + flag as u32 } fn file_io_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 12c7b0e7d..b010c401c 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -1,4 +1,3 @@ -use std::{env, fs}; use std::cell::RefCell; use std::ffi::CStr; use std::fs::File; @@ -7,6 +6,7 @@ use std::io::{self, Error, ErrorKind, Read, Write}; #[cfg(unix)] use std::os::unix::fs::OpenOptionsExt; use std::time::{Duration, SystemTime}; +use std::{env, fs}; #[cfg(unix)] use nix::errno::Errno; @@ -16,8 +16,6 @@ use nix::pty::openpty; use nix::unistd::{self, Gid, Pid, Uid}; use num_traits::cast::ToPrimitive; -use bitflags::bitflags; - use crate::function::{IntoPyNativeFunc, PyFuncArgs}; use crate::obj::objbytes::PyBytesRef; use crate::obj::objdict::PyDictRef; @@ -92,24 +90,6 @@ pub fn os_close(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.get_none()) } -bitflags! { - pub struct FileCreationFlags: u32 { - // https://elixir.bootlin.com/linux/v4.8/source/include/uapi/asm-generic/fcntl.h - const O_RDONLY = libc::O_RDONLY as u32; - const O_WRONLY = libc::O_WRONLY as u32; - const O_RDWR = libc::O_RDWR as u32; - const O_CREAT = libc::O_CREAT as u32; - const O_EXCL = libc::O_EXCL as u32; - const O_APPEND = libc::O_APPEND as u32; - const O_NONBLOCK = libc::O_NONBLOCK as u32; - const O_DSYNC = libc::O_DSYNC as u32; - const O_RSYNC = libc::O_RSYNC as u32; - const O_NDELAY = libc::O_NDELAY as u32; - const O_NOCTTY = libc::O_NOCTTY as u32; - const O_CLOEXEC = libc::O_CLOEXEC as u32; - } -} - pub fn os_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!( vm, @@ -135,32 +115,8 @@ pub fn os_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { let fname = &make_path(vm, name, &dir_fd).value; let mut options = OpenOptions::new(); - if cfg!(unix) || cfg!(windows) { - let flags = objint::get_value(flags).to_i32().unwrap(); - options.custom_flags(flags); - } else { - let flags = FileCreationFlags::from_bits(objint::get_value(flags).to_u32().unwrap()) - .ok_or_else(|| vm.new_value_error("Unsupported flag".to_string()))?; - if flags.contains(FileCreationFlags::O_WRONLY) { - options.write(true); - } else if flags.contains(FileCreationFlags::O_RDWR) { - options.read(true).write(true); - } else { - options.read(true); - } - - if flags.contains(FileCreationFlags::O_APPEND) { - options.append(true); - } - - if flags.contains(FileCreationFlags::O_CREAT) { - if flags.contains(FileCreationFlags::O_EXCL) { - options.create_new(true); - } else { - options.create(true); - } - } - } + let flags = objint::get_value(flags).to_i32().unwrap(); + options.custom_flags(flags); let handle = options .open(&fname) @@ -1008,10 +964,10 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "O_APPEND" => ctx.new_int(libc::O_APPEND), "O_EXCL" => ctx.new_int(libc::O_EXCL), "O_CREAT" => ctx.new_int(libc::O_CREAT), - "F_OK" => ctx.new_int(0), - "R_OK" => ctx.new_int(4), - "W_OK" => ctx.new_int(2), - "X_OK" => ctx.new_int(1), + "F_OK" => ctx.new_int(libc::F_OK), + "R_OK" => ctx.new_int(libc::R_OK), + "W_OK" => ctx.new_int(libc::W_OK), + "X_OK" => ctx.new_int(libc::X_OK), "getpid" => ctx.new_rustfunc(os_getpid) });