-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-use existing fd for stdout even if its a seek-able file
this is important as the fd holds the file offset we need to use
- Loading branch information
Showing
6 changed files
with
187 additions
and
11 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
pub mod display; | ||
pub mod error; | ||
pub mod io; | ||
pub mod line_ending; | ||
pub mod os; | ||
pub mod panic; | ||
|
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,83 @@ | ||
// This file is part of the uutils coreutils package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
|
||
#[cfg(not(windows))] | ||
use std::os::fd::{AsFd, OwnedFd}; | ||
#[cfg(windows)] | ||
use std::os::windows::io::{AsHandle, OwnedHandle}; | ||
use std::{ | ||
fs::{File, OpenOptions}, | ||
io::{self, Stdout}, | ||
path::Path, | ||
process::Stdio, | ||
}; | ||
|
||
#[cfg(windows)] | ||
type XType = OwnedHandle; | ||
#[cfg(not(windows))] | ||
type XType = OwnedFd; | ||
|
||
pub struct OwnedFileDescriptorOrHandle { | ||
fx: XType, | ||
} | ||
|
||
impl OwnedFileDescriptorOrHandle { | ||
pub fn new(x: XType) -> Self { | ||
Self { fx: x } | ||
} | ||
|
||
pub fn open_file(options: &OpenOptions, path: &Path) -> io::Result<Self> { | ||
let f = options.open(path)?; | ||
Self::from(f) | ||
} | ||
|
||
#[cfg(windows)] | ||
pub fn from<T: AsHandle>(t: T) -> io::Result<Self> { | ||
Ok(Self { | ||
fx: t.as_handle().try_clone_to_owned()?, | ||
}) | ||
} | ||
|
||
#[cfg(not(windows))] | ||
pub fn from<T: AsFd>(t: T) -> io::Result<Self> { | ||
Ok(Self { | ||
fx: t.as_fd().try_clone_to_owned()?, | ||
}) | ||
} | ||
|
||
pub fn into_file(self) -> File { | ||
File::from(self.fx) | ||
} | ||
|
||
pub fn into_stdio(self) -> Stdio { | ||
Stdio::from(self.fx) | ||
} | ||
|
||
pub fn try_clone(&self) -> io::Result<Self> { | ||
self.fx.try_clone().map(Self::new) | ||
} | ||
|
||
pub fn as_raw(&self) -> &XType { | ||
&self.fx | ||
} | ||
} | ||
|
||
impl From<OwnedFileDescriptorOrHandle> for Stdio { | ||
fn from(value: OwnedFileDescriptorOrHandle) -> Self { | ||
value.into_stdio() | ||
} | ||
} | ||
|
||
impl TryFrom<&Stdout> for OwnedFileDescriptorOrHandle { | ||
type Error = io::Error; | ||
|
||
fn try_from(value: &Stdout) -> Result<Self, Self::Error> { | ||
#[cfg(windows)] | ||
let x = value.as_handle().try_clone_to_owned()?; | ||
#[cfg(not(windows))] | ||
let x = value.as_fd().try_clone_to_owned()?; | ||
Ok(Self::new(x)) | ||
} | ||
} |
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