oci_util/filesystem/
mod.rs

1use anyhow::{anyhow, Result};
2use std::path::PathBuf;
3
4pub mod snapshot;
5
6pub struct FileSystem;
7
8///
9/// $HOME/hpmq
10///       ├──imagedb
11///       │  ├──images.json
12///       │  ├──sha256
13///       │  │  ├──image的manifest文件;文件名为文件的sha256摘要
14///       ├──configdb
15///       │  ├──sha256
16///       │  │  ├──image的config文件;文件名为文件的sha256摘要
17///       ├──layerdb
18///       │  ├──contents?(待实现)
19///       │  │  │  layer的描述文件(Descriptor);文件名为文件的sha256摘要
20///       │  ├──blobs
21///       │  │  ├──sha256
22///       │  │  │  ├──image的layer文件
23///       ├──containerdb
24///       │  ├──image的manifest文件的sha256
25///       │  │  ├──image展开后的文件目录
26///
27impl FileSystem {
28    ///
29    /// |Platform | Value                | Example        |
30    /// | ------- | -------------------- | -------------- |
31    /// | Linux   | `$HOME`              | /home/alice    |
32    /// | macOS   | `$HOME`              | /Users/Alice   |
33    /// | Windows | `{FOLDERID_Profile}` | C:\Users\Alice |
34    pub fn home(&self) -> Result<PathBuf> {
35        let path = dirs::home_dir()
36            .and_then(|path| Some(path.join(".hpmq")))
37            .ok_or(anyhow!("找不到HOME路径"))?;
38        std::fs::create_dir_all(&path)?;
39        Ok(path)
40    }
41    pub fn layer(&self) -> Result<PathBuf> {
42        let path = self.home()?.join("layerdb");
43        std::fs::create_dir_all(&path)?;
44        Ok(path)
45    }
46    pub fn layer_contents(&self) -> Result<PathBuf> {
47        let path = self.home()?.join("layerdb").join("contents");
48        std::fs::create_dir_all(&path)?;
49        Ok(path)
50    }
51    pub fn layer_blobs(&self) -> Result<PathBuf> {
52        let path = self.home()?.join("layerdb").join("blobs").join("sha256");
53        std::fs::create_dir_all(&path)?;
54        Ok(path)
55    }
56    pub fn manifest_sha256(&self) -> Result<PathBuf> {
57        let path = self.home()?.join("imagedb").join("sha256");
58        std::fs::create_dir_all(&path)?;
59        Ok(path)
60    }
61    pub fn config_sha256(&self) -> Result<PathBuf> {
62        let path = self.home()?.join("configdb").join("sha256");
63        std::fs::create_dir_all(&path)?;
64        Ok(path)
65    }
66    pub fn images_json(&self) -> Result<PathBuf> {
67        let path = self.home()?.join("imagedb");
68        std::fs::create_dir_all(&path)?;
69        Ok(path.join("images.json"))
70    }
71    pub fn container(&self) -> Result<PathBuf> {
72        let path = self.home()?.join("containerdb");
73        std::fs::create_dir_all(&path)?;
74        Ok(path)
75    }
76
77    pub fn exist_config(&self, sha256_digest: &String) -> Result<bool> {
78        let config_path = self.config_sha256()?;
79        Ok(config_path.join(sha256_digest).exists())
80    }
81    pub fn exist_layer(&self, sha256_digest: &String) -> Result<bool> {
82        let layer_path = self.layer_blobs()?;
83        Ok(layer_path.join(sha256_digest).exists())
84    }
85    pub fn exist_container(&self, sha256_digest: &String) -> Result<bool> {
86        let layer_path = self.container()?;
87        Ok(layer_path.join(sha256_digest).exists())
88    }
89
90    pub fn save_config(&self, sha256_digest: &String, data: &[u8]) -> Result<()> {
91        let config_path = self.config_sha256()?;
92        Ok(std::fs::write(config_path.join(sha256_digest), data)?)
93    }
94    pub fn save_manifest(&self, sha256_digest: &String, data: &[u8]) -> Result<()> {
95        let manifest_path = self.manifest_sha256()?;
96        Ok(std::fs::write(manifest_path.join(sha256_digest), data)?)
97    }
98    pub fn save_layer(&self, sha256_digest: &String, data: &[u8]) -> Result<()> {
99        let config_path = self.layer_blobs()?;
100        Ok(std::fs::write(config_path.join(sha256_digest), data)?)
101    }
102}