Skip to content

Commit 562b268

Browse files
committed
Replace system::time::Duration
1 parent a8b1fc8 commit 562b268

File tree

9 files changed

+71
-123
lines changed

9 files changed

+71
-123
lines changed

src/pam/converse.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::io;
1+
use std::{io, time::Duration};
22

33
use crate::cutils::string_from_ptr;
4-
use crate::system::time::Duration;
54

65
use super::sys::*;
76

src/pam/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use std::{
44
os::raw::c_char,
55
os::unix::prelude::OsStrExt,
66
ptr::NonNull,
7+
time::Duration,
78
};
89

9-
use crate::system::time::Duration;
10-
1110
use converse::ConverserData;
1211
use error::pam_err;
1312
pub use error::{PamError, PamErrorType, PamResult};

src/pam/rpassword.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
///
1616
use std::io::{self, Error, ErrorKind, Read};
1717
use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
18-
use std::time::Instant;
18+
use std::time::{Duration, Instant};
1919
use std::{fs, mem};
2020

2121
use libc::{tcsetattr, termios, ECHO, ECHONL, ICANON, TCSANOW, VEOF, VERASE, VKILL};
2222

2323
use crate::cutils::cerr;
24-
use crate::system::time::Duration;
2524

2625
use super::securemem::PamBuffer;
2726

@@ -187,7 +186,7 @@ struct TimeoutRead<'a> {
187186
impl<'a> TimeoutRead<'a> {
188187
fn new(fd: BorrowedFd<'a>, timeout: Option<Duration>) -> TimeoutRead<'a> {
189188
TimeoutRead {
190-
timeout_at: timeout.map(|timeout| Instant::now() + timeout.into()),
189+
timeout_at: timeout.map(|timeout| Instant::now() + timeout),
191190
fd,
192191
}
193192
}

src/sudo/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use crate::log::dev_info;
66
use crate::system::interface::UserId;
77
use crate::system::timestamp::RecordScope;
88
use crate::system::User;
9-
use crate::system::{time::Duration, timestamp::SessionRecordFile, Process};
9+
use crate::system::{timestamp::SessionRecordFile, Process};
1010
#[cfg(test)]
1111
pub(crate) use cli::SudoAction;
1212
#[cfg(not(test))]
1313
use cli::SudoAction;
14-
use std::path::PathBuf;
14+
use std::{path::PathBuf, time::Duration};
1515

1616
mod cli;
1717
pub(crate) use cli::{SudoEditOptions, SudoListOptions, SudoRunOptions, SudoValidateOptions};
@@ -91,16 +91,15 @@ fn sudo_process() -> Result<(), Error> {
9191
}
9292
SudoAction::RemoveTimestamp(_) => {
9393
let user = CurrentUser::resolve()?;
94-
let mut record_file =
95-
SessionRecordFile::open_for_user(&user, Duration::seconds(0))?;
94+
let mut record_file = SessionRecordFile::open_for_user(&user, Duration::default())?;
9695
record_file.reset()?;
9796
Ok(())
9897
}
9998
SudoAction::ResetTimestamp(_) => {
10099
if let Some(scope) = RecordScope::for_process(&Process::new()) {
101100
let user = CurrentUser::resolve()?;
102101
let mut record_file =
103-
SessionRecordFile::open_for_user(&user, Duration::seconds(0))?;
102+
SessionRecordFile::open_for_user(&user, Duration::default())?;
104103
record_file.disable(scope, None)?;
105104
}
106105
Ok(())

src/sudo/pam.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::ffi::OsString;
1+
use std::{ffi::OsString, time::Duration};
22

33
use crate::common::context::LaunchType;
44
use crate::common::error::Error;
55
use crate::log::{dev_info, user_warn};
66
use crate::pam::{PamContext, PamError, PamErrorType, PamResult};
7-
use crate::system::{term::current_tty_name, time::Duration};
7+
use crate::system::term::current_tty_name;
88

99
pub(super) struct InitPamArgs<'a> {
1010
pub(super) launch: LaunchType,

src/sudo/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::ffi::OsStr;
22
use std::process::exit;
3+
use std::time::Duration;
34

45
use super::cli::{SudoRunOptions, SudoValidateOptions};
56
use super::diagnostic;
@@ -10,7 +11,6 @@ use crate::log::{auth_info, auth_warn};
1011
use crate::pam::PamContext;
1112
use crate::sudo::env::environment;
1213
use crate::sudo::pam::{attempt_authenticate, init_pam, pre_exec, InitPamArgs};
13-
use crate::sudo::Duration;
1414
use crate::sudoers::{
1515
AuthenticatingUser, Authentication, Authorization, DirChange, Judgement, Restrictions, Sudoers,
1616
};

src/sudoers/policy.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use crate::common::{
66
};
77
use crate::exec::Umask;
88
use crate::sudoers::ast::{ExecControl, Tag};
9-
use crate::system::{time::Duration, Hostname, User};
9+
use crate::system::{Hostname, User};
1010
/// Data types and traits that represent what the "terms and conditions" are after a successful
1111
/// permission check.
1212
///
1313
/// The trait definitions can be part of some global crate in the future, if we support more
1414
/// than just the sudoers file.
15-
use std::collections::HashSet;
15+
use std::{collections::HashSet, time::Duration};
1616

1717
#[must_use]
1818
#[cfg_attr(test, derive(Debug, PartialEq))]
@@ -35,14 +35,25 @@ pub struct Authentication {
3535

3636
impl super::Settings {
3737
pub(super) fn to_auth(&self, tag: &Tag) -> Authentication {
38+
let default_passwd_timeout = |_| -> u64 {
39+
super::Settings::default()
40+
.passwd_timeout()
41+
.try_into()
42+
.unwrap()
43+
};
44+
3845
Authentication {
3946
must_authenticate: tag.needs_passwd(),
4047
allowed_attempts: self.passwd_tries().try_into().unwrap(),
41-
prior_validity: Duration::seconds(self.timestamp_timeout()),
48+
prior_validity: Duration::from_secs(self.timestamp_timeout().try_into().unwrap()),
4249
pwfeedback: self.pwfeedback(),
43-
password_timeout: match self.passwd_timeout() {
50+
password_timeout: match self
51+
.passwd_timeout()
52+
.try_into()
53+
.unwrap_or_else(default_passwd_timeout)
54+
{
4455
0 => None,
45-
timeout => Some(Duration::seconds(timeout)),
56+
timeout => Some(Duration::from_secs(timeout)),
4657
},
4758
credential: if self.rootpw() {
4859
AuthenticatingUser::Root
@@ -190,10 +201,10 @@ mod test {
190201
Authentication {
191202
must_authenticate: true,
192203
allowed_attempts: 3,
193-
prior_validity: Duration::minutes(15),
204+
prior_validity: Duration::from_secs(15 * 60),
194205
credential: AuthenticatingUser::InvokingUser,
195206
pwfeedback: false,
196-
password_timeout: Some(Duration::seconds(300)),
207+
password_timeout: Some(Duration::from_secs(300)),
197208
},
198209
);
199210

@@ -207,10 +218,10 @@ mod test {
207218
Authentication {
208219
must_authenticate: false,
209220
allowed_attempts: 3,
210-
prior_validity: Duration::minutes(15),
221+
prior_validity: Duration::from_secs(15 * 60),
211222
credential: AuthenticatingUser::InvokingUser,
212223
pwfeedback: false,
213-
password_timeout: Some(Duration::seconds(300)),
224+
password_timeout: Some(Duration::from_secs(300)),
214225
},
215226
);
216227
assert_eq!(restrictions, restrictions2);

src/system/time.rs

Lines changed: 34 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
io::{Read, Write},
33
mem::MaybeUninit,
44
ops::{Add, Sub},
5+
time::Duration,
56
};
67

78
/// A timestamp relative to `CLOCK_BOOTTIME`.
@@ -51,85 +52,47 @@ impl SystemTime {
5152
i64::from_ne_bytes(nsec_bytes),
5253
))
5354
}
54-
}
55-
56-
impl Sub<SystemTime> for SystemTime {
57-
type Output = Duration;
58-
59-
fn sub(self, rhs: SystemTime) -> Self::Output {
60-
Duration::new(self.secs - rhs.secs, self.nsecs - rhs.nsecs)
61-
}
62-
}
63-
64-
impl Add<Duration> for SystemTime {
65-
type Output = SystemTime;
66-
67-
fn add(self, rhs: Duration) -> Self::Output {
68-
SystemTime::new(self.secs + rhs.secs, self.nsecs + rhs.nsecs)
69-
}
70-
}
71-
72-
impl Sub<Duration> for SystemTime {
73-
type Output = SystemTime;
7455

75-
fn sub(self, rhs: Duration) -> Self::Output {
76-
SystemTime::new(self.secs - rhs.secs, self.nsecs - rhs.nsecs)
77-
}
78-
}
56+
#[inline]
57+
pub fn checked_add(self, rhs: Duration) -> Option<SystemTime> {
58+
let rhs_secs = rhs.as_nanos().div_euclid(1_000_000_000).try_into().ok()?;
59+
let rhs_nsecs = rhs.as_nanos().rem_euclid(1_000_000_000).try_into().ok()?;
7960

80-
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
81-
pub struct Duration {
82-
secs: i64,
83-
nsecs: i64,
84-
}
61+
let secs = self.secs.checked_add(rhs_secs)?;
62+
let nsecs = self.nsecs.checked_add(rhs_nsecs)?;
8563

86-
impl Duration {
87-
pub fn new(secs: i64, nsecs: i64) -> Duration {
88-
Duration {
89-
secs: secs + nsecs.div_euclid(1_000_000_000),
90-
nsecs: nsecs.rem_euclid(1_000_000_000),
91-
}
64+
Some(SystemTime::new(secs, nsecs))
9265
}
9366

94-
pub fn seconds(secs: i64) -> Duration {
95-
Duration::new(secs, 0)
96-
}
67+
#[inline]
68+
pub fn checked_sub(self, rhs: Duration) -> Option<SystemTime> {
69+
let rhs_secs = rhs.as_nanos().div_euclid(1_000_000_000).try_into().ok()?;
70+
let rhs_nsecs = rhs.as_nanos().rem_euclid(1_000_000_000).try_into().ok()?;
9771

98-
#[cfg(test)]
99-
pub fn minutes(minutes: i64) -> Duration {
100-
Duration::seconds(minutes * 60)
101-
}
72+
let secs = self.secs.checked_sub(rhs_secs)?;
73+
let nsecs = self.nsecs.checked_sub(rhs_nsecs)?;
10274

103-
#[cfg(test)]
104-
pub fn milliseconds(ms: i64) -> Duration {
105-
let secs = ms / 1000;
106-
let ms = ms % 1000;
107-
Duration::new(secs, ms * 1_000_000)
75+
Some(SystemTime::new(secs, nsecs))
10876
}
10977
}
11078

111-
impl Add<Duration> for Duration {
112-
type Output = Duration;
79+
impl Add<Duration> for SystemTime {
80+
type Output = SystemTime;
11381

82+
#[inline]
11483
fn add(self, rhs: Duration) -> Self::Output {
115-
Duration::new(self.secs + rhs.secs, self.nsecs + rhs.nsecs)
84+
self.checked_add(rhs)
85+
.expect("overflow when adding duration")
11686
}
11787
}
11888

119-
impl Sub<Duration> for Duration {
120-
type Output = Duration;
89+
impl Sub<Duration> for SystemTime {
90+
type Output = SystemTime;
12191

92+
#[inline]
12293
fn sub(self, rhs: Duration) -> Self::Output {
123-
Duration::new(self.secs - rhs.secs, self.nsecs - rhs.nsecs)
124-
}
125-
}
126-
127-
impl From<Duration> for std::time::Duration {
128-
fn from(dur: Duration) -> std::time::Duration {
129-
std::time::Duration::new(
130-
dur.secs.try_into().unwrap_or(0),
131-
dur.nsecs.try_into().unwrap_or(0),
132-
)
94+
self.checked_sub(rhs)
95+
.expect("overflow when subtracting duration")
13396
}
13497
}
13598

@@ -213,13 +176,7 @@ mod tests {
213176
use super::*;
214177

215178
#[test]
216-
fn test_new_durations_and_times() {
217-
assert_eq!(Duration::new(1, 1_000_000_000), Duration::seconds(2));
218-
assert_eq!(
219-
Duration::new(-2, 500_000_000),
220-
Duration::seconds(-1) + Duration::milliseconds(-500)
221-
);
222-
179+
fn test_new_system_time() {
223180
assert_eq!(SystemTime::new(-1, 2_000_000_000), SystemTime::new(1, 0));
224181
assert_eq!(
225182
SystemTime::new(2, -500_000_000),
@@ -230,37 +187,20 @@ mod tests {
230187
#[test]
231188
fn test_time_ops() {
232189
assert_eq!(
233-
Duration::seconds(2) + Duration::seconds(3),
234-
Duration::seconds(5)
235-
);
236-
assert_eq!(
237-
Duration::seconds(3) - Duration::seconds(1),
238-
Duration::seconds(2)
239-
);
240-
assert_eq!(
241-
Duration::seconds(-10) + Duration::seconds(-5),
242-
Duration::seconds(-15)
243-
);
244-
assert_eq!(
245-
Duration::milliseconds(5555) + Duration::milliseconds(5555),
246-
Duration::seconds(11) + Duration::milliseconds(110)
247-
);
248-
assert_eq!(
249-
Duration::milliseconds(-5555) + Duration::milliseconds(-1111),
250-
Duration::milliseconds(-6666)
251-
);
252-
assert_eq!(
253-
Duration::seconds(10) - Duration::seconds(-5),
254-
Duration::seconds(15)
190+
SystemTime::new(0, 0) + Duration::from_secs(3),
191+
SystemTime::new(3, 0)
255192
);
256-
257193
assert_eq!(
258-
SystemTime::new(0, 0) + Duration::seconds(3),
194+
SystemTime::new(0, 500_000_000) + Duration::from_nanos(2_500_000_000),
259195
SystemTime::new(3, 0)
260196
);
261197
assert_eq!(
262-
SystemTime::new(10, 0) - Duration::seconds(4),
198+
SystemTime::new(10, 0) - Duration::from_secs(4),
263199
SystemTime::new(6, 0)
264200
);
201+
assert_eq!(
202+
SystemTime::new(10, 0) - Duration::from_nanos(3_500_000_000),
203+
SystemTime::new(6, 500_000_000)
204+
);
265205
}
266206
}

0 commit comments

Comments
 (0)