@@ -2108,6 +2108,92 @@ pub fn set_permissions<P: AsRef<Path>>(path: P, perm: Permissions) -> io::Result
2108
2108
fs_imp:: set_perm ( path. as_ref ( ) , perm. 0 )
2109
2109
}
2110
2110
2111
+ /// Returns `true` if the path points at an existing entity.
2112
+ ///
2113
+ /// This function will traverse symbolic links to query information about the
2114
+ /// destination file. In case of broken symbolic links this will return `false`.
2115
+ ///
2116
+ /// If you cannot access the directory containing the file, e.g., because of a
2117
+ /// permission error, this will return `false`.
2118
+ ///
2119
+ /// # Examples
2120
+ ///
2121
+ /// ```no_run
2122
+ /// use std::path::Path;
2123
+ /// assert!(!Path::new("does_not_exist.txt").exists());
2124
+ /// ```
2125
+ ///
2126
+ /// # See Also
2127
+ ///
2128
+ /// This is a convenience function that coerces errors to false. If you want to
2129
+ /// check errors, call [`metadata`].
2130
+ #[ stable( feature = "fs_path_convenience" , since = "1.51.0" ) ]
2131
+ pub fn exists < P : AsRef < Path > > ( path : P ) -> bool {
2132
+ metadata ( path) . is_ok ( )
2133
+ }
2134
+
2135
+ /// Returns `true` if the path exists on disk and is pointing at a regular file.
2136
+ ///
2137
+ /// This function will traverse symbolic links to query information about the
2138
+ /// destination file. In case of broken symbolic links this will return `false`.
2139
+ ///
2140
+ /// If you cannot access the directory containing the file, e.g., because of a
2141
+ /// permission error, this will return `false`.
2142
+ ///
2143
+ /// # Examples
2144
+ ///
2145
+ /// ```no_run
2146
+ /// use std::path::Path;
2147
+ /// assert_eq!(Path::new("./is_a_directory/").is_file(), false);
2148
+ /// assert_eq!(Path::new("a_file.txt").is_file(), true);
2149
+ /// ```
2150
+ ///
2151
+ /// # See Also
2152
+ ///
2153
+ /// This is a convenience function that coerces errors to false. If you want to
2154
+ /// check errors, call [`metadata`] and handle its [`Result`]. Then call
2155
+ /// [`Metadata::is_file`] if it was [`Ok`].
2156
+ ///
2157
+ /// When the goal is simply to read from (or write to) the source, the most
2158
+ /// reliable way to test the source can be read (or written to) is to open
2159
+ /// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
2160
+ /// a Unix-like system for example. See [`File::open`] or
2161
+ /// [`OpenOptions::open`] for more information.
2162
+ #[ stable( feature = "fs_path_convenience" , since = "1.51.0" ) ]
2163
+ pub fn is_file < P : AsRef < Path > > ( path : P ) -> bool {
2164
+ metadata ( path) . map ( |m| m. is_file ( ) ) . unwrap_or ( false )
2165
+ }
2166
+
2167
+ /// Returns `true` if the path exists on disk and is pointing at a directory.
2168
+ ///
2169
+ /// This function will traverse symbolic links to query information about the
2170
+ /// destination file. In case of broken symbolic links this will return `false`.
2171
+ ///
2172
+ /// If you cannot access the directory containing the file, e.g., because of a
2173
+ /// permission error, this will return `false`.
2174
+ ///
2175
+ /// # Examples
2176
+ ///
2177
+ /// ```no_run
2178
+ /// use std::path::Path;
2179
+ /// assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
2180
+ /// assert_eq!(Path::new("a_file.txt").is_dir(), false);
2181
+ /// ```
2182
+ ///
2183
+ /// # See Also
2184
+ ///
2185
+ /// This is a convenience function that coerces errors to false. If you want to
2186
+ /// check errors, call [`metadata`] and handle its [`Result`]. Then call
2187
+ /// [`Metadata::is_dir`] if it was [`Ok`].
2188
+ ///
2189
+ /// When the goal is simply to read from the source, the most reliable way to
2190
+ /// test the source can be read is to open it. See [`read_dir`] for more
2191
+ /// information.
2192
+ #[ stable( feature = "fs_path_convenience" , since = "1.51.0" ) ]
2193
+ pub fn is_dir < P : AsRef < Path > > ( path : P ) -> bool {
2194
+ metadata ( path) . map ( |m| m. is_dir ( ) ) . unwrap_or ( false )
2195
+ }
2196
+
2111
2197
impl DirBuilder {
2112
2198
/// Creates a new set of options with default mode/security settings for all
2113
2199
/// platforms and also non-recursive.
@@ -2160,7 +2246,7 @@ impl DirBuilder {
2160
2246
/// .recursive(true)
2161
2247
/// .create(path).unwrap();
2162
2248
///
2163
- /// assert!(fs::metadata(path).unwrap(). is_dir());
2249
+ /// assert!(fs::is_dir(path ));
2164
2250
/// ```
2165
2251
#[ stable( feature = "dir_builder" , since = "1.6.0" ) ]
2166
2252
pub fn create < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < ( ) > {
@@ -2179,7 +2265,7 @@ impl DirBuilder {
2179
2265
match self . inner . mkdir ( path) {
2180
2266
Ok ( ( ) ) => return Ok ( ( ) ) ,
2181
2267
Err ( ref e) if e. kind ( ) == io:: ErrorKind :: NotFound => { }
2182
- Err ( _) if metadata ( path ) . map ( |m| m . is_dir ( ) ) . unwrap_or ( false ) => return Ok ( ( ) ) ,
2268
+ Err ( _) if is_dir ( path ) => return Ok ( ( ) ) ,
2183
2269
Err ( e) => return Err ( e) ,
2184
2270
}
2185
2271
match path. parent ( ) {
@@ -2190,7 +2276,7 @@ impl DirBuilder {
2190
2276
}
2191
2277
match self . inner . mkdir ( path) {
2192
2278
Ok ( ( ) ) => Ok ( ( ) ) ,
2193
- Err ( _) if metadata ( path ) . map ( |m| m . is_dir ( ) ) . unwrap_or ( false ) => Ok ( ( ) ) ,
2279
+ Err ( _) if is_dir ( path ) => Ok ( ( ) ) ,
2194
2280
Err ( e) => Err ( e) ,
2195
2281
}
2196
2282
}
0 commit comments