|
| 1 | +#[cfg(feature = "memmap")] |
| 2 | +use std::borrow::Cow; |
| 3 | +use std::{fs, io}; |
| 4 | + |
| 5 | +use pna::{NormalEntry, ReadEntry}; |
| 6 | + |
| 7 | +use super::TransformStrategy; |
| 8 | + |
| 9 | +pub(crate) struct SplitArchiveReader { |
| 10 | + #[cfg(feature = "memmap")] |
| 11 | + mmaps: Vec<crate::utils::mmap::Mmap>, |
| 12 | + #[cfg(not(feature = "memmap"))] |
| 13 | + files: Vec<fs::File>, |
| 14 | +} |
| 15 | + |
| 16 | +impl SplitArchiveReader { |
| 17 | + pub(crate) fn new(files: Vec<fs::File>) -> io::Result<Self> { |
| 18 | + #[cfg(feature = "memmap")] |
| 19 | + { |
| 20 | + let mmaps = files |
| 21 | + .into_iter() |
| 22 | + .map(crate::utils::mmap::Mmap::try_from) |
| 23 | + .collect::<io::Result<Vec<_>>>()?; |
| 24 | + Ok(Self { mmaps }) |
| 25 | + } |
| 26 | + #[cfg(not(feature = "memmap"))] |
| 27 | + { |
| 28 | + Ok(Self { files }) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + #[cfg(not(feature = "memmap"))] |
| 33 | + pub(crate) fn transform_entries<W, F, S>( |
| 34 | + &mut self, |
| 35 | + writer: W, |
| 36 | + password: Option<&[u8]>, |
| 37 | + processor: F, |
| 38 | + strategy: S, |
| 39 | + ) -> anyhow::Result<()> |
| 40 | + where |
| 41 | + W: io::Write, |
| 42 | + F: FnMut(io::Result<NormalEntry>) -> io::Result<Option<NormalEntry>>, |
| 43 | + S: TransformStrategy, |
| 44 | + { |
| 45 | + super::run_transform_entry( |
| 46 | + writer, |
| 47 | + self.files.drain(..), |
| 48 | + || password, |
| 49 | + processor, |
| 50 | + strategy, |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + #[cfg(feature = "memmap")] |
| 55 | + pub(crate) fn transform_entries<'s, W, F, S>( |
| 56 | + &'s mut self, |
| 57 | + writer: W, |
| 58 | + password: Option<&[u8]>, |
| 59 | + processor: F, |
| 60 | + strategy: S, |
| 61 | + ) -> anyhow::Result<()> |
| 62 | + where |
| 63 | + W: io::Write, |
| 64 | + F: FnMut( |
| 65 | + io::Result<NormalEntry<Cow<'s, [u8]>>>, |
| 66 | + ) -> io::Result<Option<NormalEntry<Cow<'s, [u8]>>>>, |
| 67 | + S: TransformStrategy, |
| 68 | + { |
| 69 | + super::run_transform_entry( |
| 70 | + writer, |
| 71 | + self.mmaps.iter().map(|m| m.as_ref()), |
| 72 | + || password, |
| 73 | + processor, |
| 74 | + strategy, |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + #[cfg(not(feature = "memmap"))] |
| 79 | + pub(crate) fn for_each_entry( |
| 80 | + &mut self, |
| 81 | + password: Option<&[u8]>, |
| 82 | + processor: impl FnMut(io::Result<NormalEntry>) -> io::Result<()>, |
| 83 | + ) -> io::Result<()> { |
| 84 | + super::run_process_archive(self.files.drain(..), || password, processor) |
| 85 | + } |
| 86 | + |
| 87 | + #[cfg(feature = "memmap")] |
| 88 | + pub(crate) fn for_each_entry<'s>( |
| 89 | + &'s mut self, |
| 90 | + password: Option<&[u8]>, |
| 91 | + mut processor: impl FnMut(io::Result<NormalEntry<Cow<'s, [u8]>>>) -> io::Result<()>, |
| 92 | + ) -> io::Result<()> { |
| 93 | + super::run_read_entries_mem( |
| 94 | + self.mmaps.iter().map(|m| m.as_ref()), |
| 95 | + |entry| match entry? { |
| 96 | + ReadEntry::Solid(s) => s |
| 97 | + .entries(password)? |
| 98 | + .try_for_each(|r| processor(r.map(Into::into))), |
| 99 | + ReadEntry::Normal(n) => processor(Ok(n)), |
| 100 | + }, |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + #[cfg(not(feature = "memmap"))] |
| 105 | + pub(crate) fn for_each_read_entry( |
| 106 | + &mut self, |
| 107 | + processor: impl FnMut(io::Result<ReadEntry>) -> io::Result<()>, |
| 108 | + ) -> io::Result<()> { |
| 109 | + super::run_read_entries(self.files.drain(..), processor) |
| 110 | + } |
| 111 | + |
| 112 | + #[cfg(feature = "memmap")] |
| 113 | + pub(crate) fn for_each_read_entry<'s>( |
| 114 | + &'s mut self, |
| 115 | + processor: impl FnMut(io::Result<ReadEntry<Cow<'s, [u8]>>>) -> io::Result<()>, |
| 116 | + ) -> io::Result<()> { |
| 117 | + super::run_read_entries_mem(self.mmaps.iter().map(|m| m.as_ref()), processor) |
| 118 | + } |
| 119 | +} |
0 commit comments