Skip to main content

void_core/unixfs/
store.rs

1//! Block store trait and adapters for UnixFS operations.
2
3use cid::Cid;
4
5use crate::store::FsStore;
6
7/// Minimal block store interface for UnixFS operations.
8///
9/// Simpler than `ObjectStoreExt` (which deals in typed `EncryptedBlob`s).
10/// UnixFS blocks are unencrypted DAG-PB/raw nodes — they go through this
11/// trait directly.
12pub trait UnixFsStore {
13    /// Check if a block exists.
14    fn contains(&self, cid: &Cid) -> bool;
15    /// Get a block's raw bytes.
16    fn get(&self, cid: &Cid) -> Option<Vec<u8>>;
17    /// Store a block. Returns the block size in bytes.
18    fn put(&self, cid: Cid, data: Vec<u8>) -> usize;
19}
20
21/// Adapts void-core's `FsStore` to the `UnixFsStore` trait.
22///
23/// UnixFS blocks are stored alongside encrypted void objects in the
24/// same objects directory. They use different CID codecs (DAG-PB vs raw)
25/// so there's no collision.
26pub struct FsStoreAdapter<'a>(pub &'a FsStore);
27
28impl UnixFsStore for FsStoreAdapter<'_> {
29    fn contains(&self, cid: &Cid) -> bool {
30        let path = FsStore::object_path(self.0.root().as_std_path(), &cid.to_string());
31        path.exists()
32    }
33
34    fn get(&self, cid: &Cid) -> Option<Vec<u8>> {
35        let path = FsStore::object_path(self.0.root().as_std_path(), &cid.to_string());
36        std::fs::read(path).ok()
37    }
38
39    fn put(&self, cid: Cid, data: Vec<u8>) -> usize {
40        let len = data.len();
41        let path = FsStore::object_path(self.0.root().as_std_path(), &cid.to_string());
42        if let Some(parent) = path.parent() {
43            let _ = std::fs::create_dir_all(parent);
44        }
45        let _ = std::fs::write(path, &data);
46        len
47    }
48}