Add unlink shim to delete files

This commit is contained in:
Christian Poveda 2019-10-03 09:33:36 -05:00
parent 9d03dd6364
commit ffc47de1b9
3 changed files with 27 additions and 3 deletions

View file

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

View file

@ -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.
///

View file

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