Skip to content

Commit

Permalink
merge #89 into openSUSE/libpathrs:main
Browse files Browse the repository at this point in the history
Aleksa Sarai (1):
  *: migrate from lazy_static! to once_cell::sync::Lazy

LGTMs: cyphar
  • Loading branch information
cyphar committed Oct 14, 2024
2 parents a856dbb + aaec3d6 commit 4baab3d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 60 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ 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.80): Use LazyLock.
once_cell = "^1"
# MSRV(1.65): Update to >=0.4.1 which uses let_else. 0.4.0 was broken.
open-enum = {version = "=0.3.0", optional = true }
open-enum = { version = "=0.3.0", optional = true }
rand = { version = "^0.8", optional = true }
rustix = { version = "^0.38", features = ["fs"] }
thiserror = "^1"
Expand Down
9 changes: 3 additions & 6 deletions src/capi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ use std::{
};

use libc::{c_char, c_int};
use once_cell::sync::Lazy;
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.
// TODO: Switch this to using a slab or similar structure, possibly using a less heavy-weight lock?
// MSRV(1.80): Use LazyLock.
lazy_static! {
static ref ERROR_MAP: Mutex<HashMap<CReturn, Error>> = Mutex::new(HashMap::new());
}
static ERROR_MAP: Lazy<Mutex<HashMap<CReturn, Error>>> = Lazy::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: 5 additions & 10 deletions src/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,12 @@ use std::{
path::{Path, PathBuf},
};

// MSRV(1.70): Use OnceLock.
use once_cell::sync::Lazy;

/// A `procfs` handle to which is used globally by libpathrs.
// 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");
}
pub(crate) static GLOBAL_PROCFS_HANDLE: Lazy<ProcfsHandle> =
Lazy::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: 6 additions & 5 deletions src/resolvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use std::{
rc::Rc,
};

use once_cell::sync::Lazy;

/// `O_PATH`-based userspace resolver.
pub(crate) mod opath;
/// `openat2(2)`-based in-kernel resolver.
Expand Down Expand Up @@ -63,15 +65,14 @@ 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: Lazy<ResolverBackend> = Lazy::new(|| {
if *syscalls::OPENAT2_IS_SUPPORTED {
ResolverBackend::KernelOpenat2
} else {
ResolverBackend::EmulatedOpath
};
}
}
});

impl Default for ResolverBackend {
fn default() -> Self {
Expand Down
19 changes: 9 additions & 10 deletions src/resolvers/opath/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use std::{
};

use itertools::Itertools;
use once_cell::sync::Lazy;

/// Ensure that the expected path within the root matches the current fd.
fn check_current<RootFd: AsFd, Fd: AsFd, P: AsRef<Path>>(
Expand Down Expand Up @@ -130,17 +131,15 @@ fn check_current<RootFd: AsFd, Fd: AsFd, P: AsRef<Path>>(
Ok(())
}

// MSRV(1.70): Use OnceLock.
/// 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.
// 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");
}
static PROTECTED_SYMLINKS_SYSCTL: Lazy<u32> = Lazy::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: 12 additions & 23 deletions src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use std::{
};

use libc::{c_int, c_uint, dev_t, mode_t, stat, statfs};
use once_cell::sync::Lazy;

// TODO: Figure out how we can put a backtrace here (it seems we can't use
// thiserror's backtrace support without nightly Rust because thiserror
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,14 @@ 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: Lazy<bool> = Lazy::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 +702,10 @@ pub(crate) fn statx<Fd: AsFd, P: AsRef<Path>>(
}
}

// MSRV(1.80): Use LazyLock.
pub(crate) static OPENAT2_IS_SUPPORTED: Lazy<bool> =
Lazy::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok());

bitflags! {
/// Wrapper for the underlying `libc`'s `RESOLVE_*` flags.
///
Expand Down

0 comments on commit 4baab3d

Please sign in to comment.