Auto merge of #1279 - divergentdave:open_O_EXCL, r=RalfJung

Add support for OpenOptions::create_new()/O_EXCL

This PR extends the POSIX shim for `open` to support the `O_EXCL` flag, when it is used alongside `O_CREAT`, and exercises it by testing `OpenOptions::create_new`.
This commit is contained in:
bors 2020-03-30 15:49:45 +00:00
commit 319f2dd9e1
2 changed files with 26 additions and 2 deletions

View File

@ -288,8 +288,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
let o_creat = this.eval_libc_i32("O_CREAT")?;
if flag & o_creat != 0 {
options.create(true);
mirror |= o_creat;
let o_excl = this.eval_libc_i32("O_EXCL")?;
if flag & o_excl != 0 {
mirror |= o_excl;
options.create_new(true);
} else {
options.create(true);
}
}
let o_cloexec = this.eval_libc_i32("O_CLOEXEC")?;
if flag & o_cloexec != 0 {

View File

@ -1,13 +1,16 @@
// ignore-windows: File handling is not implemented yet
// compile-flags: -Zmiri-disable-isolation
use std::fs::{File, create_dir, read_dir, remove_dir, remove_dir_all, remove_file, rename};
use std::fs::{
File, create_dir, OpenOptions, read_dir, remove_dir, remove_dir_all, remove_file, rename,
};
use std::io::{Read, Write, ErrorKind, Result, Seek, SeekFrom};
use std::path::{PathBuf, Path};
fn main() {
test_file();
test_file_clone();
test_file_create_new();
test_seek();
test_metadata();
test_symlink();
@ -85,6 +88,20 @@ fn test_file_clone() {
remove_file(&path).unwrap();
}
fn test_file_create_new() {
let path = prepare("miri_test_fs_file_create_new.txt");
// Creating a new file that doesn't yet exist should succeed.
OpenOptions::new().write(true).create_new(true).open(&path).unwrap();
// Creating a new file that already exists should fail.
assert_eq!(ErrorKind::AlreadyExists, OpenOptions::new().write(true).create_new(true).open(&path).unwrap_err().kind());
// Optionally creating a new file that already exists should succeed.
OpenOptions::new().write(true).create(true).open(&path).unwrap();
// Clean up
remove_file(&path).unwrap();
}
fn test_seek() {
let bytes = b"Hello, entire World!\n";
let path = prepare_with_content("miri_test_fs_seek.txt", bytes);