Throw an error when try to read a block device

This commit is contained in:
Integral 2024-11-21 00:33:31 +08:00
parent e608b33142
commit 483b1dbdd8
No known key found for this signature in database
GPG Key ID: 06313911057DD5A8
2 changed files with 13 additions and 1 deletions

View File

@ -48,6 +48,7 @@
- Support 'statically linked binary' for aarch64 in 'Release' page, see #2992 (@tzq0301) - 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 options in shell completions and the man page of `bat`, see #2995 (@akinomyoga)
- Update nix dev-dependency to v0.29.0, see #3112 (@decathorpe) - 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 ## Syntaxes

View File

@ -4,6 +4,9 @@ use std::fs::File;
use std::io::{self, BufRead, BufReader, Read}; use std::io::{self, BufRead, BufReader, Read};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
#[cfg(target_family = "unix")]
use std::os::unix::fs::FileTypeExt;
use clircle::{Clircle, Identifier}; use clircle::{Clircle, Identifier};
use content_inspector::{self, ContentType}; use content_inspector::{self, ContentType};
@ -218,10 +221,18 @@ impl<'a> Input<'a> {
reader: { reader: {
let mut file = File::open(&path) let mut file = File::open(&path)
.map_err(|e| format!("'{}': {}", path.to_string_lossy(), e))?; .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()); 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 { if let Some(stdout) = stdout_identifier {
let input_identifier = Identifier::try_from(file).map_err(|e| { let input_identifier = Identifier::try_from(file).map_err(|e| {
format!("{}: Error identifying file: {}", path.to_string_lossy(), e) format!("{}: Error identifying file: {}", path.to_string_lossy(), e)