From eac0a0687ac8268d40ba9b90d364c360b72b4cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 10:59:51 -0300 Subject: [PATCH 01/10] fix clippy warnings --- src/error.rs | 4 ++-- src/utils/question.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 34a9c99..63685b5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,7 +11,7 @@ use crate::utils::colors::*; #[allow(missing_docs)] /// All errors that can be generated by `ouch` -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub enum Error { /// Not every IoError, some of them get filtered by `From` into other variants IoError { reason: String }, @@ -42,7 +42,7 @@ pub type Result = std::result::Result; pub type CowStr = Cow<'static, str>; /// Pretty final error message for end users, crashing the program after display. -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct FinalError { /// Should be made of just one line, appears after the "\[ERROR\]" part title: CowStr, diff --git a/src/utils/question.rs b/src/utils/question.rs index 2685b92..13ef5b6 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -17,7 +17,7 @@ use crate::{ utils::colors, }; -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] /// Determines if overwrite questions should be skipped or asked to the user pub enum QuestionPolicy { /// Ask the user every time @@ -28,7 +28,7 @@ pub enum QuestionPolicy { AlwaysNo, } -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] /// Determines which action is being questioned pub enum QuestionAction { /// question called from a compression function From c65959d6487b0ffc60bd5a02372a54d21b6ea4f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 11:46:23 -0300 Subject: [PATCH 02/10] break down commands.rs into multiple files --- src/commands.rs | 764 ------------------------------------- src/commands/compress.rs | 142 +++++++ src/commands/decompress.rs | 241 ++++++++++++ src/commands/list.rs | 88 +++++ src/commands/mod.rs | 359 +++++++++++++++++ src/main.rs | 3 + 6 files changed, 833 insertions(+), 764 deletions(-) delete mode 100644 src/commands.rs create mode 100644 src/commands/compress.rs create mode 100644 src/commands/decompress.rs create mode 100644 src/commands/list.rs create mode 100644 src/commands/mod.rs diff --git a/src/commands.rs b/src/commands.rs deleted file mode 100644 index 4658e3d..0000000 --- a/src/commands.rs +++ /dev/null @@ -1,764 +0,0 @@ -//! Core of the crate, where the `compress_files` and `decompress_file` functions are implemented -//! -//! Also, where correctly call functions based on the detected `Command`. - -use std::{ - io::{self, BufReader, BufWriter, Read, Write}, - ops::ControlFlow, - path::{Path, PathBuf}, -}; - -use fs_err as fs; -use utils::colors; - -use crate::{ - archive, - error::FinalError, - extension::{ - self, - CompressionFormat::{self, *}, - Extension, - }, - info, - list::{self, FileInArchive, ListOptions}, - progress::Progress, - utils::{ - self, concatenate_os_str_list, dir_is_empty, nice_directory_display, to_utf, try_infer_extension, - user_wants_to_continue, FileVisibilityPolicy, - }, - warning, Opts, QuestionAction, QuestionPolicy, Subcommand, -}; - -// Message used to advice the user that .zip archives have limitations that require it to load everything into memory at once -// and this can lead to out-of-memory scenarios for archives that are big enough. -const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = - "\tThere is a limitation for .zip archives with extra extensions. (e.g. .zip.gz) -\tThe design of .zip makes it impossible to compress via stream, so it must be done entirely in memory. -\tBy compressing .zip with extra compression formats, you can run out of RAM if the file is too large!"; - -// Used in BufReader and BufWriter to perform less syscalls -const BUFFER_CAPACITY: usize = 1024 * 64; - -fn represents_several_files(files: &[PathBuf]) -> bool { - let is_non_empty_dir = |path: &PathBuf| { - let is_non_empty = || !dir_is_empty(path); - - path.is_dir().then(is_non_empty).unwrap_or_default() - }; - - files.iter().any(is_non_empty_dir) || files.len() > 1 -} - -/// Entrypoint of ouch, receives cli options and matches Subcommand to decide what to do -pub fn run( - args: Opts, - question_policy: QuestionPolicy, - file_visibility_policy: FileVisibilityPolicy, -) -> crate::Result<()> { - match args.cmd { - Subcommand::Compress { mut files, output: output_path } => { - // If the output_path file exists and is the same as some of the input files, warn the user and skip those inputs (in order to avoid compression recursion) - if output_path.exists() { - clean_input_files_if_needed(&mut files, &fs::canonicalize(&output_path)?); - } - // After cleaning, if there are no input files left, exit - if files.is_empty() { - return Err(FinalError::with_title("No files to compress").into()); - } - - // Formats from path extension, like "file.tar.gz.xz" -> vec![Tar, Gzip, Lzma] - let mut formats = extension::extensions_from_path(&output_path); - - if formats.is_empty() { - let error = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path))) - .detail("You shall supply the compression format") - .hint("Try adding supported extensions (see --help):") - .hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path))) - .hint(format!(" ouch compress ... {}.zip", to_utf(&output_path))) - .hint("") - .hint("Alternatively, you can overwrite this option by using the '--format' flag:") - .hint(format!(" ouch compress ... {} --format tar.gz", to_utf(&output_path))); - - return Err(error.into()); - } - - if !formats.get(0).map(Extension::is_archive).unwrap_or(false) && represents_several_files(&files) { - // This piece of code creates a suggestion for compressing multiple files - // It says: - // Change from file.bz.xz - // To file.tar.bz.xz - let extensions_text: String = formats.iter().map(|format| format.to_string()).collect(); - - let output_path = to_utf(&output_path).to_string(); - - // Breaks if Lzma is .lz or .lzma and not .xz - // Or if Bzip is .bz2 and not .bz - let extensions_start_position = output_path.rfind(&extensions_text).unwrap(); - let pos = extensions_start_position - 1; - let mut suggested_output_path = output_path.to_string(); - suggested_output_path.insert_str(pos, ".tar"); - - let error = FinalError::with_title(format!("Cannot compress to '{}'.", output_path)) - .detail("You are trying to compress multiple files.") - .detail(format!("The compression format '{}' cannot receive multiple files.", &formats[0])) - .detail("The only supported formats that archive files into an archive are .tar and .zip.") - .hint(format!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0])) - .hint(format!("From: {}", output_path)) - .hint(format!("To: {}", suggested_output_path)); - - return Err(error.into()); - } - - if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { - let error = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path))) - .detail(format!("Found the format '{}' in an incorrect position.", format)) - .detail(format!("'{}' can only be used at the start of the file extension.", format)) - .hint(format!("If you wish to compress multiple files, start the extension with '{}'.", format)) - .hint(format!("Otherwise, remove the last '{}' from '{}'.", format, to_utf(&output_path))); - - return Err(error.into()); - } - - if output_path.exists() && !utils::user_wants_to_overwrite(&output_path, question_policy)? { - // User does not want to overwrite this file, skip and return without any errors - return Ok(()); - } - - let output_file = fs::File::create(&output_path)?; - - if !represents_several_files(&files) { - // It is possible the file is already partially compressed so we don't want to compress it again - // `ouch compress file.tar.gz file.tar.gz.xz` should produce `file.tar.gz.xz` and not `file.tar.gz.tar.gz.xz` - let input_extensions = extension::extensions_from_path(&files[0]); - - // We calculate the formats that are left if we filter out a sublist at the start of what we have that's the same as the input formats - let mut new_formats = Vec::with_capacity(formats.len()); - for (inp_ext, out_ext) in input_extensions.iter().zip(&formats) { - if inp_ext.compression_formats == out_ext.compression_formats { - new_formats.push(out_ext.clone()); - } else if inp_ext - .compression_formats - .iter() - .zip(out_ext.compression_formats.iter()) - .all(|(inp, out)| inp == out) - { - let new_ext = Extension::new( - &out_ext.compression_formats[..inp_ext.compression_formats.len()], - &out_ext.display_text, - ); - new_formats.push(new_ext); - break; - } - } - // If the input is a sublist at the start of `formats` then remove the extensions - // Note: If input_extensions is empty then it will make `formats` empty too, which we don't want - if !input_extensions.is_empty() && new_formats != formats { - // Safety: - // We checked above that input_extensions isn't empty, so files[0] has an extension. - // - // Path::extension says: "if there is no file_name, then there is no extension". - // Contrapositive statement: "if there is extension, then there is file_name". - info!( - accessible, // important information - "Partial compression detected. Compressing {} into {}", - to_utf(files[0].as_path().file_name().unwrap().as_ref()), - to_utf(&output_path) - ); - formats = new_formats; - } - } - let compress_result = - compress_files(files, formats, output_file, &output_path, question_policy, file_visibility_policy); - - if let Ok(true) = compress_result { - // this is only printed once, so it doesn't result in much text. On the other hand, - // having a final status message is important especially in an accessibility context - // as screen readers may not read a commands exit code, making it hard to reason - // about whether the command succeeded without such a message - info!(accessible, "Successfully compressed '{}'.", to_utf(&output_path)); - } else { - // If Ok(false) or Err() occurred, delete incomplete file - // Print an extra alert message pointing out that we left a possibly - // CORRUPTED FILE at `output_path` - if let Err(err) = fs::remove_file(&output_path) { - eprintln!("{red}FATAL ERROR:\n", red = *colors::RED); - eprintln!(" Please manually delete '{}'.", to_utf(&output_path)); - eprintln!(" Compression failed and we could not delete '{}'.", to_utf(&output_path),); - eprintln!(" Error:{reset} {}{red}.{reset}\n", err, reset = *colors::RESET, red = *colors::RED); - } - } - - compress_result?; - } - Subcommand::Decompress { files, output_dir } => { - let mut output_paths = vec![]; - let mut formats = vec![]; - - for path in files.iter() { - let (file_output_path, file_formats) = extension::separate_known_extensions_from_name(path); - output_paths.push(file_output_path); - formats.push(file_formats); - } - - if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { - return Ok(()); - } - - let files_missing_format: Vec = files - .iter() - .zip(&formats) - .filter(|(_, formats)| formats.is_empty()) - .map(|(input_path, _)| PathBuf::from(input_path)) - .collect(); - - if !files_missing_format.is_empty() { - let error = FinalError::with_title("Cannot decompress files without extensions") - .detail(format!( - "Files without supported extensions: {}", - concatenate_os_str_list(&files_missing_format) - )) - .detail("Decompression formats are detected automatically by the file extension") - .hint("Provide a file with a supported extension:") - .hint(" ouch decompress example.tar.gz") - .hint("") - .hint("Or overwrite this option with the '--format' flag:") - .hint(format!(" ouch decompress {} --format tar.gz", to_utf(&files_missing_format[0]))); - - return Err(error.into()); - } - - // The directory that will contain the output files - // We default to the current directory if the user didn't specify an output directory with --dir - let output_dir = if let Some(dir) = output_dir { - if !utils::clear_path(&dir, question_policy)? { - // User doesn't want to overwrite - return Ok(()); - } - utils::create_dir_if_non_existent(&dir)?; - dir - } else { - PathBuf::from(".") - }; - - for ((input_path, formats), file_name) in files.iter().zip(formats).zip(output_paths) { - let output_file_path = output_dir.join(file_name); // Path used by single file format archives - decompress_file(input_path, formats, &output_dir, output_file_path, question_policy)?; - } - } - Subcommand::List { archives: files, tree } => { - let mut formats = vec![]; - - for path in files.iter() { - let (_, file_formats) = extension::separate_known_extensions_from_name(path); - formats.push(file_formats); - } - - if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { - return Ok(()); - } - - let not_archives: Vec = files - .iter() - .zip(&formats) - .filter(|(_, formats)| !formats.get(0).map(Extension::is_archive).unwrap_or(false)) - .map(|(path, _)| path.clone()) - .collect(); - - if !not_archives.is_empty() { - let error = FinalError::with_title("Cannot list archive contents") - .detail("Only archives can have their contents listed") - .detail(format!("Files are not archives: {}", concatenate_os_str_list(¬_archives))); - - return Err(error.into()); - } - - let list_options = ListOptions { tree }; - - for (i, (archive_path, formats)) in files.iter().zip(formats).enumerate() { - if i > 0 { - println!(); - } - let formats = formats.iter().flat_map(Extension::iter).map(Clone::clone).collect(); - list_archive_contents(archive_path, formats, list_options, question_policy)?; - } - } - } - Ok(()) -} - -// Compress files into an `output_file` -// -// files are the list of paths to be compressed: ["dir/file1.txt", "dir/file2.txt"] -// formats contains each format necessary for compression, example: [Tar, Gz] (in compression order) -// output_file is the resulting compressed file name, example: "compressed.tar.gz" -// -// Returns Ok(true) if compressed all files successfully, and Ok(false) if user opted to skip files -fn compress_files( - files: Vec, - formats: Vec, - output_file: fs::File, - output_dir: &Path, - question_policy: QuestionPolicy, - file_visibility_policy: FileVisibilityPolicy, -) -> crate::Result { - // The next lines are for displaying the progress bar - // If the input files contain a directory, then the total size will be underestimated - let (total_input_size, precise) = files - .iter() - .map(|f| (f.metadata().expect("file exists").len(), f.is_file())) - .fold((0, true), |(total_size, and_precise), (size, precise)| (total_size + size, and_precise & precise)); - - // NOTE: canonicalize is here to avoid a weird bug: - // > If output_file_path is a nested path and it exists and the user overwrite it - // >> output_file_path.exists() will always return false (somehow) - // - canonicalize seems to fix this - let output_file_path = output_file.path().canonicalize()?; - - let file_writer = BufWriter::with_capacity(BUFFER_CAPACITY, output_file); - - let mut writer: Box = Box::new(file_writer); - - // Grab previous encoder and wrap it inside of a new one - let chain_writer_encoder = |format: &CompressionFormat, encoder: Box| -> crate::Result> { - let encoder: Box = match format { - Gzip => Box::new(flate2::write::GzEncoder::new(encoder, Default::default())), - Bzip => Box::new(bzip2::write::BzEncoder::new(encoder, Default::default())), - Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(encoder, Default::default())?), - Lzma => Box::new(xz2::write::XzEncoder::new(encoder, 6)), - Snappy => Box::new(snap::write::FrameEncoder::new(encoder)), - Zstd => { - let zstd_encoder = zstd::stream::write::Encoder::new(encoder, Default::default()); - // Safety: - // Encoder::new() can only fail if `level` is invalid, but Default::default() - // is guaranteed to be valid - Box::new(zstd_encoder.unwrap().auto_finish()) - } - Tar | Zip => unreachable!(), - }; - Ok(encoder) - }; - - for format in formats.iter().flat_map(Extension::iter).skip(1).collect::>().iter().rev() { - writer = chain_writer_encoder(format, writer)?; - } - - match formats[0].compression_formats[0] { - Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { - let _progress = Progress::new_accessible_aware( - total_input_size, - precise, - Some(Box::new(move || output_file_path.metadata().expect("file exists").len())), - ); - - writer = chain_writer_encoder(&formats[0].compression_formats[0], writer)?; - let mut reader = fs::File::open(&files[0]).unwrap(); - io::copy(&mut reader, &mut writer)?; - } - Tar => { - let mut progress = Progress::new_accessible_aware( - total_input_size, - precise, - Some(Box::new(move || output_file_path.metadata().expect("file exists").len())), - ); - - archive::tar::build_archive_from_paths( - &files, - &mut writer, - file_visibility_policy, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), - )?; - writer.flush()?; - } - Zip => { - if formats.len() > 1 { - eprintln!("{orange}[WARNING]{reset}", orange = *colors::ORANGE, reset = *colors::RESET); - eprintln!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); - - // give user the option to continue compressing after warning is shown - if !user_wants_to_continue(output_dir, question_policy, QuestionAction::Compression)? { - return Ok(false); - } - } - - let mut vec_buffer = io::Cursor::new(vec![]); - - let current_position_fn = { - let vec_buffer_ptr = { - struct FlyPtr(*const io::Cursor>); - unsafe impl Send for FlyPtr {} - FlyPtr(&vec_buffer as *const _) - }; - Box::new(move || { - let vec_buffer_ptr = &vec_buffer_ptr; - // Safety: ptr is valid and vec_buffer is still alive - unsafe { &*vec_buffer_ptr.0 }.position() - }) - }; - - let mut progress = Progress::new_accessible_aware(total_input_size, precise, Some(current_position_fn)); - - archive::zip::build_archive_from_paths( - &files, - &mut vec_buffer, - file_visibility_policy, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), - )?; - let vec_buffer = vec_buffer.into_inner(); - io::copy(&mut vec_buffer.as_slice(), &mut writer)?; - } - } - - Ok(true) -} - -// Decompress a file -// -// File at input_file_path is opened for reading, example: "archive.tar.gz" -// formats contains each format necessary for decompression, example: [Gz, Tar] (in decompression order) -// output_dir it's where the file will be decompressed to, this function assumes that the directory exists -// output_file_path is only used when extracting single file formats, not archive formats like .tar or .zip -fn decompress_file( - input_file_path: &Path, - formats: Vec, - output_dir: &Path, - output_file_path: PathBuf, - question_policy: QuestionPolicy, -) -> crate::Result<()> { - assert!(output_dir.exists()); - let total_input_size = input_file_path.metadata().expect("file exists").len(); - let reader = fs::File::open(&input_file_path)?; - // Zip archives are special, because they require io::Seek, so it requires it's logic separated - // from decoder chaining. - // - // This is the only case where we can read and unpack it directly, without having to do - // in-memory decompression/copying first. - // - // Any other Zip decompression done can take up the whole RAM and freeze ouch. - if formats.len() == 1 && *formats[0].compression_formats == [Zip] { - let zip_archive = zip::ZipArchive::new(reader)?; - let files = if let ControlFlow::Continue(files) = smart_unpack( - Box::new(move |output_dir| { - let mut progress = Progress::new_accessible_aware(total_input_size, true, None); - crate::archive::zip::unpack_archive( - zip_archive, - output_dir, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), - ) - }), - output_dir, - &output_file_path, - question_policy, - )? { - files - } else { - return Ok(()); - }; - - // this is only printed once, so it doesn't result in much text. On the other hand, - // having a final status message is important especially in an accessibility context - // as screen readers may not read a commands exit code, making it hard to reason - // about whether the command succeeded without such a message - info!( - accessible, - "Successfully decompressed archive in {} ({} files).", - nice_directory_display(output_dir), - files.len() - ); - - return Ok(()); - } - - // Will be used in decoder chaining - let reader = BufReader::with_capacity(BUFFER_CAPACITY, reader); - let mut reader: Box = Box::new(reader); - - // Grab previous decoder and wrap it inside of a new one - let chain_reader_decoder = |format: &CompressionFormat, decoder: Box| -> crate::Result> { - let decoder: Box = match format { - Gzip => Box::new(flate2::read::GzDecoder::new(decoder)), - Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)), - Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?), - Lzma => Box::new(xz2::read::XzDecoder::new(decoder)), - Snappy => Box::new(snap::read::FrameDecoder::new(decoder)), - Zstd => Box::new(zstd::stream::Decoder::new(decoder)?), - Tar | Zip => unreachable!(), - }; - Ok(decoder) - }; - - for format in formats.iter().flat_map(Extension::iter).skip(1).collect::>().iter().rev() { - reader = chain_reader_decoder(format, reader)?; - } - - let files_unpacked; - match formats[0].compression_formats[0] { - Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { - reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?; - - let writer = utils::create_or_ask_overwrite(&output_file_path, question_policy)?; - if writer.is_none() { - // Means that the user doesn't want to overwrite - return Ok(()); - } - let mut writer = writer.unwrap(); - - let current_position_fn = Box::new({ - let output_file_path = output_file_path.clone(); - move || output_file_path.clone().metadata().expect("file exists").len() - }); - let _progress = Progress::new_accessible_aware(total_input_size, true, Some(current_position_fn)); - - io::copy(&mut reader, &mut writer)?; - files_unpacked = vec![output_file_path]; - } - Tar => { - files_unpacked = if let ControlFlow::Continue(files) = smart_unpack( - Box::new(move |output_dir| { - let mut progress = Progress::new_accessible_aware(total_input_size, true, None); - crate::archive::tar::unpack_archive( - reader, - output_dir, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), - ) - }), - output_dir, - &output_file_path, - question_policy, - )? { - files - } else { - return Ok(()); - }; - } - Zip => { - if formats.len() > 1 { - eprintln!("{orange}[WARNING]{reset}", orange = *colors::ORANGE, reset = *colors::RESET); - eprintln!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); - - // give user the option to continue decompressing after warning is shown - if !user_wants_to_continue(input_file_path, question_policy, QuestionAction::Decompression)? { - return Ok(()); - } - } - - let mut vec = vec![]; - io::copy(&mut reader, &mut vec)?; - let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?; - - files_unpacked = if let ControlFlow::Continue(files) = smart_unpack( - Box::new(move |output_dir| { - let mut progress = Progress::new_accessible_aware(total_input_size, true, None); - crate::archive::zip::unpack_archive( - zip_archive, - output_dir, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), - ) - }), - output_dir, - &output_file_path, - question_policy, - )? { - files - } else { - return Ok(()); - }; - } - } - - // this is only printed once, so it doesn't result in much text. On the other hand, - // having a final status message is important especially in an accessibility context - // as screen readers may not read a commands exit code, making it hard to reason - // about whether the command succeeded without such a message - info!(accessible, "Successfully decompressed archive in {}.", nice_directory_display(output_dir)); - info!(accessible, "Files unpacked: {}", files_unpacked.len()); - - Ok(()) -} - -// File at input_file_path is opened for reading, example: "archive.tar.gz" -// formats contains each format necessary for decompression, example: [Gz, Tar] (in decompression order) -fn list_archive_contents( - archive_path: &Path, - formats: Vec, - list_options: ListOptions, - question_policy: QuestionPolicy, -) -> crate::Result<()> { - let reader = fs::File::open(&archive_path)?; - - // Zip archives are special, because they require io::Seek, so it requires it's logic separated - // from decoder chaining. - // - // This is the only case where we can read and unpack it directly, without having to do - // in-memory decompression/copying first. - // - // Any other Zip decompression done can take up the whole RAM and freeze ouch. - if let [Zip] = *formats.as_slice() { - let zip_archive = zip::ZipArchive::new(reader)?; - let files = crate::archive::zip::list_archive(zip_archive); - list::list_files(archive_path, files, list_options)?; - - return Ok(()); - } - - // Will be used in decoder chaining - let reader = BufReader::with_capacity(BUFFER_CAPACITY, reader); - let mut reader: Box = Box::new(reader); - - // Grab previous decoder and wrap it inside of a new one - let chain_reader_decoder = - |format: &CompressionFormat, decoder: Box| -> crate::Result> { - let decoder: Box = match format { - Gzip => Box::new(flate2::read::GzDecoder::new(decoder)), - Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)), - Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?), - Lzma => Box::new(xz2::read::XzDecoder::new(decoder)), - Snappy => Box::new(snap::read::FrameDecoder::new(decoder)), - Zstd => Box::new(zstd::stream::Decoder::new(decoder)?), - Tar | Zip => unreachable!(), - }; - Ok(decoder) - }; - - for format in formats.iter().skip(1).rev() { - reader = chain_reader_decoder(format, reader)?; - } - - let files: Box>> = match formats[0] { - Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))), - Zip => { - if formats.len() > 1 { - eprintln!("{orange}[WARNING]{reset}", orange = *colors::ORANGE, reset = *colors::RESET); - eprintln!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); - - // give user the option to continue decompressing after warning is shown - if !user_wants_to_continue(archive_path, question_policy, QuestionAction::Decompression)? { - return Ok(()); - } - } - - let mut vec = vec![]; - io::copy(&mut reader, &mut vec)?; - let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?; - - Box::new(crate::archive::zip::list_archive(zip_archive)) - } - Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { - panic!("Not an archive! This should never happen, if it does, something is wrong with `CompressionFormat::is_archive()`. Please report this error!"); - } - }; - list::list_files(archive_path, files, list_options)?; - Ok(()) -} - -/// Unpacks an archive with some heuristics -/// - If the archive contains only one file, it will be extracted to the `output_dir` -/// - If the archive contains multiple files, it will be extracted to a subdirectory of the output_dir named after the archive (given by `output_file_path`) -/// Note: This functions assumes that `output_dir` exists -fn smart_unpack( - unpack_fn: Box crate::Result>>, - output_dir: &Path, - output_file_path: &Path, - question_policy: QuestionPolicy, -) -> crate::Result>> { - assert!(output_dir.exists()); - let temp_dir = tempfile::tempdir_in(output_dir)?; - let temp_dir_path = temp_dir.path(); - info!( - accessible, - "Created temporary directory {} to hold decompressed elements.", - nice_directory_display(temp_dir_path) - ); - - // unpack the files - let files = unpack_fn(temp_dir_path)?; - - let root_contains_only_one_element = fs::read_dir(&temp_dir_path)?.count() == 1; - if root_contains_only_one_element { - // Only one file in the root directory, so we can just move it to the output directory - let file = fs::read_dir(&temp_dir_path)?.next().expect("item exists")?; - let file_path = file.path(); - let file_name = - file_path.file_name().expect("Should be safe because paths in archives should not end with '..'"); - let correct_path = output_dir.join(file_name); - // One case to handle tough is we need to check if a file with the same name already exists - if !utils::clear_path(&correct_path, question_policy)? { - return Ok(ControlFlow::Break(())); - } - fs::rename(&file_path, &correct_path)?; - info!( - accessible, - "Successfully moved {} to {}.", - nice_directory_display(&file_path), - nice_directory_display(&correct_path) - ); - } else { - // Multiple files in the root directory, so: - // Rename the temporary directory to the archive name, which is output_file_path - // One case to handle tough is we need to check if a file with the same name already exists - if !utils::clear_path(output_file_path, question_policy)? { - return Ok(ControlFlow::Break(())); - } - fs::rename(&temp_dir_path, &output_file_path)?; - info!( - accessible, - "Successfully moved {} to {}.", - nice_directory_display(temp_dir_path), - nice_directory_display(output_file_path) - ); - } - Ok(ControlFlow::Continue(files)) -} - -fn check_mime_type( - files: &[PathBuf], - formats: &mut [Vec], - question_policy: QuestionPolicy, -) -> crate::Result> { - for (path, format) in files.iter().zip(formats.iter_mut()) { - if format.is_empty() { - // File with no extension - // Try to detect it automatically and prompt the user about it - if let Some(detected_format) = try_infer_extension(path) { - // Infering the file extension can have unpredicted consequences (e.g. the user just - // mistyped, ...) which we should always inform the user about. - info!(accessible, "Detected file: `{}` extension as `{}`", path.display(), detected_format); - if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { - format.push(detected_format); - } else { - return Ok(ControlFlow::Break(())); - } - } - } else if let Some(detected_format) = try_infer_extension(path) { - // File ending with extension - // Try to detect the extension and warn the user if it differs from the written one - let outer_ext = format.iter().next_back().unwrap(); - if outer_ext != &detected_format { - warning!( - "The file extension: `{}` differ from the detected extension: `{}`", - outer_ext, - detected_format - ); - if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { - return Ok(ControlFlow::Break(())); - } - } - } else { - // NOTE: If this actually produces no false positives, we can upgrade it in the future - // to a warning and ask the user if he wants to continue decompressing. - info!(accessible, "Could not detect the extension of `{}`", path.display()); - } - } - Ok(ControlFlow::Continue(())) -} - -fn clean_input_files_if_needed(files: &mut Vec, output_path: &Path) { - let mut idx = 0; - while idx < files.len() { - if files[idx] == output_path { - warning!("The output file and the input file are the same: `{}`, skipping...", output_path.display()); - files.remove(idx); - } else { - idx += 1; - } - } -} diff --git a/src/commands/compress.rs b/src/commands/compress.rs new file mode 100644 index 0000000..3f7bbdb --- /dev/null +++ b/src/commands/compress.rs @@ -0,0 +1,142 @@ +use std::{ + io::{self, BufWriter, Write}, + path::{Path, PathBuf}, +}; + +use fs_err as fs; + +use crate::{ + archive, + commands::warn_user_about_in_memory_zip_compression, + extension::{ + CompressionFormat::{self, *}, + Extension, + }, + progress::Progress, + utils::{user_wants_to_continue, FileVisibilityPolicy}, + QuestionAction, QuestionPolicy, BUFFER_CAPACITY, +}; + +// Compress files into an `output_file` +// +// files are the list of paths to be compressed: ["dir/file1.txt", "dir/file2.txt"] +// formats contains each format necessary for compression, example: [Tar, Gz] (in compression order) +// output_file is the resulting compressed file name, example: "compressed.tar.gz" +// +// Returns Ok(true) if compressed all files successfully, and Ok(false) if user opted to skip files +pub fn compress_files( + files: Vec, + formats: Vec, + output_file: fs::File, + output_dir: &Path, + question_policy: QuestionPolicy, + file_visibility_policy: FileVisibilityPolicy, +) -> crate::Result { + // The next lines are for displaying the progress bar + // If the input files contain a directory, then the total size will be underestimated + let (total_input_size, precise) = files + .iter() + .map(|f| (f.metadata().expect("file exists").len(), f.is_file())) + .fold((0, true), |(total_size, and_precise), (size, precise)| (total_size + size, and_precise & precise)); + + // NOTE: canonicalize is here to avoid a weird bug: + // > If output_file_path is a nested path and it exists and the user overwrite it + // >> output_file_path.exists() will always return false (somehow) + // - canonicalize seems to fix this + let output_file_path = output_file.path().canonicalize()?; + + let file_writer = BufWriter::with_capacity(BUFFER_CAPACITY, output_file); + + let mut writer: Box = Box::new(file_writer); + + // Grab previous encoder and wrap it inside of a new one + let chain_writer_encoder = |format: &CompressionFormat, encoder: Box| -> crate::Result> { + let encoder: Box = match format { + Gzip => Box::new(flate2::write::GzEncoder::new(encoder, Default::default())), + Bzip => Box::new(bzip2::write::BzEncoder::new(encoder, Default::default())), + Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(encoder, Default::default())?), + Lzma => Box::new(xz2::write::XzEncoder::new(encoder, 6)), + Snappy => Box::new(snap::write::FrameEncoder::new(encoder)), + Zstd => { + let zstd_encoder = zstd::stream::write::Encoder::new(encoder, Default::default()); + // Safety: + // Encoder::new() can only fail if `level` is invalid, but Default::default() + // is guaranteed to be valid + Box::new(zstd_encoder.unwrap().auto_finish()) + } + Tar | Zip => unreachable!(), + }; + Ok(encoder) + }; + + for format in formats.iter().flat_map(Extension::iter).skip(1).collect::>().iter().rev() { + writer = chain_writer_encoder(format, writer)?; + } + + match formats[0].compression_formats[0] { + Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { + let _progress = Progress::new_accessible_aware( + total_input_size, + precise, + Some(Box::new(move || output_file_path.metadata().expect("file exists").len())), + ); + + writer = chain_writer_encoder(&formats[0].compression_formats[0], writer)?; + let mut reader = fs::File::open(&files[0]).unwrap(); + io::copy(&mut reader, &mut writer)?; + } + Tar => { + let mut progress = Progress::new_accessible_aware( + total_input_size, + precise, + Some(Box::new(move || output_file_path.metadata().expect("file exists").len())), + ); + + archive::tar::build_archive_from_paths( + &files, + &mut writer, + file_visibility_policy, + progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + )?; + writer.flush()?; + } + Zip => { + if formats.len() > 1 { + warn_user_about_in_memory_zip_compression(); + + // give user the option to continue compressing after warning is shown + if !user_wants_to_continue(output_dir, question_policy, QuestionAction::Compression)? { + return Ok(false); + } + } + + let mut vec_buffer = io::Cursor::new(vec![]); + + let current_position_fn = { + let vec_buffer_ptr = { + struct FlyPtr(*const io::Cursor>); + unsafe impl Send for FlyPtr {} + FlyPtr(&vec_buffer as *const _) + }; + Box::new(move || { + let vec_buffer_ptr = &vec_buffer_ptr; + // Safety: ptr is valid and vec_buffer is still alive + unsafe { &*vec_buffer_ptr.0 }.position() + }) + }; + + let mut progress = Progress::new_accessible_aware(total_input_size, precise, Some(current_position_fn)); + + archive::zip::build_archive_from_paths( + &files, + &mut vec_buffer, + file_visibility_policy, + progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + )?; + let vec_buffer = vec_buffer.into_inner(); + io::copy(&mut vec_buffer.as_slice(), &mut writer)?; + } + } + + Ok(true) +} diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs new file mode 100644 index 0000000..98b2a50 --- /dev/null +++ b/src/commands/decompress.rs @@ -0,0 +1,241 @@ +use std::{ + io::{self, BufReader, Read, Write}, + ops::ControlFlow, + path::{Path, PathBuf}, +}; + +use fs_err as fs; + +use crate::{ + commands::warn_user_about_in_memory_zip_decompression, + extension::{ + CompressionFormat::{self, *}, + Extension, + }, + info, + progress::Progress, + utils::{self, nice_directory_display, user_wants_to_continue}, + QuestionAction, QuestionPolicy, BUFFER_CAPACITY, +}; + +// Decompress a file +// +// File at input_file_path is opened for reading, example: "archive.tar.gz" +// formats contains each format necessary for decompression, example: [Gz, Tar] (in decompression order) +// output_dir it's where the file will be decompressed to, this function assumes that the directory exists +// output_file_path is only used when extracting single file formats, not archive formats like .tar or .zip +pub fn decompress_file( + input_file_path: &Path, + formats: Vec, + output_dir: &Path, + output_file_path: PathBuf, + question_policy: QuestionPolicy, +) -> crate::Result<()> { + assert!(output_dir.exists()); + let total_input_size = input_file_path.metadata().expect("file exists").len(); + let reader = fs::File::open(&input_file_path)?; + // Zip archives are special, because they require io::Seek, so it requires it's logic separated + // from decoder chaining. + // + // This is the only case where we can read and unpack it directly, without having to do + // in-memory decompression/copying first. + // + // Any other Zip decompression done can take up the whole RAM and freeze ouch. + if formats.len() == 1 && *formats[0].compression_formats == [Zip] { + let zip_archive = zip::ZipArchive::new(reader)?; + let files = if let ControlFlow::Continue(files) = smart_unpack( + Box::new(move |output_dir| { + let mut progress = Progress::new_accessible_aware(total_input_size, true, None); + crate::archive::zip::unpack_archive( + zip_archive, + output_dir, + progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + ) + }), + output_dir, + &output_file_path, + question_policy, + )? { + files + } else { + return Ok(()); + }; + + // this is only printed once, so it doesn't result in much text. On the other hand, + // having a final status message is important especially in an accessibility context + // as screen readers may not read a commands exit code, making it hard to reason + // about whether the command succeeded without such a message + info!( + accessible, + "Successfully decompressed archive in {} ({} files).", + nice_directory_display(output_dir), + files.len() + ); + + return Ok(()); + } + + // Will be used in decoder chaining + let reader = BufReader::with_capacity(BUFFER_CAPACITY, reader); + let mut reader: Box = Box::new(reader); + + // Grab previous decoder and wrap it inside of a new one + let chain_reader_decoder = |format: &CompressionFormat, decoder: Box| -> crate::Result> { + let decoder: Box = match format { + Gzip => Box::new(flate2::read::GzDecoder::new(decoder)), + Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)), + Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?), + Lzma => Box::new(xz2::read::XzDecoder::new(decoder)), + Snappy => Box::new(snap::read::FrameDecoder::new(decoder)), + Zstd => Box::new(zstd::stream::Decoder::new(decoder)?), + Tar | Zip => unreachable!(), + }; + Ok(decoder) + }; + + for format in formats.iter().flat_map(Extension::iter).skip(1).collect::>().iter().rev() { + reader = chain_reader_decoder(format, reader)?; + } + + let files_unpacked; + match formats[0].compression_formats[0] { + Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { + reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?; + + let writer = utils::create_or_ask_overwrite(&output_file_path, question_policy)?; + if writer.is_none() { + // Means that the user doesn't want to overwrite + return Ok(()); + } + let mut writer = writer.unwrap(); + + let current_position_fn = Box::new({ + let output_file_path = output_file_path.clone(); + move || output_file_path.clone().metadata().expect("file exists").len() + }); + let _progress = Progress::new_accessible_aware(total_input_size, true, Some(current_position_fn)); + + io::copy(&mut reader, &mut writer)?; + files_unpacked = vec![output_file_path]; + } + Tar => { + files_unpacked = if let ControlFlow::Continue(files) = smart_unpack( + Box::new(move |output_dir| { + let mut progress = Progress::new_accessible_aware(total_input_size, true, None); + crate::archive::tar::unpack_archive( + reader, + output_dir, + progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + ) + }), + output_dir, + &output_file_path, + question_policy, + )? { + files + } else { + return Ok(()); + }; + } + Zip => { + if formats.len() > 1 { + warn_user_about_in_memory_zip_decompression(); + + // give user the option to continue decompressing after warning is shown + if !user_wants_to_continue(input_file_path, question_policy, QuestionAction::Decompression)? { + return Ok(()); + } + } + + let mut vec = vec![]; + io::copy(&mut reader, &mut vec)?; + let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?; + + files_unpacked = if let ControlFlow::Continue(files) = smart_unpack( + Box::new(move |output_dir| { + let mut progress = Progress::new_accessible_aware(total_input_size, true, None); + crate::archive::zip::unpack_archive( + zip_archive, + output_dir, + progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + ) + }), + output_dir, + &output_file_path, + question_policy, + )? { + files + } else { + return Ok(()); + }; + } + } + + // this is only printed once, so it doesn't result in much text. On the other hand, + // having a final status message is important especially in an accessibility context + // as screen readers may not read a commands exit code, making it hard to reason + // about whether the command succeeded without such a message + info!(accessible, "Successfully decompressed archive in {}.", nice_directory_display(output_dir)); + info!(accessible, "Files unpacked: {}", files_unpacked.len()); + + Ok(()) +} + +/// Unpacks an archive with some heuristics +/// - If the archive contains only one file, it will be extracted to the `output_dir` +/// - If the archive contains multiple files, it will be extracted to a subdirectory of the output_dir named after the archive (given by `output_file_path`) +/// Note: This functions assumes that `output_dir` exists +fn smart_unpack( + unpack_fn: Box crate::Result>>, + output_dir: &Path, + output_file_path: &Path, + question_policy: QuestionPolicy, +) -> crate::Result>> { + assert!(output_dir.exists()); + let temp_dir = tempfile::tempdir_in(output_dir)?; + let temp_dir_path = temp_dir.path(); + info!( + accessible, + "Created temporary directory {} to hold decompressed elements.", + nice_directory_display(temp_dir_path) + ); + + // unpack the files + let files = unpack_fn(temp_dir_path)?; + + let root_contains_only_one_element = fs::read_dir(&temp_dir_path)?.count() == 1; + if root_contains_only_one_element { + // Only one file in the root directory, so we can just move it to the output directory + let file = fs::read_dir(&temp_dir_path)?.next().expect("item exists")?; + let file_path = file.path(); + let file_name = + file_path.file_name().expect("Should be safe because paths in archives should not end with '..'"); + let correct_path = output_dir.join(file_name); + // One case to handle tough is we need to check if a file with the same name already exists + if !utils::clear_path(&correct_path, question_policy)? { + return Ok(ControlFlow::Break(())); + } + fs::rename(&file_path, &correct_path)?; + info!( + accessible, + "Successfully moved {} to {}.", + nice_directory_display(&file_path), + nice_directory_display(&correct_path) + ); + } else { + // Multiple files in the root directory, so: + // Rename the temporary directory to the archive name, which is output_file_path + // One case to handle tough is we need to check if a file with the same name already exists + if !utils::clear_path(output_file_path, question_policy)? { + return Ok(ControlFlow::Break(())); + } + fs::rename(&temp_dir_path, &output_file_path)?; + info!( + accessible, + "Successfully moved {} to {}.", + nice_directory_display(temp_dir_path), + nice_directory_display(output_file_path) + ); + } + Ok(ControlFlow::Continue(files)) +} diff --git a/src/commands/list.rs b/src/commands/list.rs new file mode 100644 index 0000000..6e27eb4 --- /dev/null +++ b/src/commands/list.rs @@ -0,0 +1,88 @@ +use std::{ + io::{self, BufReader, Read}, + path::Path, +}; + +use fs_err as fs; + +use crate::{ + commands::warn_user_about_in_memory_zip_decompression, + extension::CompressionFormat::{self, *}, + list::{self, FileInArchive, ListOptions}, + utils::user_wants_to_continue, + QuestionAction, QuestionPolicy, BUFFER_CAPACITY, +}; + +// File at input_file_path is opened for reading, example: "archive.tar.gz" +// formats contains each format necessary for decompression, example: [Gz, Tar] (in decompression order) +pub fn list_archive_contents( + archive_path: &Path, + formats: Vec, + list_options: ListOptions, + question_policy: QuestionPolicy, +) -> crate::Result<()> { + let reader = fs::File::open(&archive_path)?; + + // Zip archives are special, because they require io::Seek, so it requires it's logic separated + // from decoder chaining. + // + // This is the only case where we can read and unpack it directly, without having to do + // in-memory decompression/copying first. + // + // Any other Zip decompression done can take up the whole RAM and freeze ouch. + if let [Zip] = *formats.as_slice() { + let zip_archive = zip::ZipArchive::new(reader)?; + let files = crate::archive::zip::list_archive(zip_archive); + list::list_files(archive_path, files, list_options)?; + + return Ok(()); + } + + // Will be used in decoder chaining + let reader = BufReader::with_capacity(BUFFER_CAPACITY, reader); + let mut reader: Box = Box::new(reader); + + // Grab previous decoder and wrap it inside of a new one + let chain_reader_decoder = + |format: &CompressionFormat, decoder: Box| -> crate::Result> { + let decoder: Box = match format { + Gzip => Box::new(flate2::read::GzDecoder::new(decoder)), + Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)), + Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?), + Lzma => Box::new(xz2::read::XzDecoder::new(decoder)), + Snappy => Box::new(snap::read::FrameDecoder::new(decoder)), + Zstd => Box::new(zstd::stream::Decoder::new(decoder)?), + Tar | Zip => unreachable!(), + }; + Ok(decoder) + }; + + for format in formats.iter().skip(1).rev() { + reader = chain_reader_decoder(format, reader)?; + } + + let files: Box>> = match formats[0] { + Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))), + Zip => { + if formats.len() > 1 { + warn_user_about_in_memory_zip_decompression(); + + // give user the option to continue decompressing after warning is shown + if !user_wants_to_continue(archive_path, question_policy, QuestionAction::Decompression)? { + return Ok(()); + } + } + + let mut vec = vec![]; + io::copy(&mut reader, &mut vec)?; + let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?; + + Box::new(crate::archive::zip::list_archive(zip_archive)) + } + Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { + panic!("Not an archive! This should never happen, if it does, something is wrong with `CompressionFormat::is_archive()`. Please report this error!"); + } + }; + list::list_files(archive_path, files, list_options)?; + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..dfe8c8c --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,359 @@ +//! Core of the crate, where the `compress_files` and `decompress_file` functions are implemented +//! +//! Also, where correctly call functions based on the detected `Command`. + +mod compress; +mod decompress; +mod list; + +use std::{ + io::Write, + ops::ControlFlow, + path::{Path, PathBuf}, +}; + +use fs_err as fs; +use utils::colors; + +use crate::{ + commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, + error::FinalError, + extension::{self, Extension}, + info, + list::ListOptions, + utils::{ + self, concatenate_os_str_list, dir_is_empty, to_utf, try_infer_extension, user_wants_to_continue, + FileVisibilityPolicy, + }, + warning, Opts, QuestionAction, QuestionPolicy, Subcommand, +}; + +/// Warn the user that .zip archives have limitations that require it to compress everything +/// in-memory when working with multiple chained formats. +/// +/// This can sadly lead to out-of-memory scenarios for big archives. +fn warn_user_about_in_memory_zip_compression() { + const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = + "\tThere is a limitation for .zip archives with extra extensions. (e.g. .zip.gz)\n\ + \tThe design of .zip makes it impossible to compress via stream, so it must be done entirely in memory.\n\ + \tBy compressing .zip with extra compression formats, you can run out of RAM if the file is too large!"; + + warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); +} + +/// Warn the user that .zip archives have limitations that require it to decompress everything +/// in-memory when working with multiple chained formats. +/// +/// This can sadly lead to out-of-memory scenarios for big archives. +fn warn_user_about_in_memory_zip_decompression() { + const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = + "\tThere is a limitation for .zip archives with extra extensions. (e.g. .zip.gz)\n\ + \tThe design of .zip makes it impossible to compress via stream, so it must be done entirely in memory.\n\ + \tBy compressing .zip with extra compression formats, you can run out of RAM if the file is too large!"; + + warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); +} + +fn represents_several_files(files: &[PathBuf]) -> bool { + let is_non_empty_dir = |path: &PathBuf| { + let is_non_empty = || !dir_is_empty(path); + + path.is_dir().then(is_non_empty).unwrap_or_default() + }; + + files.iter().any(is_non_empty_dir) || files.len() > 1 +} + +/// This function checks what command needs to be run and performs A LOT of ahead-of-time checks +/// to assume everything is OK. +/// +/// There are a lot of custom errors to give enough error description and explanation. +pub fn run( + args: Opts, + question_policy: QuestionPolicy, + file_visibility_policy: FileVisibilityPolicy, +) -> crate::Result<()> { + match args.cmd { + Subcommand::Compress { mut files, output: output_path } => { + // If the output_path file exists and is the same as some of the input files, warn the user and skip those inputs (in order to avoid compression recursion) + if output_path.exists() { + deduplicate_input_files(&mut files, &fs::canonicalize(&output_path)?); + } + // After cleaning, if there are no input files left, exit + if files.is_empty() { + return Err(FinalError::with_title("No files to compress").into()); + } + + // Formats from path extension, like "file.tar.gz.xz" -> vec![Tar, Gzip, Lzma] + let mut formats = extension::extensions_from_path(&output_path); + + if formats.is_empty() { + let error = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path))) + .detail("You shall supply the compression format") + .hint("Try adding supported extensions (see --help):") + .hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path))) + .hint(format!(" ouch compress ... {}.zip", to_utf(&output_path))) + .hint("") + .hint("Alternatively, you can overwrite this option by using the '--format' flag:") + .hint(format!(" ouch compress ... {} --format tar.gz", to_utf(&output_path))); + + return Err(error.into()); + } + + if !formats.get(0).map(Extension::is_archive).unwrap_or(false) && represents_several_files(&files) { + // This piece of code creates a suggestion for compressing multiple files + // It says: + // Change from file.bz.xz + // To file.tar.bz.xz + let extensions_text: String = formats.iter().map(|format| format.to_string()).collect(); + + let output_path = to_utf(&output_path).to_string(); + + // Breaks if Lzma is .lz or .lzma and not .xz + // Or if Bzip is .bz2 and not .bz + let extensions_start_position = output_path.rfind(&extensions_text).unwrap(); + let pos = extensions_start_position - 1; + let mut suggested_output_path = output_path.to_string(); + suggested_output_path.insert_str(pos, ".tar"); + + let error = FinalError::with_title(format!("Cannot compress to '{}'.", output_path)) + .detail("You are trying to compress multiple files.") + .detail(format!("The compression format '{}' cannot receive multiple files.", &formats[0])) + .detail("The only supported formats that archive files into an archive are .tar and .zip.") + .hint(format!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0])) + .hint(format!("From: {}", output_path)) + .hint(format!("To: {}", suggested_output_path)); + + return Err(error.into()); + } + + if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { + let error = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path))) + .detail(format!("Found the format '{}' in an incorrect position.", format)) + .detail(format!("'{}' can only be used at the start of the file extension.", format)) + .hint(format!("If you wish to compress multiple files, start the extension with '{}'.", format)) + .hint(format!("Otherwise, remove the last '{}' from '{}'.", format, to_utf(&output_path))); + + return Err(error.into()); + } + + if output_path.exists() && !utils::user_wants_to_overwrite(&output_path, question_policy)? { + // User does not want to overwrite this file, skip and return without any errors + return Ok(()); + } + + let output_file = fs::File::create(&output_path)?; + + if !represents_several_files(&files) { + // It is possible the file is already partially compressed so we don't want to compress it again + // `ouch compress file.tar.gz file.tar.gz.xz` should produce `file.tar.gz.xz` and not `file.tar.gz.tar.gz.xz` + let input_extensions = extension::extensions_from_path(&files[0]); + + // We calculate the formats that are left if we filter out a sublist at the start of what we have that's the same as the input formats + let mut new_formats = Vec::with_capacity(formats.len()); + for (inp_ext, out_ext) in input_extensions.iter().zip(&formats) { + if inp_ext.compression_formats == out_ext.compression_formats { + new_formats.push(out_ext.clone()); + } else if inp_ext + .compression_formats + .iter() + .zip(out_ext.compression_formats.iter()) + .all(|(inp, out)| inp == out) + { + let new_ext = Extension::new( + &out_ext.compression_formats[..inp_ext.compression_formats.len()], + &out_ext.display_text, + ); + new_formats.push(new_ext); + break; + } + } + // If the input is a sublist at the start of `formats` then remove the extensions + // Note: If input_extensions is empty then it will make `formats` empty too, which we don't want + if !input_extensions.is_empty() && new_formats != formats { + // Safety: + // We checked above that input_extensions isn't empty, so files[0] has an extension. + // + // Path::extension says: "if there is no file_name, then there is no extension". + // Contrapositive statement: "if there is extension, then there is file_name". + info!( + accessible, // important information + "Partial compression detected. Compressing {} into {}", + to_utf(files[0].as_path().file_name().unwrap().as_ref()), + to_utf(&output_path) + ); + formats = new_formats; + } + } + let compress_result = + compress_files(files, formats, output_file, &output_path, question_policy, file_visibility_policy); + + if let Ok(true) = compress_result { + // this is only printed once, so it doesn't result in much text. On the other hand, + // having a final status message is important especially in an accessibility context + // as screen readers may not read a commands exit code, making it hard to reason + // about whether the command succeeded without such a message + info!(accessible, "Successfully compressed '{}'.", to_utf(&output_path)); + } else { + // If Ok(false) or Err() occurred, delete incomplete file + // Print an extra alert message pointing out that we left a possibly + // CORRUPTED FILE at `output_path` + if let Err(err) = fs::remove_file(&output_path) { + eprintln!("{red}FATAL ERROR:\n", red = *colors::RED); + eprintln!(" Please manually delete '{}'.", to_utf(&output_path)); + eprintln!(" Compression failed and we could not delete '{}'.", to_utf(&output_path),); + eprintln!(" Error:{reset} {}{red}.{reset}\n", err, reset = *colors::RESET, red = *colors::RED); + } + } + + compress_result?; + } + Subcommand::Decompress { files, output_dir } => { + let mut output_paths = vec![]; + let mut formats = vec![]; + + for path in files.iter() { + let (file_output_path, file_formats) = extension::separate_known_extensions_from_name(path); + output_paths.push(file_output_path); + formats.push(file_formats); + } + + if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { + return Ok(()); + } + + let files_missing_format: Vec = files + .iter() + .zip(&formats) + .filter(|(_, formats)| formats.is_empty()) + .map(|(input_path, _)| PathBuf::from(input_path)) + .collect(); + + if !files_missing_format.is_empty() { + let error = FinalError::with_title("Cannot decompress files without extensions") + .detail(format!( + "Files without supported extensions: {}", + concatenate_os_str_list(&files_missing_format) + )) + .detail("Decompression formats are detected automatically by the file extension") + .hint("Provide a file with a supported extension:") + .hint(" ouch decompress example.tar.gz") + .hint("") + .hint("Or overwrite this option with the '--format' flag:") + .hint(format!(" ouch decompress {} --format tar.gz", to_utf(&files_missing_format[0]))); + + return Err(error.into()); + } + + // The directory that will contain the output files + // We default to the current directory if the user didn't specify an output directory with --dir + let output_dir = if let Some(dir) = output_dir { + if !utils::clear_path(&dir, question_policy)? { + // User doesn't want to overwrite + return Ok(()); + } + utils::create_dir_if_non_existent(&dir)?; + dir + } else { + PathBuf::from(".") + }; + + for ((input_path, formats), file_name) in files.iter().zip(formats).zip(output_paths) { + let output_file_path = output_dir.join(file_name); // Path used by single file format archives + decompress_file(input_path, formats, &output_dir, output_file_path, question_policy)?; + } + } + Subcommand::List { archives: files, tree } => { + let mut formats = vec![]; + + for path in files.iter() { + let (_, file_formats) = extension::separate_known_extensions_from_name(path); + formats.push(file_formats); + } + + if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { + return Ok(()); + } + + let not_archives: Vec = files + .iter() + .zip(&formats) + .filter(|(_, formats)| !formats.get(0).map(Extension::is_archive).unwrap_or(false)) + .map(|(path, _)| path.clone()) + .collect(); + + if !not_archives.is_empty() { + let error = FinalError::with_title("Cannot list archive contents") + .detail("Only archives can have their contents listed") + .detail(format!("Files are not archives: {}", concatenate_os_str_list(¬_archives))); + + return Err(error.into()); + } + + let list_options = ListOptions { tree }; + + for (i, (archive_path, formats)) in files.iter().zip(formats).enumerate() { + if i > 0 { + println!(); + } + let formats = formats.iter().flat_map(Extension::iter).map(Clone::clone).collect(); + list_archive_contents(archive_path, formats, list_options, question_policy)?; + } + } + } + Ok(()) +} + +fn check_mime_type( + files: &[PathBuf], + formats: &mut [Vec], + question_policy: QuestionPolicy, +) -> crate::Result> { + for (path, format) in files.iter().zip(formats.iter_mut()) { + if format.is_empty() { + // File with no extension + // Try to detect it automatically and prompt the user about it + if let Some(detected_format) = try_infer_extension(path) { + // Infering the file extension can have unpredicted consequences (e.g. the user just + // mistyped, ...) which we should always inform the user about. + info!(accessible, "Detected file: `{}` extension as `{}`", path.display(), detected_format); + if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { + format.push(detected_format); + } else { + return Ok(ControlFlow::Break(())); + } + } + } else if let Some(detected_format) = try_infer_extension(path) { + // File ending with extension + // Try to detect the extension and warn the user if it differs from the written one + let outer_ext = format.iter().next_back().unwrap(); + if outer_ext != &detected_format { + warning!( + "The file extension: `{}` differ from the detected extension: `{}`", + outer_ext, + detected_format + ); + if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { + return Ok(ControlFlow::Break(())); + } + } + } else { + // NOTE: If this actually produces no false positives, we can upgrade it in the future + // to a warning and ask the user if he wants to continue decompressing. + info!(accessible, "Could not detect the extension of `{}`", path.display()); + } + } + Ok(ControlFlow::Continue(())) +} + +fn deduplicate_input_files(files: &mut Vec, output_path: &Path) { + let mut idx = 0; + while idx < files.len() { + if files[idx] == output_path { + warning!("The output file and the input file are the same: `{}`, skipping...", output_path.display()); + files.remove(idx); + } else { + idx += 1; + } + } +} diff --git a/src/main.rs b/src/main.rs index deea33c..38c86f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,9 @@ use error::{Error, Result}; use opts::{Opts, Subcommand}; use utils::{QuestionAction, QuestionPolicy}; +// Used in BufReader and BufWriter to perform less syscalls +const BUFFER_CAPACITY: usize = 1024 * 32; + /// The status code returned from `ouch` on error pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE; From 8bd78f49cd55f87746c8021f80302415bf1512d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 11:46:46 -0300 Subject: [PATCH 03/10] fix accessible warning `if` reversed --- src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index d2680a2..dfe6b62 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -68,7 +68,7 @@ macro_rules! warning { pub fn _warning_helper() { use crate::utils::colors::{ORANGE, RESET}; - if !crate::cli::ACCESSIBLE.get().unwrap() { + if *crate::cli::ACCESSIBLE.get().unwrap() { print!("{}Warning:{} ", *ORANGE, *RESET); } else { print!("{}[WARNING]{} ", *ORANGE, *RESET); From 4d518b70562bf40d7601ad82e44f50b455a57a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 12:47:17 -0300 Subject: [PATCH 04/10] small refactor and renamings --- src/archive/zip.rs | 4 ++-- src/commands/decompress.rs | 15 +++++++-------- src/extension.rs | 4 ++-- src/progress.rs | 12 ++++++------ 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/archive/zip.rs b/src/archive/zip.rs index d9d3ee4..183e6a4 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -45,7 +45,7 @@ where let file_path = output_folder.join(file_path); - check_for_comments(&file); + display_zip_comment_if_exists(&file); match (&*file.name()).ends_with('/') { _is_dir @ true => { @@ -191,7 +191,7 @@ where Ok(bytes) } -fn check_for_comments(file: &ZipFile) { +fn display_zip_comment_if_exists(file: &ZipFile) { let comment = file.comment(); if !comment.is_empty() { // Zip file comments seem to be pretty rare, but if they are used, diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 98b2a50..aa297f9 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -97,8 +97,7 @@ pub fn decompress_file( reader = chain_reader_decoder(format, reader)?; } - let files_unpacked; - match formats[0].compression_formats[0] { + let files_unpacked = match formats[0].compression_formats[0] { Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?; @@ -116,10 +115,10 @@ pub fn decompress_file( let _progress = Progress::new_accessible_aware(total_input_size, true, Some(current_position_fn)); io::copy(&mut reader, &mut writer)?; - files_unpacked = vec![output_file_path]; + vec![output_file_path] } Tar => { - files_unpacked = if let ControlFlow::Continue(files) = smart_unpack( + if let ControlFlow::Continue(files) = smart_unpack( Box::new(move |output_dir| { let mut progress = Progress::new_accessible_aware(total_input_size, true, None); crate::archive::tar::unpack_archive( @@ -135,7 +134,7 @@ pub fn decompress_file( files } else { return Ok(()); - }; + } } Zip => { if formats.len() > 1 { @@ -151,7 +150,7 @@ pub fn decompress_file( io::copy(&mut reader, &mut vec)?; let zip_archive = zip::ZipArchive::new(io::Cursor::new(vec))?; - files_unpacked = if let ControlFlow::Continue(files) = smart_unpack( + if let ControlFlow::Continue(files) = smart_unpack( Box::new(move |output_dir| { let mut progress = Progress::new_accessible_aware(total_input_size, true, None); crate::archive::zip::unpack_archive( @@ -167,9 +166,9 @@ pub fn decompress_file( files } else { return Ok(()); - }; + } } - } + }; // this is only printed once, so it doesn't result in much text. On the other hand, // having a final status message is important especially in an accessibility context diff --git a/src/extension.rs b/src/extension.rs index e8807d0..77ace4b 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -23,9 +23,9 @@ impl PartialEq for Extension { impl Extension { /// # Panics: /// Will panic if `formats` is empty - pub fn new(formats: &'static [CompressionFormat], text: impl Into) -> Self { + pub fn new(formats: &'static [CompressionFormat], text: impl ToString) -> Self { assert!(!formats.is_empty()); - Self { compression_formats: formats, display_text: text.into() } + Self { compression_formats: formats, display_text: text.to_string() } } /// Checks if the first format in `compression_formats` is an archive diff --git a/src/progress.rs b/src/progress.rs index 930262d..02dbcd3 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -78,21 +78,21 @@ impl Progress { t += "({bytes_per_sec}, {eta}) {path}"; t }; - let pb = ProgressBar::new(total_input_size); - pb.set_style(ProgressStyle::default_bar().template(&template).progress_chars("#>-")); + let bar = ProgressBar::new(total_input_size); + bar.set_style(ProgressStyle::default_bar().template(&template).progress_chars("#>-")); while draw_rx.try_recv().is_err() { if let Some(ref pos_fn) = current_position_fn { - pb.set_position(pos_fn()); + bar.set_position(pos_fn()); } else { - pb.tick(); + bar.tick(); } if let Ok(msg) = msg_rx.try_recv() { - pb.set_message(msg); + bar.set_message(msg); } thread::sleep(Duration::from_millis(100)); } - pb.finish(); + bar.finish(); let _ = clean_tx.send(()); }); From 19769223c8d1faadf96e62edbfb154910b356219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 12:55:51 -0300 Subject: [PATCH 05/10] create helper function `split_first_extension` --- src/commands/compress.rs | 9 ++++++--- src/commands/decompress.rs | 12 ++++++++---- src/extension.rs | 7 +++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 3f7bbdb..3969509 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -9,6 +9,7 @@ use crate::{ archive, commands::warn_user_about_in_memory_zip_compression, extension::{ + split_first_extension, CompressionFormat::{self, *}, Extension, }, @@ -69,11 +70,13 @@ pub fn compress_files( Ok(encoder) }; - for format in formats.iter().flat_map(Extension::iter).skip(1).collect::>().iter().rev() { + let (first_extension, extensions) = split_first_extension(&formats); + + for format in extensions.iter().rev() { writer = chain_writer_encoder(format, writer)?; } - match formats[0].compression_formats[0] { + match first_extension { Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { let _progress = Progress::new_accessible_aware( total_input_size, @@ -81,7 +84,7 @@ pub fn compress_files( Some(Box::new(move || output_file_path.metadata().expect("file exists").len())), ); - writer = chain_writer_encoder(&formats[0].compression_formats[0], writer)?; + writer = chain_writer_encoder(&first_extension, writer)?; let mut reader = fs::File::open(&files[0]).unwrap(); io::copy(&mut reader, &mut writer)?; } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index aa297f9..82bd5c4 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -9,6 +9,7 @@ use fs_err as fs; use crate::{ commands::warn_user_about_in_memory_zip_decompression, extension::{ + split_first_extension, CompressionFormat::{self, *}, Extension, }, @@ -34,6 +35,7 @@ pub fn decompress_file( assert!(output_dir.exists()); let total_input_size = input_file_path.metadata().expect("file exists").len(); let reader = fs::File::open(&input_file_path)?; + // Zip archives are special, because they require io::Seek, so it requires it's logic separated // from decoder chaining. // @@ -41,7 +43,7 @@ pub fn decompress_file( // in-memory decompression/copying first. // // Any other Zip decompression done can take up the whole RAM and freeze ouch. - if formats.len() == 1 && *formats[0].compression_formats == [Zip] { + if let [Extension { compression_formats: [Zip], .. }] = formats.as_slice() { let zip_archive = zip::ZipArchive::new(reader)?; let files = if let ControlFlow::Continue(files) = smart_unpack( Box::new(move |output_dir| { @@ -93,13 +95,15 @@ pub fn decompress_file( Ok(decoder) }; - for format in formats.iter().flat_map(Extension::iter).skip(1).collect::>().iter().rev() { + let (first_extension, extensions) = split_first_extension(&formats); + + for format in extensions.iter().rev() { reader = chain_reader_decoder(format, reader)?; } - let files_unpacked = match formats[0].compression_formats[0] { + let files_unpacked = match first_extension { Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { - reader = chain_reader_decoder(&formats[0].compression_formats[0], reader)?; + reader = chain_reader_decoder(&first_extension, reader)?; let writer = utils::create_or_ask_overwrite(&output_file_path, question_policy)?; if writer.is_none() { diff --git a/src/extension.rs b/src/extension.rs index 77ace4b..580599c 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -171,3 +171,10 @@ mod tests { assert_eq!(formats, vec![&Tar, &Gzip]); } } + +// Panics if formats has an empty list of compression formats +pub fn split_first_extension(formats: &[Extension]) -> (CompressionFormat, Vec) { + let mut extensions: Vec = formats.iter().flat_map(Extension::iter).copied().collect(); + let first_extension = extensions.remove(0); + (first_extension, extensions) +} From f5fcf7f2a39f9f9bf3d7770eaab5998c84996d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 13:01:24 -0300 Subject: [PATCH 06/10] replace flat_map by `flatten_compression_formats` --- src/commands/compress.rs | 4 ++-- src/commands/decompress.rs | 4 ++-- src/commands/mod.rs | 4 ++-- src/extension.rs | 17 ++++++++--------- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 3969509..4186996 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -9,7 +9,7 @@ use crate::{ archive, commands::warn_user_about_in_memory_zip_compression, extension::{ - split_first_extension, + split_first_compression_format, CompressionFormat::{self, *}, Extension, }, @@ -70,7 +70,7 @@ pub fn compress_files( Ok(encoder) }; - let (first_extension, extensions) = split_first_extension(&formats); + let (first_extension, extensions) = split_first_compression_format(&formats); for format in extensions.iter().rev() { writer = chain_writer_encoder(format, writer)?; diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 82bd5c4..8cff065 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -9,7 +9,7 @@ use fs_err as fs; use crate::{ commands::warn_user_about_in_memory_zip_decompression, extension::{ - split_first_extension, + split_first_compression_format, CompressionFormat::{self, *}, Extension, }, @@ -95,7 +95,7 @@ pub fn decompress_file( Ok(decoder) }; - let (first_extension, extensions) = split_first_extension(&formats); + let (first_extension, extensions) = split_first_compression_format(&formats); for format in extensions.iter().rev() { reader = chain_reader_decoder(format, reader)?; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index dfe8c8c..790d370 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -18,7 +18,7 @@ use utils::colors; use crate::{ commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::FinalError, - extension::{self, Extension}, + extension::{self, flatten_compression_formats, Extension}, info, list::ListOptions, utils::{ @@ -296,7 +296,7 @@ pub fn run( if i > 0 { println!(); } - let formats = formats.iter().flat_map(Extension::iter).map(Clone::clone).collect(); + let formats = flatten_compression_formats(&formats); list_archive_contents(archive_path, formats, list_options, question_policy)?; } } diff --git a/src/extension.rs b/src/extension.rs index 580599c..92c255d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -33,11 +33,6 @@ impl Extension { // Safety: we check that `compression_formats` is not empty in `Self::new` self.compression_formats[0].is_archive_format() } - - /// Iteration to inner compression formats, useful for flat_mapping - pub fn iter(&self) -> impl Iterator { - self.compression_formats.iter() - } } impl fmt::Display for Extension { @@ -166,15 +161,19 @@ mod tests { let path = Path::new("bolovo.tar.gz"); let extensions: Vec = extensions_from_path(path); - let formats: Vec<&CompressionFormat> = extensions.iter().flat_map(Extension::iter).collect::>(); + let formats: Vec = flatten_compression_formats(&extensions); - assert_eq!(formats, vec![&Tar, &Gzip]); + assert_eq!(formats, vec![Tar, Gzip]); } } // Panics if formats has an empty list of compression formats -pub fn split_first_extension(formats: &[Extension]) -> (CompressionFormat, Vec) { - let mut extensions: Vec = formats.iter().flat_map(Extension::iter).copied().collect(); +pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec) { + let mut extensions: Vec = flatten_compression_formats(&formats); let first_extension = extensions.remove(0); (first_extension, extensions) } + +pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec { + extensions.iter().flat_map(|extension| extension.compression_formats.iter()).copied().collect() +} From 14025c681618b85e59b1b118bd020c84547ece23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 13:06:22 -0300 Subject: [PATCH 07/10] remove `use_small_heuristics = "Max"` from rustfmt and update edition to 2021 --- rustfmt.toml | 3 +- src/archive/zip.rs | 15 ++++++-- src/commands/compress.rs | 22 ++++++++--- src/commands/decompress.rs | 32 +++++++++++---- src/commands/mod.rs | 75 +++++++++++++++++++++++++++++------- src/error.rs | 38 ++++++++++++++---- src/extension.rs | 43 +++++++++++++-------- src/list.rs | 4 +- src/progress.rs | 9 ++++- src/utils/file_visibility.rs | 17 ++++++-- src/utils/fs.rs | 4 +- src/utils/mod.rs | 5 ++- src/utils/question.rs | 23 +++++++++-- tests/integration.rs | 15 ++++++-- tests/mime.rs | 5 ++- 15 files changed, 239 insertions(+), 71 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index a3badd9..8a9367d 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -2,11 +2,10 @@ max_width = 120 use_field_init_shorthand = true newline_style = "Unix" -edition = "2018" +edition = "2021" reorder_imports = true reorder_modules = true use_try_shorthand = true -use_small_heuristics = "Max" # Unstable features (nightly only) unstable_features = true diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 183e6a4..c0bdf48 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -144,7 +144,10 @@ where if !invalid_unicode_filenames.is_empty() { let error = FinalError::with_title("Cannot build zip archive") .detail("Zip archives require files to have valid UTF-8 paths") - .detail(format!("Files with invalid paths: {}", concatenate_os_str_list(&invalid_unicode_filenames))); + .detail(format!( + "Files with invalid paths: {}", + concatenate_os_str_list(&invalid_unicode_filenames) + )); return Err(error.into()); } @@ -223,7 +226,10 @@ fn convert_zip_date_time(date_time: zip::DateTime) -> Option { let date_time = PrimitiveDateTime::new(date, time); let timestamp = date_time.assume_utc().unix_timestamp(); - Some(libc::timespec { tv_sec: timestamp, tv_nsec: 0 }) + Some(libc::timespec { + tv_sec: timestamp, + tv_nsec: 0, + }) } #[cfg(unix)] @@ -232,7 +238,10 @@ fn set_last_modified_time(file: &fs::File, zip_file: &ZipFile) -> crate::Result< use libc::UTIME_NOW; - let now = libc::timespec { tv_sec: 0, tv_nsec: UTIME_NOW }; + let now = libc::timespec { + tv_sec: 0, + tv_nsec: UTIME_NOW, + }; let last_modified = zip_file.last_modified(); let last_modified = convert_zip_date_time(last_modified).unwrap_or(now); diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 4186996..0f141bc 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -38,7 +38,9 @@ pub fn compress_files( let (total_input_size, precise) = files .iter() .map(|f| (f.metadata().expect("file exists").len(), f.is_file())) - .fold((0, true), |(total_size, and_precise), (size, precise)| (total_size + size, and_precise & precise)); + .fold((0, true), |(total_size, and_precise), (size, precise)| { + (total_size + size, and_precise & precise) + }); // NOTE: canonicalize is here to avoid a weird bug: // > If output_file_path is a nested path and it exists and the user overwrite it @@ -81,7 +83,9 @@ pub fn compress_files( let _progress = Progress::new_accessible_aware( total_input_size, precise, - Some(Box::new(move || output_file_path.metadata().expect("file exists").len())), + Some(Box::new(move || { + output_file_path.metadata().expect("file exists").len() + })), ); writer = chain_writer_encoder(&first_extension, writer)?; @@ -92,14 +96,19 @@ pub fn compress_files( let mut progress = Progress::new_accessible_aware( total_input_size, precise, - Some(Box::new(move || output_file_path.metadata().expect("file exists").len())), + Some(Box::new(move || { + output_file_path.metadata().expect("file exists").len() + })), ); archive::tar::build_archive_from_paths( &files, &mut writer, file_visibility_policy, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + progress + .as_mut() + .map(Progress::display_handle) + .unwrap_or(&mut io::stdout()), )?; writer.flush()?; } @@ -134,7 +143,10 @@ pub fn compress_files( &files, &mut vec_buffer, file_visibility_policy, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + progress + .as_mut() + .map(Progress::display_handle) + .unwrap_or(&mut io::stdout()), )?; let vec_buffer = vec_buffer.into_inner(); io::copy(&mut vec_buffer.as_slice(), &mut writer)?; diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 8cff065..f18ffe9 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -43,7 +43,11 @@ pub fn decompress_file( // in-memory decompression/copying first. // // Any other Zip decompression done can take up the whole RAM and freeze ouch. - if let [Extension { compression_formats: [Zip], .. }] = formats.as_slice() { + if let [Extension { + compression_formats: [Zip], + .. + }] = formats.as_slice() + { let zip_archive = zip::ZipArchive::new(reader)?; let files = if let ControlFlow::Continue(files) = smart_unpack( Box::new(move |output_dir| { @@ -51,7 +55,10 @@ pub fn decompress_file( crate::archive::zip::unpack_archive( zip_archive, output_dir, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + progress + .as_mut() + .map(Progress::display_handle) + .unwrap_or(&mut io::stdout()), ) }), output_dir, @@ -128,7 +135,10 @@ pub fn decompress_file( crate::archive::tar::unpack_archive( reader, output_dir, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + progress + .as_mut() + .map(Progress::display_handle) + .unwrap_or(&mut io::stdout()), ) }), output_dir, @@ -160,7 +170,10 @@ pub fn decompress_file( crate::archive::zip::unpack_archive( zip_archive, output_dir, - progress.as_mut().map(Progress::display_handle).unwrap_or(&mut io::stdout()), + progress + .as_mut() + .map(Progress::display_handle) + .unwrap_or(&mut io::stdout()), ) }), output_dir, @@ -178,7 +191,11 @@ pub fn decompress_file( // having a final status message is important especially in an accessibility context // as screen readers may not read a commands exit code, making it hard to reason // about whether the command succeeded without such a message - info!(accessible, "Successfully decompressed archive in {}.", nice_directory_display(output_dir)); + info!( + accessible, + "Successfully decompressed archive in {}.", + nice_directory_display(output_dir) + ); info!(accessible, "Files unpacked: {}", files_unpacked.len()); Ok(()) @@ -211,8 +228,9 @@ fn smart_unpack( // Only one file in the root directory, so we can just move it to the output directory let file = fs::read_dir(&temp_dir_path)?.next().expect("item exists")?; let file_path = file.path(); - let file_name = - file_path.file_name().expect("Should be safe because paths in archives should not end with '..'"); + let file_name = file_path + .file_name() + .expect("Should be safe because paths in archives should not end with '..'"); let correct_path = output_dir.join(file_name); // One case to handle tough is we need to check if a file with the same name already exists if !utils::clear_path(&correct_path, question_policy)? { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 790d370..ce971d5 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -74,7 +74,10 @@ pub fn run( file_visibility_policy: FileVisibilityPolicy, ) -> crate::Result<()> { match args.cmd { - Subcommand::Compress { mut files, output: output_path } => { + Subcommand::Compress { + mut files, + output: output_path, + } => { // If the output_path file exists and is the same as some of the input files, warn the user and skip those inputs (in order to avoid compression recursion) if output_path.exists() { deduplicate_input_files(&mut files, &fs::canonicalize(&output_path)?); @@ -95,7 +98,10 @@ pub fn run( .hint(format!(" ouch compress ... {}.zip", to_utf(&output_path))) .hint("") .hint("Alternatively, you can overwrite this option by using the '--format' flag:") - .hint(format!(" ouch compress ... {} --format tar.gz", to_utf(&output_path))); + .hint(format!( + " ouch compress ... {} --format tar.gz", + to_utf(&output_path) + )); return Err(error.into()); } @@ -118,7 +124,10 @@ pub fn run( let error = FinalError::with_title(format!("Cannot compress to '{}'.", output_path)) .detail("You are trying to compress multiple files.") - .detail(format!("The compression format '{}' cannot receive multiple files.", &formats[0])) + .detail(format!( + "The compression format '{}' cannot receive multiple files.", + &formats[0] + )) .detail("The only supported formats that archive files into an archive are .tar and .zip.") .hint(format!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0])) .hint(format!("From: {}", output_path)) @@ -130,9 +139,19 @@ pub fn run( if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { let error = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path))) .detail(format!("Found the format '{}' in an incorrect position.", format)) - .detail(format!("'{}' can only be used at the start of the file extension.", format)) - .hint(format!("If you wish to compress multiple files, start the extension with '{}'.", format)) - .hint(format!("Otherwise, remove the last '{}' from '{}'.", format, to_utf(&output_path))); + .detail(format!( + "'{}' can only be used at the start of the file extension.", + format + )) + .hint(format!( + "If you wish to compress multiple files, start the extension with '{}'.", + format + )) + .hint(format!( + "Otherwise, remove the last '{}' from '{}'.", + format, + to_utf(&output_path) + )); return Err(error.into()); } @@ -185,8 +204,14 @@ pub fn run( formats = new_formats; } } - let compress_result = - compress_files(files, formats, output_file, &output_path, question_policy, file_visibility_policy); + let compress_result = compress_files( + files, + formats, + output_file, + &output_path, + question_policy, + file_visibility_policy, + ); if let Ok(true) = compress_result { // this is only printed once, so it doesn't result in much text. On the other hand, @@ -201,8 +226,16 @@ pub fn run( if let Err(err) = fs::remove_file(&output_path) { eprintln!("{red}FATAL ERROR:\n", red = *colors::RED); eprintln!(" Please manually delete '{}'.", to_utf(&output_path)); - eprintln!(" Compression failed and we could not delete '{}'.", to_utf(&output_path),); - eprintln!(" Error:{reset} {}{red}.{reset}\n", err, reset = *colors::RESET, red = *colors::RED); + eprintln!( + " Compression failed and we could not delete '{}'.", + to_utf(&output_path), + ); + eprintln!( + " Error:{reset} {}{red}.{reset}\n", + err, + reset = *colors::RESET, + red = *colors::RED + ); } } @@ -240,7 +273,10 @@ pub fn run( .hint(" ouch decompress example.tar.gz") .hint("") .hint("Or overwrite this option with the '--format' flag:") - .hint(format!(" ouch decompress {} --format tar.gz", to_utf(&files_missing_format[0]))); + .hint(format!( + " ouch decompress {} --format tar.gz", + to_utf(&files_missing_format[0]) + )); return Err(error.into()); } @@ -285,7 +321,10 @@ pub fn run( if !not_archives.is_empty() { let error = FinalError::with_title("Cannot list archive contents") .detail("Only archives can have their contents listed") - .detail(format!("Files are not archives: {}", concatenate_os_str_list(¬_archives))); + .detail(format!( + "Files are not archives: {}", + concatenate_os_str_list(¬_archives) + )); return Err(error.into()); } @@ -316,7 +355,12 @@ fn check_mime_type( if let Some(detected_format) = try_infer_extension(path) { // Infering the file extension can have unpredicted consequences (e.g. the user just // mistyped, ...) which we should always inform the user about. - info!(accessible, "Detected file: `{}` extension as `{}`", path.display(), detected_format); + info!( + accessible, + "Detected file: `{}` extension as `{}`", + path.display(), + detected_format + ); if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { format.push(detected_format); } else { @@ -350,7 +394,10 @@ fn deduplicate_input_files(files: &mut Vec, output_path: &Path) { let mut idx = 0; while idx < files.len() { if files[idx] == output_path { - warning!("The output file and the input file are the same: `{}`, skipping...", output_path.display()); + warning!( + "The output file and the input file are the same: `{}`, skipping...", + output_path.display() + ); files.remove(idx); } else { idx += 1; diff --git a/src/error.rs b/src/error.rs index 63685b5..81ac85d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -94,7 +94,11 @@ impl FinalError { /// Only constructor #[must_use] pub fn with_title(title: impl Into) -> Self { - Self { title: title.into(), details: vec![], hints: vec![] } + Self { + title: title.into(), + details: vec![], + hints: vec![], + } } /// Add one detail line, can have multiple @@ -142,17 +146,35 @@ impl fmt::Display for Error { impl From for Error { fn from(err: std::io::Error) -> Self { match err.kind() { - std::io::ErrorKind::NotFound => Self::NotFound { error_title: err.to_string() }, - std::io::ErrorKind::PermissionDenied => Self::PermissionDenied { error_title: err.to_string() }, - std::io::ErrorKind::AlreadyExists => Self::AlreadyExists { error_title: err.to_string() }, - _other => Self::IoError { reason: err.to_string() }, + std::io::ErrorKind::NotFound => { + Self::NotFound { + error_title: err.to_string(), + } + } + std::io::ErrorKind::PermissionDenied => { + Self::PermissionDenied { + error_title: err.to_string(), + } + } + std::io::ErrorKind::AlreadyExists => { + Self::AlreadyExists { + error_title: err.to_string(), + } + } + _other => { + Self::IoError { + reason: err.to_string(), + } + } } } } impl From for Error { fn from(err: lzzzz::lz4f::Error) -> Self { - Self::Lz4Error { reason: err.to_string() } + Self::Lz4Error { + reason: err.to_string(), + } } } @@ -174,7 +196,9 @@ impl From for Error { impl From for Error { fn from(err: ignore::Error) -> Self { - Self::WalkdirError { reason: err.to_string() } + Self::WalkdirError { + reason: err.to_string(), + } } } diff --git a/src/extension.rs b/src/extension.rs index 92c255d..8995eef 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -25,7 +25,10 @@ impl Extension { /// Will panic if `formats` is empty pub fn new(formats: &'static [CompressionFormat], text: impl ToString) -> Self { assert!(!formats.is_empty()); - Self { compression_formats: formats, display_text: text.to_string() } + Self { + compression_formats: formats, + display_text: text.to_string(), + } } /// Checks if the first format in `compression_formats` is an archive @@ -80,20 +83,18 @@ impl CompressionFormat { impl fmt::Display for CompressionFormat { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - match self { - Gzip => ".gz", - Bzip => ".bz", - Zstd => ".zst", - Lz4 => ".lz4", - Lzma => ".lz", - Snappy => ".sz", - Tar => ".tar", - Zip => ".zip", - } - ) + let text = match self { + Gzip => ".gz", + Bzip => ".bz", + Zstd => ".zst", + Lz4 => ".lz4", + Lzma => ".lz", + Snappy => ".sz", + Tar => ".tar", + Zip => ".zip", + }; + + write!(f, "{text}") } } @@ -137,7 +138,11 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec (CompressionForm } pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec { - extensions.iter().flat_map(|extension| extension.compression_formats.iter()).copied().collect() + extensions + .iter() + .flat_map(|extension| extension.compression_formats.iter()) + .copied() + .collect() } diff --git a/src/list.rs b/src/list.rs index c8bd8ab..ab6d836 100644 --- a/src/list.rs +++ b/src/list.rs @@ -47,7 +47,9 @@ pub fn list_files( .map(|file| { let file = file?; if !crate::cli::ACCESSIBLE.get().unwrap() { - pb.as_ref().expect("exists").set_message(format!("Processing: {}", file.path.display())); + pb.as_ref() + .expect("exists") + .set_message(format!("Processing: {}", file.path.display())); } Ok(file) }) diff --git a/src/progress.rs b/src/progress.rs index 02dbcd3..b61cec5 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -36,7 +36,9 @@ impl io::Write for DisplayHandle { fn io_error(_: X) -> io::Error { io::Error::new(io::ErrorKind::Other, "failed to flush buffer") } - self.sender.send(String::from_utf8(self.buf.drain(..).collect()).map_err(io_error)?).map_err(io_error) + self.sender + .send(String::from_utf8(self.buf.drain(..).collect()).map_err(io_error)?) + .map_err(io_error) } } @@ -99,7 +101,10 @@ impl Progress { Progress { draw_stop: draw_tx, clean_done: clean_rx, - display_handle: DisplayHandle { buf: Vec::new(), sender: msg_tx }, + display_handle: DisplayHandle { + buf: Vec::new(), + sender: msg_tx, + }, } } diff --git a/src/utils/file_visibility.rs b/src/utils/file_visibility.rs index f0570bc..d8abd40 100644 --- a/src/utils/file_visibility.rs +++ b/src/utils/file_visibility.rs @@ -23,7 +23,12 @@ pub struct FileVisibilityPolicy { impl Default for FileVisibilityPolicy { fn default() -> Self { - Self { read_ignore: false, read_hidden: true, read_git_ignore: false, read_git_exclude: false } + Self { + read_ignore: false, + read_hidden: true, + read_git_ignore: false, + read_git_exclude: false, + } } } @@ -41,13 +46,19 @@ impl FileVisibilityPolicy { #[must_use] /// Enables reading .gitignore files. pub fn read_git_ignore(self, read_git_ignore: bool) -> Self { - Self { read_git_ignore, ..self } + Self { + read_git_ignore, + ..self + } } #[must_use] /// Enables reading `.git/info/exclude` files. pub fn read_git_exclude(self, read_git_exclude: bool) -> Self { - Self { read_git_exclude, ..self } + Self { + read_git_exclude, + ..self + } } #[must_use] diff --git a/src/utils/fs.rs b/src/utils/fs.rs index ae94097..ec321bd 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -129,5 +129,7 @@ pub fn try_infer_extension(path: &Path) -> Option { /// This is the same as the nightly https://doc.rust-lang.org/std/path/struct.Path.html#method.is_symlink // Useful to detect broken symlinks when compressing. (So we can safely ignore them) pub fn is_symlink(path: &Path) -> bool { - fs::symlink_metadata(path).map(|m| m.file_type().is_symlink()).unwrap_or(false) + fs::symlink_metadata(path) + .map(|m| m.file_type().is_symlink()) + .unwrap_or(false) } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 78fb2a9..e30037b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -29,6 +29,9 @@ mod utf8 { /// Filter out list of paths that are not utf8 valid pub fn get_invalid_utf8_paths(paths: &[PathBuf]) -> Vec<&PathBuf> { - paths.iter().filter_map(|path| is_invalid_utf8(path).then(|| path)).collect() + paths + .iter() + .filter_map(|path| is_invalid_utf8(path).then(|| path)) + .collect() } } diff --git a/src/utils/question.rs b/src/utils/question.rs index 13ef5b6..a15c660 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -110,7 +110,10 @@ pub struct Confirmation<'a> { impl<'a> Confirmation<'a> { /// Creates a new Confirmation. pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { - Self { prompt, placeholder: pattern } + Self { + prompt, + placeholder: pattern, + } } /// Creates user message and receives a boolean input to be used on the program @@ -124,9 +127,23 @@ impl<'a> Confirmation<'a> { // Ask the same question to end while no valid answers are given loop { if *crate::cli::ACCESSIBLE.get().unwrap() { - print!("{} {}yes{}/{}no{}: ", message, *colors::GREEN, *colors::RESET, *colors::RED, *colors::RESET); + print!( + "{} {}yes{}/{}no{}: ", + message, + *colors::GREEN, + *colors::RESET, + *colors::RED, + *colors::RESET + ); } else { - print!("{} [{}Y{}/{}n{}] ", message, *colors::GREEN, *colors::RESET, *colors::RED, *colors::RESET); + print!( + "{} [{}Y{}/{}n{}] ", + message, + *colors::GREEN, + *colors::RESET, + *colors::RED, + *colors::RESET + ); } io::stdout().flush()?; diff --git a/tests/integration.rs b/tests/integration.rs index 6112825..eec7a29 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -51,7 +51,10 @@ enum Extension { // converts a list of extension structs to string fn merge_extensions(ext: impl ToString, exts: Vec) -> String { - once(ext.to_string()).chain(exts.into_iter().map(|x| x.to_string())).collect::>().join(".") + once(ext.to_string()) + .chain(exts.into_iter().map(|x| x.to_string())) + .collect::>() + .join(".") } // create random nested directories and files under the specified directory @@ -64,7 +67,10 @@ fn create_random_files(dir: impl Into, depth: u8, rng: &mut SmallRng) { // create 0 to 7 random files for _ in 0..rng.gen_range(0..8u32) { - write_random_content(&mut tempfile::Builder::new().tempfile_in(dir).unwrap().keep().unwrap().0, rng); + write_random_content( + &mut tempfile::Builder::new().tempfile_in(dir).unwrap().keep().unwrap().0, + rng, + ); } // create more random files in 0 to 3 new directories @@ -83,7 +89,10 @@ fn single_empty_file(ext: Extension, #[any(size_range(0..8).lift())] exts: Vec Date: Sat, 4 Jun 2022 14:03:33 -0300 Subject: [PATCH 08/10] minor simplifications and renamings --- src/archive/zip.rs | 8 ++++---- src/commands/decompress.rs | 3 ++- src/commands/mod.rs | 6 +++--- src/error.rs | 3 +-- src/extension.rs | 4 ++-- src/utils/formatting.rs | 38 +++++++++++++++++++------------------- src/utils/mod.rs | 2 +- 7 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/archive/zip.rs b/src/archive/zip.rs index c0bdf48..a16468e 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -16,7 +16,7 @@ use crate::{ info, list::FileInArchive, utils::{ - self, cd_into_same_dir_as, concatenate_os_str_list, get_invalid_utf8_paths, strip_cur_dir, to_utf, Bytes, + self, cd_into_same_dir_as, get_invalid_utf8_paths, pretty_format_list_of_paths, strip_cur_dir, to_utf, Bytes, FileVisibilityPolicy, }, }; @@ -47,7 +47,7 @@ where display_zip_comment_if_exists(&file); - match (&*file.name()).ends_with('/') { + match file.name().ends_with('/') { _is_dir @ true => { // This is printed for every file in the archive and has little // importance for most users, but would generate lots of @@ -146,7 +146,7 @@ where .detail("Zip archives require files to have valid UTF-8 paths") .detail(format!( "Files with invalid paths: {}", - concatenate_os_str_list(&invalid_unicode_filenames) + pretty_format_list_of_paths(&invalid_unicode_filenames) )); return Err(error.into()); @@ -183,7 +183,7 @@ where return Err(e.into()); } }; - writer.write_all(&*file_bytes)?; + writer.write_all(&file_bytes)?; } } diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index f18ffe9..7ddbc9c 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -203,7 +203,8 @@ pub fn decompress_file( /// Unpacks an archive with some heuristics /// - If the archive contains only one file, it will be extracted to the `output_dir` -/// - If the archive contains multiple files, it will be extracted to a subdirectory of the output_dir named after the archive (given by `output_file_path`) +/// - If the archive contains multiple files, it will be extracted to a subdirectory of the +/// output_dir named after the archive (given by `output_file_path`) /// Note: This functions assumes that `output_dir` exists fn smart_unpack( unpack_fn: Box crate::Result>>, diff --git a/src/commands/mod.rs b/src/commands/mod.rs index ce971d5..fb4e4f8 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -22,7 +22,7 @@ use crate::{ info, list::ListOptions, utils::{ - self, concatenate_os_str_list, dir_is_empty, to_utf, try_infer_extension, user_wants_to_continue, + self, dir_is_empty, pretty_format_list_of_paths, to_utf, try_infer_extension, user_wants_to_continue, FileVisibilityPolicy, }, warning, Opts, QuestionAction, QuestionPolicy, Subcommand, @@ -266,7 +266,7 @@ pub fn run( let error = FinalError::with_title("Cannot decompress files without extensions") .detail(format!( "Files without supported extensions: {}", - concatenate_os_str_list(&files_missing_format) + pretty_format_list_of_paths(&files_missing_format) )) .detail("Decompression formats are detected automatically by the file extension") .hint("Provide a file with a supported extension:") @@ -323,7 +323,7 @@ pub fn run( .detail("Only archives can have their contents listed") .detail(format!( "Files are not archives: {}", - concatenate_os_str_list(¬_archives) + pretty_format_list_of_paths(¬_archives) )); return Err(error.into()); diff --git a/src/error.rs b/src/error.rs index 81ac85d..242a283 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,7 +9,6 @@ use std::{ use crate::utils::colors::*; -#[allow(missing_docs)] /// All errors that can be generated by `ouch` #[derive(Debug, PartialEq, Eq)] pub enum Error { @@ -53,7 +52,7 @@ pub struct FinalError { } impl Display for FinalError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Title // // When in ACCESSIBLE mode, the square brackets are suppressed diff --git a/src/extension.rs b/src/extension.rs index 8995eef..a70c60d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -39,7 +39,7 @@ impl Extension { } impl fmt::Display for Extension { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.display_text.fmt(f) } } @@ -174,7 +174,7 @@ mod tests { // Panics if formats has an empty list of compression formats pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec) { - let mut extensions: Vec = flatten_compression_formats(&formats); + let mut extensions: Vec = flatten_compression_formats(formats); let first_extension = extensions.remove(0); (first_extension, extensions) } diff --git a/src/utils/formatting.rs b/src/utils/formatting.rs index 97330c8..e250791 100644 --- a/src/utils/formatting.rs +++ b/src/utils/formatting.rs @@ -1,14 +1,11 @@ -use std::{borrow::Cow, cmp, path::Path}; +use std::{borrow::Cow, cmp, env, path::Path}; -// The lifetimes here could elided but I think they help -// comprehension in this case -#[allow(clippy::needless_lifetimes)] /// Converts an OsStr to utf8 with custom formatting. /// /// This is different from [`Path::display`]. /// /// See for a comparison. -pub fn to_utf<'a>(os_str: &'a Path) -> Cow<'a, str> { +pub fn to_utf(os_str: &Path) -> Cow { let format = || { let text = format!("{:?}", os_str); Cow::Owned(text.trim_matches('"').to_string()) @@ -17,34 +14,37 @@ pub fn to_utf<'a>(os_str: &'a Path) -> Cow<'a, str> { os_str.to_str().map_or_else(format, Cow::Borrowed) } -/// Removes the current dir from the beginning of a path -/// normally used for presentation sake. -/// If this function fails, it will return source path as a PathBuf. +/// Removes the current dir from the beginning of a path as it's redundant information, +/// useful for presentation sake. pub fn strip_cur_dir(source_path: &Path) -> &Path { - let cwd = std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf()); - source_path.strip_prefix(cwd).unwrap_or(source_path) + let current_dir = env::current_dir(); + + let current_dir = match ¤t_dir { + Ok(inner) => inner.as_path(), + Err(_) => Path::new(""), + }; + + source_path.strip_prefix(current_dir).unwrap_or(source_path) } /// Converts a slice of AsRef to comma separated String /// /// Panics if the slice is empty. -pub fn concatenate_os_str_list(os_strs: &[impl AsRef]) -> String { +pub fn pretty_format_list_of_paths(os_strs: &[impl AsRef]) -> String { let mut iter = os_strs.iter().map(AsRef::as_ref); - let mut string = to_utf(iter.next().unwrap()).to_string(); // May panic + let first_element = iter.next().unwrap(); + let mut string = to_utf(first_element).into_owned(); for os_str in iter { string += ", "; - string += &*to_utf(os_str); + string += &to_utf(os_str); } string } -// The lifetimes here could elided but I think they help -// comprehension in this case -#[allow(clippy::needless_lifetimes)] -/// Display the directory name, but change to "current directory" when necessary. -pub fn nice_directory_display<'a>(path: &'a Path) -> Cow<'a, str> { +/// Display the directory name, but use "current directory" when necessary. +pub fn nice_directory_display(path: &Path) -> Cow { if path == Path::new(".") { Cow::Borrowed("current directory") } else { @@ -67,7 +67,7 @@ impl Bytes { } impl std::fmt::Display for Bytes { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let num = self.bytes; debug_assert!(num >= 0.0); if num < 1_f64 { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e30037b..276fd74 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -10,7 +10,7 @@ mod fs; mod question; pub use file_visibility::FileVisibilityPolicy; -pub use formatting::{concatenate_os_str_list, nice_directory_display, strip_cur_dir, to_utf, Bytes}; +pub use formatting::{nice_directory_display, pretty_format_list_of_paths, strip_cur_dir, to_utf, Bytes}; pub use fs::{ cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, is_symlink, try_infer_extension, }; From 278bc980c175d2f4e6d2e5a34e29c82c2f08cc0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 14:36:57 -0300 Subject: [PATCH 09/10] small changes and rewrite zip warning message --- src/commands/compress.rs | 4 ++-- src/commands/decompress.rs | 8 ++++---- src/commands/list.rs | 6 +++--- src/commands/mod.rs | 28 ++++++---------------------- src/macros.rs | 4 ++-- src/utils/question.rs | 6 +++--- 6 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 0f141bc..05166a7 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -7,7 +7,7 @@ use fs_err as fs; use crate::{ archive, - commands::warn_user_about_in_memory_zip_compression, + commands::warn_user_about_loading_zip_in_memory, extension::{ split_first_compression_format, CompressionFormat::{self, *}, @@ -114,7 +114,7 @@ pub fn compress_files( } Zip => { if formats.len() > 1 { - warn_user_about_in_memory_zip_compression(); + warn_user_about_loading_zip_in_memory(); // give user the option to continue compressing after warning is shown if !user_wants_to_continue(output_dir, question_policy, QuestionAction::Compression)? { diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 7ddbc9c..21bae91 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -7,7 +7,7 @@ use std::{ use fs_err as fs; use crate::{ - commands::warn_user_about_in_memory_zip_decompression, + commands::warn_user_about_loading_zip_in_memory, extension::{ split_first_compression_format, CompressionFormat::{self, *}, @@ -152,7 +152,7 @@ pub fn decompress_file( } Zip => { if formats.len() > 1 { - warn_user_about_in_memory_zip_decompression(); + warn_user_about_loading_zip_in_memory(); // give user the option to continue decompressing after warning is shown if !user_wants_to_continue(input_file_path, question_policy, QuestionAction::Decompression)? { @@ -233,7 +233,7 @@ fn smart_unpack( .file_name() .expect("Should be safe because paths in archives should not end with '..'"); let correct_path = output_dir.join(file_name); - // One case to handle tough is we need to check if a file with the same name already exists + // Before moving, need to check if a file with the same name already exists if !utils::clear_path(&correct_path, question_policy)? { return Ok(ControlFlow::Break(())); } @@ -246,7 +246,7 @@ fn smart_unpack( ); } else { // Multiple files in the root directory, so: - // Rename the temporary directory to the archive name, which is output_file_path + // Rename the temporary directory to the archive name, which is output_file_path // One case to handle tough is we need to check if a file with the same name already exists if !utils::clear_path(output_file_path, question_policy)? { return Ok(ControlFlow::Break(())); diff --git a/src/commands/list.rs b/src/commands/list.rs index 6e27eb4..a5832d5 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -6,7 +6,7 @@ use std::{ use fs_err as fs; use crate::{ - commands::warn_user_about_in_memory_zip_decompression, + commands::warn_user_about_loading_zip_in_memory, extension::CompressionFormat::{self, *}, list::{self, FileInArchive, ListOptions}, utils::user_wants_to_continue, @@ -30,7 +30,7 @@ pub fn list_archive_contents( // in-memory decompression/copying first. // // Any other Zip decompression done can take up the whole RAM and freeze ouch. - if let [Zip] = *formats.as_slice() { + if let &[Zip] = formats.as_slice() { let zip_archive = zip::ZipArchive::new(reader)?; let files = crate::archive::zip::list_archive(zip_archive); list::list_files(archive_path, files, list_options)?; @@ -65,7 +65,7 @@ pub fn list_archive_contents( Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))), Zip => { if formats.len() > 1 { - warn_user_about_in_memory_zip_decompression(); + warn_user_about_loading_zip_in_memory(); // give user the option to continue decompressing after warning is shown if !user_wants_to_continue(archive_path, question_policy, QuestionAction::Decompression)? { diff --git a/src/commands/mod.rs b/src/commands/mod.rs index fb4e4f8..5e29ffa 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -28,28 +28,12 @@ use crate::{ warning, Opts, QuestionAction, QuestionPolicy, Subcommand, }; -/// Warn the user that .zip archives have limitations that require it to compress everything -/// in-memory when working with multiple chained formats. -/// -/// This can sadly lead to out-of-memory scenarios for big archives. -fn warn_user_about_in_memory_zip_compression() { - const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = - "\tThere is a limitation for .zip archives with extra extensions. (e.g. .zip.gz)\n\ - \tThe design of .zip makes it impossible to compress via stream, so it must be done entirely in memory.\n\ - \tBy compressing .zip with extra compression formats, you can run out of RAM if the file is too large!"; - - warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); -} - -/// Warn the user that .zip archives have limitations that require it to decompress everything -/// in-memory when working with multiple chained formats. -/// -/// This can sadly lead to out-of-memory scenarios for big archives. -fn warn_user_about_in_memory_zip_decompression() { - const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = - "\tThere is a limitation for .zip archives with extra extensions. (e.g. .zip.gz)\n\ - \tThe design of .zip makes it impossible to compress via stream, so it must be done entirely in memory.\n\ - \tBy compressing .zip with extra compression formats, you can run out of RAM if the file is too large!"; +/// Warn the user that (de)compressing this .zip archive might freeze their system. +fn warn_user_about_loading_zip_in_memory() { + const ZIP_IN_MEMORY_LIMITATION_WARNING: &str = "\n\ + \tThe format '.zip' is limited and cannot be (de)compressed using encoding streams.\n\ + \tWhen using '.zip' with other formats, (de)compression must be done in-memory\n\ + \tCareful, you might run out of RAM if the archive is too large!"; warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); } diff --git a/src/macros.rs b/src/macros.rs index dfe6b62..c2d209b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -69,8 +69,8 @@ pub fn _warning_helper() { use crate::utils::colors::{ORANGE, RESET}; if *crate::cli::ACCESSIBLE.get().unwrap() { - print!("{}Warning:{} ", *ORANGE, *RESET); + eprint!("{}Warning:{} ", *ORANGE, *RESET); } else { - print!("{}[WARNING]{} ", *ORANGE, *RESET); + eprint!("{}[WARNING]{} ", *ORANGE, *RESET); } } diff --git a/src/utils/question.rs b/src/utils/question.rs index a15c660..bea92dd 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -83,13 +83,13 @@ pub fn user_wants_to_continue( QuestionPolicy::AlwaysNo => Ok(false), QuestionPolicy::Ask => { let action = match question_action { - QuestionAction::Compression => "compressing", - QuestionAction::Decompression => "decompressing", + QuestionAction::Compression => "compress", + QuestionAction::Decompression => "decompress", }; let path = to_utf(strip_cur_dir(path)); let path = Some(&*path); let placeholder = Some("FILE"); - Confirmation::new(&format!("Do you want to continue {} 'FILE'?", action), placeholder).ask(path) + Confirmation::new(&format!("Do you want to {} 'FILE'?", action), placeholder).ask(path) } } } From 801189ec0256947ebcf3b9932cdbfab80a092d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 14:50:03 -0300 Subject: [PATCH 10/10] create is_running_in_accessible_mode helper also renamed some variables --- src/accessible.rs | 16 ++++++++++++++++ src/cli.rs | 10 ++-------- src/commands/compress.rs | 18 +++++++++--------- src/commands/mod.rs | 4 +--- src/error.rs | 6 +++--- src/list.rs | 10 ++++++---- src/macros.rs | 9 +++++---- src/main.rs | 1 + src/progress.rs | 4 +++- src/utils/question.rs | 3 ++- 10 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 src/accessible.rs diff --git a/src/accessible.rs b/src/accessible.rs new file mode 100644 index 0000000..5f26411 --- /dev/null +++ b/src/accessible.rs @@ -0,0 +1,16 @@ +use once_cell::sync::OnceCell; + +/// Whether to enable accessible output (removes info output and reduces other +/// output, removes visual markers like '[' and ']'). +/// Removes th progress bar as well +pub static ACCESSIBLE: OnceCell = OnceCell::new(); + +pub fn is_running_in_accessible_mode() -> bool { + ACCESSIBLE.get().copied().unwrap_or(false) +} + +pub fn set_accessible(value: bool) { + if ACCESSIBLE.get().is_none() { + ACCESSIBLE.set(value).unwrap(); + } +} diff --git a/src/cli.rs b/src/cli.rs index f8578e6..e9366fa 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -8,14 +8,8 @@ use std::{ use clap::Parser; use fs_err as fs; -use once_cell::sync::OnceCell; -use crate::{utils::FileVisibilityPolicy, Opts, QuestionPolicy, Subcommand}; - -/// Whether to enable accessible output (removes info output and reduces other -/// output, removes visual markers like '[' and ']'). -/// Removes th progress bar as well -pub static ACCESSIBLE: OnceCell = OnceCell::new(); +use crate::{accessible::set_accessible, utils::FileVisibilityPolicy, Opts, QuestionPolicy, Subcommand}; impl Opts { /// A helper method that calls `clap::Parser::parse`. @@ -26,7 +20,7 @@ impl Opts { pub fn parse_args() -> crate::Result<(Self, QuestionPolicy, FileVisibilityPolicy)> { let mut opts = Self::parse(); - ACCESSIBLE.set(opts.accessible).unwrap(); + set_accessible(opts.accessible); let (Subcommand::Compress { files, .. } | Subcommand::Decompress { files, .. } diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 05166a7..ddf24f8 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -20,14 +20,14 @@ use crate::{ // Compress files into an `output_file` // -// files are the list of paths to be compressed: ["dir/file1.txt", "dir/file2.txt"] -// formats contains each format necessary for compression, example: [Tar, Gz] (in compression order) -// output_file is the resulting compressed file name, example: "compressed.tar.gz" +// - `files`: is the list of paths to be compressed: ["dir/file1.txt", "dir/file2.txt"] +// - `extensions`: contains each compression format necessary for compressing, example: [Tar, Gz] (in compression order) +// - `output_file` is the resulting compressed file name, example: "compressed.tar.gz" // // Returns Ok(true) if compressed all files successfully, and Ok(false) if user opted to skip files pub fn compress_files( files: Vec, - formats: Vec, + extensions: Vec, output_file: fs::File, output_dir: &Path, question_policy: QuestionPolicy, @@ -72,13 +72,13 @@ pub fn compress_files( Ok(encoder) }; - let (first_extension, extensions) = split_first_compression_format(&formats); + let (first_format, formats) = split_first_compression_format(&extensions); - for format in extensions.iter().rev() { + for format in formats.iter().rev() { writer = chain_writer_encoder(format, writer)?; } - match first_extension { + match first_format { Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => { let _progress = Progress::new_accessible_aware( total_input_size, @@ -88,7 +88,7 @@ pub fn compress_files( })), ); - writer = chain_writer_encoder(&first_extension, writer)?; + writer = chain_writer_encoder(&first_format, writer)?; let mut reader = fs::File::open(&files[0]).unwrap(); io::copy(&mut reader, &mut writer)?; } @@ -113,7 +113,7 @@ pub fn compress_files( writer.flush()?; } Zip => { - if formats.len() > 1 { + if !formats.is_empty() { warn_user_about_loading_zip_in_memory(); // give user the option to continue compressing after warning is shown diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 5e29ffa..c684cf7 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,4 @@ -//! Core of the crate, where the `compress_files` and `decompress_file` functions are implemented -//! -//! Also, where correctly call functions based on the detected `Command`. +//! Receive command from the cli and call the respective function for that command. mod compress; mod decompress; diff --git a/src/error.rs b/src/error.rs index 242a283..967cb8a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,7 +7,7 @@ use std::{ fmt::{self, Display}, }; -use crate::utils::colors::*; +use crate::{accessible::is_running_in_accessible_mode, utils::colors::*}; /// All errors that can be generated by `ouch` #[derive(Debug, PartialEq, Eq)] @@ -56,7 +56,7 @@ impl Display for FinalError { // Title // // When in ACCESSIBLE mode, the square brackets are suppressed - if *crate::cli::ACCESSIBLE.get().unwrap_or(&false) { + if is_running_in_accessible_mode() { write!(f, "{}ERROR{}: {}", *RED, *RESET, self.title)?; } else { write!(f, "{}[ERROR]{} {}", *RED, *RESET, self.title)?; @@ -73,7 +73,7 @@ impl Display for FinalError { writeln!(f)?; // to reduce redundant output for text-to-speach systems, braille // displays and so on, only print "hints" once in ACCESSIBLE mode - if *crate::cli::ACCESSIBLE.get().unwrap_or(&false) { + if is_running_in_accessible_mode() { write!(f, "\n{}hints:{}", *GREEN, *RESET)?; for hint in &self.hints { write!(f, "\n{}", hint)?; diff --git a/src/list.rs b/src/list.rs index ab6d836..2f2cf93 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,10 +1,11 @@ -//! Implementation of the 'list' command, print list of files in an archive +//! Some implementation helpers related to the 'list' command. use std::path::{Path, PathBuf}; use indicatif::{ProgressBar, ProgressStyle}; use self::tree::Tree; +use crate::accessible::is_running_in_accessible_mode; /// Options controlling how archive contents should be listed #[derive(Debug, Clone, Copy)] @@ -33,7 +34,7 @@ pub fn list_files( println!("Archive: {}", archive.display()); if list_options.tree { - let pb = if !crate::cli::ACCESSIBLE.get().unwrap() { + let pb = if !is_running_in_accessible_mode() { let template = "{wide_msg} [{elapsed_precise}] {spinner:.green}"; let pb = ProgressBar::new_spinner(); pb.set_style(ProgressStyle::default_bar().template(template)); @@ -46,7 +47,7 @@ pub fn list_files( .into_iter() .map(|file| { let file = file?; - if !crate::cli::ACCESSIBLE.get().unwrap() { + if !is_running_in_accessible_mode() { pb.as_ref() .expect("exists") .set_message(format!("Processing: {}", file.path.display())); @@ -77,7 +78,7 @@ fn print_entry(name: impl std::fmt::Display, is_dir: bool) { // if in ACCESSIBLE mode, use colors but print final / in case colors // aren't read out aloud with a screen reader or aren't printed on a // braille reader - } else if *crate::cli::ACCESSIBLE.get().unwrap() { + } else if is_running_in_accessible_mode() { println!("{}{}{}/{}", *BLUE, *STYLE_BOLD, name, *ALL_RESET); } else { println!("{}{}{}{}", *BLUE, *STYLE_BOLD, name, *ALL_RESET); @@ -105,6 +106,7 @@ mod tree { file: Option, children: LinkedHashMap, } + impl Tree { /// Insert a file into the tree pub fn insert(&mut self, file: FileInArchive) { diff --git a/src/macros.rs b/src/macros.rs index c2d209b..9068b6d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,7 @@ //! Macros used on ouch. +use crate::accessible::is_running_in_accessible_mode; + /// Macro that prints \[INFO\] messages, wraps [`println`]. /// /// There are essentially two different versions of the `info!()` macro: @@ -28,7 +30,7 @@ macro_rules! info { (@$display_handle: expr, accessible, $($arg:tt)*) => { let display_handle = &mut $display_handle; // if in ACCESSIBLE mode, suppress the "[INFO]" and just print the message - if !(*$crate::cli::ACCESSIBLE.get().unwrap()) { + if !$crate::accessible::is_running_in_accessible_mode() { $crate::macros::_info_helper(display_handle); } writeln!(display_handle, $($arg)*).unwrap(); @@ -39,8 +41,7 @@ macro_rules! info { info!(@::std::io::stdout(), inaccessible, $($arg)*); }; (@$display_handle: expr, inaccessible, $($arg:tt)*) => { - if (!$crate::cli::ACCESSIBLE.get().unwrap()) - { + if !$crate::accessible::is_running_in_accessible_mode() { let display_handle = &mut $display_handle; $crate::macros::_info_helper(display_handle); writeln!(display_handle, $($arg)*).unwrap(); @@ -68,7 +69,7 @@ macro_rules! warning { pub fn _warning_helper() { use crate::utils::colors::{ORANGE, RESET}; - if *crate::cli::ACCESSIBLE.get().unwrap() { + if is_running_in_accessible_mode() { eprint!("{}Warning:{} ", *ORANGE, *RESET); } else { eprint!("{}[WARNING]{} ", *ORANGE, *RESET); diff --git a/src/main.rs b/src/main.rs index 38c86f0..db876e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ // Macros should be declared first pub mod macros; +pub mod accessible; pub mod archive; pub mod cli; pub mod commands; diff --git a/src/progress.rs b/src/progress.rs index b61cec5..5b318c9 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -8,6 +8,8 @@ use std::{ use indicatif::{ProgressBar, ProgressStyle}; +use crate::accessible::is_running_in_accessible_mode; + /// Draw a ProgressBar using a function that checks periodically for the progress pub struct Progress { draw_stop: Sender<()>, @@ -51,7 +53,7 @@ impl Progress { precise: bool, current_position_fn: Option u64 + Send>>, ) -> Option { - if *crate::cli::ACCESSIBLE.get().unwrap() { + if is_running_in_accessible_mode() { return None; } Some(Self::new(total_input_size, precise, current_position_fn)) diff --git a/src/utils/question.rs b/src/utils/question.rs index bea92dd..f8009ad 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -13,6 +13,7 @@ use fs_err as fs; use super::{strip_cur_dir, to_utf}; use crate::{ + accessible::is_running_in_accessible_mode, error::{Error, Result}, utils::colors, }; @@ -126,7 +127,7 @@ impl<'a> Confirmation<'a> { // Ask the same question to end while no valid answers are given loop { - if *crate::cli::ACCESSIBLE.get().unwrap() { + if is_running_in_accessible_mode() { print!( "{} {}yes{}/{}no{}: ", message,