Skip to content

Commit 928dd57

Browse files
committed
Replace system::time::Duration
1 parent 5bcad63 commit 928dd57

File tree

11 files changed

+67
-130
lines changed

11 files changed

+67
-130
lines changed

src/defaults/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,27 @@ defaults! {
7777
"PYTHONINSPECT", "PYTHONUSERBASE", "RUBYLIB", "RUBYOPT", "*=()*"] #ignored
7878
}
7979

80-
fn octal_mode(input: &str) -> Option<i64> {
80+
fn octal_mode(input: &str) -> Option<u64> {
8181
<libc::mode_t>::from_str_radix(input.strip_prefix('0')?, 8)
8282
.ok()
8383
.map(Into::into)
8484
}
8585

8686
/// A custom parser to parse seconds as fractional "minutes", the format used by
8787
/// passwd_timeout and timestamp_timeout.
88-
fn fractional_minutes(input: &str) -> Option<i64> {
88+
fn fractional_minutes(input: &str) -> Option<u64> {
8989
if let Some((integral, fractional)) = input.split_once('.') {
9090
// - 'input' is maximally 18 characters, making fractional.len() at most 17;
9191
// 1e17 < 2**63, so the definition of 'shift' will not overflow.
9292
// - for the same reason, if both parses in the definition of 'seconds' succeed,
9393
// we will have constructed an integer < 1e17.
9494
//- 1e17 * 60 = 6e18 < 9e18 < 2**63, so the final line also will not overflow
95-
let shift = 10i64.pow(fractional.len().try_into().ok()?);
96-
let seconds = integral.parse::<i64>().ok()? * shift + fractional.parse::<i64>().ok()?;
95+
let shift = 10u64.pow(fractional.len().try_into().ok()?);
96+
let seconds = integral.parse::<u64>().ok()? * shift + fractional.parse::<u64>().ok()?;
9797

9898
Some(seconds * 60 / shift)
9999
} else {
100-
input.parse::<i64>().ok()?.checked_mul(60)
100+
input.parse::<u64>().ok()?.checked_mul(60)
101101
}
102102
}
103103

src/defaults/settings_dsl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ macro_rules! storage_of {
22
($id:ident, true) => { bool };
33
($id:ident, false) => { bool };
44
($id:ident, [ $($value: expr),* ]) => { std::collections::HashSet<String> };
5-
($id:ident, $(=int $check: expr;)+ $_: expr) => { i64 };
5+
($id:ident, $(=int $check: expr;)+ $_: expr) => { u64 };
66
($id:ident, $(=enum $k: ident;)+ $_: ident) => { $crate::defaults::enums::$id };
77
($id:ident, None) => { Option<Box<str>> };
88
($id:ident, $_: expr) => { Box<str> };
@@ -12,7 +12,7 @@ macro_rules! referent_of {
1212
($id:ident, true) => { bool };
1313
($id:ident, false) => { bool };
1414
($id:ident, [ $($value: expr),* ]) => { &std::collections::HashSet<String> };
15-
($id:ident, $(=int $check: expr;)+ $_: expr) => { i64 };
15+
($id:ident, $(=int $check: expr;)+ $_: expr) => { u64 };
1616
($id:ident, $(=enum $k: ident;)+ $_: ident) => { $crate::defaults::enums::$id };
1717
($id:ident, None) => { Option<&str> };
1818
($id:ident, $_: expr) => { &str };
@@ -73,7 +73,7 @@ macro_rules! modifier_of {
7373
($id:ident, =int $first:literal ..= $last: literal $(@ $radix: literal)?; $value: expr) => {
7474
#[allow(clippy::from_str_radix_10)]
7575
$crate::defaults::SettingKind::Integer(|text| {
76-
i64::from_str_radix(text, 10$(*0 + $radix)?)
76+
u64::from_str_radix(text, 10$(*0 + $radix)?)
7777
.ok()
7878
.filter(|val| ($first ..= $last).contains(val))
7979
.map(|i| {

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: 8 additions & 8 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))]
@@ -38,11 +38,11 @@ impl super::Settings {
3838
Authentication {
3939
must_authenticate: tag.needs_passwd(),
4040
allowed_attempts: self.passwd_tries().try_into().unwrap(),
41-
prior_validity: Duration::seconds(self.timestamp_timeout()),
41+
prior_validity: Duration::from_secs(self.timestamp_timeout()),
4242
pwfeedback: self.pwfeedback(),
4343
password_timeout: match self.passwd_timeout() {
4444
0 => None,
45-
timeout => Some(Duration::seconds(timeout)),
45+
timeout => Some(Duration::from_secs(timeout)),
4646
},
4747
credential: if self.rootpw() {
4848
AuthenticatingUser::Root
@@ -190,10 +190,10 @@ mod test {
190190
Authentication {
191191
must_authenticate: true,
192192
allowed_attempts: 3,
193-
prior_validity: Duration::minutes(15),
193+
prior_validity: Duration::from_secs(15 * 60),
194194
credential: AuthenticatingUser::InvokingUser,
195195
pwfeedback: false,
196-
password_timeout: Some(Duration::seconds(300)),
196+
password_timeout: Some(Duration::from_secs(300)),
197197
},
198198
);
199199

@@ -207,10 +207,10 @@ mod test {
207207
Authentication {
208208
must_authenticate: false,
209209
allowed_attempts: 3,
210-
prior_validity: Duration::minutes(15),
210+
prior_validity: Duration::from_secs(15 * 60),
211211
credential: AuthenticatingUser::InvokingUser,
212212
pwfeedback: false,
213-
password_timeout: Some(Duration::seconds(300)),
213+
password_timeout: Some(Duration::from_secs(300)),
214214
},
215215
);
216216
assert_eq!(restrictions, restrictions2);

0 commit comments

Comments
 (0)