Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 97b443c

Browse files
bors[bot]qthree
andcommitted
Merge #17
17: Keep reading after EOF in follow mode r=japaric a=qthree Possible solution to #14 At least it's working for me. Co-authored-by: qthree <qthree3@gmail.com>
2 parents 335f90f + 03288f3 commit 97b443c

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/bin/itmdump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn run() -> Result<()> {
103103
let follow = matches.is_present("follow");
104104

105105
let read = open_read(&matches)?;
106-
let mut decoder = Decoder::new(read);
106+
let mut decoder = Decoder::new(read, follow);
107107

108108
let stdout = io::stdout();
109109
let mut stdout = stdout.lock();

src/decoder.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,34 @@ use std::io::{self, Cursor, Read};
77
/// Parses ITM packets.
88
pub struct Decoder<R: Read> {
99
inner: R,
10+
follow: bool
11+
}
12+
13+
// Copy&Paste from std::io::Read::read_exact
14+
fn read_exact_gently<R: Read>(reader: &mut R, mut buf: &mut [u8], keep_reading: bool) -> ::std::io::Result<()> {
15+
use std::io::{ErrorKind, Error};
16+
while !buf.is_empty() {
17+
match reader.read(buf) {
18+
Ok(0) if !keep_reading => break,
19+
Ok(0) if keep_reading => continue,
20+
Ok(n) => { let tmp = buf; buf = &mut tmp[n..]; }
21+
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
22+
Err(e) => return Err(e),
23+
}
24+
}
25+
if !buf.is_empty() {
26+
Err(Error::new(ErrorKind::UnexpectedEof,
27+
"failed to fill whole buffer"))
28+
} else {
29+
Ok(())
30+
}
1031
}
1132

1233
impl<R: Read> Decoder<R> {
1334
/// Construct a new `Decoder` that reads encoded packets from
1435
/// `inner`.
15-
pub fn new(inner: R) -> Decoder<R> {
16-
Decoder::<R> { inner: inner }
36+
pub fn new(inner: R, follow: bool) -> Decoder<R> {
37+
Decoder::<R> { inner: inner, follow: follow}
1738
}
1839

1940
// TODO: If we need config for the Decoder, my plan is to:
@@ -53,7 +74,7 @@ impl<R: Read> Decoder<R> {
5374
{
5475
// Scope the mutable borrow on buf to satisfy borrow checker.
5576
let buf = &mut ud.payload[0..payload_len];
56-
match self.inner.read_exact(buf) {
77+
match read_exact_gently(&mut self.inner, buf, self.follow) {
5778
Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => {
5879
return Err(Error::from(ErrorKind::EofDuringPacket))
5980
}
@@ -76,7 +97,7 @@ impl<R: Read> Decoder<R> {
7697

7798
/// Decode a single packet from a slice of bytes.
7899
pub fn from_slice(s: &[u8]) -> Result<Packet> {
79-
let mut d = Decoder::new(Cursor::new(Vec::from(s)));
100+
let mut d = Decoder::new(Cursor::new(Vec::from(s)), false);
80101
d.read_packet()
81102
}
82103

0 commit comments

Comments
 (0)