@@ -18,6 +18,7 @@ pub const MAX_ROOTLESS_PIPE_SIZE: usize = 1024 * 1024;
1818/// Returns two `File` objects: everything written to the second can be read
1919/// from the first.
2020/// This is used only for resolving the limitation for splice: one of a input or output should be pipe
21+ #[ inline]
2122#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
2223pub fn pipe ( ) -> std:: io:: Result < ( File , File ) > {
2324 let ( read, write) = rustix:: pipe:: pipe ( ) ?;
@@ -36,6 +37,7 @@ pub fn pipe() -> std::io::Result<(File, File)> {
3637/// To get around this requirement, consider splicing from your source into
3738/// a [`pipe`] and then from the pipe into your target (with `splice_exact`):
3839/// this is still very efficient.
40+ #[ inline]
3941#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
4042pub fn splice ( source : & impl AsFd , target : & impl AsFd , len : usize ) -> std:: io:: Result < usize > {
4143 Ok ( rustix:: pipe:: splice (
@@ -53,6 +55,7 @@ pub fn splice(source: &impl AsFd, target: &impl AsFd, len: usize) -> std::io::Re
5355/// Exactly `len` bytes are moved from `source` into `target`.
5456///
5557/// Panics if `source` runs out of data before `len` bytes have been moved.
58+ #[ inline]
5659#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
5760pub fn splice_exact ( source : & impl AsFd , target : & impl AsFd , len : usize ) -> std:: io:: Result < ( ) > {
5861 let mut left = len;
@@ -63,3 +66,22 @@ pub fn splice_exact(source: &impl AsFd, target: &impl AsFd, len: usize) -> std::
6366 }
6467 Ok ( ( ) )
6568}
69+
70+ /// Return verified /dev/null
71+ ///
72+ /// `splice` to /dev/null is faster than `read` when we skip or count the input which is not able to seek
73+ #[ inline]
74+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
75+ pub fn dev_null ( ) -> Option < File > {
76+ let null = std:: fs:: OpenOptions :: new ( )
77+ . write ( true )
78+ . open ( "/dev/null" )
79+ . ok ( ) ?;
80+ let stat = rustix:: fs:: fstat ( & null) . ok ( ) ?;
81+ let dev = stat. st_rdev ;
82+ if ( rustix:: fs:: major ( dev) , rustix:: fs:: minor ( dev) ) == ( 1 , 3 ) {
83+ Some ( null)
84+ } else {
85+ None
86+ }
87+ }
0 commit comments