This commit is contained in:
Michael Zhang 2020-11-09 01:01:28 -06:00
parent d6a3c1785c
commit 082efef439
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
5 changed files with 63 additions and 6 deletions

32
Cargo.lock generated
View file

@ -196,6 +196,7 @@ dependencies = [
"log", "log",
"percent-encoding", "percent-encoding",
"prettytable-rs", "prettytable-rs",
"stderrlog",
"structopt", "structopt",
"thiserror", "thiserror",
"walkdir", "walkdir",
@ -434,6 +435,19 @@ version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" 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]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.8.0"
@ -486,6 +500,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"
@ -515,6 +538,15 @@ dependencies = [
"syn", "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]] [[package]]
name = "time" name = "time"
version = "0.1.44" version = "0.1.44"

View file

@ -19,6 +19,7 @@ libmount = "0.1"
log = "0.4" log = "0.4"
percent-encoding = "2.1" percent-encoding = "2.1"
prettytable-rs = "0.8.0" prettytable-rs = "0.8.0"
stderrlog = "0.5.0"
structopt = "0.3" structopt = "0.3"
thiserror = "1.0" thiserror = "1.0"
walkdir = "2.3" walkdir = "2.3"

View file

@ -2,6 +2,8 @@
#![warn(missing_docs)] #![warn(missing_docs)]
#[macro_use]
extern crate log;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]

View file

@ -6,6 +6,16 @@ use garbage::{
}; };
use structopt::StructOpt; 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)] #[derive(StructOpt)]
enum Command { enum Command {
/// Empty a trash directory. /// Empty a trash directory.
@ -30,8 +40,7 @@ enum Command {
Restore(RestoreOptions), Restore(RestoreOptions),
} }
fn run() -> Result<()> { fn run(cmd: Command) -> Result<()> {
let cmd = Command::from_args();
match cmd { match cmd {
Command::Empty(options) => ops::empty(options), Command::Empty(options) => ops::empty(options),
Command::List(options) => ops::list(options), Command::List(options) => ops::list(options),
@ -41,7 +50,15 @@ fn run() -> Result<()> {
} }
fn main() { fn main() {
match run() { let opt = Opt::from_args();
stderrlog::new()
.module(module_path!())
.verbosity(opt.verbose)
.init()
.unwrap();
match run(opt.command) {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
eprintln!("Error: {}", err); eprintln!("Error: {}", err);

View file

@ -66,13 +66,18 @@ pub struct PutOptions {
/// Throw some files into the trash. /// Throw some files into the trash.
pub fn put(options: PutOptions) -> Result<()> { pub fn put(options: PutOptions) -> Result<()> {
for path in options.paths.iter() { for path in options.paths.iter() {
let abs_path = utils::into_absolute(&path)?;
// don't allow deleting '.' or '..' // don't allow deleting '.' or '..'
let current_dir = env::current_dir()?; 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() || (current_dir.parent().is_some()
&& utils::into_absolute(&path)? && abs_path == current_dir.parent().unwrap())
== current_dir.parent().unwrap()))
{ {
return Err(Error::Put(PutError::CannotTrashDotDirs)); return Err(Error::Put(PutError::CannotTrashDotDirs));
} }