Skip to content

Commit 5b0fded

Browse files
committed
std: sys: fs: uefi: Implement rmdir and unlink
- Implement remove_dir and remove_file. - Tested on qemu ovmf. Signed-off-by: Ayush Singh <[email protected]>
1 parent 3c5c558 commit 5b0fded

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,16 @@ pub fn readdir(_p: &Path) -> io::Result<ReadDir> {
344344
unsupported()
345345
}
346346

347-
pub fn unlink(_p: &Path) -> io::Result<()> {
348-
unsupported()
347+
pub fn unlink(p: &Path) -> io::Result<()> {
348+
let f = uefi_fs::File::from_path(p, file::MODE_READ | file::MODE_WRITE, 0)?;
349+
let file_info = f.file_info()?;
350+
let file_attr = FileAttr::from_uefi(file_info);
351+
352+
if file_attr.file_type().is_file() {
353+
f.delete()
354+
} else {
355+
Err(io::const_error!(io::ErrorKind::IsADirectory, "expected a file but got a directory"))
356+
}
349357
}
350358

351359
pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
@@ -364,8 +372,16 @@ pub fn set_times_nofollow(_p: &Path, _times: FileTimes) -> io::Result<()> {
364372
unsupported()
365373
}
366374

367-
pub fn rmdir(_p: &Path) -> io::Result<()> {
368-
unsupported()
375+
pub fn rmdir(p: &Path) -> io::Result<()> {
376+
let f = uefi_fs::File::from_path(p, file::MODE_READ | file::MODE_WRITE, 0)?;
377+
let file_info = f.file_info()?;
378+
let file_attr = FileAttr::from_uefi(file_info);
379+
380+
if file_attr.file_type().is_dir() {
381+
f.delete()
382+
} else {
383+
Err(io::const_error!(io::ErrorKind::NotADirectory, "expected a directory but got a file"))
384+
}
369385
}
370386

371387
pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
@@ -537,6 +553,16 @@ mod uefi_fs {
537553

538554
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(info) }
539555
}
556+
557+
pub(crate) fn delete(self) -> io::Result<()> {
558+
let file_ptr = self.0.as_ptr();
559+
let r = unsafe { ((*file_ptr).delete)(file_ptr) };
560+
561+
// Spec states that even in case of failure, the file handle will be closed.
562+
crate::mem::forget(self);
563+
564+
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
565+
}
540566
}
541567

542568
impl Drop for File {

0 commit comments

Comments
 (0)