Skip to main content

selium_filesystem_store/
lib.rs

1use std::{
2    fs::File,
3    io::Read,
4    path::{Path, PathBuf},
5};
6
7mod driver;
8pub use driver::FilesystemStoreReadDriver;
9use path_security::validate_path;
10use selium_kernel::drivers::module_store::ModuleStoreError;
11
12pub struct FilesystemStore {
13    base_dir: PathBuf,
14}
15
16impl FilesystemStore {
17    pub fn new(base_dir: impl AsRef<Path>) -> Self {
18        Self {
19            base_dir: base_dir.as_ref().into(),
20        }
21    }
22
23    pub fn fetch(&self, path: impl AsRef<Path>) -> Result<Vec<u8>, ModuleStoreError> {
24        let fq_path = validate_path(path.as_ref(), &self.base_dir).map_err(|e| {
25            ModuleStoreError::InvalidPath(
26                self.base_dir.as_path().join(path.as_ref()),
27                e.to_string(),
28            )
29        })?;
30
31        let mut fh =
32            File::open(fq_path).map_err(|e| ModuleStoreError::Filesystem(e.to_string()))?;
33        // @todo Set memory limit!
34        let mut buf = Vec::new();
35        fh.read_to_end(&mut buf)
36            .map_err(|e| ModuleStoreError::Filesystem(e.to_string()))?;
37
38        Ok(buf)
39    }
40}