pub struct Link { /* private fields */ }
Implementations§
source§impl Link
impl Link
pub fn new(hash: Cid, name: String, tsize: u64) -> Self
sourcepub fn hash(&self) -> Cid
pub fn hash(&self) -> Cid
Examples found in repository?
examples/cat_cid.rs (line 28)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
// let cid = std::env::args().nth(1).expect("use cid as argument");
let cid = "bafkreiaqv66m5nd6mwgkk7h5lwqnjzj54s4f7knmnrjhb7ylzqfg2vdo54";
let file = std::path::Path::new("test");
let file = file.join("carv1-basic.car");
let file = std::fs::File::open(file).unwrap();
let mut reader = reader::new_v1(file).unwrap();
let roots = reader.header().roots();
let file_cid = Cid::try_from(cid).expect("cid format error");
for r in roots.iter() {
let root_ipld = reader.ipld(r).unwrap();
let root: Result<UnixFs, CarError> = root_ipld.try_into();
let root_dir = root.unwrap();
let count = root_dir
.links()
.iter()
.filter(|u| u.hash() == file_cid)
.count();
if count > 0 {
cat_ipld(&mut reader, file_cid).unwrap();
}
}
}
More examples
examples/ls.rs (line 29)
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
fn walk(vecq: &mut VecDeque<Cid>, reader: &mut impl CarReader) {
let mut cache: HashMap<Cid, String> = HashMap::new();
let raw_code: u64 = RawCodec.into();
while let Some(file_cid) = vecq.pop_front() {
let codec = file_cid.codec();
let file_n = cache.get(&file_cid).map_or(file_cid.to_string(), |c| c.to_string());
println!("{file_n}");
if codec == raw_code {
continue;
}
let file_ipld: Ipld = reader.ipld(&file_cid).unwrap();
match file_ipld {
m @ Ipld::Map(_) => {
let unixfs: UnixFs = m.try_into().unwrap();
match unixfs.file_type() {
FileType::Directory => {},
_=> continue,
}
for n in unixfs.links().into_iter() {
let cid = n.hash();
cache.insert(cid, file_n.clone() + "/" + n.name_ref());
vecq.push_back(cid);
}
}
_ => {}
}
}
}
sourcepub fn name_ref(&self) -> &str
pub fn name_ref(&self) -> &str
Examples found in repository?
examples/ls.rs (line 30)
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
fn walk(vecq: &mut VecDeque<Cid>, reader: &mut impl CarReader) {
let mut cache: HashMap<Cid, String> = HashMap::new();
let raw_code: u64 = RawCodec.into();
while let Some(file_cid) = vecq.pop_front() {
let codec = file_cid.codec();
let file_n = cache.get(&file_cid).map_or(file_cid.to_string(), |c| c.to_string());
println!("{file_n}");
if codec == raw_code {
continue;
}
let file_ipld: Ipld = reader.ipld(&file_cid).unwrap();
match file_ipld {
m @ Ipld::Map(_) => {
let unixfs: UnixFs = m.try_into().unwrap();
match unixfs.file_type() {
FileType::Directory => {},
_=> continue,
}
for n in unixfs.links().into_iter() {
let cid = n.hash();
cache.insert(cid, file_n.clone() + "/" + n.name_ref());
vecq.push_back(cid);
}
}
_ => {}
}
}
}