44#[ cfg( not( feature = "no_racy_asserts" ) ) ]
55use crate :: fs:: is_same_file;
66use crate :: fs:: {
7- dir_options, errors, open_unchecked, path_requires_dir, readlink_one, FollowSymlinks ,
8- MaybeOwnedFile , OpenOptions ,
7+ dir_options, errors, open_unchecked, path_requires_dir, readlink_one, to_borrowed_component,
8+ to_owned_component, CowComponent , FollowSymlinks , MaybeOwnedFile , OpenOptions ,
9+ OpenUncheckedError ,
910} ;
1011use std:: {
11- borrow:: Cow ,
1212 ffi:: OsStr ,
1313 fs, io,
1414 path:: { Component , Path , PathBuf } ,
1515} ;
1616
17- /// Like `std::path::Component` except we combine `Prefix` and `RootDir` since
18- /// we don't support absolute paths, and `Normal` has a `Cow` instead of a plain
19- /// `OsStr` reference, so it can optionally own its own string.
20- #[ derive( Debug ) ]
21- enum CowComponent < ' borrow > {
22- PrefixOrRootDir ,
23- CurDir ,
24- ParentDir ,
25- Normal ( Cow < ' borrow , OsStr > ) ,
26- }
27-
28- /// Convert a `Component` into a `CowComponent` which borrows strings.
29- fn to_borrowed_component ( component : Component ) -> CowComponent {
30- match component {
31- Component :: Prefix ( _) | Component :: RootDir => CowComponent :: PrefixOrRootDir ,
32- Component :: CurDir => CowComponent :: CurDir ,
33- Component :: ParentDir => CowComponent :: ParentDir ,
34- Component :: Normal ( os_str) => CowComponent :: Normal ( os_str. into ( ) ) ,
35- }
36- }
37-
38- /// Convert a `Component` into a `CowComponent` which owns strings.
39- fn to_owned_component < ' borrow > ( component : Component ) -> CowComponent < ' borrow > {
40- match component {
41- Component :: Prefix ( _) | Component :: RootDir => CowComponent :: PrefixOrRootDir ,
42- Component :: CurDir => CowComponent :: CurDir ,
43- Component :: ParentDir => CowComponent :: ParentDir ,
44- Component :: Normal ( os_str) => CowComponent :: Normal ( os_str. to_os_string ( ) . into ( ) ) ,
45- }
46- }
47-
4817/// Utility for collecting the canonical path components.
4918struct CanonicalPath < ' path_buf > {
5019 /// If the user requested a canonical path, a reference to the `PathBuf` to
@@ -122,8 +91,8 @@ pub(crate) fn open_manually_wrapper(
12291) -> io:: Result < fs:: File > {
12392 let mut symlink_count = 0 ;
12493 let start = MaybeOwnedFile :: borrowed ( start) ;
125- open_manually ( start, path, options, & mut symlink_count, None )
126- . and_then ( | maybe_owned| maybe_owned . into_file ( options) )
94+ let maybe_owned = open_manually ( start, path, options, & mut symlink_count, None ) ? ;
95+ maybe_owned. into_file ( options)
12796}
12897
12998/// Implement `open` by breaking up the path into components, resolving each
@@ -171,7 +140,7 @@ pub(crate) fn open_manually<'start>(
171140 match c {
172141 CowComponent :: PrefixOrRootDir => return Err ( errors:: escape_attempt ( ) ) ,
173142 CowComponent :: CurDir => {
174- // If the path ends in `.` and we want write access , fail.
143+ // If the path ends in `.` and we can't open a directory , fail.
175144 if components. is_empty ( ) {
176145 if dir_precluded {
177146 return Err ( errors:: is_directory ( ) ) ;
@@ -204,7 +173,7 @@ pub(crate) fn open_manually<'start>(
204173 assert ! ( canonical_path. pop( ) ) ;
205174 }
206175 CowComponent :: Normal ( one) => {
207- // If the path requires a directory and we'd open it for writing , fail.
176+ // If the path requires a directory and we can't open a directory , fail.
208177 if components. is_empty ( ) && dir_required && dir_precluded {
209178 return Err ( errors:: is_directory ( ) ) ;
210179 }
@@ -275,7 +244,7 @@ pub(crate) fn open_manually<'start>(
275244 }
276245
277246 #[ cfg( not( feature = "no_racy_asserts" ) ) ]
278- check_open ( & start_clone, path, options, & canonical_path, & base) ;
247+ check_open_manually ( & start_clone, path, options, & canonical_path, & base) ;
279248
280249 canonical_path. complete ( ) ;
281250 Ok ( base)
@@ -290,7 +259,7 @@ fn should_emulate_o_path(use_options: &OpenOptions) -> bool {
290259}
291260
292261#[ cfg( not( feature = "no_racy_asserts" ) ) ]
293- fn check_open (
262+ fn check_open_manually (
294263 start : & fs:: File ,
295264 path : & Path ,
296265 options : & OpenOptions ,
@@ -332,20 +301,3 @@ fn check_open(
332301 }
333302 }
334303}
335-
336- #[ derive( Debug ) ]
337- pub ( crate ) enum OpenUncheckedError {
338- Other ( io:: Error ) ,
339- Symlink ( io:: Error ) ,
340- NotFound ( io:: Error ) ,
341- }
342-
343- impl From < OpenUncheckedError > for io:: Error {
344- fn from ( error : OpenUncheckedError ) -> Self {
345- match error {
346- OpenUncheckedError :: Other ( err)
347- | OpenUncheckedError :: Symlink ( err)
348- | OpenUncheckedError :: NotFound ( err) => err,
349- }
350- }
351- }
0 commit comments