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
#![warn(rust_2018_idioms)]
extern crate alloc;
use alloc::borrow::Cow;
use core::fmt;
pub mod file;
pub mod symlink;
pub mod dir;
pub use dir::{resolve, LookupError, MaybeResolved, ResolveError};
mod pb;
pub use pb::{FlatUnixFs, PBLink, PBNode, UnixFs, UnixFsType};
pub mod dagpb;
pub mod walk;
#[cfg(test)]
pub(crate) mod test_support;
#[derive(Debug)]
pub struct InvalidCidInLink {
pub nth: usize,
pub hash: Cow<'static, [u8]>,
pub name: Cow<'static, str>,
pub source: libipld::cid::Error,
#[allow(dead_code)]
hidden: (),
}
impl<'a> From<(usize, pb::PBLink<'a>, libipld::cid::Error)> for InvalidCidInLink {
fn from((nth, link, source): (usize, pb::PBLink<'a>, libipld::cid::Error)) -> Self {
let hash = match link.Hash {
Some(Cow::Borrowed(x)) if !x.is_empty() => Cow::Owned(x.to_vec()),
Some(Cow::Borrowed(_)) | None => Cow::Borrowed(&[][..]),
Some(Cow::Owned(x)) => Cow::Owned(x),
};
let name = match link.Name {
Some(Cow::Borrowed(x)) if !x.is_empty() => Cow::Owned(x.to_string()),
Some(Cow::Borrowed(_)) | None => Cow::Borrowed(""),
Some(Cow::Owned(x)) => Cow::Owned(x),
};
InvalidCidInLink {
nth,
hash,
name,
source,
hidden: (),
}
}
}
impl fmt::Display for InvalidCidInLink {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
fmt,
"failed to convert link #{} ({:?}) to Cid: {}",
self.nth, self.name, self.source
)
}
}
impl std::error::Error for InvalidCidInLink {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
pub struct UnexpectedNodeType(i32);
impl fmt::Debug for UnexpectedNodeType {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let converted = UnixFsType::from(self.0);
if converted == UnixFsType::Raw && self.0 != 0 {
write!(fmt, "{} or <unknown>", self.0)
} else {
write!(fmt, "{} or {:?}", self.0, converted)
}
}
}
impl From<UnixFsType> for UnexpectedNodeType {
fn from(t: UnixFsType) -> UnexpectedNodeType {
UnexpectedNodeType(t.into())
}
}
impl UnexpectedNodeType {
pub fn is_directory(&self) -> bool {
matches!(
UnixFsType::from(self.0),
UnixFsType::Directory | UnixFsType::HAMTShard
)
}
pub fn is_file(&self) -> bool {
matches!(UnixFsType::from(self.0), UnixFsType::File)
}
}
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct Metadata {
mode: Option<u32>,
mtime: Option<(i64, u32)>,
}
impl Metadata {
pub fn mode(&self) -> Option<u32> {
self.mode
}
pub fn mtime(&self) -> Option<(i64, u32)> {
self.mtime
}
#[cfg(feature = "filetime")]
pub fn mtime_as_filetime(&self) -> Option<filetime::FileTime> {
self.mtime()
.map(|(seconds, nanos)| filetime::FileTime::from_unix_time(seconds, nanos))
}
}
impl<'a> From<&'a UnixFs<'_>> for Metadata {
fn from(data: &'a UnixFs<'_>) -> Self {
let mode = data.mode;
let mtime = data
.mtime
.clone()
.map(|ut| (ut.Seconds, ut.FractionalNanoseconds.unwrap_or(0)));
Metadata { mode, mtime }
}
}