2013-12-26 10:49:31 +00:00
|
|
|
#[crate_id(name="rmdir", vers="1.0.0", author="Arcterus")];
|
2013-12-18 18:39:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the uutils coreutils package.
|
|
|
|
*
|
|
|
|
* (c) Arcterus <arcterus@mail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
#[feature(macro_rules)];
|
|
|
|
|
2013-12-18 18:39:23 +00:00
|
|
|
extern mod extra;
|
2014-02-07 06:39:07 +00:00
|
|
|
extern mod getopts;
|
2013-12-18 18:39:23 +00:00
|
|
|
|
|
|
|
use std::os;
|
2014-02-07 06:39:07 +00:00
|
|
|
use std::io::{print, fs};
|
|
|
|
|
|
|
|
#[path = "../util.rs"]
|
|
|
|
mod util;
|
|
|
|
|
|
|
|
static NAME: &'static str = "rmdir";
|
2013-12-18 18:39:23 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
|
|
|
let program = args[0].clone();
|
|
|
|
|
|
|
|
let opts = ~[
|
2014-02-07 06:39:07 +00:00
|
|
|
getopts::optflag("", "ignore-fail-on-non-empty", "ignore each failure that is solely because a directory is non-empty"),
|
|
|
|
getopts::optflag("p", "parents", "remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is similar to rmdir a/b/c a/b a"),
|
|
|
|
getopts::optflag("v", "verbose", "output a diagnostic for every directory processed"),
|
|
|
|
getopts::optflag("h", "help", "print this help and exit"),
|
|
|
|
getopts::optflag("V", "version", "output version information and exit")
|
2013-12-18 18:39:23 +00:00
|
|
|
];
|
2014-02-07 06:39:07 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
2013-12-18 18:39:23 +00:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => {
|
2014-02-07 06:39:07 +00:00
|
|
|
show_error!(1, "{}", f.to_err_msg());
|
|
|
|
return;
|
2013-12-18 18:39:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("help") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("rmdir 1.0.0");
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2013-12-18 18:39:23 +00:00
|
|
|
println!(" {0:s} [OPTION]... DIRECTORY...", program);
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("");
|
2014-02-07 06:39:07 +00:00
|
|
|
print(getopts::usage("Remove the DIRECTORY(ies), if they are empty.", opts));
|
2013-12-18 18:39:23 +00:00
|
|
|
} else if matches.opt_present("version") {
|
2014-01-13 09:05:02 +00:00
|
|
|
println!("rmdir 1.0.0");
|
2013-12-18 18:39:23 +00:00
|
|
|
} else if matches.free.is_empty() {
|
2014-02-07 06:39:07 +00:00
|
|
|
show_error!(1, "missing an argument");
|
|
|
|
show_error!(1, "for help, try '{0:s} --help'", program);
|
2013-12-18 18:39:23 +00:00
|
|
|
} else {
|
|
|
|
let ignore = matches.opt_present("ignore-fail-on-non-empty");
|
|
|
|
let parents = matches.opt_present("parents");
|
|
|
|
let verbose = matches.opt_present("verbose");
|
|
|
|
remove(matches.free, ignore, parents, verbose);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(dirs: &[~str], ignore: bool, parents: bool, verbose: bool) {
|
|
|
|
for dir in dirs.iter() {
|
|
|
|
let path = Path::new(dir.to_owned());
|
|
|
|
if path.exists() {
|
|
|
|
if path.is_dir() {
|
|
|
|
remove_dir(&path, dir, ignore, parents, verbose);
|
|
|
|
} else {
|
2014-02-07 06:39:07 +00:00
|
|
|
show_error!(1, "failed to remove '{}' (file)", *dir);
|
2013-12-18 18:39:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-07 06:39:07 +00:00
|
|
|
show_error!(1, "no such file or directory '{}'", *dir);
|
2013-12-18 18:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_dir(path: &Path, dir: &~str, ignore: bool, parents: bool, verbose: bool) {
|
2014-02-05 03:39:17 +00:00
|
|
|
let mut walk_dir = match fs::walk_dir(path) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => {
|
2014-02-07 06:39:07 +00:00
|
|
|
show_error!(1, "{}", f.to_str());
|
2014-02-05 03:39:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if walk_dir.next() == None {
|
|
|
|
match fs::rmdir(path) {
|
|
|
|
Ok(_) => {
|
|
|
|
if verbose {
|
|
|
|
println!("Removed directory '{}'", *dir);
|
|
|
|
}
|
|
|
|
if parents {
|
|
|
|
let dirname = path.dirname_str().unwrap();
|
|
|
|
if dirname != "." {
|
|
|
|
remove_dir(&Path::new(dirname), &dirname.to_owned(), ignore, parents, verbose);
|
|
|
|
}
|
2013-12-18 18:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-05 03:39:17 +00:00
|
|
|
Err(f) => {
|
2014-02-07 06:39:07 +00:00
|
|
|
show_error!(1, "{}", f.to_str());
|
2014-02-05 03:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 18:39:23 +00:00
|
|
|
} else if !ignore {
|
2014-02-07 06:39:07 +00:00
|
|
|
show_error!(1, "Failed to remove directory '{}' (non-empty)", *dir);
|
2013-12-18 18:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|