Skip to content

Commit cc0468b

Browse files
committed
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.
2 parents 9d03dd6 + 1873619 commit cc0468b

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/shims/foreign_items.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
418418
}
419419
}
420420

421-
"__errno_location" => {
421+
"__errno_location" | "__error" => {
422422
let errno_scalar: Scalar<Tag> = this.machine.last_error.unwrap().into();
423423
this.write_scalar(errno_scalar, dest)?;
424424
}
@@ -502,6 +502,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
502502
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
503503
}
504504

505+
"unlink" => {
506+
let result = this.unlink(args[0])?;
507+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
508+
}
509+
505510
"strlen" => {
506511
let ptr = this.read_scalar(args[0])?.not_undef()?;
507512
let n = this.memory().read_c_str(ptr)?.len();

src/shims/io.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::HashMap;
2-
use std::fs::{File, OpenOptions};
2+
use std::fs::{File, OpenOptions, remove_file};
33
use std::io::{Read, Write};
44

55
use rustc::ty::layout::Size;
@@ -205,6 +205,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
205205
})
206206
}
207207

208+
fn unlink( &mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
209+
let this = self.eval_context_mut();
210+
211+
if !this.machine.communicate {
212+
throw_unsup_format!("`write` not available when isolation is enabled")
213+
}
214+
215+
let path_bytes = this
216+
.memory()
217+
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
218+
let path = std::str::from_utf8(path_bytes)
219+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
220+
221+
let result = remove_file(path).map(|_| 0);
222+
223+
this.consume_result(result)
224+
}
225+
208226
/// Helper function that gets a `FileHandle` immutable reference and allows to manipulate it
209227
/// using the `f` closure.
210228
///

tests/run-pass/file_read.rs renamed to tests/run-pass/file_manipulation.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// ignore-windows: File handling is not implemented yet
22
// compile-flags: -Zmiri-disable-isolation
33

4-
use std::fs::File;
4+
use std::fs::{File, remove_file};
55
use std::io::{Read, Write};
66

77
fn main() {
8-
// FIXME: remove the file and delete it when `rm` is implemented.
98
let path = "./tests/hello.txt";
109
let bytes = b"Hello, World!\n";
1110
// Test creating, writing and closing a file (closing is tested when `file` is dropped).
@@ -22,4 +21,10 @@ fn main() {
2221
// Reading until EOF should get the whole text.
2322
file.read_to_end(&mut contents).unwrap();
2423
assert_eq!(bytes, contents.as_slice());
24+
// Removing file should succeed
25+
remove_file(path).unwrap();
26+
// Opening non-existing file should fail
27+
assert!(File::open(path).is_err());
28+
// Removing non-existing file should fail
29+
assert!(remove_file(path).is_err());
2530
}

0 commit comments

Comments
 (0)