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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::io::{self, Read, Write};
pub struct LimitReader<R: Read> {
source: R,
length: u64,
remaining: u64,
conflict: String,
}
impl<R: Read> LimitReader<R> {
pub fn new(source: R, length: u64, conflict: String) -> Self {
Self {
source,
length,
remaining: length,
conflict,
}
}
}
impl<R: Read> Read for LimitReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
let allowed = self.remaining.min(buf.len() as u64);
if allowed == 0 {
return match self.source.read(&mut buf[..1]) {
Ok(0) => Ok(0),
Ok(_) => Err(io::Error::new(
io::ErrorKind::Other,
format!("collision with {} at offset {}", self.conflict, self.length),
)),
Err(e) => Err(e),
};
}
let count = self.source.read(&mut buf[..allowed as usize])?;
self.remaining = self
.remaining
.checked_sub(count as u64)
.expect("read more bytes than allowed");
Ok(count)
}
}
pub struct LimitWriter<W: Write> {
sink: W,
length: u64,
remaining: u64,
conflict: String,
}
impl<W: Write> LimitWriter<W> {
pub fn new(sink: W, length: u64, conflict: String) -> Self {
Self {
sink,
length,
remaining: length,
conflict,
}
}
}
impl<W: Write> Write for LimitWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
let allowed = self.remaining.min(buf.len() as u64);
if allowed == 0 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("collision with {} at offset {}", self.conflict, self.length),
));
}
let count = self.sink.write(&buf[..allowed as usize])?;
self.remaining = self
.remaining
.checked_sub(count as u64)
.expect("wrote more bytes than allowed");
Ok(count)
}
fn flush(&mut self) -> io::Result<()> {
self.sink.flush()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn limit_reader_test() {
let data: Vec<u8> = (0..100).collect();
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 150, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
assert_eq!(lim.read(&mut buf).unwrap(), 40);
assert_eq!(buf[..40], data[60..100]);
assert_eq!(lim.read(&mut buf).unwrap(), 0);
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 100, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
assert_eq!(lim.read(&mut buf).unwrap(), 40);
assert_eq!(buf[..40], data[60..100]);
assert_eq!(lim.read(&mut buf).unwrap(), 0);
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 90, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
assert_eq!(lim.read(&mut buf).unwrap(), 30);
assert_eq!(buf[..30], data[60..90]);
assert_eq!(
lim.read(&mut buf).unwrap_err().to_string(),
"collision with foo at offset 90"
);
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 60, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
assert_eq!(
lim.read(&mut buf).unwrap_err().to_string(),
"collision with foo at offset 60"
);
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 50, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 50);
assert_eq!(buf[..50], data[0..50]);
assert_eq!(
lim.read(&mut buf).unwrap_err().to_string(),
"collision with foo at offset 50"
);
}
#[test]
fn limit_writer_test() {
let data: Vec<u8> = (0..100).collect();
let mut outbuf: Vec<u8> = Vec::new();
let mut lim = LimitWriter::new(&mut outbuf, 150, "foo".into());
lim.write_all(&data).unwrap();
lim.flush().unwrap();
assert_eq!(data, outbuf);
let mut outbuf: Vec<u8> = Vec::new();
let mut lim = LimitWriter::new(&mut outbuf, 100, "foo".into());
lim.write_all(&data).unwrap();
lim.flush().unwrap();
assert_eq!(data, outbuf);
let mut outbuf: Vec<u8> = Vec::new();
let mut lim = LimitWriter::new(&mut outbuf, 90, "foo".into());
assert_eq!(
lim.write_all(&data).unwrap_err().to_string(),
"collision with foo at offset 90"
);
let mut outbuf: Vec<u8> = Vec::new();
let mut lim = LimitWriter::new(&mut outbuf, 90, "foo".into());
assert_eq!(lim.write(&data[0..60]).unwrap(), 60);
assert_eq!(lim.write(&data[60..100]).unwrap(), 30);
assert_eq!(
lim.write(&data[90..100]).unwrap_err().to_string(),
"collision with foo at offset 90"
);
assert_eq!(lim.write(&data[0..0]).unwrap(), 0);
assert_eq!(&data[0..90], &outbuf);
}
}