forked from uutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm.rs
220 lines (204 loc) · 8.05 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#![crate_id(name="rm", vers="1.0.0", author="Arcterus")]
/*
* This file is part of the uutils coreutils package.
*
* (c) Arcterus <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#![feature(macro_rules)]
extern crate getopts;
use std::os;
use std::io::{print, stdin, stdio, fs, BufferedReader};
#[path = "../common/util.rs"]
mod util;
#[deriving(Eq)]
enum InteractiveMode {
InteractiveNone,
InteractiveOnce,
InteractiveAlways
}
static NAME: &'static str = "rm";
fn main() {
let args = os::args();
let program = args[0].clone();
// TODO: make getopts support -R in addition to -r
let opts = ~[
getopts::optflag("f", "force", "ignore nonexistent files and arguments, never prompt"),
getopts::optflag("i", "", "prompt before every removal"),
getopts::optflag("I", "", "prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving some protection against most mistakes"),
getopts::optflagopt("", "interactive", "prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompts always", "WHEN"),
getopts::optflag("", "one-file-system", "when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument (NOT IMPLEMENTED)"),
getopts::optflag("", "no-preserve-root", "do not treat '/' specially"),
getopts::optflag("", "preserve-root", "do not remove '/' (default)"),
getopts::optflag("r", "recursive", "remove directories and their contents recursively"),
getopts::optflag("d", "dir", "remove empty directories"),
getopts::optflag("v", "verbose", "explain what is being done"),
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit")
];
let matches = match getopts::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => {
crash!(1, "{}", f.to_err_msg())
}
};
if matches.opt_present("help") {
println!("rm 1.0.0");
println!("");
println!("Usage:");
println!(" {0:s} [OPTION]... [FILE]...", program);
println!("");
print(getopts::usage("Remove (unlink) the FILE(s).", opts));
println!("");
println!("By default, rm does not remove directories. Use the --recursive (-r)");
println!("option to remove each listed directory, too, along with all of its contents");
println!("");
println!("To remove a file whose name starts with a '-', for example '-foo',");
println!("use one of these commands:");
println!("rm -- -foo");
println!("");
println!("rm ./-foo");
println!("");
println!("Note that if you use rm to remove a file, it might be possible to recover");
println!("some of its contents, given sufficient expertise and/or time. For greater");
println!("assurance that the contents are truly unrecoverable, consider using shred.");
} else if matches.opt_present("version") {
println!("rm 1.0.0");
} else if matches.free.is_empty() {
show_error!(1, "missing an argument");
show_error!(1, "for help, try '{0:s} --help'", program)
} else {
let force = matches.opt_present("force");
let interactive =
if matches.opt_present("i") {
InteractiveAlways
} else if matches.opt_present("I") {
InteractiveOnce
} else if matches.opt_present("interactive") {
match matches.opt_str("interactive").unwrap().as_slice() {
"none" => InteractiveNone,
"once" => InteractiveOnce,
"always" => InteractiveAlways,
val => {
crash!(1, "Invalid argument to interactive ({})", val)
}
}
} else {
InteractiveNone
};
let one_fs = matches.opt_present("one-file-system");
let preserve_root = !matches.opt_present("no-preserve-root");
let recursive = matches.opt_present("recursive");
let dir = matches.opt_present("dir");
let verbose = matches.opt_present("verbose");
if interactive == InteractiveOnce && (recursive || matches.free.len() > 3) {
let msg =
if recursive {
"Remove all arguments recursively? "
} else {
"Remove all arguments? "
};
if !prompt(msg) {
return;
}
}
remove(matches.free, force, interactive, one_fs, preserve_root,
recursive, dir, verbose);
}
}
// TODO: implement one-file-system
fn remove(files: Vec<~str>, force: bool, interactive: InteractiveMode, one_fs: bool, preserve_root: bool, recursive: bool, dir: bool, verbose: bool) {
for filename in files.iter() {
let file = Path::new(filename.to_owned());
if file.exists() {
if file.is_dir() {
if recursive && (*filename != ~"/" || !preserve_root) {
let walk_dir = match fs::walk_dir(&file) {
Ok(m) => m,
Err(f) => {
crash!(1, "{}", f.to_str());
}
};
remove(walk_dir.map(|x| x.as_str().unwrap().to_owned()).collect(), force, interactive, one_fs, preserve_root, recursive, dir, verbose);
remove_dir(&file, *filename, interactive, verbose);
} else if dir && (*filename != ~"/" || !preserve_root) {
remove_dir(&file, *filename, interactive, verbose);
} else {
if recursive {
show_error!(1, "could not remove directory '{}'",
*filename);
} else {
show_error!(1,
"could not remove directory '{}' (did you mean to pass '-r'?)",
*filename);
}
}
} else {
remove_file(&file, *filename, interactive, verbose);
}
} else if !force {
show_error!(1, "no such file or directory '{}'", *filename);
}
}
}
fn remove_dir(path: &Path, name: &str, interactive: InteractiveMode, verbose: bool) {
let response =
if interactive == InteractiveAlways {
prompt_file(path, name)
} else {
true
};
if response {
match fs::rmdir(path) {
Ok(_) => if verbose { println!("Removed '{}'", name); },
Err(f) => {
show_error!(1, "{}", f.to_str());
}
}
}
}
fn remove_file(path: &Path, name: &str, interactive: InteractiveMode, verbose: bool) {
let response =
if interactive == InteractiveAlways {
prompt_file(path, name)
} else {
true
};
if response {
match fs::unlink(path) {
Ok(_) => if verbose { println!("Removed '{}'", name); },
Err(f) => {
show_error!(1, "{}", f.to_str());
}
}
}
}
fn prompt_file(path: &Path, name: &str) -> bool {
if path.is_dir() {
prompt(format!("Remove directory '{}'? ", name))
} else {
prompt(format!("Remove file '{}'? ", name))
}
}
fn prompt(msg: &str) -> bool {
print(msg);
read_prompt()
}
fn read_prompt() -> bool {
stdio::flush();
match BufferedReader::new(stdin()).read_line() {
Ok(line) => {
match line.char_at(0) {
'y' | 'Y' => true,
'n' | 'N' => false,
_ => {
print!("Please enter either Y or N: ");
read_prompt()
}
}
}
Err(_) => true
}
}