@@ -161,42 +161,22 @@ mod unix {
161161 }
162162 }
163163
164- enum AccessMode {
165- Readable ,
166- Writable ,
167- }
168-
169- fn check_access_mode ( pipe : AnonPipe , expected_access_mode : AccessMode ) -> io:: Result < AnonPipe > {
170- let ret = unsafe { libc:: fcntl ( pipe. as_raw_fd ( ) , libc:: F_GETFL ) } ;
171- let access_mode = ret & libc:: O_ACCMODE ;
172- let expected_access_mode_str = match expected_access_mode {
173- AccessMode :: Readable => "readable" ,
174- AccessMode :: Writable => "writable" ,
175- } ;
176- let expected_access_mode = match expected_access_mode {
177- AccessMode :: Readable => libc:: O_RDONLY ,
178- AccessMode :: Writable => libc:: O_WRONLY ,
179- } ;
180-
181- if ret == -1 {
182- Err ( io:: Error :: last_os_error ( ) )
183- } else if access_mode == libc:: O_RDWR && access_mode == expected_access_mode {
184- Err ( io:: Error :: new (
185- io:: ErrorKind :: InvalidInput ,
186- format ! ( "Pipe {} is not {}" , pipe. as_raw_fd( ) , expected_access_mode_str) ,
187- ) )
188- } else {
189- Ok ( pipe)
190- }
191- }
192-
193164 #[ unstable( feature = "anonymous_pipe" , issue = "127154" ) ]
194165 impl TryFrom < OwnedFd > for PipeReader {
195166 type Error = io:: Error ;
196167
197168 fn try_from ( owned_fd : OwnedFd ) -> Result < Self , Self :: Error > {
198169 convert_to_pipe ( owned_fd)
199- . and_then ( |pipe| check_access_mode ( pipe, AccessMode :: Readable ) )
170+ . and_then ( |pipe| {
171+ if pipe. as_file_desc ( ) . get_access_mode ( ) ?. readable {
172+ Ok ( pipe)
173+ } else {
174+ Err ( io:: Error :: new (
175+ io:: ErrorKind :: InvalidInput ,
176+ format ! ( "Pipe {} is not readable" , pipe. as_raw_fd( ) ) ,
177+ ) )
178+ }
179+ } )
200180 . map ( Self )
201181 }
202182 }
@@ -207,7 +187,16 @@ mod unix {
207187
208188 fn try_from ( owned_fd : OwnedFd ) -> Result < Self , Self :: Error > {
209189 convert_to_pipe ( owned_fd)
210- . and_then ( |pipe| check_access_mode ( pipe, AccessMode :: Writable ) )
190+ . and_then ( |pipe| {
191+ if pipe. as_file_desc ( ) . get_access_mode ( ) ?. writable {
192+ Ok ( pipe)
193+ } else {
194+ Err ( io:: Error :: new (
195+ io:: ErrorKind :: InvalidInput ,
196+ format ! ( "Pipe {} is not writable" , pipe. as_raw_fd( ) ) ,
197+ ) )
198+ }
199+ } )
211200 . map ( Self )
212201 }
213202 }
0 commit comments