[pbs-devel] [PATCH backup 10/11] backup: remove lazy_static dependency

Maximiliano Sandoval m.sandoval at proxmox.com
Tue Aug 13 10:44:15 CEST 2024


Signed-off-by: Maximiliano Sandoval <m.sandoval at proxmox.com>
---
 Cargo.toml                       |  2 --
 src/api2/config/acme.rs          |  9 ++----
 src/api2/node/disks/directory.rs |  7 ++--
 src/api2/node/dns.rs             | 16 ++++------
 src/config/acme/plugin.rs        |  7 ++--
 src/tape/file_formats/mod.rs     | 55 ++++++++++++++++++++++++--------
 src/tools/disks/lvm.rs           | 14 ++++----
 src/tools/disks/mod.rs           |  8 ++---
 src/tools/disks/smart.rs         |  8 ++---
 src/tools/disks/zfs.rs           | 23 ++++++-------
 src/tools/systemd/config.rs      | 11 +++----
 src/traffic_control_cache.rs     | 10 +++---
 12 files changed, 87 insertions(+), 83 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 221dcf53..ef430d43 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -128,7 +128,6 @@ handlebars = "3.0"
 hex = "0.4.3"
 http = "0.2"
 hyper = { version = "0.14", features = [ "full" ] }
-lazy_static = "1.4"
 libc = "0.2"
 log = "0.4.17"
 nix = "0.26.1"
@@ -180,7 +179,6 @@ handlebars.workspace = true
 hex.workspace = true
 http.workspace = true
 hyper.workspace = true
-lazy_static.workspace = true
 libc.workspace = true
 log.workspace = true
 nix.workspace = true
diff --git a/src/api2/config/acme.rs b/src/api2/config/acme.rs
index e2ccaded..422b9720 100644
--- a/src/api2/config/acme.rs
+++ b/src/api2/config/acme.rs
@@ -1,12 +1,11 @@
 use std::fs;
 use std::ops::ControlFlow;
 use std::path::Path;
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
 use std::time::SystemTime;
 
 use anyhow::{bail, format_err, Error};
 use hex::FromHex;
-use lazy_static::lazy_static;
 use serde::{Deserialize, Serialize};
 use serde_json::{json, Value};
 use tracing::{info, warn};
@@ -430,10 +429,8 @@ impl Serialize for ChallengeSchemaWrapper {
 }
 
 fn get_cached_challenge_schemas() -> Result<ChallengeSchemaWrapper, Error> {
-    lazy_static! {
-        static ref CACHE: Mutex<Option<(Arc<Vec<AcmeChallengeSchema>>, SystemTime)>> =
-            Mutex::new(None);
-    }
+    static CACHE: LazyLock<Mutex<Option<(Arc<Vec<AcmeChallengeSchema>>, SystemTime)>>> =
+        LazyLock::new(|| Mutex::new(None));
 
     // the actual loading code
     let mut last = CACHE.lock().unwrap();
diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs
index 2aa9571d..e971f40d 100644
--- a/src/api2/node/disks/directory.rs
+++ b/src/api2/node/disks/directory.rs
@@ -1,3 +1,5 @@
+use std::sync::LazyLock;
+
 use ::serde::{Deserialize, Serialize};
 use anyhow::{bail, Error};
 use serde_json::json;
@@ -71,9 +73,8 @@ pub struct DatastoreMountInfo {
 )]
 /// List systemd datastore mount units.
 pub fn list_datastore_mounts() -> Result<Vec<DatastoreMountInfo>, Error> {
-    lazy_static::lazy_static! {
-        static ref MOUNT_NAME_REGEX: regex::Regex = regex::Regex::new(r"^mnt-datastore-(.+)\.mount$").unwrap();
-    }
+    static MOUNT_NAME_REGEX: LazyLock<regex::Regex> =
+        LazyLock::new(|| regex::Regex::new(r"^mnt-datastore-(.+)\.mount$").unwrap());
 
     let mut list = Vec::new();
 
diff --git a/src/api2/node/dns.rs b/src/api2/node/dns.rs
index 87a11738..bd1f528f 100644
--- a/src/api2/node/dns.rs
+++ b/src/api2/node/dns.rs
@@ -1,9 +1,8 @@
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
 
 use ::serde::{Deserialize, Serialize};
 use anyhow::Error;
 use const_format::concatcp;
-use lazy_static::lazy_static;
 use openssl::sha;
 use regex::Regex;
 use serde_json::{json, Value};
@@ -46,11 +45,10 @@ pub fn read_etc_resolv_conf() -> Result<Value, Error> {
 
     let data = String::from_utf8(raw)?;
 
-    lazy_static! {
-        static ref DOMAIN_REGEX: Regex = Regex::new(r"^\s*(?:search|domain)\s+(\S+)\s*").unwrap();
-        static ref SERVER_REGEX: Regex =
-            Regex::new(concatcp!(r"^\s*nameserver\s+(", IPRE_STR, r")\s*")).unwrap();
-    }
+    static DOMAIN_REGEX: LazyLock<Regex> =
+        LazyLock::new(|| Regex::new(r"^\s*(?:search|domain)\s+(\S+)\s*").unwrap());
+    static SERVER_REGEX: LazyLock<Regex> =
+        LazyLock::new(|| Regex::new(concatcp!(r"^\s*nameserver\s+(", IPRE_STR, r")\s*")).unwrap());
 
     let mut options = String::new();
 
@@ -131,9 +129,7 @@ pub fn update_dns(
     delete: Option<Vec<DeletableProperty>>,
     digest: Option<String>,
 ) -> Result<Value, Error> {
-    lazy_static! {
-        static ref MUTEX: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
-    }
+    static MUTEX: LazyLock<Arc<Mutex<()>>> = LazyLock::new(|| Arc::new(Mutex::new(())));
 
     let _guard = MUTEX.lock();
 
diff --git a/src/config/acme/plugin.rs b/src/config/acme/plugin.rs
index ff66dec3..74ef0459 100644
--- a/src/config/acme/plugin.rs
+++ b/src/config/acme/plugin.rs
@@ -1,5 +1,6 @@
+use std::sync::LazyLock;
+
 use anyhow::Error;
-use lazy_static::lazy_static;
 use serde::{Deserialize, Serialize};
 use serde_json::Value;
 
@@ -15,9 +16,7 @@ pub const PLUGIN_ID_SCHEMA: Schema = StringSchema::new("ACME Challenge Plugin ID
     .max_length(32)
     .schema();
 
-lazy_static! {
-    pub static ref CONFIG: SectionConfig = init();
-}
+pub static CONFIG: LazyLock<SectionConfig> = LazyLock::new(init);
 
 #[api(
     properties: {
diff --git a/src/tape/file_formats/mod.rs b/src/tape/file_formats/mod.rs
index b1d8c455..7b99cb99 100644
--- a/src/tape/file_formats/mod.rs
+++ b/src/tape/file_formats/mod.rs
@@ -2,6 +2,7 @@
 //! tapes
 
 use std::collections::HashMap;
+use std::sync::LazyLock;
 
 use endian_trait::Endian;
 use serde::{Deserialize, Serialize};
@@ -56,22 +57,48 @@ pub const PROXMOX_BACKUP_CATALOG_ARCHIVE_MAGIC_1_0: [u8; 8] =
 // openssl::sha::sha256(b"Proxmox Backup Catalog Archive v1.1")[0..8];
 pub const PROXMOX_BACKUP_CATALOG_ARCHIVE_MAGIC_1_1: [u8; 8] = [179, 236, 113, 240, 173, 236, 2, 96];
 
-lazy_static::lazy_static! {
-    // Map content magic numbers to human readable names.
-    static ref PROXMOX_TAPE_CONTENT_NAME: HashMap<&'static [u8;8], &'static str> = {
+// Map content magic numbers to human readable names.
+static PROXMOX_TAPE_CONTENT_NAME: LazyLock<HashMap<&'static [u8; 8], &'static str>> =
+    LazyLock::new(|| {
         let mut map = HashMap::new();
-        map.insert(&PROXMOX_BACKUP_MEDIA_LABEL_MAGIC_1_0, "Proxmox Backup Tape Label v1.0");
-        map.insert(&PROXMOX_BACKUP_MEDIA_SET_LABEL_MAGIC_1_0, "Proxmox Backup MediaSet Label v1.0");
-        map.insert(&PROXMOX_BACKUP_CHUNK_ARCHIVE_MAGIC_1_0, "Proxmox Backup Chunk Archive v1.0");
-        map.insert(&PROXMOX_BACKUP_CHUNK_ARCHIVE_MAGIC_1_1, "Proxmox Backup Chunk Archive v1.1");
-        map.insert(&PROXMOX_BACKUP_SNAPSHOT_ARCHIVE_MAGIC_1_0, "Proxmox Backup Snapshot Archive v1.0");
-        map.insert(&PROXMOX_BACKUP_SNAPSHOT_ARCHIVE_MAGIC_1_1, "Proxmox Backup Snapshot Archive v1.1");
-        map.insert(&PROXMOX_BACKUP_SNAPSHOT_ARCHIVE_MAGIC_1_2, "Proxmox Backup Snapshot Archive v1.2");
-        map.insert(&PROXMOX_BACKUP_CATALOG_ARCHIVE_MAGIC_1_0, "Proxmox Backup Catalog Archive v1.0");
-        map.insert(&PROXMOX_BACKUP_CATALOG_ARCHIVE_MAGIC_1_1, "Proxmox Backup Catalog Archive v1.1");
+        map.insert(
+            &PROXMOX_BACKUP_MEDIA_LABEL_MAGIC_1_0,
+            "Proxmox Backup Tape Label v1.0",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_MEDIA_SET_LABEL_MAGIC_1_0,
+            "Proxmox Backup MediaSet Label v1.0",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_CHUNK_ARCHIVE_MAGIC_1_0,
+            "Proxmox Backup Chunk Archive v1.0",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_CHUNK_ARCHIVE_MAGIC_1_1,
+            "Proxmox Backup Chunk Archive v1.1",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_SNAPSHOT_ARCHIVE_MAGIC_1_0,
+            "Proxmox Backup Snapshot Archive v1.0",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_SNAPSHOT_ARCHIVE_MAGIC_1_1,
+            "Proxmox Backup Snapshot Archive v1.1",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_SNAPSHOT_ARCHIVE_MAGIC_1_2,
+            "Proxmox Backup Snapshot Archive v1.2",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_CATALOG_ARCHIVE_MAGIC_1_0,
+            "Proxmox Backup Catalog Archive v1.0",
+        );
+        map.insert(
+            &PROXMOX_BACKUP_CATALOG_ARCHIVE_MAGIC_1_1,
+            "Proxmox Backup Catalog Archive v1.1",
+        );
         map
-    };
-}
+    });
 
 /// Map content magic numbers to human readable names.
 pub fn proxmox_tape_magic_to_text(magic: &[u8; 8]) -> Option<String> {
diff --git a/src/tools/disks/lvm.rs b/src/tools/disks/lvm.rs
index b5e79e22..1456a21c 100644
--- a/src/tools/disks/lvm.rs
+++ b/src/tools/disks/lvm.rs
@@ -1,19 +1,17 @@
 use std::collections::HashSet;
 use std::os::unix::fs::MetadataExt;
+use std::sync::LazyLock;
 
 use anyhow::Error;
-use lazy_static::lazy_static;
 use serde_json::Value;
 
 use super::LsblkInfo;
 
-lazy_static! {
-    static ref LVM_UUIDS: HashSet<&'static str> = {
-        let mut set = HashSet::new();
-        set.insert("e6d6d379-f507-44c2-a23c-238f2a3df928");
-        set
-    };
-}
+static LVM_UUIDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
+    let mut set = HashSet::new();
+    set.insert("e6d6d379-f507-44c2-a23c-238f2a3df928");
+    set
+});
 
 /// Get set of devices used by LVM (pvs).
 ///
diff --git a/src/tools/disks/mod.rs b/src/tools/disks/mod.rs
index 9b5aaf6b..c729c26a 100644
--- a/src/tools/disks/mod.rs
+++ b/src/tools/disks/mod.rs
@@ -6,7 +6,7 @@ use std::io;
 use std::os::unix::ffi::{OsStrExt, OsStringExt};
 use std::os::unix::fs::MetadataExt;
 use std::path::{Path, PathBuf};
-use std::sync::Arc;
+use std::sync::{Arc, LazyLock};
 
 use anyhow::{bail, format_err, Error};
 use libc::dev_t;
@@ -32,10 +32,8 @@ pub use lvm::*;
 mod smart;
 pub use smart::*;
 
-lazy_static::lazy_static! {
-    static ref ISCSI_PATH_REGEX: regex::Regex =
-        regex::Regex::new(r"host[^/]*/session[^/]*").unwrap();
-}
+static ISCSI_PATH_REGEX: LazyLock<regex::Regex> =
+    LazyLock::new(|| regex::Regex::new(r"host[^/]*/session[^/]*").unwrap());
 
 /// Disk management context.
 ///
diff --git a/src/tools/disks/smart.rs b/src/tools/disks/smart.rs
index e666eb1a..3ad782b7 100644
--- a/src/tools/disks/smart.rs
+++ b/src/tools/disks/smart.rs
@@ -1,8 +1,8 @@
 use std::collections::{HashMap, HashSet};
+use std::sync::LazyLock;
 
 use ::serde::{Deserialize, Serialize};
 use anyhow::{bail, Error};
-use lazy_static::lazy_static;
 
 use proxmox_schema::api;
 
@@ -224,7 +224,5 @@ static WEAROUT_FIELD_ORDER: &[&str] = &[
     "Perc_Rated_Life_Used",
 ];
 
-lazy_static! {
-    static ref WEAROUT_FIELD_NAMES: HashSet<&'static str> =
-        WEAROUT_FIELD_ORDER.iter().cloned().collect();
-}
+static WEAROUT_FIELD_NAMES: LazyLock<HashSet<&'static str>> =
+    LazyLock::new(|| WEAROUT_FIELD_ORDER.iter().cloned().collect());
diff --git a/src/tools/disks/zfs.rs b/src/tools/disks/zfs.rs
index b12a948b..2abb5176 100644
--- a/src/tools/disks/zfs.rs
+++ b/src/tools/disks/zfs.rs
@@ -1,23 +1,20 @@
 use std::collections::HashSet;
 use std::os::unix::fs::MetadataExt;
 use std::path::PathBuf;
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
 
 use anyhow::{bail, Error};
-use lazy_static::lazy_static;
 
 use proxmox_schema::const_regex;
 
 use super::*;
 
-lazy_static! {
-    static ref ZFS_UUIDS: HashSet<&'static str> = {
-        let mut set = HashSet::new();
-        set.insert("6a898cc3-1dd2-11b2-99a6-080020736631"); // apple
-        set.insert("516e7cba-6ecf-11d6-8ff8-00022d09712b"); // bsd
-        set
-    };
-}
+static ZFS_UUIDS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
+    let mut set = HashSet::new();
+    set.insert("6a898cc3-1dd2-11b2-99a6-080020736631"); // apple
+    set.insert("516e7cba-6ecf-11d6-8ff8-00022d09712b"); // bsd
+    set
+});
 
 fn get_pool_from_dataset(dataset: &str) -> &str {
     if let Some(idx) = dataset.find('/') {
@@ -100,10 +97,8 @@ const_regex! {
     OBJSET_REGEX = r"^objset-0x[a-fA-F0-9]+$";
 }
 
-lazy_static::lazy_static! {
-    pub static ref ZFS_DATASET_OBJSET_MAP: Arc<Mutex<HashMap<String, (String, String)>>> =
-        Arc::new(Mutex::new(HashMap::new()));
-}
+pub static ZFS_DATASET_OBJSET_MAP: LazyLock<Arc<Mutex<HashMap<String, (String, String)>>>> =
+    LazyLock::new(|| Arc::new(Mutex::new(HashMap::new())));
 
 // parses /proc/spl/kstat/zfs/POOL/objset-ID files
 // they have the following format:
diff --git a/src/tools/systemd/config.rs b/src/tools/systemd/config.rs
index 3e1ac1bb..9b94531b 100644
--- a/src/tools/systemd/config.rs
+++ b/src/tools/systemd/config.rs
@@ -1,5 +1,6 @@
+use std::sync::LazyLock;
+
 use anyhow::Error;
-use lazy_static::lazy_static;
 
 use super::types::*;
 
@@ -8,11 +9,9 @@ use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlug
 
 use proxmox_sys::{fs::replace_file, fs::CreateOptions};
 
-lazy_static! {
-    pub static ref SERVICE_CONFIG: SectionConfig = init_service();
-    pub static ref TIMER_CONFIG: SectionConfig = init_timer();
-    pub static ref MOUNT_CONFIG: SectionConfig = init_mount();
-}
+pub static SERVICE_CONFIG: LazyLock<SectionConfig> = LazyLock::new(init_service);
+pub static TIMER_CONFIG: LazyLock<SectionConfig> = LazyLock::new(init_timer);
+pub static MOUNT_CONFIG: LazyLock<SectionConfig> = LazyLock::new(init_mount);
 
 fn init_service() -> SectionConfig {
     let mut config = SectionConfig::with_systemd_syntax(&SYSTEMD_SECTION_NAME_SCHEMA);
diff --git a/src/traffic_control_cache.rs b/src/traffic_control_cache.rs
index 32da6f4c..830a8c04 100644
--- a/src/traffic_control_cache.rs
+++ b/src/traffic_control_cache.rs
@@ -2,7 +2,7 @@
 
 use std::collections::HashMap;
 use std::net::{IpAddr, Ipv4Addr, SocketAddr};
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
 use std::time::Instant;
 
 use anyhow::Error;
@@ -21,11 +21,9 @@ use crate::tools::SharedRateLimiter;
 
 pub type SharedRateLimit = Arc<dyn ShareableRateLimit>;
 
-lazy_static::lazy_static! {
-    /// Shared traffic control cache singleton.
-    pub static ref TRAFFIC_CONTROL_CACHE: Arc<Mutex<TrafficControlCache>> =
-        Arc::new(Mutex::new(TrafficControlCache::new()));
-}
+/// Shared traffic control cache singleton.
+pub static TRAFFIC_CONTROL_CACHE: LazyLock<Arc<Mutex<TrafficControlCache>>> =
+    LazyLock::new(|| Arc::new(Mutex::new(TrafficControlCache::new())));
 
 struct ParsedTcRule {
     config: TrafficControlRule,    // original rule config
-- 
2.39.2





More information about the pbs-devel mailing list