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
use std::fs::{File, OpenOptions};
use std::hash::{Hash, Hasher};
use std::io;
use std::mem;
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::{
AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle,
};
use std::path::Path;
use winapi::shared::minwindef::DWORD;
use winapi::um::fileapi::{
BY_HANDLE_FILE_INFORMATION,
GetFileInformationByHandle,
};
use winapi::um::processenv::GetStdHandle;
use winapi::um::winbase::{
FILE_FLAG_BACKUP_SEMANTICS,
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE,
};
#[derive(Debug)]
pub struct Handle {
file: Option<File>,
is_std: bool,
key: Option<Key>,
}
#[derive(Debug, Eq, PartialEq, Hash)]
struct Key {
volume: DWORD,
idx_high: DWORD,
idx_low: DWORD,
}
impl Drop for Handle {
fn drop(&mut self) {
if self.is_std {
self.file.take().unwrap().into_raw_handle();
}
}
}
impl Eq for Handle {}
impl PartialEq for Handle {
fn eq(&self, other: &Handle) -> bool {
if self as *const Handle == other as *const Handle {
return true;
} else if self.key.is_none() || other.key.is_none() {
return false;
}
self.key == other.key
}
}
impl AsRawHandle for ::Handle {
fn as_raw_handle(&self) -> RawHandle {
self.0.file.as_ref().take().unwrap().as_raw_handle()
}
}
impl IntoRawHandle for ::Handle {
fn into_raw_handle(mut self) -> RawHandle {
self.0.file.take().unwrap().into_raw_handle()
}
}
impl Hash for Handle {
fn hash<H: Hasher>(&self, state: &mut H) {
self.key.hash(state);
}
}
impl Handle {
pub fn from_path<P: AsRef<Path>>(p: P) -> io::Result<Handle> {
let file = OpenOptions::new()
.read(true)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.open(p)?;
Handle::from_file(file)
}
pub fn from_file(file: File) -> io::Result<Handle> {
file_info(&file).map(|info| Handle::from_file_info(file, false, info))
}
fn from_std_handle(file: File) -> io::Result<Handle> {
match file_info(&file) {
Ok(info) => Ok(Handle::from_file_info(file, true, info)),
Err(_) => Ok(Handle { file: Some(file), is_std: true, key: None }),
}
}
fn from_file_info(
file: File,
is_std: bool,
info: BY_HANDLE_FILE_INFORMATION,
) -> Handle {
Handle {
file: Some(file),
is_std: is_std,
key: Some(Key {
volume: info.dwVolumeSerialNumber,
idx_high: info.nFileIndexHigh,
idx_low: info.nFileIndexLow,
}),
}
}
pub fn stdin() -> io::Result<Handle> {
Handle::from_std_handle(unsafe {
File::from_raw_handle(GetStdHandle(STD_INPUT_HANDLE))
})
}
pub fn stdout() -> io::Result<Handle> {
Handle::from_std_handle(unsafe {
File::from_raw_handle(GetStdHandle(STD_OUTPUT_HANDLE))
})
}
pub fn stderr() -> io::Result<Handle> {
Handle::from_std_handle(unsafe {
File::from_raw_handle(GetStdHandle(STD_ERROR_HANDLE))
})
}
pub fn as_file(&self) -> &File {
self.file.as_ref().take().unwrap()
}
pub fn as_file_mut(&mut self) -> &mut File {
self.file.as_mut().take().unwrap()
}
}
fn file_info(file: &File) -> io::Result<BY_HANDLE_FILE_INFORMATION> {
let (r, info) = unsafe {
let mut info: BY_HANDLE_FILE_INFORMATION = mem::zeroed();
(GetFileInformationByHandle(file.as_raw_handle(), &mut info), info)
};
if r == 0 {
Err(io::Error::last_os_error())
} else {
Ok(info)
}
}