Skip to content

Commit 483b1db

Browse files
committed
Throw an error when try to read a block device
1 parent e608b33 commit 483b1db

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- Support 'statically linked binary' for aarch64 in 'Release' page, see #2992 (@tzq0301)
4949
- Update options in shell completions and the man page of `bat`, see #2995 (@akinomyoga)
5050
- Update nix dev-dependency to v0.29.0, see #3112 (@decathorpe)
51+
- Throw an error when try to read a block device, see #3128 (@Integral-Tech)
5152

5253
## Syntaxes
5354

src/input.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::fs::File;
44
use std::io::{self, BufRead, BufReader, Read};
55
use std::path::{Path, PathBuf};
66

7+
#[cfg(target_family = "unix")]
8+
use std::os::unix::fs::FileTypeExt;
9+
710
use clircle::{Clircle, Identifier};
811
use content_inspector::{self, ContentType};
912

@@ -218,10 +221,18 @@ impl<'a> Input<'a> {
218221
reader: {
219222
let mut file = File::open(&path)
220223
.map_err(|e| format!("'{}': {}", path.to_string_lossy(), e))?;
221-
if file.metadata()?.is_dir() {
224+
let metadata = file.metadata()?;
225+
if metadata.is_dir() {
222226
return Err(format!("'{}' is a directory.", path.to_string_lossy()).into());
223227
}
224228

229+
#[cfg(target_family = "unix")]
230+
if metadata.file_type().is_block_device() {
231+
return Err(
232+
format!("'{}' is a block device.", path.to_string_lossy()).into()
233+
);
234+
}
235+
225236
if let Some(stdout) = stdout_identifier {
226237
let input_identifier = Identifier::try_from(file).map_err(|e| {
227238
format!("{}: Error identifying file: {}", path.to_string_lossy(), e)

0 commit comments

Comments
 (0)