Skip to main content

zoi_core/
pkgdir.rs

1use std::path::PathBuf;
2use std::sync::{OnceLock, RwLock};
3
4fn pkg_dirs_store() -> &'static RwLock<Vec<PathBuf>> {
5    static PKG_DIRS: OnceLock<RwLock<Vec<PathBuf>>> = OnceLock::new();
6    PKG_DIRS.get_or_init(|| RwLock::new(Vec::new()))
7}
8
9/// Sets the global package search directories.
10pub fn set_pkg_dirs(dirs: Vec<PathBuf>) {
11    if let Ok(mut guard) = pkg_dirs_store().write() {
12        *guard = dirs;
13    }
14}
15
16/// Returns the list of global package search directories.
17pub fn get_pkg_dirs() -> Vec<PathBuf> {
18    pkg_dirs_store()
19        .read()
20        .map(|dirs| dirs.clone())
21        .unwrap_or_default()
22}
23
24/// Checks if an archive exists in any of the configured pkg-dirs.
25/// Returns the path to the archive if found.
26pub fn find_in_pkg_dirs(filename: &str) -> Option<PathBuf> {
27    for dir in get_pkg_dirs() {
28        let path = dir.join(filename);
29        if path.exists() && path.is_file() {
30            return Some(path);
31        }
32    }
33    None
34}