From 483b1dbdd809b72874e0d4ca3b8bf77c45213a5d Mon Sep 17 00:00:00 2001 From: Integral Date: Thu, 21 Nov 2024 00:33:31 +0800 Subject: [PATCH] Throw an error when try to read a block device --- CHANGELOG.md | 1 + src/input.rs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c0f591b..41cadb51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ - Support 'statically linked binary' for aarch64 in 'Release' page, see #2992 (@tzq0301) - Update options in shell completions and the man page of `bat`, see #2995 (@akinomyoga) - Update nix dev-dependency to v0.29.0, see #3112 (@decathorpe) +- Throw an error when try to read a block device, see #3128 (@Integral-Tech) ## Syntaxes diff --git a/src/input.rs b/src/input.rs index 0ebaa4ce..7ea552c6 100644 --- a/src/input.rs +++ b/src/input.rs @@ -4,6 +4,9 @@ use std::fs::File; use std::io::{self, BufRead, BufReader, Read}; use std::path::{Path, PathBuf}; +#[cfg(target_family = "unix")] +use std::os::unix::fs::FileTypeExt; + use clircle::{Clircle, Identifier}; use content_inspector::{self, ContentType}; @@ -218,10 +221,18 @@ impl<'a> Input<'a> { reader: { let mut file = File::open(&path) .map_err(|e| format!("'{}': {}", path.to_string_lossy(), e))?; - if file.metadata()?.is_dir() { + let metadata = file.metadata()?; + if metadata.is_dir() { return Err(format!("'{}' is a directory.", path.to_string_lossy()).into()); } + #[cfg(target_family = "unix")] + if metadata.file_type().is_block_device() { + return Err( + format!("'{}' is a block device.", path.to_string_lossy()).into() + ); + } + if let Some(stdout) = stdout_identifier { let input_identifier = Identifier::try_from(file).map_err(|e| { format!("{}: Error identifying file: {}", path.to_string_lossy(), e)