Skip to content

Commit 435be5b

Browse files
Rollup merge of #151991 - Ayush1325:uefi-split-path, r=joboet
std: sys: pal: uefi: os: Implement split_paths - Tested using OVMF on QEMU @rustbot label +O-UEFI cc @nicholasbishop
2 parents b42920d + c37e448 commit 435be5b

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use r_efi::efi::protocols::{device_path, loaded_image_device_path};
22

33
use super::{helpers, unsupported_err};
44
use crate::ffi::{OsStr, OsString};
5-
use crate::marker::PhantomData;
65
use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
76
use crate::path::{self, PathBuf};
87
use crate::{fmt, io};
@@ -38,16 +37,37 @@ pub fn chdir(p: &path::Path) -> io::Result<()> {
3837
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
3938
}
4039

41-
pub struct SplitPaths<'a>(!, PhantomData<&'a ()>);
40+
pub struct SplitPaths<'a> {
41+
data: crate::os::uefi::ffi::EncodeWide<'a>,
42+
must_yield: bool,
43+
}
4244

43-
pub fn split_paths(_unparsed: &OsStr) -> SplitPaths<'_> {
44-
panic!("unsupported")
45+
pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> {
46+
SplitPaths { data: unparsed.encode_wide(), must_yield: true }
4547
}
4648

4749
impl<'a> Iterator for SplitPaths<'a> {
4850
type Item = PathBuf;
51+
4952
fn next(&mut self) -> Option<PathBuf> {
50-
self.0
53+
let must_yield = self.must_yield;
54+
self.must_yield = false;
55+
56+
let mut in_progress = Vec::new();
57+
for b in self.data.by_ref() {
58+
if b == PATHS_SEP {
59+
self.must_yield = true;
60+
break;
61+
} else {
62+
in_progress.push(b)
63+
}
64+
}
65+
66+
if !must_yield && in_progress.is_empty() {
67+
None
68+
} else {
69+
Some(PathBuf::from(OsString::from_wide(&in_progress)))
70+
}
5171
}
5272
}
5373

library/std/tests/env.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ fn split_paths_unix() {
5959
assert!(check_parse("/:/usr/local", &mut ["/", "/usr/local"]));
6060
}
6161

62+
#[test]
63+
#[cfg(target_os = "uefi")]
64+
fn split_paths_uefi() {
65+
use std::path::PathBuf;
66+
67+
fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
68+
split_paths(unparsed).collect::<Vec<_>>()
69+
== parsed.iter().map(|s| PathBuf::from(*s)).collect::<Vec<_>>()
70+
}
71+
72+
assert!(check_parse("", &mut [""]));
73+
assert!(check_parse(";;", &mut ["", "", ""]));
74+
assert!(check_parse(r"fs0:\", &mut [r"fs0:\"]));
75+
assert!(check_parse(r"fs0:\;", &mut [r"fs0:\", ""]));
76+
assert!(check_parse(r"fs0:\;fs0:\boot\", &mut [r"fs0:\", r"fs0:\boot\"]));
77+
}
78+
6279
#[test]
6380
#[cfg(unix)]
6481
fn join_paths_unix() {

0 commit comments

Comments
 (0)