diff --git a/Cargo.lock b/Cargo.lock index 51a87f6..fea4482 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,6 +196,7 @@ dependencies = [ "log", "percent-encoding", "prettytable-rs", + "stderrlog", "structopt", "thiserror", "walkdir", @@ -434,6 +435,19 @@ version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" +[[package]] +name = "stderrlog" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b02f316286ae558d83acc93dd81eaba096e746987a7961d4a9ae026842bae67f" +dependencies = [ + "atty", + "chrono", + "log", + "termcolor", + "thread_local", +] + [[package]] name = "strsim" version = "0.8.0" @@ -486,6 +500,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -515,6 +538,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + [[package]] name = "time" version = "0.1.44" diff --git a/Cargo.toml b/Cargo.toml index 81e7833..e3e0c86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ libmount = "0.1" log = "0.4" percent-encoding = "2.1" prettytable-rs = "0.8.0" +stderrlog = "0.5.0" structopt = "0.3" thiserror = "1.0" walkdir = "2.3" diff --git a/src/lib.rs b/src/lib.rs index c696cc3..119d3ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ #![warn(missing_docs)] +#[macro_use] +extern crate log; #[macro_use] extern crate lazy_static; #[macro_use] diff --git a/src/main.rs b/src/main.rs index 091929a..8804dab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,16 @@ use garbage::{ }; use structopt::StructOpt; +#[derive(StructOpt)] +struct Opt { + #[structopt(subcommand)] + command: Command, + + /// Verbosity (-v, -vv, -vvv, etc) + #[structopt(short = "v", long = "verbose", parse(from_occurrences))] + verbose: usize, +} + #[derive(StructOpt)] enum Command { /// Empty a trash directory. @@ -30,8 +40,7 @@ enum Command { Restore(RestoreOptions), } -fn run() -> Result<()> { - let cmd = Command::from_args(); +fn run(cmd: Command) -> Result<()> { match cmd { Command::Empty(options) => ops::empty(options), Command::List(options) => ops::list(options), @@ -41,7 +50,15 @@ fn run() -> Result<()> { } fn main() { - match run() { + let opt = Opt::from_args(); + + stderrlog::new() + .module(module_path!()) + .verbosity(opt.verbose) + .init() + .unwrap(); + + match run(opt.command) { Ok(_) => (), Err(err) => { eprintln!("Error: {}", err); diff --git a/src/ops/put.rs b/src/ops/put.rs index 844fd88..6d42b7d 100644 --- a/src/ops/put.rs +++ b/src/ops/put.rs @@ -66,13 +66,18 @@ pub struct PutOptions { /// Throw some files into the trash. pub fn put(options: PutOptions) -> Result<()> { for path in options.paths.iter() { + let abs_path = utils::into_absolute(&path)?; + // don't allow deleting '.' or '..' let current_dir = env::current_dir()?; + let parent = current_dir.parent(); + info!("Checking if {:?} is . or ..", abs_path); + trace!("curr = {:?}", current_dir); + trace!("parent = {:?}", parent); - if !(utils::into_absolute(&path)? == current_dir.as_path() + if abs_path == current_dir.as_path() || (current_dir.parent().is_some() - && utils::into_absolute(&path)? - == current_dir.parent().unwrap())) + && abs_path == current_dir.parent().unwrap()) { return Err(Error::Put(PutError::CannotTrashDotDirs)); }