forked from bearcove/rc-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_reader.rs
76 lines (70 loc) · 2.04 KB
/
entry_reader.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use rc_zip::{
fsm::{EntryFsm, FsmResult},
parse::Entry,
};
use std::io;
use tracing::trace;
/// Reader for an entry inside an archive
pub struct EntryReader<R>
where
R: io::Read,
{
rd: R,
fsm: Option<EntryFsm>,
}
impl<R> EntryReader<R>
where
R: io::Read,
{
pub(crate) fn new(entry: &Entry, rd: R) -> Self {
Self {
rd,
fsm: Some(EntryFsm::new(Some(entry.clone()), None)),
}
}
}
impl<R> io::Read for EntryReader<R>
where
R: io::Read,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
loop {
let mut fsm = match self.fsm.take() {
Some(fsm) => fsm,
None => return Ok(0),
};
#[allow(clippy::needless_late_init)] // don't tell me what to do
let filled_bytes;
if fsm.wants_read() {
tracing::trace!(space_avail = fsm.space().len(), "fsm wants read");
let n = self.rd.read(fsm.space())?;
fsm.fill(n);
filled_bytes = n;
} else {
trace!("fsm does not want read");
filled_bytes = 0;
}
match fsm.process(buf)? {
FsmResult::Continue((fsm, outcome)) => {
self.fsm = Some(fsm);
if outcome.bytes_written > 0 {
tracing::trace!("wrote {} bytes", outcome.bytes_written);
return Ok(outcome.bytes_written);
} else if filled_bytes > 0 || outcome.bytes_read > 0 {
// progress was made, keep reading
continue;
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"entry reader: no progress",
));
}
}
FsmResult::Done(_) => {
// neat!
return Ok(0);
}
}
}
}
}