wayvr/wlx-common/src/cache_dir.rs

38 lines
908 B
Rust

use std::{path::PathBuf, sync::LazyLock};
use freedesktop::xdg_cache_home;
static CACHE_ROOT_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
// Panics if neither $XDG_CACHE_HOME nor $HOME is set
let mut dir = xdg_cache_home();
dir.push("wayvr");
dir
});
fn get_cache_root() -> PathBuf {
CACHE_ROOT_PATH.clone()
}
// todo: mutex
pub async fn get_data(data_path: &str) -> Option<Vec<u8>> {
let mut path = get_cache_root();
path.push(data_path);
smol::fs::read(path).await.ok()
}
// todo: mutex
pub async fn set_data(data_path: &str, data: &[u8]) -> std::io::Result<()> {
let mut path = get_cache_root();
path.push(data_path);
log::debug!(
"Writing cache data ({} bytes) to path {}",
data.len(),
path.to_string_lossy()
);
let mut dir_path = path.clone();
dir_path.pop();
smol::fs::create_dir_all(dir_path).await?; // make sure directory is available
smol::fs::write(path, data).await
}