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
use std::io;
use std::mem;
use winapi::shared::minwindef::FILETIME;
use winapi::shared::winerror::NO_ERROR;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::fileapi::{
GetFileInformationByHandle, GetFileType, BY_HANDLE_FILE_INFORMATION,
};
use winapi::um::winnt;
use crate::AsHandleRef;
pub fn information<H: AsHandleRef>(h: H) -> io::Result<Information> {
unsafe {
let mut info: BY_HANDLE_FILE_INFORMATION = mem::zeroed();
let rc = GetFileInformationByHandle(h.as_raw(), &mut info);
if rc == 0 {
return Err(io::Error::last_os_error());
};
Ok(Information(info))
}
}
pub fn typ<H: AsHandleRef>(h: H) -> io::Result<Type> {
unsafe {
let rc = GetFileType(h.as_raw());
if rc == 0 && GetLastError() != NO_ERROR {
return Err(io::Error::last_os_error());
}
Ok(Type(rc))
}
}
pub fn is_hidden(file_attributes: u64) -> bool {
file_attributes & (winnt::FILE_ATTRIBUTE_HIDDEN as u64) > 0
}
#[derive(Clone)]
pub struct Information(BY_HANDLE_FILE_INFORMATION);
impl Information {
pub fn file_attributes(&self) -> u64 {
self.0.dwFileAttributes as u64
}
pub fn is_hidden(&self) -> bool {
is_hidden(self.file_attributes())
}
pub fn creation_time(&self) -> Option<u64> {
filetime_to_u64(self.0.ftCreationTime)
}
pub fn last_access_time(&self) -> Option<u64> {
filetime_to_u64(self.0.ftLastAccessTime)
}
pub fn last_write_time(&self) -> Option<u64> {
filetime_to_u64(self.0.ftLastWriteTime)
}
pub fn volume_serial_number(&self) -> u64 {
self.0.dwVolumeSerialNumber as u64
}
pub fn file_size(&self) -> u64 {
((self.0.nFileSizeHigh as u64) << 32) | (self.0.nFileSizeLow as u64)
}
pub fn number_of_links(&self) -> u64 {
self.0.nNumberOfLinks as u64
}
pub fn file_index(&self) -> u64 {
((self.0.nFileIndexHigh as u64) << 32) | (self.0.nFileIndexLow as u64)
}
}
#[derive(Clone)]
pub struct Type(u32);
impl Type {
pub fn is_char(&self) -> bool {
self.0 == ::winapi::um::winbase::FILE_TYPE_CHAR
}
pub fn is_disk(&self) -> bool {
self.0 == ::winapi::um::winbase::FILE_TYPE_DISK
}
pub fn is_pipe(&self) -> bool {
self.0 == ::winapi::um::winbase::FILE_TYPE_PIPE
}
pub fn is_unknown(&self) -> bool {
self.0 == ::winapi::um::winbase::FILE_TYPE_UNKNOWN
}
}
fn filetime_to_u64(t: FILETIME) -> Option<u64> {
let v = ((t.dwHighDateTime as u64) << 32) | (t.dwLowDateTime as u64);
if v == 0 {
None
} else {
Some(v)
}
}