4
4
#[ cfg( not( feature = "no_racy_asserts" ) ) ]
5
5
use crate :: fs:: is_same_file;
6
6
use 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 ,
9
10
} ;
10
11
use std:: {
11
- borrow:: Cow ,
12
12
ffi:: OsStr ,
13
13
fs, io,
14
14
path:: { Component , Path , PathBuf } ,
15
15
} ;
16
16
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
-
48
17
/// Utility for collecting the canonical path components.
49
18
struct CanonicalPath < ' path_buf > {
50
19
/// If the user requested a canonical path, a reference to the `PathBuf` to
@@ -122,8 +91,8 @@ pub(crate) fn open_manually_wrapper(
122
91
) -> io:: Result < fs:: File > {
123
92
let mut symlink_count = 0 ;
124
93
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)
127
96
}
128
97
129
98
/// Implement `open` by breaking up the path into components, resolving each
@@ -171,7 +140,7 @@ pub(crate) fn open_manually<'start>(
171
140
match c {
172
141
CowComponent :: PrefixOrRootDir => return Err ( errors:: escape_attempt ( ) ) ,
173
142
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.
175
144
if components. is_empty ( ) {
176
145
if dir_precluded {
177
146
return Err ( errors:: is_directory ( ) ) ;
@@ -204,7 +173,7 @@ pub(crate) fn open_manually<'start>(
204
173
assert ! ( canonical_path. pop( ) ) ;
205
174
}
206
175
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.
208
177
if components. is_empty ( ) && dir_required && dir_precluded {
209
178
return Err ( errors:: is_directory ( ) ) ;
210
179
}
@@ -275,7 +244,7 @@ pub(crate) fn open_manually<'start>(
275
244
}
276
245
277
246
#[ 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) ;
279
248
280
249
canonical_path. complete ( ) ;
281
250
Ok ( base)
@@ -290,7 +259,7 @@ fn should_emulate_o_path(use_options: &OpenOptions) -> bool {
290
259
}
291
260
292
261
#[ cfg( not( feature = "no_racy_asserts" ) ) ]
293
- fn check_open (
262
+ fn check_open_manually (
294
263
start : & fs:: File ,
295
264
path : & Path ,
296
265
options : & OpenOptions ,
@@ -332,20 +301,3 @@ fn check_open(
332
301
}
333
302
}
334
303
}
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