-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): handle blocking stdin (#21672)
- Loading branch information
Showing
5 changed files
with
113 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::{ | ||
io::{Read, Stdin, Write}, | ||
os::fd::AsRawFd, | ||
}; | ||
|
||
use mio::{unix::SourceFd, Events}; | ||
use tracing::trace; | ||
|
||
pub fn handle_path_space(path: String) -> String { | ||
if path.contains(' ') { | ||
format!("'{}'", path) | ||
} else { | ||
path | ||
} | ||
} | ||
|
||
pub fn write_to_pty(stdin: &mut Stdin, writer: &mut impl Write) -> anyhow::Result<()> { | ||
let mut buffer = [0; 1024]; | ||
|
||
let mut poll = mio::Poll::new()?; | ||
let mut events = Events::with_capacity(3); | ||
|
||
// Register stdin to the poll instance | ||
let token = mio::Token(0); | ||
poll.registry() | ||
.register( | ||
&mut SourceFd(&stdin.as_raw_fd()), | ||
token, | ||
mio::Interest::READABLE, | ||
) | ||
.map_err(|e| anyhow::anyhow!("Failed to register stdin to poll: {}", e))?; | ||
|
||
loop { | ||
// Poll for events | ||
if let Err(e) = poll.poll(&mut events, None) { | ||
if e.kind() == std::io::ErrorKind::Interrupted { | ||
continue; | ||
} | ||
trace!("poll error: {:?}", e); | ||
anyhow::bail!("Failed to poll for events: {}", e); | ||
} | ||
|
||
for event in events.iter().map(|x| x.token()) { | ||
match event { | ||
mio::Token(0) => { | ||
// Read data from stdin | ||
loop { | ||
match stdin.read(&mut buffer) { | ||
Ok(n) => { | ||
writer.write_all(&buffer[..n])?; | ||
writer.flush()?; | ||
} | ||
Err(e) => { | ||
if e.kind() == std::io::ErrorKind::WouldBlock { | ||
break; | ||
} else if e.kind() == std::io::ErrorKind::Interrupted { | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::io::{Stdin, Write}; | ||
use std::os::windows::ffi::OsStrExt; | ||
use std::{ffi::OsString, os::windows::ffi::OsStringExt}; | ||
|
||
use winapi::um::fileapi::GetShortPathNameW; | ||
|
||
pub fn handle_path_space(path: String) -> String { | ||
let wide: Vec<u16> = std::path::PathBuf::from(&path) | ||
.as_os_str() | ||
.encode_wide() | ||
.chain(Some(0)) | ||
.collect(); | ||
let mut buffer: Vec<u16> = vec![0; wide.len() * 2]; | ||
let result = | ||
unsafe { GetShortPathNameW(wide.as_ptr(), buffer.as_mut_ptr(), buffer.len() as u32) }; | ||
if result == 0 { | ||
path | ||
} else { | ||
let len = buffer.iter().position(|&x| x == 0).unwrap(); | ||
let short_path: String = OsString::from_wide(&buffer[..len]) | ||
.to_string_lossy() | ||
.into_owned(); | ||
short_path | ||
} | ||
} | ||
|
||
pub fn write_to_pty(stdin: &mut Stdin, writer: &mut impl Write) -> anyhow::Result<()> { | ||
std::io::copy(stdin, writer) | ||
.map_err(|e| anyhow::anyhow!(e)) | ||
.map(|_| ()) | ||
} |