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
// Copyright (c) Mike Grier.
//! The close entries.
//!
//! Entry 4 of the audited catalogue, and the one whose membership surprises
//! people.
//!
//! # Why closing is a catalogue entry at all
//!
//! `CloseHandle` looks like bookkeeping, but it is a blocking namespace call.
//! It waits for outstanding I/O on the handle to complete, and on a dead
//! network path or an ejected removable device it can block hard -- which is
//! the whole reason this facility exists. A consumer that carefully moved its
//! opens onto a worker and then closed on its own thread would have moved the
//! wrong half.
//!
//! # A handle carries its close routine
//!
//! The audit found that a close entry **cannot assume its routine**:
//! `FindCloseChangeNotification` closes an
//! [`crate::watch::ChangeNotification`] and `CloseHandle` is wrong for it,
//! silently. So the routine travels with the handle rather than being chosen at
//! the call site, which is the same shape
//! [windows-threadpool-sys](https://docs.rs/windows-threadpool-sys) already
//! needed for wait targets.
//!
//! # A request is consumed by performing it
//!
//! [`CloseRequest::perform`] takes `self`, so a handle cannot be closed twice
//! through this type. An unperformed request still closes its handle when
//! dropped, because the alternative is a leak: a request that quietly did
//! nothing would be worse than one that closes late.
use c_void;
use fmt;
use ManuallyDrop;
use ;
use ;
use FindCloseChangeNotification;
use BOOL;
use crate;
use crateChangeNotification;
/// A Win32 routine that closes a handle.
///
/// This is the shape Win32 close routines already have, so one can be passed
/// directly with no shim: `CloseHandle` and `FindCloseChangeNotification` both
/// match it.
pub type CloseFn = unsafe extern "system" fn ;
/// An owned, marshalable request to close one handle.
///
/// The request owns the handle it will close, so the handle cannot be closed by
/// anyone else in the meantime, and cannot outlive the request unclosed.
///
/// # Example
///
/// ```
/// use std::fs;
///
/// use windows_namespace_request_sys::close::CloseRequest;
///
/// let path = std::env::temp_dir().join(format!("wnrs-close-{}.tmp", std::process::id()));
/// fs::write(&path, b"example")?;
/// let file = fs::File::open(&path)?;
///
/// // The close is a value now, so it can be performed wherever blocking is
/// // acceptable rather than wherever the handle happens to be dropped.
/// let request = CloseRequest::for_handle(file.into());
/// request.perform()?;
/// # fs::remove_file(&path)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: the routine travels with the handle
///
/// A change notification is closed with `FindCloseChangeNotification`, and
/// `CloseHandle` is silently wrong for it. A caller never has to know that,
/// because the constructor pairs them:
///
/// ```
/// use std::fs;
///
/// use windows_namespace_request_sys::close::CloseRequest;
/// use windows_namespace_request_sys::prepare;
/// use windows_namespace_request_sys::watch::{NotifyFilter, WatchDirectory};
/// use wtf_string::Wtf16String;
///
/// let directory = std::env::temp_dir().join(format!("wnrs-cr-{}", std::process::id()));
/// let _ = fs::remove_dir_all(&directory);
/// fs::create_dir_all(&directory)?;
/// let text = directory.to_str().expect("a temporary path is valid UTF-8");
///
/// let notification = WatchDirectory::new(prepare(&Wtf16String::from(text))?)
/// .with_filter(NotifyFilter::FILE_NAME)
/// .perform()?;
///
/// let request = CloseRequest::for_change_notification(notification);
/// assert!(format!("{request:?}").contains("FindCloseChangeNotification"));
/// request.perform()?;
/// # let _ = fs::remove_dir_all(&directory);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Example: performing consumes the request
///
/// This is what makes closing twice through this type impossible -- the second
/// call does not compile:
///
/// ```compile_fail
/// use std::fs;
///
/// use windows_namespace_request_sys::close::CloseRequest;
///
/// let path = std::env::temp_dir().join("wnrs-doc-double-close.tmp");
/// fs::write(&path, b"x").unwrap();
/// let request = CloseRequest::for_handle(fs::File::open(&path).unwrap().into());
///
/// request.perform().unwrap();
/// request.perform().unwrap(); // error: use of moved value
/// ```
// SAFETY: the request owns its handle exclusively and has no interior
// mutability. A Windows handle is process-wide rather than thread-affine, so a
// close performed on another thread closes the same object; the raw pointer is
// what blocks the automatic derivation.
unsafe
// SAFETY: as above. Every method that could close the handle takes `self`.
unsafe