void_core/unixfs/
store.rs1use cid::Cid;
4
5use crate::store::FsStore;
6
7pub trait UnixFsStore {
13 fn contains(&self, cid: &Cid) -> bool;
15 fn get(&self, cid: &Cid) -> Option<Vec<u8>>;
17 fn put(&self, cid: Cid, data: Vec<u8>) -> usize;
19}
20
21pub 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}