coreutils/rmdir/rmdir.rs

131 lines
4 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +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-03-31 16:40:21 +00:00
#![feature(macro_rules)]
extern crate getopts;
extern crate libc;
2013-12-18 18:39:23 +00:00
use std::os;
use std::io::{print, fs};
2014-02-23 22:17:48 +00:00
#[path = "../common/util.rs"]
mod util;
static NAME: &'static str = "rmdir";
2013-12-18 18:39:23 +00:00
#[allow(dead_code)]
fn main() { os::set_exit_status(uumain(os::args())); }
2014-05-28 11:43:37 +00:00
pub fn uumain(args: Vec<String>) -> int {
2014-05-16 08:32:58 +00:00
let program = args.get(0).clone();
2013-12-18 18:39:23 +00:00
2014-05-30 08:35:54 +00:00
let opts = [
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
];
let matches = match getopts::getopts(args.tail(), opts) {
2013-12-18 18:39:23 +00:00
Ok(m) => m,
Err(f) => {
show_errer!("{}", f.to_err_msg());
return 1;
2013-12-18 18:39:23 +00:00
}
};
if matches.opt_present("help") {
println!("rmdir 1.0.0");
println!("");
println!("Usage:");
2013-12-18 18:39:23 +00:00
println!(" {0:s} [OPTION]... DIRECTORY...", program);
println!("");
2014-05-17 10:32:14 +00:00
print(getopts::usage("Remove the DIRECTORY(ies), if they are empty.", opts).as_slice());
2013-12-18 18:39:23 +00:00
} else if matches.opt_present("version") {
println!("rmdir 1.0.0");
2013-12-18 18:39:23 +00:00
} else if matches.free.is_empty() {
show_errer!("missing an argument");
show_errer!("for help, try '{0:s} --help'", program);
return 1;
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");
match remove(matches.free, ignore, parents, verbose) {
Ok(()) => ( /* pass */ ),
Err(e) => return e
}
2013-12-18 18:39:23 +00:00
}
return 0;
2013-12-18 18:39:23 +00:00
}
fn remove(dirs: Vec<String>, ignore: bool, parents: bool, verbose: bool) -> Result<(), int>{
2013-12-18 18:39:23 +00:00
for dir in dirs.iter() {
2014-05-17 10:32:14 +00:00
let path = Path::new(dir.as_slice());
2013-12-18 18:39:23 +00:00
if path.exists() {
if path.is_dir() {
match remove_dir(&path, dir.as_slice(), ignore, parents, verbose) {
Ok(()) => ( /* pass */ ),
Err(e) => return Err(e)
}
2013-12-18 18:39:23 +00:00
} else {
show_errer!("failed to remove '{}' (file)", *dir);
return Err(1);
2013-12-18 18:39:23 +00:00
}
} else {
show_errer!("no such file or directory '{}'", *dir);
return Err(1);
2013-12-18 18:39:23 +00:00
}
}
return Ok(());
2013-12-18 18:39:23 +00:00
}
fn remove_dir(path: &Path, dir: &str, ignore: bool, parents: bool, verbose: bool) -> Result<(), int> {
2014-02-05 03:39:17 +00:00
let mut walk_dir = match fs::walk_dir(path) {
Ok(m) => m,
Err(f) => {
show_errer!("{}", f.to_str());
return Err(1);
2014-02-05 03:39:17 +00:00
}
};
if walk_dir.next() == None {
match fs::rmdir(path) {
Ok(_) => {
if verbose {
2014-05-17 10:32:14 +00:00
println!("Removed directory '{}'", dir);
2014-02-05 03:39:17 +00:00
}
if parents {
let dirname = path.dirname_str().unwrap();
if dirname != "." {
match remove_dir(&Path::new(dirname), dirname, ignore, parents, verbose) {
Ok(()) => ( /* pass */ ),
Err(e) => return Err(e)
}
2014-02-05 03:39:17 +00:00
}
2013-12-18 18:39:23 +00:00
}
}
2014-02-05 03:39:17 +00:00
Err(f) => {
show_errer!("{}", f.to_str());
return Err(1);
2014-02-05 03:39:17 +00:00
}
}
2013-12-18 18:39:23 +00:00
} else if !ignore {
show_errer!("Failed to remove directory '{}' (non-empty)", dir);
return Err(1);
2013-12-18 18:39:23 +00:00
}
return Ok(());
2013-12-18 18:39:23 +00:00
}