1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
use std::ffi::OsStr;
use std::fmt;
use std::fs::{self, FileType};
use std::path::{Path, PathBuf};
use crate::error::Error;
use crate::Result;
/// A directory entry.
///
/// This is the type of value that is yielded from the iterators defined in
/// this crate.
///
/// On Unix systems, this type implements the [`DirEntryExt`] trait, which
/// provides efficient access to the inode number of the directory entry.
///
/// # Differences with `std::fs::DirEntry`
///
/// This type mostly mirrors the type by the same name in [`std::fs`]. There
/// are some differences however:
///
/// * All recursive directory iterators must inspect the entry's type.
/// Therefore, the value is stored and its access is guaranteed to be cheap and
/// successful.
/// * [`path`] and [`file_name`] return borrowed variants.
/// * If [`follow_links`] was enabled on the originating iterator, then all
/// operations except for [`path`] operate on the link target. Otherwise, all
/// operations operate on the symbolic link.
///
/// [`std::fs`]: https://doc.rust-lang.org/stable/std/fs/index.html
/// [`path`]: #method.path
/// [`file_name`]: #method.file_name
/// [`follow_links`]: struct.WalkDir.html#method.follow_links
/// [`DirEntryExt`]: trait.DirEntryExt.html
pub struct DirEntry {
/// The path as reported by the [`fs::ReadDir`] iterator (even if it's a
/// symbolic link).
///
/// [`fs::ReadDir`]: https://doc.rust-lang.org/stable/std/fs/struct.ReadDir.html
path: PathBuf,
/// The file type. Necessary for recursive iteration, so store it.
ty: FileType,
/// Is set when this entry was created from a symbolic link and the user
/// expects the iterator to follow symbolic links.
follow_link: bool,
/// The depth at which this entry was generated relative to the root.
depth: usize,
/// The underlying inode number (Unix only).
#[cfg(unix)]
ino: u64,
/// The underlying metadata (Windows only). We store this on Windows
/// because this comes for free while reading a directory.
///
/// We use this to determine whether an entry is a directory or not, which
/// works around a bug in Rust's standard library:
/// https://github.com/rust-lang/rust/issues/46484
#[cfg(windows)]
metadata: fs::Metadata,
}
impl DirEntry {
/// The full path that this entry represents.
///
/// The full path is created by joining the parents of this entry up to the
/// root initially given to [`WalkDir::new`] with the file name of this
/// entry.
///
/// Note that this *always* returns the path reported by the underlying
/// directory entry, even when symbolic links are followed. To get the
/// target path, use [`path_is_symlink`] to (cheaply) check if this entry
/// corresponds to a symbolic link, and [`std::fs::read_link`] to resolve
/// the target.
///
/// [`WalkDir::new`]: struct.WalkDir.html#method.new
/// [`path_is_symlink`]: struct.DirEntry.html#method.path_is_symlink
/// [`std::fs::read_link`]: https://doc.rust-lang.org/stable/std/fs/fn.read_link.html
pub fn path(&self) -> &Path {
&self.path
}
/// The full path that this entry represents.
///
/// Analogous to [`path`], but moves ownership of the path.
///
/// [`path`]: struct.DirEntry.html#method.path
pub fn into_path(self) -> PathBuf {
self.path
}
/// Returns `true` if and only if this entry was created from a symbolic
/// link. This is unaffected by the [`follow_links`] setting.
///
/// When `true`, the value returned by the [`path`] method is a
/// symbolic link name. To get the full target path, you must call
/// [`std::fs::read_link(entry.path())`].
///
/// [`path`]: struct.DirEntry.html#method.path
/// [`follow_links`]: struct.WalkDir.html#method.follow_links
/// [`std::fs::read_link(entry.path())`]: https://doc.rust-lang.org/stable/std/fs/fn.read_link.html
pub fn path_is_symlink(&self) -> bool {
self.ty.is_symlink() || self.follow_link
}
/// Return the metadata for the file that this entry points to.
///
/// This will follow symbolic links if and only if the [`WalkDir`] value
/// has [`follow_links`] enabled.
///
/// # Platform behavior
///
/// This always calls [`std::fs::symlink_metadata`].
///
/// If this entry is a symbolic link and [`follow_links`] is enabled, then
/// [`std::fs::metadata`] is called instead.
///
/// # Errors
///
/// Similar to [`std::fs::metadata`], returns errors for path values that
/// the program does not have permissions to access or if the path does not
/// exist.
///
/// [`WalkDir`]: struct.WalkDir.html
/// [`follow_links`]: struct.WalkDir.html#method.follow_links
/// [`std::fs::metadata`]: https://doc.rust-lang.org/std/fs/fn.metadata.html
/// [`std::fs::symlink_metadata`]: https://doc.rust-lang.org/stable/std/fs/fn.symlink_metadata.html
pub fn metadata(&self) -> Result<fs::Metadata> {
self.metadata_internal()
}
#[cfg(windows)]
fn metadata_internal(&self) -> Result<fs::Metadata> {
if self.follow_link {
fs::metadata(&self.path)
} else {
Ok(self.metadata.clone())
}
.map_err(|err| Error::from_entry(self, err))
}
#[cfg(not(windows))]
fn metadata_internal(&self) -> Result<fs::Metadata> {
if self.follow_link {
fs::metadata(&self.path)
} else {
fs::symlink_metadata(&self.path)
}
.map_err(|err| Error::from_entry(self, err))
}
/// Return the file type for the file that this entry points to.
///
/// If this is a symbolic link and [`follow_links`] is `true`, then this
/// returns the type of the target.
///
/// This never makes any system calls.
///
/// [`follow_links`]: struct.WalkDir.html#method.follow_links
pub fn file_type(&self) -> fs::FileType {
self.ty
}
/// Return the file name of this entry.
///
/// If this entry has no file name (e.g., `/`), then the full path is
/// returned.
pub fn file_name(&self) -> &OsStr {
self.path.file_name().unwrap_or_else(|| self.path.as_os_str())
}
/// Returns the depth at which this entry was created relative to the root.
///
/// The smallest depth is `0` and always corresponds to the path given
/// to the `new` function on `WalkDir`. Its direct descendents have depth
/// `1`, and their descendents have depth `2`, and so on.
pub fn depth(&self) -> usize {
self.depth
}
/// Returns true if and only if this entry points to a directory.
///
/// This works around a bug in Rust's standard library:
/// https://github.com/rust-lang/rust/issues/46484
#[cfg(windows)]
pub(crate) fn is_dir(&self) -> bool {
use std::os::windows::fs::MetadataExt;
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
self.metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0
}
/// Returns true if and only if this entry points to a directory.
#[cfg(not(windows))]
pub(crate) fn is_dir(&self) -> bool {
self.ty.is_dir()
}
#[cfg(windows)]
pub(crate) fn from_entry(
depth: usize,
ent: &fs::DirEntry,
) -> Result<DirEntry> {
let path = ent.path();
let ty = ent
.file_type()
.map_err(|err| Error::from_path(depth, path.clone(), err))?;
let md = ent
.metadata()
.map_err(|err| Error::from_path(depth, path.clone(), err))?;
Ok(DirEntry {
path: path,
ty: ty,
follow_link: false,
depth: depth,
metadata: md,
})
}
#[cfg(unix)]
pub(crate) fn from_entry(
depth: usize,
ent: &fs::DirEntry,
) -> Result<DirEntry> {
use std::os::unix::fs::DirEntryExt;
let ty = ent
.file_type()
.map_err(|err| Error::from_path(depth, ent.path(), err))?;
Ok(DirEntry {
path: ent.path(),
ty: ty,
follow_link: false,
depth: depth,
ino: ent.ino(),
})
}
#[cfg(not(any(unix, windows)))]
pub(crate) fn from_entry(
depth: usize,
ent: &fs::DirEntry,
) -> Result<DirEntry> {
let ty = ent
.file_type()
.map_err(|err| Error::from_path(depth, ent.path(), err))?;
Ok(DirEntry {
path: ent.path(),
ty: ty,
follow_link: false,
depth: depth,
})
}
#[cfg(windows)]
pub(crate) fn from_path(
depth: usize,
pb: PathBuf,
follow: bool,
) -> Result<DirEntry> {
let md = if follow {
fs::metadata(&pb)
.map_err(|err| Error::from_path(depth, pb.clone(), err))?
} else {
fs::symlink_metadata(&pb)
.map_err(|err| Error::from_path(depth, pb.clone(), err))?
};
Ok(DirEntry {
path: pb,
ty: md.file_type(),
follow_link: follow,
depth: depth,
metadata: md,
})
}
#[cfg(unix)]
pub(crate) fn from_path(
depth: usize,
pb: PathBuf,
follow: bool,
) -> Result<DirEntry> {
use std::os::unix::fs::MetadataExt;
let md = if follow {
fs::metadata(&pb)
.map_err(|err| Error::from_path(depth, pb.clone(), err))?
} else {
fs::symlink_metadata(&pb)
.map_err(|err| Error::from_path(depth, pb.clone(), err))?
};
Ok(DirEntry {
path: pb,
ty: md.file_type(),
follow_link: follow,
depth: depth,
ino: md.ino(),
})
}
#[cfg(not(any(unix, windows)))]
pub(crate) fn from_path(
depth: usize,
pb: PathBuf,
follow: bool,
) -> Result<DirEntry> {
let md = if follow {
fs::metadata(&pb)
.map_err(|err| Error::from_path(depth, pb.clone(), err))?
} else {
fs::symlink_metadata(&pb)
.map_err(|err| Error::from_path(depth, pb.clone(), err))?
};
Ok(DirEntry {
path: pb,
ty: md.file_type(),
follow_link: follow,
depth: depth,
})
}
}
impl Clone for DirEntry {
#[cfg(windows)]
fn clone(&self) -> DirEntry {
DirEntry {
path: self.path.clone(),
ty: self.ty,
follow_link: self.follow_link,
depth: self.depth,
metadata: self.metadata.clone(),
}
}
#[cfg(unix)]
fn clone(&self) -> DirEntry {
DirEntry {
path: self.path.clone(),
ty: self.ty,
follow_link: self.follow_link,
depth: self.depth,
ino: self.ino,
}
}
#[cfg(not(any(unix, windows)))]
fn clone(&self) -> DirEntry {
DirEntry {
path: self.path.clone(),
ty: self.ty,
follow_link: self.follow_link,
depth: self.depth,
}
}
}
impl fmt::Debug for DirEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DirEntry({:?})", self.path)
}
}
/// Unix-specific extension methods for `walkdir::DirEntry`
#[cfg(unix)]
pub trait DirEntryExt {
/// Returns the underlying `d_ino` field in the contained `dirent`
/// structure.
fn ino(&self) -> u64;
}
#[cfg(unix)]
impl DirEntryExt for DirEntry {
/// Returns the underlying `d_ino` field in the contained `dirent`
/// structure.
fn ino(&self) -> u64 {
self.ino
}
}