Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fb1751f

Browse files
committedFeb 15, 2025··
std: move process implementations to sys
As per #117276, this moves the implementations of `Process` and friends out of the `pal` module and into the `sys` module, removing quite a lot of error-prone `#[path]` imports in the process (hah, get it ;-)). I've also made the `zircon` module a dedicated submodule of `pal::unix`, hopefully we can move some other definitions there as well (they are currently quite a lot of duplications in `sys`). Also, the `ensure_no_nuls` function on Windows now lives in `sys::pal::windows` – it's not specific to processes and shared by the argument implementation.
1 parent f77247a commit fb1751f

File tree

32 files changed

+90
-104
lines changed

32 files changed

+90
-104
lines changed
 

‎library/std/src/sys/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mod io;
1616
pub mod net;
1717
pub mod os_str;
1818
pub mod path;
19+
pub mod process;
1920
pub mod random;
2021
pub mod sync;
2122
pub mod thread_local;

‎library/std/src/sys/pal/hermit/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ pub mod futex;
2626
pub mod os;
2727
#[path = "../unsupported/pipe.rs"]
2828
pub mod pipe;
29-
#[path = "../unsupported/process.rs"]
30-
pub mod process;
3129
pub mod stdio;
3230
pub mod thread;
3331
pub mod time;

‎library/std/src/sys/pal/sgx/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ mod libunwind_integration;
1818
pub mod os;
1919
#[path = "../unsupported/pipe.rs"]
2020
pub mod pipe;
21-
#[path = "../unsupported/process.rs"]
22-
pub mod process;
2321
pub mod stdio;
2422
pub mod thread;
2523
pub mod thread_parking;

‎library/std/src/sys/pal/solid/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ pub mod fs;
2626
pub mod os;
2727
#[path = "../unsupported/pipe.rs"]
2828
pub mod pipe;
29-
#[path = "../unsupported/process.rs"]
30-
pub mod process;
3129
pub mod stdio;
3230
pub use self::itron::{thread, thread_parking};
3331
pub mod time;

‎library/std/src/sys/pal/teeos/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ pub mod fs;
1616
pub mod os;
1717
#[path = "../unsupported/pipe.rs"]
1818
pub mod pipe;
19-
#[path = "../unsupported/process.rs"]
20-
pub mod process;
2119
pub mod stdio;
2220
pub mod thread;
2321
#[allow(non_upper_case_globals)]

‎library/std/src/sys/pal/uefi/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub mod helpers;
2020
pub mod os;
2121
#[path = "../unsupported/pipe.rs"]
2222
pub mod pipe;
23-
pub mod process;
2423
pub mod stdio;
2524
pub mod thread;
2625
pub mod time;

‎library/std/src/sys/pal/unix/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ pub mod args;
1010
pub mod env;
1111
pub mod fd;
1212
pub mod fs;
13+
#[cfg(target_os = "fuchsia")]
14+
pub mod fuchsia;
1315
pub mod futex;
1416
#[cfg(any(target_os = "linux", target_os = "android"))]
1517
pub mod kernel_copy;
1618
#[cfg(target_os = "linux")]
1719
pub mod linux;
1820
pub mod os;
1921
pub mod pipe;
20-
pub mod process;
2122
pub mod stack_overflow;
2223
pub mod stdio;
2324
pub mod sync;
@@ -421,7 +422,7 @@ cfg_if::cfg_if! {
421422
}
422423

423424
#[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita", target_os = "nuttx"))]
424-
mod unsupported {
425+
pub mod unsupported {
425426
use crate::io;
426427

427428
pub fn unsupported<T>() -> io::Result<T> {

‎library/std/src/sys/pal/unix/process/mod.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎library/std/src/sys/pal/unsupported/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub mod env;
55
pub mod fs;
66
pub mod os;
77
pub mod pipe;
8-
pub mod process;
98
pub mod stdio;
109
pub mod thread;
1110
pub mod time;

‎library/std/src/sys/pal/wasi/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ pub mod futex;
2424
pub mod os;
2525
#[path = "../unsupported/pipe.rs"]
2626
pub mod pipe;
27-
#[path = "../unsupported/process.rs"]
28-
pub mod process;
2927
pub mod stdio;
3028
pub mod thread;
3129
pub mod time;

‎library/std/src/sys/pal/wasip2/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ pub mod futex;
2222
pub mod os;
2323
#[path = "../unsupported/pipe.rs"]
2424
pub mod pipe;
25-
#[path = "../unsupported/process.rs"]
26-
pub mod process;
2725
#[path = "../wasi/stdio.rs"]
2826
pub mod stdio;
2927
#[path = "../wasi/thread.rs"]

‎library/std/src/sys/pal/wasm/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ pub mod fs;
2525
pub mod os;
2626
#[path = "../unsupported/pipe.rs"]
2727
pub mod pipe;
28-
#[path = "../unsupported/process.rs"]
29-
pub mod process;
3028
#[path = "../unsupported/stdio.rs"]
3129
pub mod stdio;
3230
#[path = "../unsupported/time.rs"]

‎library/std/src/sys/pal/windows/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
#[cfg(test)]
77
mod tests;
88

9+
use super::ensure_no_nuls;
910
use super::os::current_exe;
1011
use crate::ffi::{OsStr, OsString};
1112
use crate::num::NonZero;
1213
use crate::os::windows::prelude::*;
1314
use crate::path::{Path, PathBuf};
1415
use crate::sys::path::get_long_path;
15-
use crate::sys::process::ensure_no_nuls;
1616
use crate::sys::{c, to_u16s};
1717
use crate::sys_common::AsInner;
1818
use crate::sys_common::wstr::WStrUnits;

‎library/std/src/sys/pal/windows/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub mod futex;
2323
pub mod handle;
2424
pub mod os;
2525
pub mod pipe;
26-
pub mod process;
2726
pub mod stdio;
2827
pub mod thread;
2928
pub mod time;
@@ -289,6 +288,14 @@ pub fn truncate_utf16_at_nul(v: &[u16]) -> &[u16] {
289288
}
290289
}
291290

291+
pub fn ensure_no_nuls<T: AsRef<OsStr>>(s: T) -> crate::io::Result<T> {
292+
if s.as_ref().encode_wide().any(|b| b == 0) {
293+
Err(crate::io::const_error!(ErrorKind::InvalidInput, "nul byte found in provided data"))
294+
} else {
295+
Ok(s)
296+
}
297+
}
298+
292299
pub trait IsZero {
293300
fn is_zero(&self) -> bool;
294301
}

‎library/std/src/sys/pal/xous/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ pub mod fs;
88
pub mod os;
99
#[path = "../unsupported/pipe.rs"]
1010
pub mod pipe;
11-
#[path = "../unsupported/process.rs"]
12-
pub mod process;
1311
pub mod stdio;
1412
pub mod thread;
1513
pub mod time;

‎library/std/src/sys/pal/zkvm/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ pub mod fs;
1919
pub mod os;
2020
#[path = "../unsupported/pipe.rs"]
2121
pub mod pipe;
22-
#[path = "../unsupported/process.rs"]
23-
pub mod process;
2422
pub mod stdio;
2523
#[path = "../unsupported/thread.rs"]
2624
pub mod thread;

‎library/std/src/sys/process/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cfg_if::cfg_if! {
2+
if #[cfg(target_family = "unix")] {
3+
mod unix;
4+
use unix as imp;
5+
} else if #[cfg(target_os = "windows")] {
6+
mod windows;
7+
use windows as imp;
8+
} else if #[cfg(target_os = "uefi")] {
9+
mod uefi;
10+
use uefi as imp;
11+
} else {
12+
mod unsupported;
13+
use unsupported as imp;
14+
}
15+
}
16+
17+
pub use imp::{
18+
Command, CommandArgs, EnvKey, ExitCode, ExitStatus, ExitStatusError, Process, Stdio, StdioPipes,
19+
};

‎library/std/src/sys/pal/uefi/process.rs renamed to ‎library/std/src/sys/process/uefi.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use r_efi::protocols::simple_text_output;
22

3-
use super::helpers;
43
use crate::collections::BTreeMap;
54
pub use crate::ffi::OsString as EnvKey;
65
use crate::ffi::{OsStr, OsString};
76
use crate::num::{NonZero, NonZeroI32};
87
use crate::path::Path;
98
use crate::sys::fs::File;
9+
use crate::sys::pal::helpers;
10+
use crate::sys::pal::os::error_string;
1011
use crate::sys::pipe::AnonPipe;
1112
use crate::sys::unsupported;
1213
use crate::sys_common::process::{CommandEnv, CommandEnvs};
@@ -225,7 +226,7 @@ impl ExitStatus {
225226

226227
impl fmt::Display for ExitStatus {
227228
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228-
let err_str = super::os::error_string(self.0.as_usize());
229+
let err_str = error_string(self.0.as_usize());
229230
write!(f, "{}", err_str)
230231
}
231232
}
@@ -241,7 +242,7 @@ pub struct ExitStatusError(r_efi::efi::Status);
241242

242243
impl fmt::Debug for ExitStatusError {
243244
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
244-
let err_str = super::os::error_string(self.0.as_usize());
245+
let err_str = error_string(self.0.as_usize());
245246
write!(f, "{}", err_str)
246247
}
247248
}
@@ -335,15 +336,14 @@ impl<'a> fmt::Debug for CommandArgs<'a> {
335336
mod uefi_command_internal {
336337
use r_efi::protocols::{loaded_image, simple_text_output};
337338

338-
use super::super::helpers;
339339
use crate::ffi::{OsStr, OsString};
340340
use crate::io::{self, const_error};
341341
use crate::mem::MaybeUninit;
342342
use crate::os::uefi::env::{boot_services, image_handle, system_table};
343343
use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
344344
use crate::ptr::NonNull;
345345
use crate::slice;
346-
use crate::sys::pal::uefi::helpers::OwnedTable;
346+
use crate::sys::pal::helpers::{self, OwnedTable};
347347
use crate::sys_common::wstr::WStrUnits;
348348

349349
pub struct Image {

‎library/std/src/sys/pal/unix/process/process_common.rs renamed to ‎library/std/src/sys/process/unix/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ cfg_if::cfg_if! {
5959

6060
let bit = (signum - 1) as usize;
6161
if set.is_null() || bit >= (8 * size_of::<sigset_t>()) {
62-
crate::sys::pal::unix::os::set_errno(libc::EINVAL);
62+
crate::sys::pal::os::set_errno(libc::EINVAL);
6363
return -1;
6464
}
6565
let raw = slice::from_raw_parts_mut(

‎library/std/src/sys/pal/unix/process/process_fuchsia.rs renamed to ‎library/std/src/sys/process/unix/fuchsia.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use libc::{c_int, size_t};
22

3+
use super::common::*;
34
use crate::num::NonZero;
4-
use crate::sys::process::process_common::*;
5-
use crate::sys::process::zircon::{Handle, zx_handle_t};
5+
use crate::sys::pal::fuchsia::*;
66
use crate::{fmt, io, mem, ptr};
77

88
////////////////////////////////////////////////////////////////////////////////
@@ -58,8 +58,6 @@ impl Command {
5858
stdio: ChildPipes,
5959
maybe_envp: Option<&CStringArray>,
6060
) -> io::Result<zx_handle_t> {
61-
use crate::sys::process::zircon::*;
62-
6361
let envp = match maybe_envp {
6462
// None means to clone the current environment, which is done in the
6563
// flags below.
@@ -152,8 +150,6 @@ impl Process {
152150
}
153151

154152
pub fn kill(&mut self) -> io::Result<()> {
155-
use crate::sys::process::zircon::*;
156-
157153
unsafe {
158154
zx_cvt(zx_task_kill(self.handle.raw()))?;
159155
}
@@ -162,8 +158,6 @@ impl Process {
162158
}
163159

164160
pub fn wait(&mut self) -> io::Result<ExitStatus> {
165-
use crate::sys::process::zircon::*;
166-
167161
let mut proc_info: zx_info_process_t = Default::default();
168162
let mut actual: size_t = 0;
169163
let mut avail: size_t = 0;
@@ -194,8 +188,6 @@ impl Process {
194188
}
195189

196190
pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
197-
use crate::sys::process::zircon::*;
198-
199191
let mut proc_info: zx_info_process_t = Default::default();
200192
let mut actual: size_t = 0;
201193
let mut avail: size_t = 0;
@@ -251,7 +243,7 @@ impl ExitStatus {
251243
None
252244
}
253245

254-
// FIXME: The actually-Unix implementation in process_unix.rs uses WSTOPSIG, WCOREDUMP et al.
246+
// FIXME: The actually-Unix implementation in unix.rs uses WSTOPSIG, WCOREDUMP et al.
255247
// I infer from the implementation of `success`, `code` and `signal` above that these are not
256248
// available on Fuchsia.
257249
//
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[cfg_attr(any(target_os = "espidf", target_os = "horizon", target_os = "nuttx"), allow(unused))]
2+
mod common;
3+
4+
cfg_if::cfg_if! {
5+
if #[cfg(target_os = "fuchsia")] {
6+
mod fuchsia;
7+
use fuchsia as imp;
8+
} else if #[cfg(target_os = "vxworks")] {
9+
mod vxworks;
10+
use vxworks as imp;
11+
} else if #[cfg(any(target_os = "espidf", target_os = "horizon", target_os = "vita", target_os = "nuttx"))] {
12+
mod unsupported;
13+
use unsupported as imp;
14+
} else {
15+
mod unix;
16+
use unix as imp;
17+
}
18+
}
19+
20+
pub use imp::{ExitStatus, ExitStatusError, Process};
21+
22+
pub use self::common::{Command, CommandArgs, ExitCode, Stdio, StdioPipes};
23+
pub use crate::ffi::OsString as EnvKey;

‎library/std/src/sys/pal/unix/process/process_unix.rs renamed to ‎library/std/src/sys/process/unix/unix.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use libc::{c_int, pid_t};
1010
)))]
1111
use libc::{gid_t, uid_t};
1212

13+
use super::common::*;
1314
use crate::io::{self, Error, ErrorKind};
1415
use crate::num::NonZero;
1516
use crate::sys::cvt;
1617
#[cfg(target_os = "linux")]
17-
use crate::sys::pal::unix::linux::pidfd::PidFd;
18-
use crate::sys::process::process_common::*;
18+
use crate::sys::pal::linux::pidfd::PidFd;
1919
use crate::{fmt, mem, sys};
2020

2121
cfg_if::cfg_if! {
@@ -1033,7 +1033,7 @@ impl ExitStatus {
10331033
// true on all actual versions of Unix, is widely assumed, and is specified in SuS
10341034
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html. If it is not
10351035
// true for a platform pretending to be Unix, the tests (our doctests, and also
1036-
// process_unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too.
1036+
// unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too.
10371037
match NonZero::try_from(self.0) {
10381038
/* was nonzero */ Ok(failure) => Err(ExitStatusError(failure)),
10391039
/* was zero, couldn't convert */ Err(_) => Ok(()),
@@ -1213,10 +1213,9 @@ impl ExitStatusError {
12131213

12141214
#[cfg(target_os = "linux")]
12151215
mod linux_child_ext {
1216-
1216+
use crate::io::ErrorKind;
12171217
use crate::os::linux::process as os;
1218-
use crate::sys::pal::unix::ErrorKind;
1219-
use crate::sys::pal::unix::linux::pidfd as imp;
1218+
use crate::sys::pal::linux::pidfd as imp;
12201219
use crate::sys_common::FromInner;
12211220
use crate::{io, mem};
12221221

@@ -1242,10 +1241,9 @@ mod linux_child_ext {
12421241
}
12431242

12441243
#[cfg(test)]
1245-
#[path = "process_unix/tests.rs"]
12461244
mod tests;
12471245

1248-
// See [`process_unsupported_wait_status::compare_with_linux`];
1246+
// See [`unsupported_wait_status::compare_with_linux`];
12491247
#[cfg(all(test, target_os = "linux"))]
1250-
#[path = "process_unsupported/wait_status.rs"]
1251-
mod process_unsupported_wait_status;
1248+
#[path = "unsupported/wait_status.rs"]
1249+
mod unsupported_wait_status;

‎library/std/src/sys/pal/unix/process/process_unsupported.rs renamed to ‎library/std/src/sys/process/unix/unsupported.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use libc::{c_int, pid_t};
22

3+
use super::common::*;
34
use crate::io;
45
use crate::num::NonZero;
5-
use crate::sys::pal::unix::unsupported::*;
6-
use crate::sys::process::process_common::*;
6+
use crate::sys::pal::unsupported::*;
77

88
////////////////////////////////////////////////////////////////////////////////
99
// Command

‎library/std/src/sys/pal/unix/process/process_unsupported/wait_status.rs renamed to ‎library/std/src/sys/process/unix/unsupported/wait_status.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ffi::c_int;
77
use crate::fmt;
88
use crate::num::NonZero;
99

10-
/// Emulated wait status for use by `process_unsupported.rs`
10+
/// Emulated wait status for use by `unsupported.rs`
1111
///
1212
/// Uses the "traditional unix" encoding. For use on platfors which are `#[cfg(unix)]`
1313
/// but do not actually support subprocesses at all.
@@ -48,7 +48,7 @@ impl ExitStatus {
4848
// true on all actual versions of Unix, is widely assumed, and is specified in SuS
4949
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html. If it is not
5050
// true for a platform pretending to be Unix, the tests (our doctests, and also
51-
// process_unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too.
51+
// unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too.
5252
match NonZero::try_from(self.wait_status) {
5353
/* was nonzero */ Ok(failure) => Err(ExitStatusError(failure)),
5454
/* was zero, couldn't convert */ Err(_) => Ok(()),
@@ -79,5 +79,6 @@ impl ExitStatus {
7979
}
8080

8181
#[cfg(test)]
82-
#[path = "wait_status/tests.rs"] // needed because of strange layout of process_unsupported
82+
#[path = "wait_status/tests.rs"]
83+
// needed because this module is also imported through #[path] for testing purposes
8384
mod tests;

‎library/std/src/sys/pal/unix/process/process_unsupported/wait_status/tests.rs renamed to ‎library/std/src/sys/process/unix/unsupported/wait_status/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Note that tests in this file are run on Linux as well as on platforms using process_unsupported
1+
// Note that tests in this file are run on Linux as well as on platforms using unsupported
22

33
// Test that our emulation exactly matches Linux
44
//

‎library/std/src/sys/pal/unix/process/process_vxworks.rs renamed to ‎library/std/src/sys/process/unix/vxworks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![forbid(unsafe_op_in_unsafe_fn)]
22
use libc::{self, RTP_ID, c_char, c_int};
33

4+
use super::common::*;
45
use crate::io::{self, ErrorKind};
56
use crate::num::NonZero;
67
use crate::sys::cvt;
7-
use crate::sys::pal::unix::thread;
8-
use crate::sys::process::process_common::*;
8+
use crate::sys::pal::thread;
99
use crate::{fmt, sys};
1010

1111
////////////////////////////////////////////////////////////////////////////////
@@ -200,7 +200,7 @@ impl ExitStatus {
200200
// true on all actual versions of Unix, is widely assumed, and is specified in SuS
201201
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html. If it is not
202202
// true for a platform pretending to be Unix, the tests (our doctests, and also
203-
// process_unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too.
203+
// unix/tests.rs) will spot it. `ExitStatusError::code` assumes this too.
204204
match NonZero::try_from(self.0) {
205205
Ok(failure) => Err(ExitStatusError(failure)),
206206
Err(_) => Ok(()),

‎library/std/src/sys/pal/windows/process.rs renamed to ‎library/std/src/sys/process/windows.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ mod tests;
55

66
use core::ffi::c_void;
77

8-
use super::api::{self, WinError};
98
use crate::collections::BTreeMap;
109
use crate::env::consts::{EXE_EXTENSION, EXE_SUFFIX};
1110
use crate::ffi::{OsStr, OsString};
12-
use crate::io::{self, Error, ErrorKind};
11+
use crate::io::{self, Error};
1312
use crate::num::NonZero;
1413
use crate::os::windows::ffi::{OsStrExt, OsStringExt};
1514
use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle};
@@ -20,6 +19,8 @@ use crate::sys::args::{self, Arg};
2019
use crate::sys::c::{self, EXIT_FAILURE, EXIT_SUCCESS};
2120
use crate::sys::fs::{File, OpenOptions};
2221
use crate::sys::handle::Handle;
22+
use crate::sys::pal::api::{self, WinError};
23+
use crate::sys::pal::{ensure_no_nuls, fill_utf16_buf};
2324
use crate::sys::pipe::{self, AnonPipe};
2425
use crate::sys::{cvt, path, stdio};
2526
use crate::sys_common::IntoInner;
@@ -142,14 +143,6 @@ impl AsRef<OsStr> for EnvKey {
142143
}
143144
}
144145

145-
pub(crate) fn ensure_no_nuls<T: AsRef<OsStr>>(s: T) -> io::Result<T> {
146-
if s.as_ref().encode_wide().any(|b| b == 0) {
147-
Err(io::const_error!(ErrorKind::InvalidInput, "nul byte found in provided data"))
148-
} else {
149-
Ok(s)
150-
}
151-
}
152-
153146
pub struct Command {
154147
program: OsString,
155148
args: Vec<Arg>,
@@ -278,7 +271,7 @@ impl Command {
278271
let is_batch_file = if path::is_verbatim(&program) {
279272
has_bat_extension(&program[..program.len() - 1])
280273
} else {
281-
super::fill_utf16_buf(
274+
fill_utf16_buf(
282275
|buffer, size| unsafe {
283276
// resolve the path so we can test the final file name.
284277
c::GetFullPathNameW(program.as_ptr(), size, buffer, ptr::null_mut())
@@ -522,15 +515,15 @@ where
522515
// 3 & 4. System paths
523516
// SAFETY: This uses `fill_utf16_buf` to safely call the OS functions.
524517
unsafe {
525-
if let Ok(Some(path)) = super::fill_utf16_buf(
518+
if let Ok(Some(path)) = fill_utf16_buf(
526519
|buf, size| c::GetSystemDirectoryW(buf, size),
527520
|buf| exists(PathBuf::from(OsString::from_wide(buf))),
528521
) {
529522
return Some(path);
530523
}
531524
#[cfg(not(target_vendor = "uwp"))]
532525
{
533-
if let Ok(Some(path)) = super::fill_utf16_buf(
526+
if let Ok(Some(path)) = fill_utf16_buf(
534527
|buf, size| c::GetWindowsDirectoryW(buf, size),
535528
|buf| exists(PathBuf::from(OsString::from_wide(buf))),
536529
) {
@@ -846,10 +839,8 @@ fn make_command_line(argv0: &OsStr, args: &[Arg], force_quotes: bool) -> io::Res
846839

847840
// Get `cmd.exe` for use with bat scripts, encoded as a UTF-16 string.
848841
fn command_prompt() -> io::Result<Vec<u16>> {
849-
let mut system: Vec<u16> = super::fill_utf16_buf(
850-
|buf, size| unsafe { c::GetSystemDirectoryW(buf, size) },
851-
|buf| buf.into(),
852-
)?;
842+
let mut system: Vec<u16> =
843+
fill_utf16_buf(|buf, size| unsafe { c::GetSystemDirectoryW(buf, size) }, |buf| buf.into())?;
853844
system.extend("\\cmd.exe".encode_utf16().chain([0]));
854845
Ok(system)
855846
}

0 commit comments

Comments
 (0)
Please sign in to comment.