Skip to content

Commit 85647ca

Browse files
committed
std: sys: uefi: os: Implement join_paths
- PATHS_SEP is defined as global const since I will implement split_paths in the future. - Tested using OVMF using QEMU. Signed-off-by: Ayush Singh <ayush@beagleboard.org>
1 parent b6fdaf2 commit 85647ca

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

library/std/src/env.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,9 @@ 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 both double
522+
/// quote/colon on UEFI), or if the system does not have a `PATH`-like
523+
/// variable (e.g. UEFI or WASI).
523524
///
524525
/// # Examples
525526
///

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ 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+
const QUOTE: u16 = b'"' as u16;
15+
1216
pub fn getcwd() -> io::Result<PathBuf> {
1317
match helpers::open_shell() {
1418
Some(shell) => {
@@ -54,17 +58,34 @@ impl<'a> Iterator for SplitPaths<'a> {
5458
#[derive(Debug)]
5559
pub struct JoinPathsError;
5660

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

6586
impl fmt::Display for JoinPathsError {
6687
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67-
"not supported on this platform yet".fmt(f)
88+
"path segment contains `\"` or `;`".fmt(f)
6889
}
6990
}
7091

0 commit comments

Comments
 (0)