15 utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue, EscapedPathDisplay},
16 warning, QuestionAction, QuestionPolicy, Result,
19 /// Check, for each file, if the mime type matches the detected extensions.
21 /// In case the file doesn't has any extensions, try to infer the format.
23 /// TODO: maybe the name of this should be "magic numbers" or "file signature",
25 pub fn check_mime_type(
27 formats: &mut [Vec<Extension>],
28 question_policy: QuestionPolicy,
29 ) -> Result<ControlFlow<()>> {
30 for (path, format) in files.iter().zip(formats.iter_mut()) {
31 if format.is_empty() {
32 // File with no extension
33 // Try to detect it automatically and prompt the user about it
34 if let Some(detected_format) = try_infer_extension(path) {
35 // Inferring the file extension can have unpredicted consequences (e.g. the user just
36 // mistyped, ...) which we should always inform the user about.
39 "Detected file: `{}` extension as `{}`",
43 if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
44 format.push(detected_format);
46 return Ok(ControlFlow::Break(()));
49 } else if let Some(detected_format) = try_infer_extension(path) {
50 // File ending with extension
51 // Try to detect the extension and warn the user if it differs from the written one
52 let outer_ext = format.iter().next_back().unwrap();
55 .ends_with(detected_format.compression_formats)
58 "The file extension: `{}` differ from the detected extension: `{}`",
62 if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
63 return Ok(ControlFlow::Break(()));
67 // NOTE: If this actually produces no false positives, we can upgrade it in the future
68 // to a warning and ask the user if he wants to continue decompressing.
69 info!(accessible, "Could not detect the extension of `{}`", path.display());
72 Ok(ControlFlow::Continue(()))
75 /// In the context of listing archives, this function checks if `ouch` was told to list
76 /// the contents of a compressed file that is not an archive
77 pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec<Extension>]) -> Result<()> {
78 let mut not_archives = files
81 .filter(|(_, formats)| !formats.first().map(Extension::is_archive).unwrap_or(false))
82 .map(|(path, _)| path)
85 if not_archives.peek().is_some() {
86 let not_archives: Vec<_> = not_archives.collect();
87 let error = FinalError::with_title("Cannot list archive contents")
88 .detail("Only archives can have their contents listed")
90 "Files are not archives: {}",
91 pretty_format_list_of_paths(¬_archives)
94 return Err(error.into());
100 /// Show error if archive format is not the first format in the chain.
101 pub fn check_archive_formats_position(formats: &[extension::Extension], output_path: &Path) -> Result<()> {
102 if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) {
103 let error = FinalError::with_title(format!(
104 "Cannot compress to '{}'.",
105 EscapedPathDisplay::new(output_path)
107 .detail(format!("Found the format '{format}' in an incorrect position."))
109 "'{format}' can only be used at the start of the file extension."
112 "If you wish to compress multiple files, start the extension with '{format}'."
115 "Otherwise, remove the last '{}' from '{}'.",
117 EscapedPathDisplay::new(output_path)
120 return Err(error.into());