diff --git a/Cargo.toml b/Cargo.toml index a4b4297..dad253c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,9 +48,6 @@ lto = true [dependencies] bitflags = "^2" itertools = "^0.13" -# MSRV(1.70): Use OnceLock. -# MSRV(1.80): Use LazyLock. -lazy_static = "^1" libc = "^0.2" memchr = "^2" # MSRV(1.65): Update to >=0.4.1 which uses let_else. 0.4.0 was broken. diff --git a/src/capi/error.rs b/src/capi/error.rs index de658f6..8bfb79d 100644 --- a/src/capi/error.rs +++ b/src/capi/error.rs @@ -27,19 +27,15 @@ use std::{ error::Error as StdError, ffi::CString, ptr, - sync::Mutex, + sync::{LazyLock, Mutex}, }; use libc::{c_char, c_int}; use rand::{self, Rng}; -// TODO: Switch this to using a slab or similar structure, possibly using a less -// heavy-weight lock? Maybe sharded-slab? -// MSRV(1.70): Use OnceLock. -// MSRV(1.80): Use LazyLock. -lazy_static! { - static ref ERROR_MAP: Mutex> = Mutex::new(HashMap::new()); -} +// TODO: Switch this to using a slab or similar structure, possibly using a less heavy-weight lock? +static ERROR_MAP: LazyLock>> = + LazyLock::new(|| Mutex::new(HashMap::new())); pub(crate) fn store_error(err: Error) -> CReturn { let mut err_map = ERROR_MAP.lock().unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 006ffc7..a0042b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -153,8 +153,6 @@ #[macro_use] extern crate bitflags; -#[macro_use] -extern crate lazy_static; extern crate libc; // `Handle` implementation. diff --git a/src/procfs.rs b/src/procfs.rs index 89fb6d5..fe84ab9 100644 --- a/src/procfs.rs +++ b/src/procfs.rs @@ -34,19 +34,12 @@ use std::{ io::Error as IOError, os::unix::io::{AsFd, BorrowedFd, OwnedFd}, path::{Path, PathBuf}, + sync::LazyLock, }; -// MSRV(1.70): Use OnceLock. -// MSRV(1.80): Use LazyLock. -lazy_static! { - /// A lazy-allocated `procfs` handle which is used globally by libpathrs. - /// - /// As creating `procfs` handles can be somewhat expensive, library users - /// are recommended to make use of this handle for `procfs` operations if - /// possible. - pub static ref GLOBAL_PROCFS_HANDLE: ProcfsHandle = - ProcfsHandle::new().expect("should be able to get some /proc handle"); -} +/// A `procfs` handle to which is used globally by libpathrs. +pub(crate) static GLOBAL_PROCFS_HANDLE: LazyLock = + LazyLock::new(|| ProcfsHandle::new().expect("should be able to get some /proc handle")); /// Indicate what base directory should be used when doing `/proc/...` /// operations with a [`ProcfsHandle`]. diff --git a/src/resolvers/mod.rs b/src/resolvers/mod.rs index 60fc286..0185659 100644 --- a/src/resolvers/mod.rs +++ b/src/resolvers/mod.rs @@ -31,6 +31,7 @@ use std::{ os::unix::io::{AsFd, OwnedFd}, path::{Path, PathBuf}, rc::Rc, + sync::LazyLock, }; /// `O_PATH`-based userspace resolver. @@ -63,15 +64,13 @@ pub(crate) enum ResolverBackend { // hyper-concerned users. } -// MSRV(1.70): Use OnceLock. -// MSRV(1.80): Use LazyLock. -lazy_static! { - static ref DEFAULT_RESOLVER_TYPE: ResolverBackend = if *syscalls::OPENAT2_IS_SUPPORTED { +static DEFAULT_RESOLVER_TYPE: LazyLock = LazyLock::new(|| { + if *syscalls::OPENAT2_IS_SUPPORTED { ResolverBackend::KernelOpenat2 } else { ResolverBackend::EmulatedOpath - }; -} + } +}); impl Default for ResolverBackend { fn default() -> Self { diff --git a/src/resolvers/opath/impl.rs b/src/resolvers/opath/impl.rs index d99b226..9c63766 100644 --- a/src/resolvers/opath/impl.rs +++ b/src/resolvers/opath/impl.rs @@ -58,6 +58,7 @@ use std::{ }, path::{Path, PathBuf}, rc::Rc, + sync::LazyLock, }; use itertools::Itertools; @@ -130,17 +131,14 @@ fn check_current>( Ok(()) } -// MSRV(1.70): Use OnceLock. -// MSRV(1.80): Use LazyLock. -lazy_static! { - /// Cached copy of `fs.protected_symlinks` sysctl. - // TODO: In theory this value could change during the lifetime of the - // program, but there's no nice way of detecting that, and the overhead of - // checking this for every symlink lookup is more likely to be an issue. - static ref PROTECTED_SYMLINKS_SYSCTL: u32 = - utils::sysctl_read_parse(&GLOBAL_PROCFS_HANDLE, "fs.protected_symlinks") - .expect("should be able to parse fs.protected_symlinks"); -} +/// Cached copy of `fs.protected_symlinks` sysctl. +// TODO: In theory this value could change during the lifetime of the +// program, but there's no nice way of detecting that, and the overhead of +// checking this for every symlink lookup is more likely to be an issue. +static PROTECTED_SYMLINKS_SYSCTL: LazyLock = LazyLock::new(|| { + utils::sysctl_read_parse(&GLOBAL_PROCFS_HANDLE, "fs.protected_symlinks") + .expect("should be able to parse fs.protected_symlinks") +}); /// Verify that we should follow the symlink as per `fs.protected_symlinks`. /// diff --git a/src/syscalls.rs b/src/syscalls.rs index ac2e0fc..5e2e18e 100644 --- a/src/syscalls.rs +++ b/src/syscalls.rs @@ -35,6 +35,7 @@ use std::{ }, path::{Path, PathBuf}, ptr, + sync::LazyLock, }; use libc::{c_int, c_uint, dev_t, mode_t, stat, statfs}; @@ -52,13 +53,6 @@ pub(crate) const AT_FDCWD: BorrowedFd<'static> = unsafe { BorrowedFd::borrow_raw #[cfg(test)] pub(crate) const BADFD: BorrowedFd<'static> = unsafe { BorrowedFd::borrow_raw(-libc::EBADF) }; -// MSRV(1.70): Use OnceLock. -// MSRV(1.80): Use LazyLock. -lazy_static! { - pub(crate) static ref OPENAT2_IS_SUPPORTED: bool = - openat2(AT_FDCWD, ".", &Default::default()).is_ok(); -} - /// Representation of a file descriptor and its associated path at a given point /// in time. /// @@ -561,23 +555,13 @@ pub(crate) fn renameat, Fd2: AsFd, P2: AsRef>( } } -// MSRV(1.70): Use OnceLock. -// MSRV(1.80): Use LazyLock. -lazy_static! { - pub(crate) static ref RENAME_FLAGS_SUPPORTED: bool = { - match renameat2( - AT_FDCWD, - ".", - AT_FDCWD, - ".", - libc::RENAME_EXCHANGE, - ) { - Ok(_) => true, - // We expect EBUSY, but just to be safe we only check for ENOSYS. - Err(err) => err.root_cause().raw_os_error() != Some(libc::ENOSYS), - } - }; -} +pub(crate) static RENAME_FLAGS_SUPPORTED: LazyLock = LazyLock::new(|| { + match renameat2(AT_FDCWD, ".", AT_FDCWD, ".", libc::RENAME_EXCHANGE) { + Ok(_) => true, + // We expect EBUSY, but just to be safe we only check for ENOSYS. + Err(err) => err.root_cause().raw_os_error() != Some(libc::ENOSYS), + } +}); /// Wrapper for `renameat2(2)`. /// @@ -717,6 +701,9 @@ pub(crate) fn statx>( } } +pub(crate) static OPENAT2_IS_SUPPORTED: LazyLock = + LazyLock::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok()); + /// Arguments for how `openat2` should open the target path. // TODO: Maybe switch to libc::open_how? #[repr(C)]