Skip to content

Commit

Permalink
*: migrate from lazy_static! to std::sync::LazyLock
Browse files Browse the repository at this point in the history
This was only stabilised in Rust 1.80.0, so this might cause issues when
building on older distributions (depending on when libpathrs starts
getting packaged).

Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed Oct 8, 2024
1 parent 714cd27 commit 7f8a83c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 65 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 4 additions & 8 deletions src/capi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<CReturn, Error>> = 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<Mutex<HashMap<CReturn, Error>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));

pub(crate) fn store_error(err: Error) -> CReturn {
let mut err_map = ERROR_MAP.lock().unwrap();
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@

#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate lazy_static;
extern crate libc;

// `Handle` implementation.
Expand Down
15 changes: 4 additions & 11 deletions src/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcfsHandle> =
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`].
Expand Down
11 changes: 5 additions & 6 deletions src/resolvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::{
os::unix::io::{AsFd, OwnedFd},
path::{Path, PathBuf},
rc::Rc,
sync::LazyLock,
};

/// `O_PATH`-based userspace resolver.
Expand Down Expand Up @@ -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<ResolverBackend> = LazyLock::new(|| {
if *syscalls::OPENAT2_IS_SUPPORTED {
ResolverBackend::KernelOpenat2
} else {
ResolverBackend::EmulatedOpath
};
}
}
});

impl Default for ResolverBackend {
fn default() -> Self {
Expand Down
20 changes: 9 additions & 11 deletions src/resolvers/opath/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use std::{
},
path::{Path, PathBuf},
rc::Rc,
sync::LazyLock,
};

use itertools::Itertools;
Expand Down Expand Up @@ -130,17 +131,14 @@ fn check_current<RootFd: AsFd, Fd: AsFd, P: AsRef<Path>>(
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<u32> = 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`.
///
Expand Down
35 changes: 11 additions & 24 deletions src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use std::{
},
path::{Path, PathBuf},
ptr,
sync::LazyLock,
};

use libc::{c_int, c_uint, dev_t, mode_t, stat, statfs};
Expand All @@ -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.
///
Expand Down Expand Up @@ -561,23 +555,13 @@ pub(crate) fn renameat<Fd1: AsFd, P1: AsRef<Path>, Fd2: AsFd, P2: AsRef<Path>>(
}
}

// 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<bool> = 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)`.
///
Expand Down Expand Up @@ -717,6 +701,9 @@ pub(crate) fn statx<Fd: AsFd, P: AsRef<Path>>(
}
}

pub(crate) static OPENAT2_IS_SUPPORTED: LazyLock<bool> =
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)]
Expand Down

0 comments on commit 7f8a83c

Please sign in to comment.