Skip to content

Commit d343822

Browse files
committed
Improve UEFI fs
1. Implement copy 2. Fix copying cross device Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
1 parent 2345867 commit d343822

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

library/std/src/sys/uefi/fs.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,19 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
400400
let open_mode = file::MODE_READ | file::MODE_WRITE;
401401
let file = uefi_fs::FileProtocol::from_path(old, open_mode, 0)?;
402402

403-
// Delete if new already exists
404-
if let Ok(f) = uefi_fs::FileProtocol::from_path(new, open_mode, 0) {
405-
f.delete()?;
403+
// If tbe device prefix is same or both path are relative (in which case None will be
404+
// returned), then we can just use `set_file_name`.
405+
if super::path::device_prefix(old.as_os_str()) == super::path::device_prefix(new.as_os_str()) {
406+
// Delete if new already exists
407+
if let Ok(f) = uefi_fs::FileProtocol::from_path(new, open_mode, 0) {
408+
f.delete()?;
409+
}
410+
file.set_file_name(new.as_os_str())
411+
} else {
412+
// Use simple copy if the new path is in a different device.
413+
copy(old, new)?;
414+
file.delete()
406415
}
407-
408-
file.set_file_name(new.as_os_str())
409416
}
410417

411418
pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
@@ -475,9 +482,23 @@ pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
475482
unsupported()
476483
}
477484

478-
// FIXME: Find an efficient implementation
479-
pub fn copy(_from: &Path, _to: &Path) -> io::Result<u64> {
480-
unsupported()
485+
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
486+
let from_file = uefi_fs::FileProtocol::from_path(from, file::MODE_READ, 0)?;
487+
let to_file = uefi_fs::FileProtocol::from_path(
488+
to,
489+
file::MODE_READ | file::MODE_WRITE | file::MODE_CREATE,
490+
0,
491+
)?;
492+
// Truncate destination file.
493+
to_file.set_file_size(0)?;
494+
495+
let info = from_file.get_file_info()?;
496+
let file_size = unsafe { (*info.as_ptr()).file_size };
497+
let mut buffer = Vec::<u8>::with_capacity(file_size as usize);
498+
let mut buffer_size = buffer.capacity();
499+
from_file.read(&mut buffer, &mut buffer_size)?;
500+
unsafe { buffer.set_len(buffer_size) };
501+
Ok(to_file.write(&buffer)? as u64)
481502
}
482503

483504
// Liberal Cascade Delete

library/std/src/sys/uefi/os.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::os::uefi;
1010
use crate::path::{self, PathBuf};
1111
use crate::{error::Error as StdError, os::uefi::ffi::OsStringExt};
1212

13-
// Return EFI_ABORTED as Status
13+
// Return EFI_SUCCESS as Status
1414
pub fn errno() -> i32 {
1515
r_efi::efi::Status::SUCCESS.as_usize() as i32
1616
}
@@ -169,7 +169,7 @@ pub fn unsetenv(key: &OsStr) -> io::Result<()> {
169169
}
170170

171171
pub fn temp_dir() -> PathBuf {
172-
panic!("no filesystem on this platform")
172+
panic!("no supported on this platform")
173173
}
174174

175175
pub fn home_dir() -> Option<PathBuf> {

library/std/src/sys/uefi/time.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ impl From<r_efi::system::Time> for SystemTime {
123123

124124
// FIXME: Don't know how to use Daylight Saving thing
125125
fn uefi_time_to_duration(t: r_efi::system::Time) -> Duration {
126+
assert!(t.month <= 12);
127+
assert!(t.month != 0);
128+
126129
const MONTH_DAYS: [u64; 12] = [0, 31, 59, 90, 120, 151, 181, 211, 242, 272, 303, 333];
127130

128131
let localtime_epoch: u64 = u64::from(t.year - 1970) * SEC_IN_YEAR

library/std/src/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub use core::time::TryFromFloatSecsError;
114114
/// | SOLID | `get_tim` |
115115
/// | WASI | [__wasi_clock_time_get (Monotonic Clock)] |
116116
/// | Windows | [QueryPerformanceCounter] |
117-
/// | UEFI | EFI_RUNTIME_SERVICES->GetTime() |
117+
/// | UEFI | EFI_TIMESTAMP_PROTOCOL->GetTimestamp() |
118118
///
119119
/// [currently]: crate::io#platform-specific-behavior
120120
/// [QueryPerformanceCounter]: https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter

0 commit comments

Comments
 (0)