forked from extern/nushell
Limit open streaming to non-files, and files > 32mb (#2570)
This commit is contained in:
parent
6eb4a0e87b
commit
dd27aaef1b
@ -682,6 +682,40 @@ impl Shell for FilesystemShell {
|
||||
name: Span,
|
||||
with_encoding: Option<&'static Encoding>,
|
||||
) -> Result<BoxStream<'static, Result<StringOrBinary, ShellError>>, ShellError> {
|
||||
let metadata = std::fs::metadata(&path);
|
||||
|
||||
let read_full = if let Ok(metadata) = metadata {
|
||||
// Arbitrarily capping the file at 32 megs, so we don't try to read large files in all at once
|
||||
metadata.is_file() && metadata.len() < (1024 * 1024 * 32)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
if read_full {
|
||||
use futures_codec::Decoder;
|
||||
|
||||
// We should, in theory, be able to read in the whole file as one chunk
|
||||
let buffer = std::fs::read(&path).map_err(|e| {
|
||||
ShellError::labeled_error(
|
||||
format!("Error opening file: {:?}", e),
|
||||
"Error opening file",
|
||||
name,
|
||||
)
|
||||
})?;
|
||||
|
||||
let mut bytes_mut = bytes::BytesMut::from(&buffer[..]);
|
||||
|
||||
let mut codec = MaybeTextCodec::new(with_encoding);
|
||||
|
||||
match codec.decode(&mut bytes_mut).map_err(|e| {
|
||||
ShellError::unexpected(format!("AsyncRead failed in open function: {:?}", e))
|
||||
})? {
|
||||
Some(sb) => Ok(futures::stream::iter(vec![Ok(sb)].into_iter()).boxed()),
|
||||
None => Ok(futures::stream::iter(vec![].into_iter()).boxed()),
|
||||
}
|
||||
} else {
|
||||
// We don't know that this is a finite file, so treat it as a stream
|
||||
|
||||
let f = std::fs::File::open(&path).map_err(|e| {
|
||||
ShellError::labeled_error(
|
||||
format!("Error opening file: {:?}", e),
|
||||
@ -698,6 +732,7 @@ impl Shell for FilesystemShell {
|
||||
|
||||
Ok(sob_stream.boxed())
|
||||
}
|
||||
}
|
||||
|
||||
fn save(
|
||||
&mut self,
|
||||
|
Loading…
Reference in New Issue
Block a user