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

Use 64-bit time on 32-bit linux-gnu #96657

Merged
merged 4 commits into from
May 7, 2022
Merged
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
21 changes: 18 additions & 3 deletions library/std/src/os/linux/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,34 @@ impl MetadataExt for Metadata {
self.as_inner().as_inner().st_size as u64
}
fn st_atime(&self) -> i64 {
self.as_inner().as_inner().st_atime as i64
let file_attr = self.as_inner();
#[cfg(all(target_env = "gnu", target_pointer_width = "32"))]
if let Some(atime) = file_attr.stx_atime() {
return atime.tv_sec;
}
file_attr.as_inner().st_atime as i64
}
fn st_atime_nsec(&self) -> i64 {
self.as_inner().as_inner().st_atime_nsec as i64
}
fn st_mtime(&self) -> i64 {
self.as_inner().as_inner().st_mtime as i64
let file_attr = self.as_inner();
#[cfg(all(target_env = "gnu", target_pointer_width = "32"))]
if let Some(mtime) = file_attr.stx_mtime() {
return mtime.tv_sec;
}
file_attr.as_inner().st_mtime as i64
}
fn st_mtime_nsec(&self) -> i64 {
self.as_inner().as_inner().st_mtime_nsec as i64
}
fn st_ctime(&self) -> i64 {
self.as_inner().as_inner().st_ctime as i64
let file_attr = self.as_inner();
#[cfg(all(target_env = "gnu", target_pointer_width = "32"))]
if let Some(ctime) = file_attr.stx_ctime() {
return ctime.tv_sec;
}
file_attr.as_inner().st_ctime as i64
}
fn st_ctime_nsec(&self) -> i64 {
self.as_inner().as_inner().st_ctime_nsec as i64
Expand Down
109 changes: 71 additions & 38 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,19 @@ cfg_has_statx! {{
// This is needed to check if btime is supported by the filesystem.
stx_mask: u32,
stx_btime: libc::statx_timestamp,
// With statx, we can overcome 32-bit `time_t` too.
#[cfg(target_pointer_width = "32")]
stx_atime: libc::statx_timestamp,
#[cfg(target_pointer_width = "32")]
stx_ctime: libc::statx_timestamp,
#[cfg(target_pointer_width = "32")]
stx_mtime: libc::statx_timestamp,

}

// We prefer `statx` on Linux if available, which contains file creation time.
// Default `stat64` contains no creation time.
// We prefer `statx` on Linux if available, which contains file creation time,
// as well as 64-bit timestamps of all kinds.
// Default `stat64` contains no creation time and may have 32-bit `time_t`.
unsafe fn try_statx(
fd: c_int,
path: *const c_char,
Expand Down Expand Up @@ -192,6 +201,13 @@ cfg_has_statx! {{
let extra = StatxExtraFields {
stx_mask: buf.stx_mask,
stx_btime: buf.stx_btime,
// Store full times to avoid 32-bit `time_t` truncation.
#[cfg(target_pointer_width = "32")]
stx_atime: buf.stx_atime,
#[cfg(target_pointer_width = "32")]
stx_ctime: buf.stx_ctime,
#[cfg(target_pointer_width = "32")]
stx_mtime: buf.stx_mtime,
};

Some(Ok(FileAttr { stat, statx_extra_fields: Some(extra) }))
Expand Down Expand Up @@ -310,6 +326,36 @@ cfg_has_statx! {{
fn from_stat64(stat: stat64) -> Self {
Self { stat, statx_extra_fields: None }
}

#[cfg(target_pointer_width = "32")]
pub fn stx_mtime(&self) -> Option<&libc::statx_timestamp> {
if let Some(ext) = &self.statx_extra_fields {
if (ext.stx_mask & libc::STATX_MTIME) != 0 {
return Some(&ext.stx_mtime);
}
}
None
}

#[cfg(target_pointer_width = "32")]
pub fn stx_atime(&self) -> Option<&libc::statx_timestamp> {
if let Some(ext) = &self.statx_extra_fields {
if (ext.stx_mask & libc::STATX_ATIME) != 0 {
return Some(&ext.stx_atime);
}
}
None
}

#[cfg(target_pointer_width = "32")]
pub fn stx_ctime(&self) -> Option<&libc::statx_timestamp> {
if let Some(ext) = &self.statx_extra_fields {
if (ext.stx_mask & libc::STATX_CTIME) != 0 {
return Some(&ext.stx_ctime);
}
}
None
}
}
} else {
impl FileAttr {
Expand All @@ -335,59 +381,52 @@ impl FileAttr {
#[cfg(target_os = "netbsd")]
impl FileAttr {
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_mtime as libc::time_t,
tv_nsec: self.stat.st_mtimensec as libc::c_long,
}))
Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtimensec as i64))
}

pub fn accessed(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_atime as libc::time_t,
tv_nsec: self.stat.st_atimensec as libc::c_long,
}))
Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atimensec as i64))
}

pub fn created(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_birthtime as libc::time_t,
tv_nsec: self.stat.st_birthtimensec as libc::c_long,
}))
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtimensec as i64))
}
}

#[cfg(not(target_os = "netbsd"))]
impl FileAttr {
#[cfg(all(not(target_os = "vxworks"), not(target_os = "espidf")))]
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_mtime as libc::time_t,
tv_nsec: self.stat.st_mtime_nsec as _,
}))
#[cfg(target_pointer_width = "32")]
cfg_has_statx! {
if let Some(mtime) = self.stx_mtime() {
return Ok(SystemTime::new(mtime.tv_sec, mtime.tv_nsec as i64));
}
}

Ok(SystemTime::new(self.stat.st_mtime as i64, self.stat.st_mtime_nsec as i64))
}

#[cfg(any(target_os = "vxworks", target_os = "espidf"))]
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_mtime as libc::time_t,
tv_nsec: 0,
}))
Ok(SystemTime::new(self.stat.st_mtime as i64, 0))
}

#[cfg(all(not(target_os = "vxworks"), not(target_os = "espidf")))]
pub fn accessed(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_atime as libc::time_t,
tv_nsec: self.stat.st_atime_nsec as _,
}))
#[cfg(target_pointer_width = "32")]
cfg_has_statx! {
if let Some(atime) = self.stx_atime() {
return Ok(SystemTime::new(atime.tv_sec, atime.tv_nsec as i64));
}
}

Ok(SystemTime::new(self.stat.st_atime as i64, self.stat.st_atime_nsec as i64))
}

#[cfg(any(target_os = "vxworks", target_os = "espidf"))]
pub fn accessed(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_atime as libc::time_t,
tv_nsec: 0,
}))
Ok(SystemTime::new(self.stat.st_atime as i64, 0))
}

#[cfg(any(
Expand All @@ -397,10 +436,7 @@ impl FileAttr {
target_os = "ios"
))]
pub fn created(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_birthtime as libc::time_t,
tv_nsec: self.stat.st_birthtime_nsec as libc::c_long,
}))
Ok(SystemTime::new(self.stat.st_birthtime as i64, self.stat.st_birthtime_nsec as i64))
}

#[cfg(not(any(
Expand All @@ -413,10 +449,7 @@ impl FileAttr {
cfg_has_statx! {
if let Some(ext) = &self.statx_extra_fields {
return if (ext.stx_mask & libc::STATX_BTIME) != 0 {
Ok(SystemTime::from(libc::timespec {
tv_sec: ext.stx_btime.tv_sec as libc::time_t,
tv_nsec: ext.stx_btime.tv_nsec as _,
}))
Ok(SystemTime::new(ext.stx_btime.tv_sec, ext.stx_btime.tv_nsec as i64))
} else {
Err(io::const_io_error!(
io::ErrorKind::Uncategorized,
Expand Down
9 changes: 5 additions & 4 deletions library/std/src/sys/unix/futex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
// Calculate the timeout as an absolute timespec.
//
// Overflows are rounded up to an infinite timeout (None).
let timespec =
timeout.and_then(|d| Some(Timespec::now(libc::CLOCK_MONOTONIC).checked_add_duration(&d)?));
let timespec = timeout
.and_then(|d| Some(Timespec::now(libc::CLOCK_MONOTONIC).checked_add_duration(&d)?))
.and_then(|t| t.to_timespec());

loop {
// No need to wait if the value already changed.
Expand All @@ -41,7 +42,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
// identical. It supports absolute timeouts through a flag
// in the _umtx_time struct.
let umtx_timeout = timespec.map(|t| libc::_umtx_time {
_timeout: t.t,
_timeout: t,
_flags: libc::UMTX_ABSTIME,
_clockid: libc::CLOCK_MONOTONIC as u32,
});
Expand All @@ -62,7 +63,7 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
futex as *const AtomicU32,
libc::FUTEX_WAIT_BITSET | libc::FUTEX_PRIVATE_FLAG,
expected,
timespec.as_ref().map_or(null(), |t| &t.t as *const libc::timespec),
timespec.as_ref().map_or(null(), |t| t as *const libc::timespec),
null::<u32>(), // This argument is unused for FUTEX_WAIT_BITSET.
!0u32, // A full bitmask, to make it behave like a regular FUTEX_WAIT.
)
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sys/unix/thread_parker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ unsafe fn wait_timeout(
(Timespec::now(libc::CLOCK_MONOTONIC), dur)
};

let timeout = now.checked_add_duration(&dur).map(|t| t.t).unwrap_or(TIMESPEC_MAX);
let timeout =
now.checked_add_duration(&dur).and_then(|t| t.to_timespec()).unwrap_or(TIMESPEC_MAX);
let r = libc::pthread_cond_timedwait(cond, lock, &timeout);
debug_assert!(r == libc::ETIMEDOUT || r == 0);
}
Expand Down
Loading