use std::path::PathBuf;
use directories::ProjectDirs;
const QUALIFIER: &str = "io";
const ORGANIZATION: &str = "";
const APPLICATION: &str = "intmod";
pub fn image_blobs_cache_root() -> std::io::Result<PathBuf> {
let mut blobs_cache_dir = match ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) {
Some(p) => PathBuf::from(p.cache_dir()),
None => std::env::temp_dir(),
};
let _ = blobs_cache_dir.push("blobs");
if !blobs_cache_dir.exists() {
log::debug!("The Parent Cache directory does not exist. Creating.");
std::fs::create_dir_all(&blobs_cache_dir)?;
}
Ok(blobs_cache_dir)
}
pub fn oci_images_root() -> std::io::Result<PathBuf> {
let mut images_root_dir = match ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) {
Some(p) => p.data_local_dir().to_path_buf(),
None => {
log::warn!("No Local Data Directory found, using temporary directory.");
std::env::temp_dir()
}
};
let _ = images_root_dir.push("images");
if !images_root_dir.exists() {
log::debug!("Images Root Directory does not exist. Creating.");
std::fs::create_dir_all(&images_root_dir)?;
}
Ok(images_root_dir)
}
pub fn storage_root_for_fs(fs: &str) -> std::io::Result<PathBuf> {
let mut storage_root_dir = match ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION) {
Some(p) => p.data_local_dir().to_path_buf(),
None => {
log::warn!("No Local Data Directory found, using temporary directory.");
std::env::temp_dir()
}
};
let _ = storage_root_dir.push("storage");
let _ = storage_root_dir.push(fs);
if !storage_root_dir.exists() {
log::debug!(
"{}",
format!("Creating Storage Root directory for : {}", fs)
);
std::fs::create_dir_all(&storage_root_dir)?;
}
Ok(storage_root_dir)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_blobs_cache_dir() {
let r = image_blobs_cache_root();
assert!(r.is_ok());
}
#[test]
fn test_get_oci_images_root() {
let r = oci_images_root();
assert!(r.is_ok());
}
}