diff --git a/proxmox-product-config/src/filesystem_helpers.rs b/proxmox-product-config/src/filesystem_helpers.rs index 2c486949..09d07e38 100644 --- a/proxmox-product-config/src/filesystem_helpers.rs +++ b/proxmox-product-config/src/filesystem_helpers.rs @@ -2,11 +2,11 @@ use anyhow::Error; use proxmox_sys::fs::CreateOptions; -use super::product_config; +use super::get_api_user; /// Return [CreateOptions] for files owned by `api_user.uid/api_user.gid` with mode `0640`. pub fn default_create_options() -> CreateOptions { - let api_user = &product_config().api_user; + let api_user = get_api_user(); let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640); proxmox_sys::fs::CreateOptions::new() .perm(mode) @@ -18,7 +18,7 @@ pub fn default_create_options() -> CreateOptions { /// /// Only the superuser can write those files, but group `api-user.gid` can read them. pub fn privileged_create_options() -> CreateOptions { - let api_user = &product_config().api_user; + let api_user = get_api_user(); let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640); proxmox_sys::fs::CreateOptions::new() .perm(mode) @@ -51,7 +51,7 @@ pub fn system_config_create_options() -> CreateOptions { /// Return [CreateOptions] for lock files, owner `api_user.uid/api_user.gid` and mode `0660`. pub fn lockfile_create_options() -> CreateOptions { - let api_user = &product_config().api_user; + let api_user = get_api_user(); proxmox_sys::fs::CreateOptions::new() .perm(nix::sys::stat::Mode::from_bits_truncate(0o660)) .owner(api_user.uid) diff --git a/proxmox-product-config/src/init.rs b/proxmox-product-config/src/init.rs new file mode 100644 index 00000000..9135311f --- /dev/null +++ b/proxmox-product-config/src/init.rs @@ -0,0 +1,26 @@ + +struct ProxmoxProductConfig { + // Configuration file owner. + api_user: nix::unistd::User, +} + +static mut PRODUCT_CONFIG: Option = None; + +/// Initialize the global product configuration. +pub fn init(api_user: nix::unistd::User) { + unsafe { + PRODUCT_CONFIG = Some(ProxmoxProductConfig { + api_user, + }); + } +} + +/// Returns the global product configuration (see [init_product_config]) +pub(crate) fn get_api_user() -> &'static nix::unistd::User { + unsafe { + &PRODUCT_CONFIG + .as_ref() + .expect("ProxmoxProductConfig is not initialized!") + .api_user + } +} diff --git a/proxmox-product-config/src/lib.rs b/proxmox-product-config/src/lib.rs index a5c4abb3..c6e08e5e 100644 --- a/proxmox-product-config/src/lib.rs +++ b/proxmox-product-config/src/lib.rs @@ -1,5 +1,5 @@ mod filesystem_helpers; pub use filesystem_helpers::*; -mod product_config; -pub use product_config::*; +mod init; +pub use init::*; diff --git a/proxmox-product-config/src/product_config.rs b/proxmox-product-config/src/product_config.rs deleted file mode 100644 index f5cc364f..00000000 --- a/proxmox-product-config/src/product_config.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::path::{Path, PathBuf}; - -static mut PRODUCT_CONFIG: Option = None; - -/// Initialize the global product configuration. -pub fn init_product_config(config_dir: &'static str, api_user: nix::unistd::User) { - unsafe { - PRODUCT_CONFIG = Some(ProxmoxProductConfig { - config_dir, - api_user, - }); - } -} - -/// Returns the global product configuration (see [init_product_config]) -pub fn product_config() -> &'static ProxmoxProductConfig { - unsafe { - PRODUCT_CONFIG - .as_ref() - .expect("ProxmoxProductConfig is not initialized!") - } -} - -pub struct ProxmoxProductConfig { - /// Path to the main product configuration directory. - pub(crate) config_dir: &'static str, - - /// Configuration file owner. - pub(crate) api_user: nix::unistd::User, -} - -impl ProxmoxProductConfig { - /// Returns the absolute path (prefix with 'config_dir') - pub fn absolute_path(&self, rel_path: &str) -> PathBuf { - let path = Path::new(self.config_dir); - path.join(rel_path) - } -}