cap_primitives/fs/
remove_file.rs1use crate::fs::remove_file_impl;
5#[cfg(racy_asserts)]
6use crate::fs::{
7 manually, map_result, remove_file_unchecked, stat_unchecked, FollowSymlinks, Metadata,
8};
9use std::path::Path;
10use std::{fs, io};
11
12#[cfg_attr(not(racy_asserts), allow(clippy::let_and_return))]
15#[inline]
16pub fn remove_file(start: &fs::File, path: &Path) -> io::Result<()> {
17 #[cfg(racy_asserts)]
18 let stat_before = stat_unchecked(start, path, FollowSymlinks::No);
19
20 let result = remove_file_impl(start, path);
22
23 #[cfg(racy_asserts)]
24 let stat_after = stat_unchecked(start, path, FollowSymlinks::No);
25
26 #[cfg(racy_asserts)]
27 check_remove_file(start, path, &stat_before, &result, &stat_after);
28
29 result
30}
31
32#[cfg(racy_asserts)]
33#[allow(clippy::enum_glob_use)]
34fn check_remove_file(
35 start: &fs::File,
36 path: &Path,
37 stat_before: &io::Result<Metadata>,
38 result: &io::Result<()>,
39 stat_after: &io::Result<Metadata>,
40) {
41 use io::ErrorKind::*;
42
43 match (
44 map_result(stat_before),
45 map_result(result),
46 map_result(stat_after),
47 ) {
48 (Ok(metadata), Ok(()), Err((NotFound, _))) => {
49 assert!(!metadata.is_dir());
51 }
52
53 (Err((Other, _)), Ok(()), Err((NotFound, _))) => {
54 }
56
57 (_, Err((_kind, _message)), _) => {
58 match map_result(&manually::canonicalize_with(
59 start,
60 path,
61 FollowSymlinks::No,
62 )) {
63 Ok(canon) => match map_result(&remove_file_unchecked(start, &canon)) {
64 Err((_unchecked_kind, _unchecked_message)) => {
65 }
80 _ => panic!("unsandboxed remove_file success"),
81 },
82 Err((_canon_kind, _canon_message)) => {
83 }
88 }
89 }
90
91 other => panic!(
92 "inconsistent remove_file checks: start='{:?}' path='{}':\n{:#?}",
93 start,
94 path.display(),
95 other,
96 ),
97 }
98
99 match (result, stat_after) {
100 (Ok(()), Ok(_unchecked_metadata)) => panic!(
101 "file still exists after remove_file start='{:?}', path='{}'",
102 start,
103 path.display()
104 ),
105 (Err(e), Ok(unchecked_metadata)) => match e.kind() {
106 #[cfg(io_error_more)]
107 io::ErrorKind::IsADirectory => assert!(unchecked_metadata.is_dir()),
108 io::ErrorKind::PermissionDenied => (),
109 #[cfg(not(io_error_more))]
110 io::ErrorKind::Other if unchecked_metadata.is_dir() => (),
111 _ => panic!(
112 "unexpected error removing file start='{:?}', path='{}': {:?}",
113 start,
114 path.display(),
115 e
116 ),
117 },
118 (Ok(()), Err(_unchecked_error)) => (),
119 (Err(result_error), Err(_unchecked_error)) => match result_error.kind() {
120 io::ErrorKind::PermissionDenied => (),
121 _ => {
122 }
127 },
128 }
129}