File: resolve.rs

package info (click to toggle)
rust-rust-unixfs 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 652 kB
  • sloc: sh: 17; makefile: 2
file content (292 lines) | stat: -rw-r--r-- 8,773 bytes parent folder | download
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
use cid::Cid;
use rust_unixfs::dir::{resolve, LookupError, ResolveError};
use std::convert::TryFrom;
use std::fmt;
use std::io::{Error as IoError, Read};
use std::path::PathBuf;

fn main() {
    let path = match std::env::args()
        .nth(1)
        .map(|s| IpfsPath::try_from(s.as_str()))
    {
        Some(Ok(path)) => path,
        Some(Err(e)) => {
            eprintln!("Invalid path given as argument: {e}");
            std::process::exit(1);
        }
        None => {
            eprintln!("USAGE: {} IPFSPATH\n", std::env::args().next().unwrap());
            eprintln!(
                "Will resolve the given IPFSPATH to a CID through any UnixFS \
                directories or HAMT shards from default go-ipfs 0.5 \
                configuration flatfs blockstore and write the final CID into \
                stdout"
            );
            std::process::exit(0);
        }
    };

    let ipfs_path = match std::env::var("IPFS_PATH") {
        Ok(s) => s,
        Err(e) => {
            eprintln!("IPFS_PATH is not set or could not be read: {e}");
            std::process::exit(1);
        }
    };

    let mut blocks = PathBuf::from(ipfs_path);
    blocks.push("blocks");

    let blockstore = ShardedBlockStore { root: blocks };

    match walk(blockstore, path) {
        Ok(Some(cid)) => {
            println!("{cid}");
        }
        Ok(None) => {
            eprintln!("not found");
        }
        Err(Error::OpeningFailed(e)) => {
            eprintln!("{e}\n");
            eprintln!("This is likely caused by either:");
            eprintln!(" - ipfs does not have the block");
            eprintln!(" - ipfs is configured to use non-flatfs storage");
            eprintln!(" - ipfs is configured to use flatfs with different sharding");
            std::process::exit(1);
        }
        Err(e) => {
            eprintln!("Failed to walk the merkle tree: {e}");
            std::process::exit(1);
        }
    }
}

#[derive(Debug)]
pub enum PathError {
    InvalidCid(cid::Error),
    InvalidPath,
}

impl fmt::Display for PathError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            PathError::InvalidCid(e) => write!(fmt, "{e}"),
            PathError::InvalidPath => write!(fmt, "invalid path"),
        }
    }
}

impl std::error::Error for PathError {}

/// Ipfs path following https://github.com/ipfs/go-path/
#[derive(Debug)]
pub struct IpfsPath {
    /// Option to support moving the cid
    root: Option<Cid>,
    path: std::vec::IntoIter<String>,
}

impl From<Cid> for IpfsPath {
    /// Creates a new `IpfsPath` from just the `Cid`, which is the same as parsing from a string
    /// representation of a `Cid`, but cannot fail.
    fn from(root: Cid) -> IpfsPath {
        IpfsPath {
            root: Some(root),
            path: Vec::new().into_iter(),
        }
    }
}

impl TryFrom<&str> for IpfsPath {
    type Error = PathError;

    fn try_from(path: &str) -> Result<Self, Self::Error> {
        let mut split = path.splitn(2, "/ipfs/");
        let first = split.next();
        let (_root, path) = match first {
            Some("") => {
                /* started with /ipfs/ */
                if let Some(x) = split.next() {
                    // was /ipfs/x
                    ("ipfs", x)
                } else {
                    // just the /ipfs/
                    return Err(PathError::InvalidPath);
                }
            }
            Some(x) => {
                /* maybe didn't start with /ipfs/, need to check second */
                if split.next().is_some() {
                    // x/ipfs/_
                    return Err(PathError::InvalidPath);
                }

                ("", x)
            }
            None => return Err(PathError::InvalidPath),
        };

        let mut split = path.splitn(2, '/');
        let root = split
            .next()
            .expect("first value from splitn(2, _) must exist");

        let path = split
            .next()
            .iter()
            .flat_map(|s| s.split('/').filter(|s| !s.is_empty()).map(String::from))
            .collect::<Vec<_>>()
            .into_iter();

        let root = Some(Cid::try_from(root).map_err(PathError::InvalidCid)?);

        Ok(IpfsPath { root, path })
    }
}

impl IpfsPath {
    pub fn take_root(&mut self) -> Option<Cid> {
        self.root.take()
    }
}

#[allow(clippy::result_large_err)]
fn walk(blocks: ShardedBlockStore, mut path: IpfsPath) -> Result<Option<Cid>, Error> {
    use rust_unixfs::dir::MaybeResolved::*;

    let mut buf = Vec::new();
    let mut root = path.take_root().unwrap();

    let mut cache = None;

    for segment in path.path {
        println!("cache {cache:?}");
        buf.clear();
        eprintln!("reading {root} to resolve {segment:?}");
        blocks.as_file(&root.to_bytes())?.read_to_end(&mut buf)?;

        let mut walker = match resolve(&buf, segment.as_str(), &mut cache)? {
            Found(cid) => {
                // either root was a Directory or we got lucky with a HAMT directory.
                // With HAMTDirectories the top level can contain a direct link to the target, but
                // it's more likely it will be found under some bucket, which would be the third
                // case in this match.
                println!("got lucky: found {cid} for {segment:?}");
                println!("cache {cache:?}");
                root = cid;
                continue;
            }

            NotFound => return Ok(None),

            // when we stumble upon a HAMT shard, we'll need to look up other blocks in order to
            // find the final link. The current implementation cannot search for the directory by
            // hashing the name and looking it up, but the implementation can be changed underneath
            // without changes to the API.
            //
            // HAMTDirecotories or HAMT shards are multi-block directories where the entires are
            // bucketed per their hash value.
            NeedToLoadMore(walker) => walker,
        };

        eprintln!("walking {root} on {segment:?}");

        let mut other_blocks = 1;

        loop {
            let (first, _) = walker.pending_links();
            buf.clear();
            eprintln!("  -> reading {first} while searching for {segment:?}");
            blocks.as_file(&first.to_bytes())?.read_to_end(&mut buf)?;

            match walker.continue_walk(&buf, &mut cache)? {
                NotFound => {
                    println!("cache {cache:?}");
                    return Ok(None);
                }
                Found(cid) => {
                    eprintln!(
                        "     resolved {segment} from {root} after {other_blocks} blocks to {cid}"
                    );
                    root = cid;
                    break;
                }
                NeedToLoadMore(next) => walker = next,
            }
            other_blocks += 1;
        }
    }

    println!("cache {cache:?}");
    Ok(Some(root))
}

enum Error {
    OpeningFailed(IoError),
    Other(IoError),
    Traversal(ResolveError),
}

impl From<IoError> for Error {
    fn from(e: IoError) -> Error {
        Error::Other(e)
    }
}

impl From<ResolveError> for Error {
    fn from(e: ResolveError) -> Error {
        Error::Traversal(e)
    }
}

impl From<LookupError> for Error {
    fn from(e: LookupError) -> Error {
        Error::Traversal(e.into())
    }
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Error::*;
        match self {
            OpeningFailed(e) => write!(fmt, "File opening failed: {e}"),
            Other(e) => write!(fmt, "Other file related io error: {e}"),
            Traversal(e) => write!(fmt, "Walking failed, please report this as a bug: {e:?}"),
        }
    }
}

struct ShardedBlockStore {
    root: PathBuf,
}

impl ShardedBlockStore {
    fn as_path(&self, key: &[u8]) -> PathBuf {
        // assume that we have a block store with second-to-last/2 sharding
        // files in Base32Upper

        let encoded = multibase::Base::Base32Upper.encode(key);
        let len = encoded.len();

        // this is safe because base32 is ascii
        let dir = &encoded[(len - 3)..(len - 1)];
        assert_eq!(dir.len(), 2);

        let mut path = self.root.clone();
        path.push(dir);
        path.push(encoded);
        path.set_extension("data");
        path
    }

    #[allow(clippy::result_large_err)]
    fn as_file(&self, key: &[u8]) -> Result<std::fs::File, Error> {
        let path = self.as_path(key);

        std::fs::OpenOptions::new()
            .read(true)
            .open(path)
            .map_err(Error::OpeningFailed)
    }
}