Skip to main content

ferripfs_unixfs/
lib.rs

1// Ported from: kubo/boxo/ipld/unixfs
2// Kubo version: v0.39.0
3// Original: https://github.com/ipfs/kubo/tree/v0.39.0/boxo/ipld/unixfs
4//
5// Original work: Copyright (c) Protocol Labs, Inc.
6// Port: Copyright (c) 2026 ferripfs contributors
7// SPDX-License-Identifier: MIT OR Apache-2.0
8
9//! UnixFS implementation for ferripfs, ported from Kubo's boxo/ipld/unixfs.
10//!
11//! This module provides:
12//! - Chunking algorithms (size-based, rabin fingerprinting)
13//! - DAG building (balanced, trickle layouts)
14//! - UnixFS protobuf encoding
15//! - File, directory, and symlink node types
16
17pub mod pb {
18    //! Protobuf definitions for UnixFS and MerkleDAG
19    include!(concat!(env!("OUT_DIR"), "/unixfs.pb.rs"));
20    include!(concat!(env!("OUT_DIR"), "/merkledag.pb.rs"));
21}
22
23mod chunker;
24mod dag;
25mod node;
26mod reader;
27
28pub use chunker::*;
29pub use dag::*;
30pub use node::*;
31pub use reader::*;
32
33use thiserror::Error;
34
35/// Default chunk size (256 KiB)
36pub const DEFAULT_CHUNK_SIZE: usize = 256 * 1024;
37
38/// Maximum links per node in balanced DAG
39pub const DEFAULT_LINKS_PER_BLOCK: usize = 174;
40
41/// UnixFS error type
42#[derive(Debug, Error)]
43pub enum UnixfsError {
44    #[error("IO error: {0}")]
45    Io(#[from] std::io::Error),
46
47    #[error("Blockstore error: {0}")]
48    Blockstore(#[from] ferripfs_blockstore::BlockstoreError),
49
50    #[error("Protobuf encode error: {0}")]
51    ProtobufEncode(#[from] prost::EncodeError),
52
53    #[error("Protobuf decode error: {0}")]
54    ProtobufDecode(#[from] prost::DecodeError),
55
56    #[error("Invalid UnixFS data: {0}")]
57    InvalidData(String),
58
59    #[error("CID error: {0}")]
60    Cid(String),
61
62    #[error("Not a file: {0}")]
63    NotAFile(String),
64
65    #[error("Not a directory: {0}")]
66    NotADirectory(String),
67
68    #[error("Entry not found: {0}")]
69    EntryNotFound(String),
70}
71
72/// Result type for UnixFS operations
73pub type UnixfsResult<T> = Result<T, UnixfsError>;
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_constants() {
81        assert_eq!(DEFAULT_CHUNK_SIZE, 262144);
82        assert_eq!(DEFAULT_LINKS_PER_BLOCK, 174);
83    }
84}