Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit 1f8cd25

Browse files
committed
Split Entry into two different enums
1 parent cb002eb commit 1f8cd25

File tree

3 files changed

+136
-145
lines changed

3 files changed

+136
-145
lines changed

http/src/v0/root_files.rs

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use async_stream::try_stream;
66
use bytes::Bytes;
77
use cid::{Cid, Codec};
88
use futures::stream::TryStream;
9-
use ipfs::unixfs::ll::walk::{self, ContinuedWalk, Walker};
9+
use ipfs::unixfs::ll::walk::{self, ContinuedWalk, Entry, MetadataEntry, Walker};
1010
use ipfs::unixfs::{ll::file::FileReadFailed, TraversalFailed};
1111
use ipfs::Block;
1212
use ipfs::{Ipfs, IpfsTypes};
@@ -159,75 +159,63 @@ fn walk<Types: IpfsTypes>(
159159

160160
visit = match walker.continue_walk(&data, &mut cache)? {
161161
ContinuedWalk::File(segment, item) => {
162-
let total_size = item.as_entry()
163-
.total_file_size()
164-
.expect("files do have total_size");
165-
166-
if segment.is_first() {
167-
let path = item.as_entry().path();
168-
let metadata = item
169-
.as_entry()
170-
.metadata()
171-
.expect("files must have metadata");
172-
173-
for mut bytes in tar_helper.apply_file(path, metadata, total_size)?.iter_mut() {
174-
if let Some(bytes) = bytes.take() {
175-
yield bytes;
162+
if let Entry::Metadata(MetadataEntry::File(.., p, md, size)) = item.as_entry() {
163+
if segment.is_first() {
164+
for mut bytes in tar_helper.apply_file(p, md, size)?.iter_mut() {
165+
if let Some(bytes) = bytes.take() {
166+
yield bytes;
167+
}
176168
}
177169
}
178-
}
179170

180-
// even if the largest of files can have 256 kB blocks and about the same
181-
// amount of content, try to consume it in small parts not to grow the buffers
182-
// too much.
171+
// even if the largest of files can have 256 kB blocks and about the same
172+
// amount of content, try to consume it in small parts not to grow the buffers
173+
// too much.
183174

184-
let mut n = 0usize;
185-
let slice = segment.as_ref();
186-
let total = slice.len();
175+
let mut n = 0usize;
176+
let slice = segment.as_ref();
177+
let total = slice.len();
187178

188-
while n < total {
189-
let next = tar_helper.buffer_file_contents(&slice[n..]);
190-
n += next.len();
191-
yield next;
192-
}
179+
while n < total {
180+
let next = tar_helper.buffer_file_contents(&slice[n..]);
181+
n += next.len();
182+
yield next;
183+
}
193184

194-
if segment.is_last() {
195-
if let Some(zeroes) = tar_helper.pad(total_size) {
196-
yield zeroes;
185+
if segment.is_last() {
186+
if let Some(zeroes) = tar_helper.pad(size) {
187+
yield zeroes;
188+
}
197189
}
198190
}
199-
200191
item.into_inner()
201192
},
202193
ContinuedWalk::Directory(item) => {
203-
204-
// only first instances of directories will have the metadata
205-
if let Some(metadata) = item.as_entry().metadata() {
206-
let path = item.as_entry().path();
207-
194+
if let Entry::Metadata(metadata_entry) = item.as_entry() {
195+
let metadata = metadata_entry.metadata();
196+
let path = metadata_entry.path();
208197
for mut bytes in tar_helper.apply_directory(path, metadata)?.iter_mut() {
209198
if let Some(bytes) = bytes.take() {
210199
yield bytes;
211200
}
212201
}
213202
}
214-
215203
item.into_inner()
216204
},
217205
ContinuedWalk::Symlink(bytes, item) => {
218-
219-
// converting a symlink is the most tricky part
220-
let path = item.as_entry().path();
221-
let target = std::str::from_utf8(bytes).map_err(|_| GetError::NonUtf8Symlink)?;
222-
let target = Path::new(target);
223-
let metadata = item.as_entry().metadata().expect("symlink must have metadata");
224-
225-
for mut bytes in tar_helper.apply_symlink(path, target, metadata)?.iter_mut() {
226-
if let Some(bytes) = bytes.take() {
227-
yield bytes;
206+
if let Entry::Metadata(metadata_entry) = item.as_entry() {
207+
// converting a symlink is the most tricky part
208+
let path = metadata_entry.path();
209+
let target = std::str::from_utf8(bytes).map_err(|_| GetError::NonUtf8Symlink)?;
210+
let target = Path::new(target);
211+
let metadata = metadata_entry.metadata();
212+
213+
for mut bytes in tar_helper.apply_symlink(path, target, metadata)?.iter_mut() {
214+
if let Some(bytes) = bytes.take() {
215+
yield bytes;
216+
}
228217
}
229218
}
230-
231219
item.into_inner()
232220
},
233221
};

unixfs/examples/get.rs

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn main() {
5252
}
5353

5454
fn walk(blocks: ShardedBlockStore, start: &Cid) -> Result<(), Error> {
55-
use ipfs_unixfs::walk::{ContinuedWalk, Walker};
55+
use ipfs_unixfs::walk::{ContinuedWalk, Entry, MetadataEntry, Walker};
5656

5757
let mut buf = Vec::new();
5858
let mut cache = None;
@@ -72,56 +72,45 @@ fn walk(blocks: ShardedBlockStore, start: &Cid) -> Result<(), Error> {
7272
// items.
7373
visit = match walker.continue_walk(&buf, &mut cache)? {
7474
ContinuedWalk::File(segment, item) => {
75-
let entry = item.as_entry();
76-
let total_size = entry.total_file_size().expect("all files have total size");
77-
// metadata is picked up from the root file and carried until the last block
78-
let metadata = entry.metadata().expect("all files have metadata");
79-
80-
if segment.is_first() {
81-
// this is set on the root block, no actual bytes are present for multiblock
82-
// files
75+
if let Entry::Metadata(MetadataEntry::File(.., path, md, size)) = item.as_entry() {
76+
if segment.is_first() {
77+
// this is set on the root block, no actual bytes are present for multiblock
78+
// files
79+
}
80+
81+
if segment.is_last() {
82+
let mode = md.mode().unwrap_or(0o0644) & 0o7777;
83+
let (seconds, _) = md.mtime().unwrap_or((0, 0));
84+
println!("f {:o} {:>12} {:>16} {:?}", mode, seconds, size, path);
85+
}
8386
}
84-
85-
if segment.is_last() {
86-
let path = entry.path();
87-
let mode = metadata.mode().unwrap_or(0o0644) & 0o7777;
88-
let (seconds, _) = metadata.mtime().unwrap_or((0, 0));
89-
90-
println!("f {:o} {:>12} {:>16} {:?}", mode, seconds, total_size, path);
91-
}
92-
93-
// continue the walk
9487
item.into_inner()
9588
}
9689
ContinuedWalk::Directory(item) => {
9790
// presense of metadata can be used to determine if this is the first apperiance of
9891
// a directory by looking at the metadata: sibling hamt shard buckets do not have
9992
// metadata.
100-
if let Some(metadata) = item.as_entry().metadata() {
93+
if let Entry::Metadata(metadata_entry) = item.as_entry() {
94+
let metadata = metadata_entry.metadata();
10195
let path = item.as_entry().path();
102-
10396
let mode = metadata.mode().unwrap_or(0o0755) & 0o7777;
10497
let (seconds, _) = metadata.mtime().unwrap_or((0, 0));
105-
10698
println!("d {:o} {:>12} {:>16} {:?}", mode, seconds, "-", path);
10799
}
108-
109100
item.into_inner()
110101
}
111102
ContinuedWalk::Symlink(bytes, item) => {
112-
let entry = item.as_entry();
113-
let metadata = entry.metadata().expect("symlink must have metadata");
114-
115-
let path = entry.path();
116-
let target = Path::new(std::str::from_utf8(bytes).unwrap());
117-
let mode = metadata.mode().unwrap_or(0o0755) & 0o7777;
118-
let (seconds, _) = metadata.mtime().unwrap_or((0, 0));
119-
120-
println!(
121-
"s {:o} {:>12} {:>16} {:?} -> {:?}",
122-
mode, seconds, "-", path, target
123-
);
124-
103+
if let Entry::Metadata(metadata_entry) = item.as_entry() {
104+
let metadata = metadata_entry.metadata();
105+
let path = metadata_entry.path();
106+
let target = Path::new(std::str::from_utf8(bytes).unwrap());
107+
let mode = metadata.mode().unwrap_or(0o0755) & 0o7777;
108+
let (seconds, _) = metadata.mtime().unwrap_or((0, 0));
109+
println!(
110+
"s {:o} {:>12} {:>16} {:?} -> {:?}",
111+
mode, seconds, "-", path, target
112+
);
113+
}
125114
item.into_inner()
126115
}
127116
};

0 commit comments

Comments
 (0)