Files
bat/src/input.rs
T

432 lines
12 KiB
Rust
Raw Normal View History

2020-11-25 00:24:04 +01:00
use std::convert::TryFrom;
use std::fs;
2018-10-07 11:54:01 +02:00
use std::fs::File;
2020-04-21 21:14:44 +02:00
use std::io::{self, BufRead, BufReader, Read};
2021-03-02 11:15:49 +03:00
use std::path::{Path, PathBuf};
2018-10-07 11:54:01 +02:00
use clircle::{Clircle, Identifier};
2018-10-07 16:44:59 +02:00
use content_inspector::{self, ContentType};
2020-04-22 21:45:47 +02:00
use crate::error::*;
2018-10-07 11:54:01 +02:00
2020-05-15 16:18:10 -07:00
/// A description of an Input source.
2020-05-15 14:21:38 -07:00
/// This tells bat how to refer to the input.
2020-05-15 16:18:10 -07:00
#[derive(Clone)]
pub struct InputDescription {
pub(crate) name: String,
/// The input title.
/// This replaces the name if provided.
title: Option<String>,
/// The input kind.
2020-05-15 16:18:10 -07:00
kind: Option<String>,
/// A summary description of the input.
/// Defaults to "{kind} '{name}'"
2020-05-15 16:18:10 -07:00
summary: Option<String>,
}
2020-05-15 14:21:38 -07:00
2020-05-15 16:18:10 -07:00
impl InputDescription {
/// Creates a description for an input.
pub fn new(name: impl Into<String>) -> Self {
InputDescription {
name: name.into(),
title: None,
2020-05-15 16:18:10 -07:00
kind: None,
summary: None,
}
}
2020-05-15 14:21:38 -07:00
2020-05-27 16:26:28 -07:00
pub fn set_kind(&mut self, kind: Option<String>) {
self.kind = kind;
2020-05-15 16:18:10 -07:00
}
2020-05-27 16:26:28 -07:00
pub fn set_summary(&mut self, summary: Option<String>) {
self.summary = summary;
2020-05-15 16:18:10 -07:00
}
2020-05-27 16:26:28 -07:00
pub fn set_title(&mut self, title: Option<String>) {
self.title = title;
}
pub fn title(&self) -> &String {
match &self.title {
2021-08-18 20:37:38 +02:00
Some(title) => title,
None => &self.name,
}
2020-05-15 16:18:10 -07:00
}
pub fn kind(&self) -> Option<&String> {
self.kind.as_ref()
}
pub fn summary(&self) -> String {
self.summary.clone().unwrap_or_else(|| match &self.kind {
None => self.name.clone(),
Some(kind) => format!("{} '{}'", kind.to_lowercase(), self.name),
})
}
2020-04-21 22:24:47 +02:00
}
2020-04-22 18:30:06 +02:00
pub(crate) enum InputKind<'a> {
2021-03-02 11:15:49 +03:00
OrdinaryFile(PathBuf),
2020-04-22 16:27:34 +02:00
StdIn,
CustomReader(Box<dyn Read + 'a>),
2020-04-22 16:27:34 +02:00
}
2025-02-02 14:20:05 +01:00
impl InputKind<'_> {
2020-05-15 16:18:10 -07:00
pub fn description(&self) -> InputDescription {
match self {
InputKind::OrdinaryFile(ref path) => InputDescription::new(path.to_string_lossy()),
2020-05-15 16:18:10 -07:00
InputKind::StdIn => InputDescription::new("STDIN"),
InputKind::CustomReader(_) => InputDescription::new("READER"),
}
}
}
2020-04-22 16:27:34 +02:00
#[derive(Clone, Default)]
2020-04-22 18:30:06 +02:00
pub(crate) struct InputMetadata {
2021-03-02 11:15:49 +03:00
pub(crate) user_provided_name: Option<PathBuf>,
pub(crate) size: Option<u64>,
2020-04-22 16:27:34 +02:00
}
pub struct Input<'a> {
2020-04-22 18:30:06 +02:00
pub(crate) kind: InputKind<'a>,
pub(crate) metadata: InputMetadata,
pub(crate) description: InputDescription,
2020-04-22 16:27:34 +02:00
}
2020-04-22 18:30:06 +02:00
pub(crate) enum OpenedInputKind {
2021-03-02 11:15:49 +03:00
OrdinaryFile(PathBuf),
2020-04-22 16:27:34 +02:00
StdIn,
CustomReader,
}
2020-04-22 18:30:06 +02:00
pub(crate) struct OpenedInput<'a> {
pub(crate) kind: OpenedInputKind,
pub(crate) metadata: InputMetadata,
pub(crate) reader: InputReader<'a>,
2020-05-15 16:18:10 -07:00
pub(crate) description: InputDescription,
2020-04-21 22:24:47 +02:00
}
impl OpenedInput<'_> {
/// Get the path of the file:
/// If this was set by the metadata, that will take priority.
/// If it wasn't, it will use the real file path (if available).
pub(crate) fn path(&self) -> Option<&PathBuf> {
self.metadata
.user_provided_name
.as_ref()
.or(match self.kind {
OpenedInputKind::OrdinaryFile(ref path) => Some(path),
_ => None,
})
}
}
impl<'a> Input<'a> {
2021-03-02 11:15:49 +03:00
pub fn ordinary_file(path: impl AsRef<Path>) -> Self {
Self::_ordinary_file(path.as_ref())
}
2021-03-07 14:41:52 +01:00
2021-03-02 11:15:49 +03:00
fn _ordinary_file(path: &Path) -> Self {
let kind = InputKind::OrdinaryFile(path.to_path_buf());
let metadata = InputMetadata {
size: fs::metadata(path).map(|m| m.len()).ok(),
..InputMetadata::default()
};
2020-04-22 16:27:34 +02:00
Input {
description: kind.description(),
metadata,
kind,
2020-04-21 22:24:47 +02:00
}
}
2020-04-22 16:27:34 +02:00
pub fn stdin() -> Self {
let kind = InputKind::StdIn;
2020-04-22 16:27:34 +02:00
Input {
description: kind.description(),
2020-04-22 16:27:34 +02:00
metadata: InputMetadata::default(),
kind,
2020-04-22 16:27:34 +02:00
}
}
pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
let kind = InputKind::CustomReader(reader);
Input {
description: kind.description(),
metadata: InputMetadata::default(),
kind,
}
}
2020-04-22 16:27:34 +02:00
pub fn is_stdin(&self) -> bool {
matches!(self.kind, InputKind::StdIn)
2020-04-22 16:27:34 +02:00
}
2021-03-07 14:41:52 +01:00
pub fn with_name(self, provided_name: Option<impl AsRef<Path>>) -> Self {
2021-03-02 11:15:49 +03:00
self._with_name(provided_name.as_ref().map(|it| it.as_ref()))
}
2021-03-07 14:41:52 +01:00
fn _with_name(mut self, provided_name: Option<&Path>) -> Self {
2020-11-05 22:29:04 +01:00
if let Some(name) = provided_name {
self.description.name = name.to_string_lossy().to_string()
}
2020-04-22 16:27:34 +02:00
self.metadata.user_provided_name = provided_name.map(|n| n.to_owned());
2020-04-22 22:41:25 +02:00
self
2020-04-22 16:27:34 +02:00
}
pub fn description(&self) -> &InputDescription {
&self.description
}
pub fn description_mut(&mut self) -> &mut InputDescription {
&mut self.description
2020-05-15 16:18:10 -07:00
}
pub(crate) fn open<R: BufRead + 'a>(
self,
stdin: R,
stdout_identifier: Option<&Identifier>,
) -> Result<OpenedInput<'a>> {
2020-05-15 16:18:10 -07:00
let description = self.description().clone();
2020-04-22 16:27:34 +02:00
match self.kind {
InputKind::StdIn => {
if let Some(stdout) = stdout_identifier {
let input_identifier = Identifier::try_from(clircle::Stdio::Stdin)
2024-02-24 22:36:14 +01:00
.map_err(|e| format!("Stdin: Error identifying file: {e}"))?;
if stdout.surely_conflicts_with(&input_identifier) {
return Err("IO circle detected. The input from stdin is also an output. Aborting to avoid infinite loop.".into());
}
}
Ok(OpenedInput {
kind: OpenedInputKind::StdIn,
description,
metadata: self.metadata,
reader: InputReader::new(stdin),
})
}
2020-04-22 16:27:34 +02:00
InputKind::OrdinaryFile(path) => Ok(OpenedInput {
kind: OpenedInputKind::OrdinaryFile(path.clone()),
2020-05-15 16:18:10 -07:00
description,
2020-04-22 16:27:34 +02:00
metadata: self.metadata,
reader: {
let mut file = File::open(&path)
2025-02-27 00:30:05 -05:00
.map_err(|e| format!("'{}': {e}", path.to_string_lossy()))?;
2020-04-22 16:27:34 +02:00
if file.metadata()?.is_dir() {
return Err(format!("'{}' is a directory.", path.to_string_lossy()).into());
}
if let Some(stdout) = stdout_identifier {
let input_identifier = Identifier::try_from(file).map_err(|e| {
2025-02-27 00:30:05 -05:00
format!("{}: Error identifying file: {e}", path.to_string_lossy())
})?;
if stdout.surely_conflicts_with(&input_identifier) {
return Err(format!(
"IO circle detected. The input from '{}' is also an output. Aborting to avoid infinite loop.",
path.to_string_lossy()
)
.into());
}
2021-06-13 22:32:08 +03:00
file = input_identifier.into_inner().expect("The file was lost in the clircle::Identifier, this should not have happened...");
}
2020-04-22 16:27:34 +02:00
InputReader::new(BufReader::new(file))
},
}),
InputKind::CustomReader(reader) => Ok(OpenedInput {
2020-05-15 16:18:10 -07:00
description,
2020-04-22 16:27:34 +02:00
kind: OpenedInputKind::CustomReader,
metadata: self.metadata,
reader: InputReader::new(BufReader::new(reader)),
}),
}
}
}
2020-04-22 18:30:06 +02:00
pub(crate) struct InputReader<'a> {
inner: Box<dyn BufRead + 'a>,
2020-03-21 20:45:46 +01:00
pub(crate) first_line: Vec<u8>,
pub(crate) content_type: Option<ContentType>,
}
2020-04-21 21:19:06 +02:00
impl<'a> InputReader<'a> {
pub(crate) fn new<R: BufRead + 'a>(mut reader: R) -> InputReader<'a> {
2018-10-07 13:47:54 +02:00
let mut first_line = vec![];
reader.read_until(b'\n', &mut first_line).ok();
2018-10-07 13:47:54 +02:00
2019-05-15 21:53:22 +02:00
let content_type = if first_line.is_empty() {
None
} else {
Some(content_inspector::inspect(&first_line[..]))
};
2018-10-07 16:44:59 +02:00
2019-05-15 21:53:22 +02:00
if content_type == Some(ContentType::UTF_16LE) {
2025-08-16 15:33:53 +03:00
read_utf16_line(&mut reader, &mut first_line, 0x00, 0x0A).ok();
} else if content_type == Some(ContentType::UTF_16BE) {
read_utf16_line(&mut reader, &mut first_line, 0x0A, 0x00).ok();
2018-10-07 16:44:59 +02:00
}
2020-04-21 21:19:06 +02:00
InputReader {
inner: Box::new(reader),
2018-10-07 13:47:54 +02:00
first_line,
2018-10-07 16:44:59 +02:00
content_type,
}
}
2020-03-21 20:45:46 +01:00
pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
if !self.first_line.is_empty() {
2018-10-07 13:47:54 +02:00
buf.append(&mut self.first_line);
return Ok(true);
2018-10-07 13:25:49 +02:00
}
if self.content_type == Some(ContentType::UTF_16LE) {
2025-08-16 15:33:53 +03:00
return read_utf16_line(&mut self.inner, buf, 0x00, 0x0A);
}
if self.content_type == Some(ContentType::UTF_16BE) {
return read_utf16_line(&mut self.inner, buf, 0x0A, 0x00);
}
2025-08-16 15:33:53 +03:00
let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
Ok(res)
}
}
2025-08-16 15:35:13 +03:00
fn read_utf16_line<R: BufRead>(
reader: &mut R,
buf: &mut Vec<u8>,
read_until_char: u8,
preceded_by_char: u8,
) -> io::Result<bool> {
2025-08-07 23:31:15 +03:00
loop {
let mut temp = Vec::new();
2025-08-16 15:33:53 +03:00
let n = reader.read_until(read_until_char, &mut temp)?;
2025-08-07 23:31:15 +03:00
if n == 0 {
// EOF reached
break;
}
buf.extend_from_slice(&temp);
2025-08-16 15:35:13 +03:00
if buf.len() >= 2
&& buf[buf.len() - 2] == preceded_by_char
&& buf[buf.len() - 1] == read_until_char
{
2025-08-07 23:31:15 +03:00
// end of line found
break;
}
// end of line not found, keep going
}
2025-08-18 20:06:01 -04:00
Ok(!buf.is_empty())
2025-08-07 23:31:15 +03:00
}
2018-10-07 12:29:38 +02:00
#[test]
fn basic() {
2018-10-07 13:25:49 +02:00
let content = b"#!/bin/bash\necho hello";
2020-04-21 21:19:06 +02:00
let mut reader = InputReader::new(&content[..]);
2018-10-07 12:29:38 +02:00
2018-10-07 13:47:54 +02:00
assert_eq!(b"#!/bin/bash\n", &reader.first_line[..]);
2018-10-07 13:25:49 +02:00
2018-10-07 12:29:38 +02:00
let mut buffer = vec![];
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(res.unwrap());
2018-10-07 13:25:49 +02:00
assert_eq!(b"#!/bin/bash\n", &buffer[..]);
2018-10-07 12:29:38 +02:00
buffer.clear();
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(res.unwrap());
2018-10-07 13:25:49 +02:00
assert_eq!(b"echo hello", &buffer[..]);
2018-10-07 12:29:38 +02:00
buffer.clear();
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(!res.unwrap());
2018-10-07 12:29:38 +02:00
assert!(buffer.is_empty());
}
2018-10-07 16:44:59 +02:00
#[test]
fn utf16le() {
let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00";
2020-04-21 21:19:06 +02:00
let mut reader = InputReader::new(&content[..]);
2018-10-07 16:44:59 +02:00
assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &reader.first_line[..]);
let mut buffer = vec![];
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(res.unwrap());
2018-10-07 16:44:59 +02:00
assert_eq!(b"\xFF\xFE\x73\x00\x0A\x00", &buffer[..]);
buffer.clear();
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(res.unwrap());
2018-10-07 16:44:59 +02:00
assert_eq!(b"\x64\x00", &buffer[..]);
buffer.clear();
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(!res.unwrap());
2018-10-07 16:44:59 +02:00
assert!(buffer.is_empty());
}
2025-08-07 23:31:15 +03:00
#[test]
fn utf16le_issue3367() {
2025-08-16 15:33:30 +03:00
let content = b"\xFF\xFE\x0A\x4E\x00\x4E\x0A\x4F\x00\x52\x0A\x00\
\x6F\x00\x20\x00\x62\x00\x61\x00\x72\x00\x0A\x00\
\x68\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00\x20\x00\x77\x00\x6F\x00\x72\x00\x6C\x00\x64\x00";
2025-08-07 23:31:15 +03:00
let mut reader = InputReader::new(&content[..]);
assert_eq!(
2025-08-16 15:33:30 +03:00
b"\xFF\xFE\x0A\x4E\x00\x4E\x0A\x4F\x00\x52\x0A\x00",
2025-08-07 23:31:15 +03:00
&reader.first_line[..]
);
let mut buffer = vec![];
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(res.unwrap());
2025-08-16 15:35:13 +03:00
assert_eq!(
b"\xFF\xFE\x0A\x4E\x00\x4E\x0A\x4F\x00\x52\x0A\x00",
&buffer[..]
);
2025-08-07 23:31:15 +03:00
buffer.clear();
2025-08-16 15:33:30 +03:00
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(res.unwrap());
2025-08-16 15:35:13 +03:00
assert_eq!(
b"\x6F\x00\x20\x00\x62\x00\x61\x00\x72\x00\x0A\x00",
&buffer[..]
);
2025-08-16 15:33:30 +03:00
buffer.clear();
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(res.unwrap());
2025-08-16 15:35:13 +03:00
assert_eq!(
b"\x68\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00\x20\x00\x77\x00\x6F\x00\x72\x00\x6C\x00\x64\x00",
&buffer[..]
);
2025-08-16 15:33:30 +03:00
buffer.clear();
2025-08-16 15:33:53 +03:00
2025-08-07 23:31:15 +03:00
let res = reader.read_line(&mut buffer);
assert!(res.is_ok());
assert!(!res.unwrap());
assert!(buffer.is_empty());
}