1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::fs;
use std::path::Path;
use std::io;
pub fn force_remove_file<P: AsRef<Path>>(path: P) -> io::Result<()> {
set_readonly(&path,false).unwrap();
fs::remove_file(&path)
}
pub fn force_remove_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
set_readonly(&path, false).unwrap();
fs::remove_dir(&path)
}
pub fn force_remove_dir_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
for child in fs::read_dir(&path)?{
let child = child?;
let metadata = child.metadata()?;
let path = child.path();
if metadata.is_dir(){
force_remove_dir_all(&path)?;
}else if metadata.is_file(){
force_remove_file(&path)?;
}
}
force_remove_dir(&path)
}
fn set_readonly<P: AsRef<Path>>(path: P, readonly : bool) -> io::Result<()> {
let mut permissions = fs::metadata(&path)?.permissions();
permissions.set_readonly(readonly);
fs::set_permissions(&path,permissions)
}
#[cfg(test)]
mod tests {
use std::os;
use std::fs;
use std::path::Path;
use std::fs::File;
use std::io;
#[test]
fn test_force_remove_file() {
let path = Path::new("single_file");
File::create(&path).unwrap();
assert!(Path::exists(&path));
super::set_readonly(&path, true).unwrap();
super::force_remove_file(path).unwrap();
assert!(!Path::exists(&path));
}
#[test]
fn test_force_remove_directory() {
let path = Path::new("single_directory");
fs::create_dir(&path).unwrap();
assert!(Path::exists(&path));
super::set_readonly(&path, true).unwrap();
super::force_remove_dir(path).unwrap();
assert!(!Path::exists(&path));
}
#[test]
fn test_force_remove_directory_all() {
let directory = Path::new("directory");
let sub_directory = directory.join(Path::new("sub_directory"));
let file = directory.join(Path::new("file"));
let sub_directory_file = sub_directory.join(Path::new("sub_directory_file"));
fs::create_dir(&directory).unwrap();
fs::create_dir(&sub_directory).unwrap();
File::create(&file).unwrap();
File::create(&sub_directory_file).unwrap();
assert!(Path::exists(&directory));
assert!(Path::exists(&sub_directory));
assert!(Path::exists(&file));
assert!(Path::exists(&sub_directory_file));
super::set_readonly(&directory, true).unwrap();
super::set_readonly(&sub_directory, true).unwrap();
super::set_readonly(&file, true).unwrap();
super::set_readonly(&sub_directory_file, true).unwrap();
super::force_remove_dir_all(&directory).unwrap();
assert!(!Path::exists(&directory));
assert!(!Path::exists(&sub_directory));
assert!(!Path::exists(&file));
assert!(!Path::exists(&sub_directory_file));
}
#[cfg(target_os = "unix")]
fn test_force_remove_directory_all_with_symlink() {
let directory = Path::new("directory");
let sub_directory = directory.join(Path::new("sub_directory"));
let file = directory.join(Path::new("file"));
let sub_directory_file = sub_directory.join(Path::new("sub_directory_file"));
let symlink = directory.join(Path::new("symlink"));
let linked_directory = Path::new("linked_directory");
fs::create_dir(&directory).unwrap();
fs::create_dir(&sub_directory).unwrap();
File::create(&file).unwrap();
File::create(&sub_directory_file).unwrap();
make_symlink_dir(&symlink, &linked_directory).unwrap();
fs::create_dir(&linked_directory).unwrap();
assert!(Path::exists(&directory));
assert!(Path::exists(&sub_directory));
assert!(Path::exists(&file));
assert!(Path::exists(&sub_directory_file));
assert!(Path::exists(&symlink));
assert!(Path::exists(&linked_directory));
super::set_readonly(&directory, true).unwrap();
super::set_readonly(&sub_directory, true).unwrap();
super::set_readonly(&file, true).unwrap();
super::set_readonly(&sub_directory_file, true).unwrap();
super::set_readonly(&symlink, true).unwrap();
super::set_readonly(&linked_directory, true).unwrap();
super::force_remove_dir_all(&directory).unwrap();
assert!(!Path::exists(&directory));
assert!(!Path::exists(&sub_directory));
assert!(!Path::exists(&file));
assert!(!Path::exists(&sub_directory_file));
assert!(!Path::exists(&symlink));
assert!(Path::exists(&linked_directory));
super::force_remove_dir(&linked_directory);
}
#[cfg(target_os = "windows")]
fn make_symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()>{
os::windows::fs::symlink_dir(src, dst)
}
#[cfg(target_os = "unix")]
fn make_symlink_dir<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()>{
os::unix::fs::symlink(src, dst)
}
}