Skip to content

Commit 512afea

Browse files
authored
Rollup merge of #150993 - Ayush1325:uefi-join-path, r=Mark-Simulacrum
std: sys: uefi: os: Implement join_paths - Tested using OVMF using QEMU. @rustbot label +O-UEFI
2 parents e6e2d39 + 90f0f4b commit 512afea

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

library/std/src/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ pub struct JoinPathsError {
518518
///
519519
/// Returns an [`Err`] (containing an error message) if one of the input
520520
/// [`Path`]s contains an invalid character for constructing the `PATH`
521-
/// variable (a double quote on Windows or a colon on Unix), or if the system
522-
/// does not have a `PATH`-like variable (e.g. UEFI or WASI).
521+
/// variable (a double quote on Windows or a colon on Unix or semicolon on
522+
/// UEFI), or if the system does not have a `PATH`-like variable (e.g. WASI).
523523
///
524524
/// # Examples
525525
///

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ use super::{helpers, unsupported_err};
55
use crate::ffi::{OsStr, OsString};
66
use crate::marker::PhantomData;
77
use crate::os::uefi;
8+
use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
89
use crate::path::{self, PathBuf};
910
use crate::ptr::NonNull;
1011
use crate::{fmt, io};
1112

13+
const PATHS_SEP: u16 = b';' as u16;
14+
1215
pub fn getcwd() -> io::Result<PathBuf> {
1316
match helpers::open_shell() {
1417
Some(shell) => {
@@ -54,17 +57,34 @@ impl<'a> Iterator for SplitPaths<'a> {
5457
#[derive(Debug)]
5558
pub struct JoinPathsError;
5659

57-
pub fn join_paths<I, T>(_paths: I) -> Result<OsString, JoinPathsError>
60+
// UEFI Shell Path variable is defined in Section 3.6.1
61+
// [UEFI Shell Specification](https://uefi.org/sites/default/files/resources/UEFI_Shell_2_2.pdf).
62+
pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
5863
where
5964
I: Iterator<Item = T>,
6065
T: AsRef<OsStr>,
6166
{
62-
Err(JoinPathsError)
67+
let mut joined = Vec::new();
68+
69+
for (i, path) in paths.enumerate() {
70+
if i > 0 {
71+
joined.push(PATHS_SEP)
72+
}
73+
74+
let v = path.as_ref().encode_wide().collect::<Vec<u16>>();
75+
if v.contains(&PATHS_SEP) {
76+
return Err(JoinPathsError);
77+
}
78+
79+
joined.extend_from_slice(&v);
80+
}
81+
82+
Ok(OsString::from_wide(&joined))
6383
}
6484

6585
impl fmt::Display for JoinPathsError {
6686
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67-
"not supported on this platform yet".fmt(f)
87+
"path segment contains `;`".fmt(f)
6888
}
6989
}
7090

0 commit comments

Comments
 (0)