mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-08 20:45:29 +00:00
Use a new thread for listing
This commit is contained in:
parent
b1667bd474
commit
569f39c6c9
@ -4,6 +4,8 @@ use std::{
|
|||||||
env,
|
env,
|
||||||
io::prelude::*,
|
io::prelude::*,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
sync::mpsc::{self, Receiver},
|
||||||
|
thread,
|
||||||
};
|
};
|
||||||
|
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
@ -48,21 +50,31 @@ pub fn unpack_archive(
|
|||||||
|
|
||||||
/// List contents of `archive`, returning a vector of archive entries
|
/// List contents of `archive`, returning a vector of archive entries
|
||||||
pub fn list_archive(
|
pub fn list_archive(
|
||||||
archive: tar::Archive<impl Read + 'static>,
|
mut archive: tar::Archive<impl Read + Send + 'static>,
|
||||||
) -> crate::Result<impl Iterator<Item = crate::Result<FileInArchive>>> {
|
) -> impl Iterator<Item = crate::Result<FileInArchive>> {
|
||||||
// NOTE: tar::Archive::entries takes a &mut self
|
struct Files(Receiver<crate::Result<FileInArchive>>);
|
||||||
// This makes returning an iterator impossible
|
impl Iterator for Files {
|
||||||
// Current workaround is just to leak the archive
|
type Item = crate::Result<FileInArchive>;
|
||||||
let archive = Box::leak(Box::new(archive));
|
|
||||||
|
|
||||||
Ok(archive.entries()?.map(|file| {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let file = file?;
|
self.0.recv().ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let path = file.path()?.into_owned();
|
let (tx, rx) = mpsc::channel();
|
||||||
let is_dir = file.header().entry_type().is_dir();
|
thread::spawn(move || {
|
||||||
|
for file in archive.entries().expect("entries is only used once") {
|
||||||
|
let file_in_archive = (|| {
|
||||||
|
let file = file?;
|
||||||
|
let path = file.path()?.into_owned();
|
||||||
|
let is_dir = file.header().entry_type().is_dir();
|
||||||
|
Ok(FileInArchive { path, is_dir })
|
||||||
|
})();
|
||||||
|
tx.send(file_in_archive).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Ok(FileInArchive { path, is_dir })
|
Files(rx)
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
|
/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
|
||||||
|
@ -4,6 +4,8 @@ use std::{
|
|||||||
env,
|
env,
|
||||||
io::{self, prelude::*},
|
io::{self, prelude::*},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
sync::mpsc,
|
||||||
|
thread,
|
||||||
};
|
};
|
||||||
|
|
||||||
use fs_err as fs;
|
use fs_err as fs;
|
||||||
@ -82,22 +84,41 @@ where
|
|||||||
/// List contents of `archive`, returning a vector of archive entries
|
/// List contents of `archive`, returning a vector of archive entries
|
||||||
pub fn list_archive<R>(mut archive: ZipArchive<R>) -> impl Iterator<Item = crate::Result<FileInArchive>>
|
pub fn list_archive<R>(mut archive: ZipArchive<R>) -> impl Iterator<Item = crate::Result<FileInArchive>>
|
||||||
where
|
where
|
||||||
R: Read + Seek + 'static,
|
R: Read + Seek + Send + 'static,
|
||||||
{
|
{
|
||||||
(0..archive.len()).filter_map(move |idx| {
|
struct Files(mpsc::Receiver<crate::Result<FileInArchive>>);
|
||||||
let file = match archive.by_index(idx) {
|
impl Iterator for Files {
|
||||||
Ok(f) => f,
|
type Item = crate::Result<FileInArchive>;
|
||||||
Err(e) => return Some(Err(e.into())),
|
|
||||||
};
|
|
||||||
|
|
||||||
let path = match file.enclosed_name() {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
Some(path) => path.to_owned(),
|
self.0.recv().ok()
|
||||||
None => return None,
|
}
|
||||||
};
|
}
|
||||||
let is_dir = file.is_dir();
|
|
||||||
|
|
||||||
Some(Ok(FileInArchive { path, is_dir }))
|
let (tx, rx) = mpsc::channel();
|
||||||
})
|
thread::spawn(move || {
|
||||||
|
for idx in 0..archive.len() {
|
||||||
|
let maybe_file_in_archive = (|| {
|
||||||
|
let file = match archive.by_index(idx) {
|
||||||
|
Ok(f) => f,
|
||||||
|
Err(e) => return Some(Err(e.into())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let path = match file.enclosed_name() {
|
||||||
|
Some(path) => path.to_owned(),
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
let is_dir = file.is_dir();
|
||||||
|
|
||||||
|
Some(Ok(FileInArchive { path, is_dir }))
|
||||||
|
})();
|
||||||
|
if let Some(file_in_archive) = maybe_file_in_archive {
|
||||||
|
tx.send(file_in_archive).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Files(rx)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
|
/// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
|
||||||
|
@ -309,7 +309,6 @@ fn compress_files(
|
|||||||
Bzip => Box::new(bzip2::write::BzEncoder::new(encoder, Default::default())),
|
Bzip => Box::new(bzip2::write::BzEncoder::new(encoder, Default::default())),
|
||||||
Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(encoder, Default::default())?),
|
Lz4 => Box::new(lzzzz::lz4f::WriteCompressor::new(encoder, Default::default())?),
|
||||||
Lzma => Box::new(xz2::write::XzEncoder::new(encoder, 6)),
|
Lzma => Box::new(xz2::write::XzEncoder::new(encoder, 6)),
|
||||||
Snappy => Box::new(snap::write::FrameEncoder::new(encoder)),
|
|
||||||
Zstd => {
|
Zstd => {
|
||||||
let zstd_encoder = zstd::stream::write::Encoder::new(encoder, Default::default());
|
let zstd_encoder = zstd::stream::write::Encoder::new(encoder, Default::default());
|
||||||
// Safety:
|
// Safety:
|
||||||
@ -463,7 +462,6 @@ fn decompress_file(
|
|||||||
Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)),
|
Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)),
|
||||||
Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?),
|
Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?),
|
||||||
Lzma => Box::new(xz2::read::XzDecoder::new(decoder)),
|
Lzma => Box::new(xz2::read::XzDecoder::new(decoder)),
|
||||||
Snappy => Box::new(snap::read::FrameDecoder::new(decoder)),
|
|
||||||
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
|
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
|
||||||
Tar | Zip => unreachable!(),
|
Tar | Zip => unreachable!(),
|
||||||
};
|
};
|
||||||
@ -567,7 +565,6 @@ fn list_archive_contents(
|
|||||||
archive_path: &Path,
|
archive_path: &Path,
|
||||||
formats: Vec<CompressionFormat>,
|
formats: Vec<CompressionFormat>,
|
||||||
list_options: ListOptions,
|
list_options: ListOptions,
|
||||||
question_policy: QuestionPolicy,
|
|
||||||
) -> crate::Result<()> {
|
) -> crate::Result<()> {
|
||||||
let reader = fs::File::open(&archive_path)?;
|
let reader = fs::File::open(&archive_path)?;
|
||||||
|
|
||||||
@ -588,18 +585,21 @@ fn list_archive_contents(
|
|||||||
|
|
||||||
// Will be used in decoder chaining
|
// Will be used in decoder chaining
|
||||||
let reader = BufReader::with_capacity(BUFFER_CAPACITY, reader);
|
let reader = BufReader::with_capacity(BUFFER_CAPACITY, reader);
|
||||||
let mut reader: Box<dyn Read> = Box::new(reader);
|
let mut reader: Box<dyn Read + Send> = Box::new(reader);
|
||||||
|
|
||||||
// Grab previous decoder and wrap it inside of a new one
|
// Grab previous decoder and wrap it inside of a new one
|
||||||
let chain_reader_decoder = |format: &CompressionFormat, decoder: Box<dyn Read>| -> crate::Result<Box<dyn Read>> {
|
let chain_reader_decoder =
|
||||||
let decoder: Box<dyn Read> = match format {
|
|format: &CompressionFormat, decoder: Box<dyn Read + Send>| -> crate::Result<Box<dyn Read + Send>> {
|
||||||
Gzip => Box::new(flate2::read::GzDecoder::new(decoder)),
|
let decoder: Box<dyn Read + Send> = match format {
|
||||||
Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)),
|
Gzip => Box::new(flate2::read::GzDecoder::new(decoder)),
|
||||||
Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?),
|
Bzip => Box::new(bzip2::read::BzDecoder::new(decoder)),
|
||||||
Lzma => Box::new(xz2::read::XzDecoder::new(decoder)),
|
Lz4 => Box::new(lzzzz::lz4f::ReadDecompressor::new(decoder)?),
|
||||||
Snappy => Box::new(snap::read::FrameDecoder::new(decoder)),
|
Lzma => Box::new(xz2::read::XzDecoder::new(decoder)),
|
||||||
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
|
Snappy => Box::new(snap::read::FrameDecoder::new(decoder)),
|
||||||
Tar | Zip => unreachable!(),
|
Zstd => Box::new(zstd::stream::Decoder::new(decoder)?),
|
||||||
|
Tar | Zip => unreachable!(),
|
||||||
|
};
|
||||||
|
Ok(decoder)
|
||||||
};
|
};
|
||||||
Ok(decoder)
|
Ok(decoder)
|
||||||
};
|
};
|
||||||
@ -609,7 +609,7 @@ fn list_archive_contents(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let files: Box<dyn Iterator<Item = crate::Result<FileInArchive>>> = match formats[0] {
|
let files: Box<dyn Iterator<Item = crate::Result<FileInArchive>>> = match formats[0] {
|
||||||
Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))?),
|
Tar => Box::new(crate::archive::tar::list_archive(tar::Archive::new(reader))),
|
||||||
Zip => {
|
Zip => {
|
||||||
eprintln!("{orange}[WARNING]{reset}", orange = *colors::ORANGE, reset = *colors::RESET);
|
eprintln!("{orange}[WARNING]{reset}", orange = *colors::ORANGE, reset = *colors::RESET);
|
||||||
eprintln!(
|
eprintln!(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user