From 9b8dcb40fa5381b88204bbb70f594c253f5170d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Miguel?= Date: Tue, 6 Apr 2021 04:12:00 -0300 Subject: [PATCH] cli: Canonicalize input files when decompressing --- src/cli.rs | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 1e646e8..865d3e1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use std::{env, ffi::OsString, io, path::PathBuf, vec::Vec}; +use std::{env, ffi::OsString, path::{Path, PathBuf}, vec::Vec}; use oof::{arg_flag, flag}; @@ -39,8 +39,28 @@ pub struct ParsedArgs { // pub program_called: OsString, // Useful? } -fn canonicalize_files(files: Vec) -> io::Result> { - files.into_iter().map(|path| path.canonicalize()).collect() +fn canonicalize<'a, P>(path: P) -> crate::Result +where + P: AsRef + 'a +{ + match std::fs::canonicalize(&path.as_ref()) { + Ok(abs_path) => Ok(abs_path), + Err(io_err) => { + if !path.as_ref().exists() { + Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref()))) + } else { + eprintln!("{} {}", "[ERROR]", io_err); + Err(crate::Error::IoError) + } + } + } +} + +fn canonicalize_files<'a, P>(files: Vec

) -> crate::Result> +where + P: AsRef + 'a, +{ + files.into_iter().map(canonicalize).collect() } pub fn parse_args_from(mut args: Vec) -> crate::Result { @@ -89,12 +109,17 @@ pub fn parse_args_from(mut args: Vec) -> crate::Result { // Parse flags let (args, mut flags) = oof::filter_flags(args, &flags_info)?; - let files: Vec<_> = args.into_iter().map(PathBuf::from).collect(); - // TODO: This line doesn't seem to be working correctly + let files = args.into_iter().map(canonicalize); + for file in files.clone() { + if let Err(err) = file { + return Err(err); + } + } + let files = files.map(Result::unwrap).collect(); + let output_folder = flags.take_arg("output").map(PathBuf::from); + // TODO: ensure all files are decompressible - // Is the output here fully correct? - // With the paths not canonicalized? let command = Command::Decompress { files, output_folder,