-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfile_system_handler.rs
483 lines (455 loc) · 16.5 KB
/
file_system_handler.rs
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use dokan_sys::DOKAN_IO_SECURITY_CONTEXT;
use widestring::U16CStr;
use winapi::{
shared::{ntdef::NTSTATUS, ntstatus::STATUS_NOT_IMPLEMENTED},
um::winnt::{ACCESS_MASK, PSECURITY_DESCRIPTOR},
};
use crate::data::{
CreateFileInfo, DiskSpaceInfo, FileInfo, FileTimeOperation, FillDataResult, FindData,
FindStreamData, OperationInfo, VolumeInfo,
};
/// Returned by [`FileSystemHandler`]'s methods.
pub type OperationResult<T> = Result<T, NTSTATUS>;
/// Handles operations for a mounted file system.
///
/// Dokan invokes the callback functions in this trait to handle file system operations. These
/// functions have similar semantics to that of corresponding Windows API functions.
///
/// Implementation of most callback functions can be omitted by returning `Err(`[`STATUS_NOT_IMPLEMENTED`]`)`
/// if the corresponding feature is not supported. To make things flexible, all of the functions are
/// provided with a default implementation which is a no-op and returns `Err(`[`STATUS_NOT_IMPLEMENTED`]`)`
/// (except [`cleanup`] and [`close_file`] which don't have return values). However, omitting the
/// implementation of some important callbacks such as [`create_file`] will make the file system
/// unusable.
///
/// `Err` type is [`NTSTATUS`]. Use [`map_win32_error_to_ntstatus`] to convert from Win32 errors
/// (e.g. returned by [`GetLastError`]).
///
/// [`cleanup`]: Self::cleanup
/// [`close_file`]: Self::close_file
/// [`create_file`]: Self::create_file
/// [`map_win32_error_to_ntstatus`]: crate::map_win32_error_to_ntstatus
/// [`GetLastError`]: winapi::um::errhandlingapi::GetLastError
#[allow(unused_variables)]
pub trait FileSystemHandler<'c, 'h: 'c>: Sync + Sized + 'h {
/// Type of the context associated with an open file object.
type Context: Sync + 'c;
/// Called when a file object is created.
///
/// The flags p-them to flags accepted by [`CreateFile`] using the
/// [`map_kernel_to_user_create_file_flags`] helper function.
///
/// [`ZwCreateFile`]: https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-zwcreatefile
/// [`CreateFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
/// [`map_kernel_to_user_create_file_flags`]: crate::map_kernel_to_user_create_file_flags
fn create_file(
&'h self,
file_name: &U16CStr,
security_context: &DOKAN_IO_SECURITY_CONTEXT,
desired_access: ACCESS_MASK,
file_attributes: u32,
share_access: u32,
create_disposition: u32,
create_options: u32,
info: &mut OperationInfo<'c, 'h, Self>,
) -> OperationResult<CreateFileInfo<Self::Context>> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Called when the last handle for the file object has been closed.
///
/// If [`info.delete_on_close`] returns `true`, the file should be deleted in this function. As the function doesn't
/// have a return value, you should make sure the file is deletable in [`delete_file`] or [`delete_directory`].
///
/// Note that the file object hasn't been released and there might be more I/O operations before
/// [`close_file`] gets called. (This typically happens when the file is memory-mapped.)
///
/// Normally [`close_file`] will be called shortly after this function. However, the file object
/// may also be reused, and in that case [`create_file`] will be called instead.
///
/// [`info.delete_on_close`]: OperationInfo::delete_on_close
/// [`delete_file`]: Self::delete_file
/// [`delete_directory`]: Self::delete_directory
/// [`close_file`]: Self::close_file
/// [`create_file`]: Self::create_file
fn cleanup(
&'h self,
file_name: &U16CStr,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) {
}
/// Called when the last handle for the handle object has been closed and released.
///
/// This is the last function called during the lifetime of the file object. You can safely
/// release any resources allocated for it (such as file handles, buffers, etc.). The associated
/// [`context`] object will also be dropped once this function returns. In case the file object is
/// reused and thus this function isn't called, the [`context`] will be dropped before
/// [`create_file`] gets called.
///
/// [`context`]: Self::Context
/// [`create_file`]: Self::create_file
fn close_file(
&'h self,
file_name: &U16CStr,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) {
}
/// Reads data from the file.
///
/// The number of bytes that actually gets read should be returned.
///
/// See [`ReadFile`] for more information.
///
/// [`ReadFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfile
fn read_file(
&'h self,
file_name: &U16CStr,
offset: i64,
buffer: &mut [u8],
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<u32> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Writes data to the file.
///
/// The number of bytes that actually gets written should be returned.
///
/// If [`info.write_to_eof`] returns `true`, data should be written to the end of file and the
/// `offset` parameter should be ignored.
///
/// See [`WriteFile`] for more information.
///
/// [`info.write_to_eof`]: OperationInfo::write_to_eof
/// [`WriteFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
fn write_file(
&'h self,
file_name: &U16CStr,
offset: i64,
buffer: &[u8],
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<u32> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Flushes the buffer of the file and causes all buffered data to be written to the file.
///
/// See [`FlushFileBuffers`] for more information.
///
/// [`FlushFileBuffers`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers
fn flush_file_buffers(
&'h self,
file_name: &U16CStr,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Gets information about the file.
///
/// See [`GetFileInformationByHandle`] for more information.
///
/// [`GetFileInformationByHandle`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfileinformationbyhandle
fn get_file_information(
&'h self,
file_name: &U16CStr,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<FileInfo> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Lists all child items in the directory.
///
/// `fill_find_data` should be called for every child item in the directory.
///
/// It will only be called if [`find_files_with_pattern`] returns [`STATUS_NOT_IMPLEMENTED`].
///
/// See [`FindFirstFile`] for more information.
///
/// [`find_files_with_pattern`]: Self::find_files_with_pattern
/// [`FindFirstFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew
fn find_files(
&'h self,
file_name: &U16CStr,
fill_find_data: impl FnMut(&FindData) -> FillDataResult,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Lists all child items that matches the specified `pattern` in the directory.
///
/// `fill_find_data` should be called for every matching child item in the directory.
///
/// [`is_name_in_expression`] can be used to determine if a file name matches the pattern.
///
/// If this function returns [`STATUS_NOT_IMPLEMENTED`], [`find_files`] will be called instead and
/// pattern matching will be handled directly by Dokan.
///
/// See [`FindFirstFile`] for more information.
///
/// [`is_name_in_expression`]: crate::is_name_in_expression
/// [`find_files`]: Self::find_files
/// [`FindFirstFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirstfilew
fn find_files_with_pattern(
&'h self,
file_name: &U16CStr,
pattern: &U16CStr,
fill_find_data: impl FnMut(&FindData) -> FillDataResult,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Sets attributes of the file.
///
/// `file_attributes` can be combination of one or more [file attribute constants] defined by
/// Windows.
///
/// See [`SetFileAttributes`] for more information.
///
/// [file attribute constants]: https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
/// [`SetFileAttributes`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesw
fn set_file_attributes(
&'h self,
file_name: &U16CStr,
file_attributes: u32,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Sets the time when the file was created, last accessed and last written.
///
/// See [`SetFileTime`] for more information.
///
/// [`SetFileTime`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfiletime
fn set_file_time(
&'h self,
file_name: &U16CStr,
creation_time: FileTimeOperation,
last_access_time: FileTimeOperation,
last_write_time: FileTimeOperation,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Checks if the file can be deleted.
///
/// The file should not be deleted in this function. Instead, it should only check if the file
/// can be deleted and return `Ok` if that is possible.
///
/// It will also be called with [`info.delete_on_close`] returning `false` to notify that the
/// file is no longer requested to be deleted.
///
/// [`info.delete_on_close`]: OperationInfo::delete_on_close
fn delete_file(
&'h self,
file_name: &U16CStr,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Checks if the directory can be deleted.
///
/// Similar to [`delete_file`], it should only check if the directory can be deleted and delay
/// the actual deletion to the [`cleanup`] function.
///
/// It will also be called with [`info.delete_on_close`] returning `false` to notify that the
/// directory is no longer requested to be deleted.
///
/// [`delete_file`]: Self::delete_file
/// [`cleanup`]: Self::cleanup
/// [`info.delete_on_close`]: OperationInfo::delete_on_close
fn delete_directory(
&'h self,
file_name: &U16CStr,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Moves the file.
///
/// If the `new_file_name` already exists, the function should only replace the existing file
/// when `replace_if_existing` is `true`, otherwise it should return appropriate error.
///
/// Note that renaming is a special kind of moving and is also handled by this function.
///
/// See [`MoveFileEx`] for more information.
///
/// [`MoveFileEx`]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-movefileexw
fn move_file(
&'h self,
file_name: &U16CStr,
new_file_name: &U16CStr,
replace_if_existing: bool,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Sets end-of-file position of the file.
///
/// The `offset` value is zero-based, so it actually refers to the offset to the byte
/// immediately following the last valid byte in the file.
///
/// See [`FILE_END_OF_FILE_INFORMATION`] for more information.
///
/// [`FILE_END_OF_FILE_INFORMATION`]: https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-_file_end_of_file_information
fn set_end_of_file(
&'h self,
file_name: &U16CStr,
offset: i64,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Sets allocation size of the file.
///
/// The allocation size is the number of bytes allocated in the underlying physical device for
/// the file.
///
/// See [`FILE_ALLOCATION_INFORMATION`] for more information.
///
/// [`FILE_ALLOCATION_INFORMATION`]: https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_allocation_information
fn set_allocation_size(
&'h self,
file_name: &U16CStr,
alloc_size: i64,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Locks the file for exclusive access.
///
/// It will only be called if [`MountFlags::FILELOCK_USER_MODE`] was specified when mounting the
/// volume, otherwise Dokan will take care of file locking.
///
/// See [`LockFile`] for more information.
///
/// [`MountFlags::FILELOCK_USER_MODE`]: crate::MountFlags::FILELOCK_USER_MODE
/// [`LockFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfile
fn lock_file(
&'h self,
file_name: &U16CStr,
offset: i64,
length: i64,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Unlocks the previously locked file.
///
/// It will only be called if [`MountFlags::FILELOCK_USER_MODE`] was specified when mounting the
/// volume, otherwise Dokan will take care of file locking.
///
/// See [`UnlockFile`] for more information.
///
/// [`MountFlags::FILELOCK_USER_MODE`]: crate::MountFlags::FILELOCK_USER_MODE
/// [`UnlockFile`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-unlockfile
fn unlock_file(
&'h self,
file_name: &U16CStr,
offset: i64,
length: i64,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Gets free space information about the disk.
///
/// See [`GetDiskFreeSpaceEx`] for more information.
///
/// [`GetDiskFreeSpaceEx`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexw
fn get_disk_free_space(
&'h self,
info: &OperationInfo<'c, 'h, Self>,
) -> OperationResult<DiskSpaceInfo> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Gets information about the volume and file system.
///
/// See [`GetVolumeInformation`] for more information.
///
/// [`GetVolumeInformation`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationbyhandlew
fn get_volume_information(
&'h self,
info: &OperationInfo<'c, 'h, Self>,
) -> OperationResult<VolumeInfo> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Called when Dokan has successfully mounted the volume.
fn mounted(
&'h self,
mount_point: &U16CStr,
info: &OperationInfo<'c, 'h, Self>,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Called when Dokan is unmounting the volume.
fn unmounted(&'h self, info: &OperationInfo<'c, 'h, Self>) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Gets security information of a file.
///
/// Size of the security descriptor in bytes should be returned on success. If the buffer is not
/// large enough, the number should still be returned, and [`STATUS_BUFFER_OVERFLOW`] will be
/// automatically passed to Dokan if it is larger than `buffer_length`.
///
/// See [`GetFileSecurity`] for more information.
///
/// [`STATUS_BUFFER_OVERFLOW`]: winapi::shared::ntstatus::STATUS_BUFFER_OVERFLOW
/// [`GetFileSecurity`]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getfilesecuritya
fn get_file_security(
&'h self,
file_name: &U16CStr,
security_information: u32,
security_descriptor: PSECURITY_DESCRIPTOR,
buffer_length: u32,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<u32> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Sets security information of a file.
///
/// See [`SetFileSecurity`] for more information.
///
/// [`SetFileSecurity`]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setfilesecuritya
fn set_file_security(
&'h self,
file_name: &U16CStr,
security_information: u32,
security_descriptor: PSECURITY_DESCRIPTOR,
buffer_length: u32,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
/// Lists all alternative streams of the file.
///
/// `fill_find_stream_data` should be called for every stream of the file, including the default
/// data stream `::$DATA`.
///
/// See [`FindFirstStream`] for more information.
///
/// [`FindFirstStream`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirststreamw
fn find_streams(
&'h self,
file_name: &U16CStr,
fill_find_stream_data: impl FnMut(&FindStreamData) -> FillDataResult,
info: &OperationInfo<'c, 'h, Self>,
context: &'c Self::Context,
) -> OperationResult<()> {
Err(STATUS_NOT_IMPLEMENTED)
}
}