diff --git a/library/std/src/sys/fs/uefi.rs b/library/std/src/sys/fs/uefi.rs index bd4ae56974f0c..44f8b6e80f903 100644 --- a/library/std/src/sys/fs/uefi.rs +++ b/library/std/src/sys/fs/uefi.rs @@ -18,9 +18,8 @@ pub struct File(!); pub struct FileAttr { attr: u64, size: u64, - accessed: SystemTime, - modified: SystemTime, - created: SystemTime, + file_time: FileTimes, + created: Option, } pub struct ReadDir(!); @@ -66,15 +65,20 @@ impl FileAttr { } pub fn modified(&self) -> io::Result { - Ok(self.modified) + self.file_time + .modified + .ok_or(io::const_error!(io::ErrorKind::InvalidData, "modification time is not valid")) } pub fn accessed(&self) -> io::Result { - Ok(self.accessed) + self.file_time + .accessed + .ok_or(io::const_error!(io::ErrorKind::InvalidData, "last access time is not valid")) } pub fn created(&self) -> io::Result { - Ok(self.created) + self.created + .ok_or(io::const_error!(io::ErrorKind::InvalidData, "creation time is not valid")) } fn from_uefi(info: helpers::UefiBox) -> Self { @@ -82,8 +86,10 @@ impl FileAttr { Self { attr: (*info.as_ptr()).attribute, size: (*info.as_ptr()).file_size, - modified: uefi_fs::uefi_to_systemtime((*info.as_ptr()).modification_time), - accessed: uefi_fs::uefi_to_systemtime((*info.as_ptr()).last_access_time), + file_time: FileTimes { + modified: uefi_fs::uefi_to_systemtime((*info.as_ptr()).modification_time), + accessed: uefi_fs::uefi_to_systemtime((*info.as_ptr()).last_access_time), + }, created: uefi_fs::uefi_to_systemtime((*info.as_ptr()).create_time), } } @@ -627,9 +633,9 @@ mod uefi_fs { /// EDK2 FAT driver uses EFI_UNSPECIFIED_TIMEZONE to represent localtime. So for proper /// conversion to SystemTime, we use the current time to get the timezone in such cases. - pub(crate) fn uefi_to_systemtime(mut time: r_efi::efi::Time) -> SystemTime { + pub(crate) fn uefi_to_systemtime(mut time: r_efi::efi::Time) -> Option { time.timezone = if time.timezone == r_efi::efi::UNSPECIFIED_TIMEZONE { - time::system_time_internal::now().unwrap().timezone + time::system_time_internal::now().timezone } else { time.timezone }; @@ -639,7 +645,7 @@ mod uefi_fs { /// Convert to UEFI Time with the current timezone. #[expect(dead_code)] fn systemtime_to_uefi(time: SystemTime) -> r_efi::efi::Time { - let now = time::system_time_internal::now().unwrap(); + let now = time::system_time_internal::now(); time.to_uefi_loose(now.timezone, now.daylight) } } diff --git a/library/std/src/sys/pal/uefi/time.rs b/library/std/src/sys/pal/uefi/time.rs index f9f90a454976a..28dacbe3068a7 100644 --- a/library/std/src/sys/pal/uefi/time.rs +++ b/library/std/src/sys/pal/uefi/time.rs @@ -24,7 +24,8 @@ pub const UNIX_EPOCH: SystemTime = SystemTime::from_uefi(r_efi::efi::Time { daylight: 0, pad1: 0, pad2: 0, -}); +}) +.unwrap(); const MAX_UEFI_TIME: SystemTime = SystemTime::from_uefi(r_efi::efi::Time { year: 9999, @@ -38,7 +39,8 @@ const MAX_UEFI_TIME: SystemTime = SystemTime::from_uefi(r_efi::efi::Time { daylight: 0, pad1: 0, pad2: 0, -}); +}) +.unwrap(); impl Instant { pub fn now() -> Instant { @@ -68,8 +70,11 @@ impl Instant { } impl SystemTime { - pub(crate) const fn from_uefi(t: r_efi::efi::Time) -> Self { - Self(system_time_internal::from_uefi(&t)) + pub(crate) const fn from_uefi(t: r_efi::efi::Time) -> Option { + match system_time_internal::from_uefi(&t) { + Some(x) => Some(Self(x)), + None => None, + } } pub(crate) const fn to_uefi( @@ -96,9 +101,8 @@ impl SystemTime { } pub fn now() -> SystemTime { - system_time_internal::now() - .map(Self::from_uefi) - .unwrap_or_else(|| panic!("time not implemented on this platform")) + Self::from_uefi(system_time_internal::now()) + .expect("time incorrectly implemented on this platform") } pub fn sub_time(&self, other: &SystemTime) -> Result { @@ -129,17 +133,18 @@ pub(crate) mod system_time_internal { const SECS_IN_DAY: u64 = SECS_IN_HOUR * 24; const SYSTEMTIME_TIMEZONE: i64 = -1440 * SECS_IN_MINUTE as i64; - pub(crate) fn now() -> Option