Skip to content

Commit 37a34c0

Browse files
committed
Implement DeleteFileW
1 parent 52b204e commit 37a34c0

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

src/tools/miri/src/shims/windows/foreign_items.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
317317
let res = this.GetFileInformationByHandle(handle, info)?;
318318
this.write_scalar(res, dest)?;
319319
}
320+
"DeleteFileW" => {
321+
let [file_name] = this.check_shim(abi, sys_conv, link_name, args)?;
322+
let res = this.DeleteFileW(file_name)?;
323+
this.write_scalar(res, dest)?;
324+
}
320325

321326
// Allocation
322327
"HeapAlloc" => {

src/tools/miri/src/shims/windows/fs.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,25 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
371371

372372
interp_ok(this.eval_windows("c", "TRUE"))
373373
}
374+
375+
fn DeleteFileW(
376+
&mut self,
377+
file_name: &OpTy<'tcx>, // LPCWSTR
378+
) -> InterpResult<'tcx, Scalar> {
379+
// ^ Returns BOOL (i32 on Windows)
380+
let this = self.eval_context_mut();
381+
this.assert_target_os("windows", "DeleteFileW");
382+
this.check_no_isolation("`DeleteFileW`")?;
383+
384+
let file_name = this.read_path_from_wide_str(this.read_pointer(file_name)?)?;
385+
match std::fs::remove_file(file_name) {
386+
Ok(_) => interp_ok(this.eval_windows("c", "TRUE")),
387+
Err(e) => {
388+
this.set_last_error(e)?;
389+
interp_ok(this.eval_windows("c", "FALSE"))
390+
}
391+
}
392+
}
374393
}
375394

376395
/// Windows FILETIME is measured in 100-nanosecs since 1601

src/tools/miri/tests/pass-dep/shims/windows-fs.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//@compile-flags: -Zmiri-disable-isolation
33
#![allow(nonstandard_style)]
44

5+
use std::io::ErrorKind;
56
use std::os::windows::ffi::OsStrExt;
67
use std::path::Path;
78
use std::ptr;
@@ -15,10 +16,10 @@ use windows_sys::Win32::Foundation::{
1516
STATUS_IO_DEVICE_ERROR,
1617
};
1718
use windows_sys::Win32::Storage::FileSystem::{
18-
BY_HANDLE_FILE_INFORMATION, CREATE_ALWAYS, CREATE_NEW, CreateFileW, FILE_ATTRIBUTE_DIRECTORY,
19-
FILE_ATTRIBUTE_NORMAL, FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT,
20-
FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, GetFileInformationByHandle, OPEN_ALWAYS,
21-
OPEN_EXISTING,
19+
BY_HANDLE_FILE_INFORMATION, CREATE_ALWAYS, CREATE_NEW, CreateFileW, DeleteFileW,
20+
FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_NORMAL, FILE_FLAG_BACKUP_SEMANTICS,
21+
FILE_FLAG_OPEN_REPARSE_POINT, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE,
22+
GetFileInformationByHandle, OPEN_ALWAYS, OPEN_EXISTING,
2223
};
2324

2425
fn main() {
@@ -28,6 +29,7 @@ fn main() {
2829
test_create_always_twice();
2930
test_open_always_twice();
3031
test_open_dir_reparse();
32+
test_delete_file();
3133
test_ntstatus_to_dos();
3234
}
3335
}
@@ -194,6 +196,21 @@ unsafe fn test_open_dir_reparse() {
194196
};
195197
}
196198

199+
unsafe fn test_delete_file() {
200+
let temp = utils::tmp().join("test_delete_file.txt");
201+
let raw_path = to_wide_cstr(&temp);
202+
let _ = std::fs::File::create(&temp).unwrap();
203+
204+
if DeleteFileW(raw_path.as_ptr()) == 0 {
205+
panic!("Failed to delete file");
206+
}
207+
208+
match std::fs::File::open(temp) {
209+
Ok(_) => panic!("File not deleted"),
210+
Err(e) => assert!(e.kind() == ErrorKind::NotFound, "File not deleted"),
211+
}
212+
}
213+
197214
unsafe fn test_ntstatus_to_dos() {
198215
// We won't test all combinations, just a couple common ones
199216
assert_eq!(RtlNtStatusToDosError(STATUS_IO_DEVICE_ERROR), ERROR_IO_DEVICE);

src/tools/miri/tests/pass/shims/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ mod utils;
1717

1818
fn main() {
1919
test_path_conversion();
20+
test_file_create_new();
2021
// Windows file handling is very incomplete.
2122
if cfg!(not(windows)) {
2223
test_file();
23-
test_file_create_new();
2424
test_seek();
2525
test_file_clone();
2626
test_metadata();

0 commit comments

Comments
 (0)