-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm.rs
93 lines (76 loc) · 2.14 KB
/
rm.rs
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
use std::env;
use std::fs;
use std::io::{self, Write};
fn main() {
let args: Vec<String> = env::args().collect();
let mut recursive = false;
let mut force = false;
let mut interactive = false;
let mut files: Vec<String> = Vec::new();
let mut i = 1;
while i < args.len() {
match args[i].as_ref() {
"-r" | "-R" => recursive = true,
"-f" => force = true,
"-i" => interactive = true,
_ => files.push(args[i].clone()),
}
i += 1;
}
if files.is_empty() {
eprintln!("rm: missing operand");
return;
}
for file in files.iter() {
if file == "." || file == ".." {
eprintln!("rm: cannot remove '.' or '..'");
continue;
}
let metadata = match fs::metadata(file) {
Ok(metadata) => metadata,
Err(_) => {
eprintln!("rm: cannot remove '{}': No such file or directory", file);
continue;
}
};
if metadata.is_dir() {
if !recursive {
eprintln!("rm: cannot remove '{}': Is a directory", file);
continue;
}
remove_directory(file, force, interactive);
} else {
remove_file(file, force, interactive);
}
}
}
fn remove_directory(dir: &str, force: bool, interactive: bool) {
if !force && interactive {
print!("rm: remove directory '{}'? ", dir);
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
if !input.starts_with('y') && !input.starts_with('Y') {
return;
}
}
match fs::remove_dir_all(dir) {
Ok(_) => (),
Err(err) => eprintln!("rm: cannot remove '{}': {}", dir, err),
}
}
fn remove_file(file: &str, force: bool, interactive: bool) {
if !force && interactive {
print!("rm: remove regular file '{}'? ", file);
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
if !input.starts_with('y') && !input.starts_with('Y') {
return;
}
}
match fs::remove_file(file) {
Ok(_) => (),
Err(err) => eprintln!("rm: cannot remove '{}': {}", file, err),
}
}