Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: migrate from once_cell to std::sync::LazyLock #49

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ on:
name: rust-ci

env:
RUST_MSRV: "1.63"
RUST_MSRV: "1.80"

jobs:
codespell:
Expand Down
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ readme = "README.md"
keywords = ["file", "fs", "security", "linux"]
categories = ["filesystem"]
edition = "2021"
rust-version = "1.63"
rust-version = "1.80"

[badges]
maintenance = { status = "experimental" }
Expand All @@ -50,8 +50,6 @@ bitflags = "^2"
itertools = "^0.13"
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 }
rand = { version = "^0.8", optional = true }
Expand Down
7 changes: 3 additions & 4 deletions src/capi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ use std::{
error::Error as StdError,
ffi::CString,
ptr,
sync::Mutex,
sync::{LazyLock, Mutex},
};

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?
// MSRV(1.80): Use LazyLock.
static ERROR_MAP: Lazy<Mutex<HashMap<CReturn, Error>>> = Lazy::new(|| Mutex::new(HashMap::new()));
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
7 changes: 3 additions & 4 deletions src/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ use std::{
io::{AsFd, BorrowedFd, OwnedFd},
},
path::{Path, PathBuf},
sync::LazyLock,
};

use once_cell::sync::Lazy;
use rustix::{
fs::{self as rustix_fs, Access, AtFlags},
mount::{FsMountFlags, FsOpenFlags, MountAttrFlags, OpenTreeFlags},
};

/// A `procfs` handle to which is used globally by libpathrs.
// MSRV(1.80): Use LazyLock.
pub(crate) static GLOBAL_PROCFS_HANDLE: Lazy<ProcfsHandle> =
Lazy::new(|| ProcfsHandle::new().expect("should be able to get some /proc handle"));
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
6 changes: 2 additions & 4 deletions src/resolvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ use std::{
os::unix::io::{AsFd, OwnedFd},
path::{Path, PathBuf},
rc::Rc,
sync::LazyLock,
};

use once_cell::sync::Lazy;

/// `O_PATH`-based userspace resolver.
pub(crate) mod opath;
/// `openat2(2)`-based in-kernel resolver.
Expand Down Expand Up @@ -65,8 +64,7 @@ pub(crate) enum ResolverBackend {
// hyper-concerned users.
}

// MSRV(1.80): Use LazyLock.
static DEFAULT_RESOLVER_TYPE: Lazy<ResolverBackend> = Lazy::new(|| {
static DEFAULT_RESOLVER_TYPE: LazyLock<ResolverBackend> = LazyLock::new(|| {
if *syscalls::OPENAT2_IS_SUPPORTED {
ResolverBackend::KernelOpenat2
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers/opath/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ use std::{
},
path::{Path, PathBuf},
rc::Rc,
sync::LazyLock,
};

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 @@ -135,8 +135,7 @@ fn check_current<RootFd: AsFd, Fd: AsFd, P: AsRef<Path>>(
// 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.
static PROTECTED_SYMLINKS_SYSCTL: Lazy<u32> = Lazy::new(|| {
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")
});
Expand Down
10 changes: 4 additions & 6 deletions src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use std::{
io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd},
},
path::{Path, PathBuf},
sync::LazyLock,
};

use once_cell::sync::Lazy;
use rustix::{
fs::{
self as rustix_fs, AtFlags, Dev, FileType, Mode, RawMode, Stat, StatFs, Statx, StatxFlags,
Expand Down Expand Up @@ -524,8 +524,7 @@ pub(crate) fn renameat<Fd1: AsFd, P1: AsRef<Path>, Fd2: AsFd, P2: AsRef<Path>>(
})
}

// MSRV(1.80): Use LazyLock.
pub(crate) static RENAME_FLAGS_SUPPORTED: Lazy<bool> = Lazy::new(|| {
pub(crate) static RENAME_FLAGS_SUPPORTED: LazyLock<bool> = LazyLock::new(|| {
match renameat2(AT_FDCWD, ".", AT_FDCWD, ".", RenameFlags::RENAME_EXCHANGE) {
Ok(_) => true,
// We expect EBUSY, but just to be safe we only check for ENOSYS.
Expand Down Expand Up @@ -611,9 +610,8 @@ 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());
pub(crate) static OPENAT2_IS_SUPPORTED: LazyLock<bool> =
LazyLock::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok());

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