remove FileCreationFlags

Signed-off-by: yjhmelody <465402634@qq.com>
This commit is contained in:
yjhmelody
2019-08-17 16:47:35 +08:00
parent 15cde81081
commit bb9945af61
2 changed files with 17 additions and 61 deletions

View File

@@ -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 {

View File

@@ -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)
});