Skip to main content

zoi_core/
pkgdir.rs

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