Skip to content

Commit e7923c4

Browse files
committed
[library/std pal::safaos process]: fix Command::output giving empty output always, and a bug fix related to File::duplicate
1 parent 93e047e commit e7923c4

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

library/std/src/sys/pal/safaos/pipe.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ impl AnonPipe {
6060
/// FIXME: This is a temporary implementation I have no idea what an AnonPipe is forgive my naiveness
6161
/// BUT I NEED IT FOR [`crate::sys::process::Command::output`]
6262
pub fn read2(p1: AnonPipe, v1: &mut Vec<u8>, p2: AnonPipe, v2: &mut Vec<u8>) -> io::Result<()> {
63-
let mut p1 = &p1.into_raw();
64-
let mut p2 = &p2.into_raw();
6563
p1.read_to_end(v1)?;
6664
p2.read_to_end(v2)?;
6765
Ok(())

library/std/src/sys/pal/safaos/process.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Command {
2828
stderr: Option<Stdio>,
2929
}
3030

31+
#[derive(Debug)]
3132
// passed back to std::process with the pipes connected to the child, if any
3233
// were requested
3334
pub struct StdioPipes {
@@ -70,13 +71,13 @@ impl Stdio {
7071
match self {
7172
Stdio::Inherit => None,
7273
Stdio::InheritStdout => {
73-
Some(AnonPipe::from_fd(unsafe { FileDesc::from_raw(sysmeta_stdout()) }))
74+
Some(AnonPipe::from_fd(unsafe { FileDesc::from_raw_dup(sysmeta_stdout()) }))
7475
}
7576
Stdio::InheritStderr => {
76-
Some(AnonPipe::from_fd(unsafe { FileDesc::from_raw(sysmeta_stderr()) }))
77+
Some(AnonPipe::from_fd(unsafe { FileDesc::from_raw_dup(sysmeta_stderr()) }))
7778
}
7879
Stdio::InheritStdin => {
79-
Some(AnonPipe::from_fd(unsafe { FileDesc::from_raw(sysmeta_stdin()) }))
80+
Some(AnonPipe::from_fd(unsafe { FileDesc::from_raw_dup(sysmeta_stdin()) }))
8081
}
8182
Stdio::Null => None,
8283
Stdio::InheritFile(fd) => Some(AnonPipe::from_fd(fd)),
@@ -182,8 +183,20 @@ impl Command {
182183
}
183184

184185
pub fn output(&mut self) -> io::Result<(ExitStatus, Vec<u8>, Vec<u8>)> {
185-
let (proc, pipes) = self.spawn(Stdio::Inherit, false)?;
186-
crate::sys_common::process::wait_with_output(proc, pipes)
186+
let (mut proc, mut pipes) = self.spawn(Stdio::Inherit, false)?;
187+
let status = proc.wait()?;
188+
drop(pipes.stdin.take());
189+
190+
let (mut stdout, mut stderr) = (Vec::new(), Vec::new());
191+
// TODO: properly add Pipes
192+
if let Some(out) = pipes.stdout.take() {
193+
out.read_to_end(&mut stdout)?;
194+
}
195+
196+
if let Some(err) = pipes.stderr.take() {
197+
err.read_to_end(&mut stderr)?;
198+
}
199+
Ok((status, stdout, stderr))
187200
}
188201
}
189202

library/std/src/sys/pal/safaos/resources.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::cell::UnsafeCell;
2+
use core::mem::ManuallyDrop;
23

34
use crate::io::{self, SeekFrom};
45
use crate::sys::fs::FileAttr;
@@ -81,13 +82,25 @@ impl Drop for FileResource {
8182
}
8283
}
8384

85+
impl Clone for FileResource {
86+
fn clone(&self) -> Self {
87+
Self(syscalls::dup(self.0).unwrap())
88+
}
89+
}
90+
8491
// FIXME: make seek_at a mutex?
8592
#[derive(Debug)]
8693
pub(crate) struct FileDesc {
8794
fd: FileResource,
8895
seek_at: UnsafeCell<isize>,
8996
}
9097

98+
impl Clone for FileDesc {
99+
fn clone(&self) -> Self {
100+
unsafe { Self { fd: self.fd.clone(), seek_at: UnsafeCell::new(*self.seek_at.get()) } }
101+
}
102+
}
103+
91104
impl PartialEq for FileDesc {
92105
fn eq(&self, other: &Self) -> bool {
93106
self.fd == other.fd
@@ -98,10 +111,20 @@ unsafe impl Send for FileDesc {}
98111
unsafe impl Sync for FileDesc {}
99112

100113
impl FileDesc {
114+
/// converts a raw resource id into a FileDesc
115+
/// this is unsafe because the resource id is not checked for validity
101116
pub unsafe fn from_raw(ri: usize) -> Self {
102117
Self { fd: FileResource(ri), seek_at: UnsafeCell::new(0) }
103118
}
104119

120+
/// duplicates a raw resource id into a FileDesc
121+
/// this is unsafe because the resource id is not checked for validity
122+
/// the returned FileDesc is a duplicate of the original with a different resource id and therefore doesn't take ownership of the resource
123+
pub unsafe fn from_raw_dup(ri: usize) -> Self {
124+
let fd = unsafe { ManuallyDrop::new(Self::from_raw(ri)) };
125+
ManuallyDrop::into_inner(fd.clone())
126+
}
127+
105128
pub(crate) fn fd(&self) -> usize {
106129
self.fd.0
107130
}

0 commit comments

Comments
 (0)