From 1d3fe8402f41944f4da68e57f0b8d4a0667fe84d Mon Sep 17 00:00:00 2001 From: Arcterus Date: Wed, 18 Dec 2013 10:39:23 -0800 Subject: [PATCH] Implemented rmdir, which fixes #37 --- Makefile | 1 + README.md | 1 - rmdir/rmdir.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 rmdir/rmdir.rs diff --git a/Makefile b/Makefile index dd8a2e248..9d8d17163 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ EXES := \ false \ printenv \ pwd \ + rmdir \ true \ wc \ whoami \ diff --git a/README.md b/README.md index b0acd8962..759b5d1cb 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,6 @@ To do - relpath - remove - rm -- rmdir - runcon - seq - setuidgid diff --git a/rmdir/rmdir.rs b/rmdir/rmdir.rs new file mode 100644 index 000000000..1f9e3f7ee --- /dev/null +++ b/rmdir/rmdir.rs @@ -0,0 +1,103 @@ +#[link(name="rmdir", vers="1.0.0", author="Arcterus")]; + +/* + * This file is part of the uutils coreutils package. + * + * (c) Arcterus + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern mod extra; + +use std::os; +use std::io::{stderr, io_error, fs}; +use extra::getopts::groups; + +fn main() { + let args = os::args(); + let program = args[0].clone(); + + let opts = ~[ + groups::optflag("", "ignore-fail-on-non-empty", "ignore each failure that is solely because a directory is non-empty"), + groups::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"), + groups::optflag("v", "verbose", "output a diagnostic for every directory processed"), + groups::optflag("", "help", "print this help and exit"), + groups::optflag("", "version", "output version information and exit") + ]; + let matches = match groups::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => { + writeln!(&mut stderr() as &mut Writer, "{}", f.to_err_msg()); + os::set_exit_status(1); + return + } + }; + + if matches.opt_present("help") { + println("rmdir 1.0.0"); + println(""); + println("Usage:"); + println!(" {0:s} [OPTION]... DIRECTORY...", program); + println(""); + print(groups::usage("Remove the DIRECTORY(ies), if they are empty.", opts)); + } else if matches.opt_present("version") { + println("rmdir 1.0.0"); + } else if matches.free.is_empty() { + writeln!(&mut stderr() as &mut Writer, "Missing an argument"); + writeln!(&mut stderr() as &mut Writer, + "For help, try '{0:s} --help'", program); + os::set_exit_status(1); + } 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 { + writeln!(&mut stderr() as &mut Writer, + "Failed to remove '{}' (file)", *dir); + os::set_exit_status(1); + } + } else { + writeln!(&mut stderr() as &mut Writer, + "No such file or directory '{}'", *dir); + os::set_exit_status(1); + } + } +} + +fn remove_dir(path: &Path, dir: &~str, ignore: bool, parents: bool, verbose: bool) { + if fs::walk_dir(path).next() == None { + io_error::cond.trap(|_| { + writeln!(&mut stderr() as &mut Writer, + "Failed to remove directory '{}'", *dir); + os::set_exit_status(1); + }).inside(|| { + fs::rmdir(path); + 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); + } + } + }); + } else if !ignore { + writeln!(&mut stderr() as &mut Writer, + "Failed to remove directory '{}' (non-empty)", *dir); + os::set_exit_status(1); + } +} +