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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use crate::pb::ParsingFailed;
use crate::{InvalidCidInLink, Metadata, UnexpectedNodeType};
use alloc::borrow::Cow;
use core::fmt;
mod reader;
pub mod visit;
pub mod adder;
#[derive(Debug)]
pub enum FileReadFailed {
File(FileError),
UnexpectedType(UnexpectedNodeType),
Read(Option<quick_protobuf::Error>),
InvalidCid(InvalidCidInLink),
}
impl fmt::Display for FileReadFailed {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
use FileReadFailed::*;
match self {
File(e) => write!(fmt, "{}", e),
UnexpectedType(ut) => write!(fmt, "unexpected type for UnixFs: {:?}", ut),
Read(Some(e)) => write!(fmt, "reading failed: {}", e),
Read(None) => write!(fmt, "reading failed: missing UnixFS message"),
InvalidCid(e) => write!(fmt, "{}", e),
}
}
}
impl std::error::Error for FileReadFailed {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use FileReadFailed::*;
match self {
InvalidCid(e) => Some(e),
Read(Some(e)) => Some(e),
_ => None,
}
}
}
impl<'a> From<ParsingFailed<'a>> for FileReadFailed {
fn from(e: ParsingFailed<'a>) -> Self {
use ParsingFailed::*;
match e {
InvalidDagPb(e) => FileReadFailed::Read(Some(e)),
InvalidUnixFs(e, _) => FileReadFailed::Read(Some(e)),
NoData(_) => FileReadFailed::Read(None),
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum FileError {
LinksAndBlocksizesMismatch,
NoLinksNoContent,
NonRootDefinesMetadata(Metadata),
IntermediateNodeWithoutFileSize,
TreeExpandsOnLinks,
TreeOverlapsBetweenLinks,
EarlierLink,
TreeJumpsBetweenLinks,
UnexpectedRawOrFileProperties {
hash_type: Option<u64>,
fanout: Option<u64>,
},
}
impl fmt::Display for FileError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
use FileError::*;
match self {
LinksAndBlocksizesMismatch => write!(
fmt,
"different number of links and blocksizes: cannot determine subtree ranges"
),
NoLinksNoContent => write!(
fmt,
"filesize is non-zero while there are no links or content"
),
NonRootDefinesMetadata(metadata) => {
write!(fmt, "unsupported: non-root defines {:?}", metadata)
}
IntermediateNodeWithoutFileSize => {
write!(fmt, "intermediatery node with links but no filesize")
}
TreeExpandsOnLinks => write!(
fmt,
"total size of tree expands through links, it should only get smaller or keep size"
),
TreeOverlapsBetweenLinks => write!(fmt, "unsupported: tree contains overlap"),
EarlierLink => write!(fmt, "error: earlier link given"),
TreeJumpsBetweenLinks => write!(fmt, "unsupported: tree contains holes"),
UnexpectedRawOrFileProperties { hash_type, fanout } => write!(
fmt,
"unsupported: File or Raw with hash_type {:?} or fanount {:?}",
hash_type, fanout
),
}
}
}
impl std::error::Error for FileError {}
impl From<FileError> for FileReadFailed {
fn from(e: FileError) -> Self {
Self::File(e)
}
}
pub(crate) trait UnwrapBorrowedExt<'a> {
fn unwrap_borrowed(self) -> &'a [u8];
fn unwrap_borrowed_or_empty(self) -> &'a [u8]
where
Self: 'a;
}
impl<'a> UnwrapBorrowedExt<'a> for Option<Cow<'a, [u8]>> {
fn unwrap_borrowed(self) -> &'a [u8] {
match self {
Some(Cow::Borrowed(x)) => x,
Some(Cow::Owned(_)) => panic!("unexpected Cow::Owned"),
None => panic!("Unexpected None"),
}
}
fn unwrap_borrowed_or_empty(self) -> &'a [u8] {
match self {
Some(Cow::Borrowed(x)) => x,
None => &[][..],
_ => panic!("should not be Cow::Owned"),
}
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::{reader::*, visit::*, UnwrapBorrowedExt};
use crate::test_support::FakeBlockstore;
use hex_literal::hex;
const CONTENT_FILE: &[u8] = &hex!("0a0d08021207636f6e74656e741807");
#[test]
fn just_content() {
let fr = FileReader::from_block(CONTENT_FILE).unwrap();
let (content, _) = fr.content();
assert!(
matches!(content, FileContent::Bytes(b"content")),
"{:?}",
content
);
}
#[test]
fn visiting_just_content() {
let res = IdleFileVisit::default().start(CONTENT_FILE);
assert!(matches!(res, Ok((b"content", _, _, None))), "{:?}", res);
}
#[test]
fn visiting_too_large_range_of_singleblock_file() {
let res = IdleFileVisit::default()
.with_target_range(500_000..600_000)
.start(CONTENT_FILE);
assert!(matches!(res, Ok((b"", _, _, None))), "{:?}", res);
}
#[test]
fn empty_file() {
let block = &hex!("0a0408021800");
let fr = FileReader::from_block(block).unwrap();
let (content, _) = fr.content();
assert!(matches!(content, FileContent::Bytes(b"")), "{:?}", content);
}
#[test]
fn balanced_traversal() {
let target = "QmRJHYTNvC3hmd9gJQARxLR1QMEincccBV53bBw524yyq6";
let blocks = FakeBlockstore::with_fixtures();
let (mut links_and_ranges, mut traversal) = {
let root = FileReader::from_block(blocks.get_by_str(target)).unwrap();
let (mut links_and_ranges, traversal) = match root.content() {
(FileContent::Links(iter), traversal) => {
let links_and_ranges = iter
.map(|(link, range)| (link.Hash.unwrap_borrowed().to_vec(), range))
.collect::<Vec<_>>();
(links_and_ranges, traversal)
}
x => unreachable!("unexpected {:?}", x),
};
links_and_ranges.reverse();
(links_and_ranges, traversal)
};
let mut combined: Vec<u8> = Vec::new();
while let Some((key, range)) = links_and_ranges.pop() {
let next = blocks.get_by_raw(&key);
let fr = traversal.continue_walk(next, &range).unwrap();
let (content, next) = fr.content();
combined.extend(content.unwrap_content());
traversal = next;
}
assert_eq!(combined, b"foobar\n");
}
fn collect_bytes(blocks: &FakeBlockstore, visit: IdleFileVisit, start: &str) -> Vec<u8> {
let mut ret = Vec::new();
let (content, _, _, mut step) = visit.start(blocks.get_by_str(start)).unwrap();
ret.extend(content);
while let Some(visit) = step {
let (first, _) = visit.pending_links();
let block = blocks.get_by_cid(first);
let (content, next_step) = visit.continue_walk(block, &mut None).unwrap();
ret.extend(content);
step = next_step;
}
ret
}
#[test]
fn visitor_traversal() {
let blocks = FakeBlockstore::with_fixtures();
let start = "QmRJHYTNvC3hmd9gJQARxLR1QMEincccBV53bBw524yyq6";
let bytes = collect_bytes(&blocks, IdleFileVisit::default(), start);
assert_eq!(&bytes[..], b"foobar\n");
}
#[test]
fn scoped_visitor_traversal_from_blockstore() {
let blocks = FakeBlockstore::with_fixtures();
let start = "QmRJHYTNvC3hmd9gJQARxLR1QMEincccBV53bBw524yyq6";
let visit = IdleFileVisit::default().with_target_range(1..6);
let bytes = collect_bytes(&blocks, visit, start);
assert_eq!(&bytes[..], b"oobar");
}
#[test]
fn less_than_block_scoped_traversal_from_blockstore() {
let blocks = FakeBlockstore::with_fixtures();
let start = "QmRJHYTNvC3hmd9gJQARxLR1QMEincccBV53bBw524yyq6";
let visit = IdleFileVisit::default().with_target_range(0..1);
let bytes = collect_bytes(&blocks, visit, start);
assert_eq!(&bytes[..], b"f");
}
#[test]
fn scoped_traversal_out_of_bounds_from_blockstore() {
let blocks = FakeBlockstore::with_fixtures();
let start = "QmRJHYTNvC3hmd9gJQARxLR1QMEincccBV53bBw524yyq6";
let visit = IdleFileVisit::default().with_target_range(7..20);
let bytes = collect_bytes(&blocks, visit, start);
assert_eq!(&bytes[..], b"");
}
#[test]
fn trickle_traversal() {
let blocks = FakeBlockstore::with_fixtures();
let start = "QmWfQ48ChJUj4vWKFsUDe4646xCBmXgdmNfhjz9T7crywd";
let bytes = collect_bytes(&blocks, IdleFileVisit::default(), start);
assert_eq!(&bytes[..], b"foobar\n");
}
}