Auto merge of #976 - christianpoveda:unlink-shim, r=oli-obk
Add unlink shim to delete files r? @oli-obk This closes the whole create, write, read, delete circle for file handling.
This commit is contained in:
commit
cc0468b4bc
3 changed files with 32 additions and 4 deletions
|
|
@ -418,7 +418,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
|||
}
|
||||
}
|
||||
|
||||
"__errno_location" => {
|
||||
"__errno_location" | "__error" => {
|
||||
let errno_scalar: Scalar<Tag> = this.machine.last_error.unwrap().into();
|
||||
this.write_scalar(errno_scalar, dest)?;
|
||||
}
|
||||
|
|
@ -502,6 +502,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
|||
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
|
||||
}
|
||||
|
||||
"unlink" => {
|
||||
let result = this.unlink(args[0])?;
|
||||
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
|
||||
}
|
||||
|
||||
"strlen" => {
|
||||
let ptr = this.read_scalar(args[0])?.not_undef()?;
|
||||
let n = this.memory().read_c_str(ptr)?.len();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::fs::{File, OpenOptions, remove_file};
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use rustc::ty::layout::Size;
|
||||
|
|
@ -205,6 +205,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
|||
})
|
||||
}
|
||||
|
||||
fn unlink( &mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
||||
let this = self.eval_context_mut();
|
||||
|
||||
if !this.machine.communicate {
|
||||
throw_unsup_format!("`write` not available when isolation is enabled")
|
||||
}
|
||||
|
||||
let path_bytes = this
|
||||
.memory()
|
||||
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
|
||||
let path = std::str::from_utf8(path_bytes)
|
||||
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
|
||||
|
||||
let result = remove_file(path).map(|_| 0);
|
||||
|
||||
this.consume_result(result)
|
||||
}
|
||||
|
||||
/// Helper function that gets a `FileHandle` immutable reference and allows to manipulate it
|
||||
/// using the `f` closure.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
// ignore-windows: File handling is not implemented yet
|
||||
// compile-flags: -Zmiri-disable-isolation
|
||||
|
||||
use std::fs::File;
|
||||
use std::fs::{File, remove_file};
|
||||
use std::io::{Read, Write};
|
||||
|
||||
fn main() {
|
||||
// FIXME: remove the file and delete it when `rm` is implemented.
|
||||
let path = "./tests/hello.txt";
|
||||
let bytes = b"Hello, World!\n";
|
||||
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
|
||||
|
|
@ -22,4 +21,10 @@ fn main() {
|
|||
// Reading until EOF should get the whole text.
|
||||
file.read_to_end(&mut contents).unwrap();
|
||||
assert_eq!(bytes, contents.as_slice());
|
||||
// Removing file should succeed
|
||||
remove_file(path).unwrap();
|
||||
// Opening non-existing file should fail
|
||||
assert!(File::open(path).is_err());
|
||||
// Removing non-existing file should fail
|
||||
assert!(remove_file(path).is_err());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue