Skip to content

Commit 4b3107f

Browse files
committed
Switch from lazy_static to once_cell.
1 parent 58d804b commit 4b3107f

File tree

6 files changed

+45
-58
lines changed

6 files changed

+45
-58
lines changed

cap-primitives/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ posish = "0.3.0"
2323
libc = "0.2.78"
2424

2525
[target.'cfg(target_os = "linux")'.dependencies]
26-
lazy_static = "1.4.0"
26+
once_cell = "1.4.1"
2727

2828
[target.'cfg(target_os = "macos")'.dependencies]
2929
errno = "0.2.6"

cap-primitives/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
html_favicon_url = "https://raw.githubusercontent.com/sunfishcode/cap-std/main/media/cap-std.ico"
1818
)]
1919

20-
#[cfg(target_os = "linux")]
21-
#[macro_use]
22-
extern crate lazy_static;
23-
2420
#[cfg(not(windows))]
2521
mod posish;
2622
#[cfg(windows)]

cap-primitives/src/posish/linux/fs/procfs.rs

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::fs::{
1111
errors, open, open_unchecked, readlink_unchecked, set_times_follow_unchecked, FollowSymlinks,
1212
Metadata, OpenOptions, SystemTimeSpec,
1313
};
14+
use once_cell::sync::Lazy;
1415
use posish::{
1516
fs::{chmodat, fstatfs, major, renameat, Mode},
1617
path::DecInt,
@@ -35,10 +36,6 @@ const PROC_SUPER_MAGIC: libc::__fsword_t = 0x0000_9fa0;
3536
#[cfg(target_env = "musl")]
3637
const PROC_SUPER_MAGIC: libc::c_ulong = 0x0000_9fa0;
3738

38-
lazy_static! {
39-
static ref PROC_SELF_FD: io::Result<fs::File> = init_proc_self_fd();
40-
}
41-
4239
// Identify a subdirectory of "/proc", to determine which anomalies to
4340
// check for.
4441
enum Subdir {
@@ -47,46 +44,6 @@ enum Subdir {
4744
Fd,
4845
}
4946

50-
/// Open a handle for "/proc/self/fd".
51-
#[allow(clippy::useless_conversion)]
52-
fn init_proc_self_fd() -> io::Result<fs::File> {
53-
// When libc does have this constant, check that our copy has the same value.
54-
#[cfg(not(target_env = "musl"))]
55-
assert_eq!(
56-
PROC_SUPER_MAGIC,
57-
libc::__fsword_t::from(libc::PROC_SUPER_MAGIC)
58-
);
59-
60-
// Open "/proc". Here and below, use `read(true)` even though we don't need
61-
// read permissions, because Rust's libstd requires an access mode, and
62-
// Linux ignores `O_RDONLY` with `O_PATH`.
63-
let proc = fs::OpenOptions::new()
64-
.read(true)
65-
.custom_flags(libc::O_PATH | libc::O_DIRECTORY | libc::O_NOFOLLOW)
66-
.open("/proc")?;
67-
let proc_metadata = check_proc_dir(Subdir::Proc, &proc, None, 0, 0)?;
68-
69-
let (uid, gid, pid) = (getuid(), getgid(), getpid());
70-
let mut options = OpenOptions::new();
71-
let options = options
72-
.read(true)
73-
.follow(FollowSymlinks::No)
74-
.custom_flags(libc::O_PATH | libc::O_DIRECTORY);
75-
76-
// Open "/proc/self". Use our pid to compute the name rather than literally
77-
// using "self", as "self" is a symlink.
78-
let proc_self = open_unchecked(&proc, &DecInt::new(pid), options)?;
79-
drop(proc);
80-
check_proc_dir(Subdir::Pid, &proc_self, Some(&proc_metadata), uid, gid)?;
81-
82-
// Open "/proc/self/fd".
83-
let proc_self_fd = open_unchecked(&proc_self, Path::new("fd"), options)?;
84-
drop(proc_self);
85-
check_proc_dir(Subdir::Fd, &proc_self_fd, Some(&proc_metadata), uid, gid)?;
86-
87-
Ok(proc_self_fd)
88-
}
89-
9047
/// Check a subdirectory of "/proc" for anomalies.
9148
fn check_proc_dir(
9249
kind: Subdir,
@@ -222,6 +179,45 @@ fn is_mountpoint(file: &fs::File) -> io::Result<bool> {
222179
}
223180

224181
fn proc_self_fd() -> io::Result<&'static fs::File> {
182+
#[allow(clippy::useless_conversion)]
183+
static PROC_SELF_FD: Lazy<io::Result<fs::File>> = Lazy::new(|| {
184+
// When libc does have this constant, check that our copy has the same value.
185+
#[cfg(not(target_env = "musl"))]
186+
assert_eq!(
187+
PROC_SUPER_MAGIC,
188+
libc::__fsword_t::from(libc::PROC_SUPER_MAGIC)
189+
);
190+
191+
// Open "/proc". Here and below, use `read(true)` even though we don't need
192+
// read permissions, because Rust's libstd requires an access mode, and
193+
// Linux ignores `O_RDONLY` with `O_PATH`.
194+
let proc = fs::OpenOptions::new()
195+
.read(true)
196+
.custom_flags(libc::O_PATH | libc::O_DIRECTORY | libc::O_NOFOLLOW)
197+
.open("/proc")?;
198+
let proc_metadata = check_proc_dir(Subdir::Proc, &proc, None, 0, 0)?;
199+
200+
let (uid, gid, pid) = (getuid(), getgid(), getpid());
201+
let mut options = OpenOptions::new();
202+
let options = options
203+
.read(true)
204+
.follow(FollowSymlinks::No)
205+
.custom_flags(libc::O_PATH | libc::O_DIRECTORY);
206+
207+
// Open "/proc/self". Use our pid to compute the name rather than literally
208+
// using "self", as "self" is a symlink.
209+
let proc_self = open_unchecked(&proc, &DecInt::new(pid), options)?;
210+
drop(proc);
211+
check_proc_dir(Subdir::Pid, &proc_self, Some(&proc_metadata), uid, gid)?;
212+
213+
// Open "/proc/self/fd".
214+
let proc_self_fd = open_unchecked(&proc_self, Path::new("fd"), options)?;
215+
drop(proc_self);
216+
check_proc_dir(Subdir::Fd, &proc_self_fd, Some(&proc_metadata), uid, gid)?;
217+
218+
Ok(proc_self_fd)
219+
});
220+
225221
PROC_SELF_FD.as_ref().map_err(|e| {
226222
io::Error::new(
227223
io::ErrorKind::Other,

cap-time-ext/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cap-std = { path = "../cap-std", optional = true, version = "^0.4.1-alpha.0"}
2121
posish = "0.3.0"
2222

2323
[target.'cfg(windows)'.dependencies]
24-
lazy_static = "1.4.0"
24+
once_cell = "1.4.1"
2525
winx = "0.20.0"
2626

2727
[badges]

cap-time-ext/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
html_favicon_url = "https://raw.githubusercontent.com/sunfishcode/cap-std/main/media/cap-std.ico"
99
)]
1010

11-
#[cfg(windows)]
12-
#[macro_use]
13-
extern crate lazy_static;
14-
1511
mod monotonic_clock;
1612
mod system_clock;
1713

cap-time-ext/src/monotonic_clock.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use posish::time::{clock_getres, ClockId};
33
use std::{convert::TryInto, time, time::Duration};
44
#[cfg(windows)]
5-
use winx::time::perf_counter_frequency;
5+
use {once_cell::sync::Lazy, winx::time::perf_counter_frequency};
66

77
/// Extension trait for `cap_std::time::MonotonicClock`.
88
pub trait MonotonicClockExt {
@@ -55,6 +55,5 @@ impl MonotonicClockExt for cap_primitives::time::MonotonicClock {
5555
}
5656

5757
#[cfg(windows)]
58-
lazy_static! {
59-
static ref PERF_COUNTER_RES: u64 = 1_000_000_000 / perf_counter_frequency().unwrap();
60-
}
58+
static PERF_COUNTER_RES: Lazy<u64> =
59+
Lazy::new(|| 1_000_000_000 / perf_counter_frequency().unwrap());

0 commit comments

Comments
 (0)