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
use super::open_options_to_std;
use crate::ambient_authority;
use crate::fs::{
open, open_ambient_dir, FileType, FollowSymlinks, ImplFileTypeExt, Metadata, OpenOptions,
ReadDir, ReadDirInner,
};
use std::ffi::OsString;
use std::os::windows::fs::OpenOptionsExt;
use std::{fmt, fs, io};
use windows_sys::Win32::Storage::FileSystem::{
FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT,
};
pub(crate) struct DirEntryInner {
std: fs::DirEntry,
}
impl DirEntryInner {
#[inline]
pub(crate) fn open(&self, options: &OpenOptions) -> io::Result<fs::File> {
match options.follow {
FollowSymlinks::No => {
let (opts, manually_trunc) = open_options_to_std(options);
let file = opts.open(self.std.path())?;
if manually_trunc {
file.set_len(0).unwrap();
}
Ok(file)
}
FollowSymlinks::Yes => {
let path = self.std.path();
open(
&open_ambient_dir(path.parent().unwrap(), ambient_authority())?,
path.file_name().unwrap().as_ref(),
options,
)
}
}
}
#[inline]
pub(crate) fn metadata(&self) -> io::Result<Metadata> {
self.std.metadata().map(Metadata::from_just_metadata)
}
#[inline]
pub(crate) fn full_metadata(&self) -> io::Result<Metadata> {
let mut opts = OpenOptions::new();
opts.access_mode(0);
opts.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS);
opts.follow(FollowSymlinks::No);
let opened = self.open(&opts)?;
Metadata::from_file(&opened)
}
#[inline]
pub(crate) fn remove_file(&self) -> io::Result<()> {
fs::remove_file(self.std.path())
}
#[inline]
pub(crate) fn remove_dir(&self) -> io::Result<()> {
fs::remove_dir(self.std.path())
}
#[inline]
pub(crate) fn read_dir(&self, follow: FollowSymlinks) -> io::Result<ReadDir> {
assert_eq!(
follow,
FollowSymlinks::Yes,
"`read_dir` without following symlinks is not implemented yet"
);
let std = fs::read_dir(self.std.path())?;
let inner = ReadDirInner::from_std(std);
Ok(ReadDir { inner })
}
#[inline]
pub(crate) fn file_type(&self) -> io::Result<FileType> {
self.std.file_type().map(ImplFileTypeExt::from_std)
}
#[inline]
pub(crate) fn file_name(&self) -> OsString {
self.std.file_name()
}
#[inline]
#[cfg(windows_by_handle)]
pub(crate) fn is_same_file(&self, metadata: &Metadata) -> io::Result<bool> {
Ok(Metadata::from_just_metadata(fs::metadata(self.std.path())?).is_same_file(metadata))
}
#[inline]
pub(super) fn from_std(std: fs::DirEntry) -> Self {
Self { std }
}
}
impl fmt::Debug for DirEntryInner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("DirEntry").field(&self.file_name()).finish()
}
}
#[doc(hidden)]
impl crate::fs::_WindowsDirEntryExt for crate::fs::DirEntry {
#[inline]
fn full_metadata(&self) -> io::Result<Metadata> {
DirEntryInner::full_metadata(&self.inner)
}
}