bitcoind_cache/store/
filesystem.rs1use crate::store::{AsyncStoreResult, Store, StoreError};
2use std::{fs, io};
3
4#[derive(Clone)]
5pub struct FileStore {
6 root_dir: String,
7}
8
9impl FileStore {
10 pub fn new(root_dir: &str) -> io::Result<Self> {
11 fs::create_dir_all(root_dir)?;
12 Ok(Self { root_dir: root_dir.to_string() })
13 }
14}
15
16impl Store for FileStore {
17 fn get_object(&self, filename: String) -> AsyncStoreResult<Option<Vec<u8>>> {
18 Box::pin(async move {
19 match tokio::fs::read(format!("{}/{}", self.root_dir, filename)).await {
20 Ok(content) => Ok(Some(content)),
21 Err(e) => {
22 if e.kind() == io::ErrorKind::NotFound {
23 Ok(None)
24 } else {
25 Err(StoreError::Io(e))
26 }
27 }
28 }
29 })
30 }
31
32 fn put_object<'a>(&'a self, filename: String, content: &'a [u8]) -> AsyncStoreResult<()> {
33 Box::pin(async move {
34 tokio::fs::write(format!("{}/{}", self.root_dir, filename), content)
35 .await
36 .map_err(StoreError::Io)
37 })
38 }
39}