Skip to content

Commit 8b45676

Browse files
committed
Replace system::time::Duration
1 parent f68e342 commit 8b45676

File tree

11 files changed

+67
-128
lines changed

11 files changed

+67
-128
lines changed

src/defaults/mod.rs

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

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

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

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

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::ffi::{c_int, c_void};
22
use std::io;
3+
use std::time::Duration;
34

45
use crate::cutils::string_from_ptr;
56
use crate::pam::rpassword::Hidden;
67
use crate::system::{
78
signal::{self, SignalSet},
8-
time::Duration,
99
};
1010

1111
use super::sys::*;

src/pam/mod.rs

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

910
use crate::system::{
1011
signal::{self, SignalSet},
11-
time::Duration,
1212
};
1313

1414
use converse::ConverserData;

src/pam/rpassword.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
use std::ffi::c_void;
1717
use std::io::{self, Error, ErrorKind, Read};
1818
use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
19-
use std::time::Instant;
19+
use std::time::{Duration, Instant};
2020
use std::{fs, mem};
2121

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

2424
use crate::cutils::{cerr, safe_isatty};
25-
use crate::system::time::Duration;
2625

2726
use super::securemem::PamBuffer;
2827

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

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};
@@ -90,16 +90,15 @@ fn sudo_process() -> Result<(), Error> {
9090
}
9191
SudoAction::RemoveTimestamp(_) => {
9292
let user = CurrentUser::resolve()?;
93-
let mut record_file =
94-
SessionRecordFile::open_for_user(&user, Duration::seconds(0))?;
93+
let mut record_file = SessionRecordFile::open_for_user(&user, Duration::default())?;
9594
record_file.reset()?;
9695
Ok(())
9796
}
9897
SudoAction::ResetTimestamp(_) => {
9998
if let Some(scope) = RecordScope::for_process(&Process::new()) {
10099
let user = CurrentUser::resolve()?;
101100
let mut record_file =
102-
SessionRecordFile::open_for_user(&user, Duration::seconds(0))?;
101+
SessionRecordFile::open_for_user(&user, Duration::default())?;
103102
record_file.disable(scope)?;
104103
}
105104
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
@@ -191,10 +191,10 @@ mod test {
191191
Authentication {
192192
must_authenticate: true,
193193
allowed_attempts: 3,
194-
prior_validity: Duration::minutes(15),
194+
prior_validity: Duration::from_secs(15 * 60),
195195
credential: AuthenticatingUser::InvokingUser,
196196
pwfeedback: false,
197-
password_timeout: Some(Duration::seconds(300)),
197+
password_timeout: Some(Duration::from_secs(300)),
198198
},
199199
);
200200

@@ -208,10 +208,10 @@ mod test {
208208
Authentication {
209209
must_authenticate: false,
210210
allowed_attempts: 3,
211-
prior_validity: Duration::minutes(15),
211+
prior_validity: Duration::from_secs(15 * 60),
212212
credential: AuthenticatingUser::InvokingUser,
213213
pwfeedback: false,
214-
password_timeout: Some(Duration::seconds(300)),
214+
password_timeout: Some(Duration::from_secs(300)),
215215
},
216216
);
217217
assert_eq!(restrictions, restrictions2);

0 commit comments

Comments
 (0)