Skip to content

Commit b468e9b

Browse files
Rollup merge of rust-lang#138667 - Ayush1325:uefi-mkdir, r=joboet
std: uefi: fs: Implement mkdir - Since there is no direct mkdir in UEFI, first check if a file/dir with same path exists and then create the directory. cc `@dvdhrm` `@nicholasbishop`
2 parents a452bf7 + b8de9b2 commit b468e9b

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

std/src/sys/fs/uefi.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct FilePermissions(bool);
3636
pub struct FileType(bool);
3737

3838
#[derive(Debug)]
39-
pub struct DirBuilder {}
39+
pub struct DirBuilder;
4040

4141
impl FileAttr {
4242
pub fn size(&self) -> u64 {
@@ -248,11 +248,11 @@ impl File {
248248

249249
impl DirBuilder {
250250
pub fn new() -> DirBuilder {
251-
DirBuilder {}
251+
DirBuilder
252252
}
253253

254-
pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
255-
unsupported()
254+
pub fn mkdir(&self, p: &Path) -> io::Result<()> {
255+
uefi_fs::mkdir(p)
256256
}
257257
}
258258

@@ -452,4 +452,30 @@ mod uefi_fs {
452452
}
453453
}
454454
}
455+
456+
/// An implementation of mkdir to allow creating new directory without having to open the
457+
/// volume twice (once for checking and once for creating)
458+
pub(crate) fn mkdir(path: &Path) -> io::Result<()> {
459+
let absolute = crate::path::absolute(path)?;
460+
461+
let p = helpers::OwnedDevicePath::from_text(absolute.as_os_str())?;
462+
let (vol, mut path_remaining) = File::open_volume_from_device_path(p.borrow())?;
463+
464+
// Check if file exists
465+
match vol.open(&mut path_remaining, file::MODE_READ, 0) {
466+
Ok(_) => {
467+
return Err(io::Error::new(io::ErrorKind::AlreadyExists, "Path already exists"));
468+
}
469+
Err(e) if e.kind() == io::ErrorKind::NotFound => {}
470+
Err(e) => return Err(e),
471+
}
472+
473+
let _ = vol.open(
474+
&mut path_remaining,
475+
file::MODE_READ | file::MODE_WRITE | file::MODE_CREATE,
476+
file::DIRECTORY,
477+
)?;
478+
479+
Ok(())
480+
}
455481
}

0 commit comments

Comments
 (0)