diff --git a/Cargo.lock b/Cargo.lock index a183f2a..8f7a713 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7035,8 +7035,10 @@ dependencies = [ "glam", "idmap", "idmap-derive", + "log", "serde", "wayvr-ipc", + "xdg 3.0.0", ] [[package]] diff --git a/wlx-common/Cargo.toml b/wlx-common/Cargo.toml index 50bd84c..3336269 100644 --- a/wlx-common/Cargo.toml +++ b/wlx-common/Cargo.toml @@ -12,3 +12,5 @@ idmap = { workspace = true, features = ["serde"] } idmap-derive = { workspace = true } wayvr-ipc = { path = "../wayvr-ipc", default-features = false } anyhow = { workspace = true } +xdg = "3.0" +log = { workspace = true } diff --git a/wlx-common/src/cache_dir.rs b/wlx-common/src/cache_dir.rs new file mode 100644 index 0000000..94ce9ba --- /dev/null +++ b/wlx-common/src/cache_dir.rs @@ -0,0 +1,35 @@ +use std::{path::PathBuf, sync::LazyLock}; + +const FALLBACK_CACHE_PATH: &str = "/tmp/wayvr_cache"; + +static CACHE_ROOT_PATH: LazyLock = LazyLock::new(|| { + if let Some(mut dir) = xdg::BaseDirectories::new().get_cache_home() { + dir.push("wayvr"); + return dir; + } + + //Return fallback cache path + log::error!("Err: Failed to find cache path, using {FALLBACK_CACHE_PATH}"); + PathBuf::from(FALLBACK_CACHE_PATH) +}); + +fn get_cache_root() -> PathBuf { + CACHE_ROOT_PATH.clone() +} + +fn ensure_dir(cache_root_path: &PathBuf) { + let _ = std::fs::create_dir(cache_root_path); +} + +pub fn get_data(data_path: &str) -> Option> { + let mut path = get_cache_root(); + ensure_dir(&path); + path.push(data_path); + std::fs::read(path).ok() +} + +pub fn set_data(data_path: &str, data: &[u8]) -> std::io::Result<()> { + let mut path = get_cache_root(); + path.push(data_path); + std::fs::write(path, data) +} diff --git a/wlx-common/src/lib.rs b/wlx-common/src/lib.rs index db5ea04..287c1c5 100644 --- a/wlx-common/src/lib.rs +++ b/wlx-common/src/lib.rs @@ -1,4 +1,5 @@ pub mod astr_containers; +pub mod cache_dir; pub mod common; pub mod config; pub mod dash_interface;