-
Notifications
You must be signed in to change notification settings - Fork 673
/
Copy pathdirectory.c
817 lines (678 loc) · 28.9 KB
/
directory.c
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/*
Dokan : user-mode file system library for Windows
Copyright (C) 2020 - 2022 Google, Inc.
Copyright (C) 2015 - 2019 Adrien J. <[email protected]> and Maxime C. <[email protected]>
Copyright (C) 2007 - 2011 Hiroki Asakawa <[email protected]>
http://dokan-dev.github.io
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dokani.h"
#include "fileinfo.h"
#include "list.h"
#include "dokan_pool.h"
#include <assert.h>
/**
* \struct DOKAN_FIND_DATA
* \brief Dokan find file list
*
* Used by FindFiles
*/
typedef struct _DOKAN_FIND_DATA {
/**
* File data information link
*/
WIN32_FIND_DATAW FindData;
/**
* Current list entry informations
*/
LIST_ENTRY ListEntry;
} DOKAN_FIND_DATA, *PDOKAN_FIND_DATA;
VOID DokanFillDirInfo(PFILE_DIRECTORY_INFORMATION Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileAttributes = FindData->dwFileAttributes;
Buffer->FileNameLength = nameBytes;
Buffer->EndOfFile.HighPart = FindData->nFileSizeHigh;
Buffer->EndOfFile.LowPart = FindData->nFileSizeLow;
Buffer->AllocationSize.HighPart = FindData->nFileSizeHigh;
Buffer->AllocationSize.LowPart = FindData->nFileSizeLow;
ALIGN_ALLOCATION_SIZE(&Buffer->AllocationSize, DokanInstance->DokanOptions);
Buffer->CreationTime.HighPart = FindData->ftCreationTime.dwHighDateTime;
Buffer->CreationTime.LowPart = FindData->ftCreationTime.dwLowDateTime;
Buffer->LastAccessTime.HighPart = FindData->ftLastAccessTime.dwHighDateTime;
Buffer->LastAccessTime.LowPart = FindData->ftLastAccessTime.dwLowDateTime;
Buffer->LastWriteTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->LastWriteTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->ChangeTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->ChangeTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
VOID DokanFillFullDirInfo(PFILE_FULL_DIR_INFORMATION Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileAttributes = FindData->dwFileAttributes;
Buffer->FileNameLength = nameBytes;
Buffer->EndOfFile.HighPart = FindData->nFileSizeHigh;
Buffer->EndOfFile.LowPart = FindData->nFileSizeLow;
Buffer->AllocationSize.HighPart = FindData->nFileSizeHigh;
Buffer->AllocationSize.LowPart = FindData->nFileSizeLow;
ALIGN_ALLOCATION_SIZE(&Buffer->AllocationSize, DokanInstance->DokanOptions);
Buffer->CreationTime.HighPart = FindData->ftCreationTime.dwHighDateTime;
Buffer->CreationTime.LowPart = FindData->ftCreationTime.dwLowDateTime;
Buffer->LastAccessTime.HighPart = FindData->ftLastAccessTime.dwHighDateTime;
Buffer->LastAccessTime.LowPart = FindData->ftLastAccessTime.dwLowDateTime;
Buffer->LastWriteTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->LastWriteTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->ChangeTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->ChangeTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->EaSize = 0;
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
VOID DokanFillIdFullDirInfo(PFILE_ID_FULL_DIR_INFORMATION Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileAttributes = FindData->dwFileAttributes;
Buffer->FileNameLength = nameBytes;
Buffer->EndOfFile.HighPart = FindData->nFileSizeHigh;
Buffer->EndOfFile.LowPart = FindData->nFileSizeLow;
Buffer->AllocationSize.HighPart = FindData->nFileSizeHigh;
Buffer->AllocationSize.LowPart = FindData->nFileSizeLow;
ALIGN_ALLOCATION_SIZE(&Buffer->AllocationSize, DokanInstance->DokanOptions);
Buffer->CreationTime.HighPart = FindData->ftCreationTime.dwHighDateTime;
Buffer->CreationTime.LowPart = FindData->ftCreationTime.dwLowDateTime;
Buffer->LastAccessTime.HighPart = FindData->ftLastAccessTime.dwHighDateTime;
Buffer->LastAccessTime.LowPart = FindData->ftLastAccessTime.dwLowDateTime;
Buffer->LastWriteTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->LastWriteTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->ChangeTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->ChangeTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->EaSize = 0;
Buffer->FileId.QuadPart = 0;
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
VOID DokanFillIdBothDirInfo(PFILE_ID_BOTH_DIR_INFORMATION Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileAttributes = FindData->dwFileAttributes;
Buffer->FileNameLength = nameBytes;
Buffer->ShortNameLength = 0;
Buffer->EndOfFile.HighPart = FindData->nFileSizeHigh;
Buffer->EndOfFile.LowPart = FindData->nFileSizeLow;
Buffer->AllocationSize.HighPart = FindData->nFileSizeHigh;
Buffer->AllocationSize.LowPart = FindData->nFileSizeLow;
ALIGN_ALLOCATION_SIZE(&Buffer->AllocationSize, DokanInstance->DokanOptions);
Buffer->CreationTime.HighPart = FindData->ftCreationTime.dwHighDateTime;
Buffer->CreationTime.LowPart = FindData->ftCreationTime.dwLowDateTime;
Buffer->LastAccessTime.HighPart = FindData->ftLastAccessTime.dwHighDateTime;
Buffer->LastAccessTime.LowPart = FindData->ftLastAccessTime.dwLowDateTime;
Buffer->LastWriteTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->LastWriteTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->ChangeTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->ChangeTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->EaSize = 0;
Buffer->FileId.QuadPart = 0;
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
VOID DokanFillIdExtdDirInfo(PFILE_ID_EXTD_DIR_INFO Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileAttributes = FindData->dwFileAttributes;
Buffer->FileNameLength = nameBytes;
Buffer->EndOfFile.HighPart = FindData->nFileSizeHigh;
Buffer->EndOfFile.LowPart = FindData->nFileSizeLow;
Buffer->AllocationSize.HighPart = FindData->nFileSizeHigh;
Buffer->AllocationSize.LowPart = FindData->nFileSizeLow;
ALIGN_ALLOCATION_SIZE(&Buffer->AllocationSize, DokanInstance->DokanOptions);
Buffer->CreationTime.HighPart = FindData->ftCreationTime.dwHighDateTime;
Buffer->CreationTime.LowPart = FindData->ftCreationTime.dwLowDateTime;
Buffer->LastAccessTime.HighPart = FindData->ftLastAccessTime.dwHighDateTime;
Buffer->LastAccessTime.LowPart = FindData->ftLastAccessTime.dwLowDateTime;
Buffer->LastWriteTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->LastWriteTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->ChangeTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->ChangeTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->EaSize = 0;
Buffer->ReparsePointTag = 0;
RtlFillMemory(&Buffer->FileId.Identifier, sizeof Buffer->FileId.Identifier, 0);
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
VOID DokanFillIdExtdBothDirInfo(PFILE_ID_EXTD_BOTH_DIR_INFORMATION Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileAttributes = FindData->dwFileAttributes;
Buffer->FileNameLength = nameBytes;
Buffer->ShortNameLength = 0;
Buffer->EndOfFile.HighPart = FindData->nFileSizeHigh;
Buffer->EndOfFile.LowPart = FindData->nFileSizeLow;
Buffer->AllocationSize.HighPart = FindData->nFileSizeHigh;
Buffer->AllocationSize.LowPart = FindData->nFileSizeLow;
ALIGN_ALLOCATION_SIZE(&Buffer->AllocationSize, DokanInstance->DokanOptions);
Buffer->CreationTime.HighPart = FindData->ftCreationTime.dwHighDateTime;
Buffer->CreationTime.LowPart = FindData->ftCreationTime.dwLowDateTime;
Buffer->LastAccessTime.HighPart = FindData->ftLastAccessTime.dwHighDateTime;
Buffer->LastAccessTime.LowPart = FindData->ftLastAccessTime.dwLowDateTime;
Buffer->LastWriteTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->LastWriteTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->ChangeTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->ChangeTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->EaSize = 0;
Buffer->ReparsePointTag = 0;
RtlFillMemory(&Buffer->FileId.Identifier, sizeof Buffer->FileId.Identifier, 0);
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
VOID DokanFillBothDirInfo(PFILE_BOTH_DIR_INFORMATION Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileAttributes = FindData->dwFileAttributes;
Buffer->FileNameLength = nameBytes;
Buffer->ShortNameLength = 0;
Buffer->EndOfFile.HighPart = FindData->nFileSizeHigh;
Buffer->EndOfFile.LowPart = FindData->nFileSizeLow;
Buffer->AllocationSize.HighPart = FindData->nFileSizeHigh;
Buffer->AllocationSize.LowPart = FindData->nFileSizeLow;
ALIGN_ALLOCATION_SIZE(&Buffer->AllocationSize, DokanInstance->DokanOptions);
Buffer->CreationTime.HighPart = FindData->ftCreationTime.dwHighDateTime;
Buffer->CreationTime.LowPart = FindData->ftCreationTime.dwLowDateTime;
Buffer->LastAccessTime.HighPart = FindData->ftLastAccessTime.dwHighDateTime;
Buffer->LastAccessTime.LowPart = FindData->ftLastAccessTime.dwLowDateTime;
Buffer->LastWriteTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->LastWriteTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->ChangeTime.HighPart = FindData->ftLastWriteTime.dwHighDateTime;
Buffer->ChangeTime.LowPart = FindData->ftLastWriteTime.dwLowDateTime;
Buffer->EaSize = 0;
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
VOID DokanFillNamesInfo(PFILE_NAMES_INFORMATION Buffer,
PWIN32_FIND_DATAW FindData, ULONG Index) {
ULONG nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
Buffer->FileIndex = Index;
Buffer->FileNameLength = nameBytes;
RtlCopyMemory(Buffer->FileName, FindData->cFileName, nameBytes);
}
ULONG
DokanFillDirectoryInformation(FILE_INFORMATION_CLASS DirectoryInfo,
PVOID Buffer, PULONG LengthRemaining,
PWIN32_FIND_DATAW FindData, ULONG Index,
PDOKAN_INSTANCE DokanInstance) {
ULONG nameBytes;
ULONG thisEntrySize;
nameBytes = (ULONG)wcslen(FindData->cFileName) * sizeof(WCHAR);
thisEntrySize = nameBytes;
switch (DirectoryInfo) {
case FileDirectoryInformation:
thisEntrySize += sizeof(FILE_DIRECTORY_INFORMATION);
break;
case FileFullDirectoryInformation:
thisEntrySize += sizeof(FILE_FULL_DIR_INFORMATION);
break;
case FileIdFullDirectoryInformation:
thisEntrySize += sizeof(FILE_ID_FULL_DIR_INFORMATION);
break;
case FileNamesInformation:
thisEntrySize += sizeof(FILE_NAMES_INFORMATION);
break;
case FileBothDirectoryInformation:
thisEntrySize += sizeof(FILE_BOTH_DIR_INFORMATION);
break;
case FileIdBothDirectoryInformation:
thisEntrySize += sizeof(FILE_ID_BOTH_DIR_INFORMATION);
break;
case FileIdExtdDirectoryInformation:
thisEntrySize += sizeof(FILE_ID_EXTD_DIR_INFO);
break;
case FileIdExtdBothDirectoryInformation:
thisEntrySize += sizeof(FILE_ID_EXTD_BOTH_DIR_INFORMATION);
break;
default:
break;
}
// Must be align on a 8-byte boundary.
thisEntrySize = QuadAlign(thisEntrySize);
// no more memory, don't fill any more
if (*LengthRemaining < thisEntrySize) {
DbgPrint(" no memory\n");
return 0;
}
RtlZeroMemory(Buffer, thisEntrySize);
switch (DirectoryInfo) {
case FileDirectoryInformation:
DokanFillDirInfo(Buffer, FindData, Index, DokanInstance);
break;
case FileFullDirectoryInformation:
DokanFillFullDirInfo(Buffer, FindData, Index, DokanInstance);
break;
case FileIdFullDirectoryInformation:
DokanFillIdFullDirInfo(Buffer, FindData, Index, DokanInstance);
break;
case FileNamesInformation:
DokanFillNamesInfo(Buffer, FindData, Index);
break;
case FileBothDirectoryInformation:
DokanFillBothDirInfo(Buffer, FindData, Index, DokanInstance);
break;
case FileIdBothDirectoryInformation:
DokanFillIdBothDirInfo(Buffer, FindData, Index, DokanInstance);
break;
case FileIdExtdDirectoryInformation:
DokanFillIdExtdDirInfo(Buffer, FindData, Index, DokanInstance);
break;
case FileIdExtdBothDirectoryInformation:
DokanFillIdExtdBothDirInfo(Buffer, FindData, Index, DokanInstance);
break;
default:
break;
}
*LengthRemaining -= thisEntrySize;
return thisEntrySize;
}
int WINAPI DokanFillFileData(PWIN32_FIND_DATAW FindData,
PDOKAN_FILE_INFO FileInfo) {
assert(FileInfo->ProcessingContext);
PDOKAN_VECTOR dirList = (PDOKAN_VECTOR )FileInfo->ProcessingContext;
DokanVector_PushBack(dirList, FindData);
return 0;
}
// add entry which matches the pattern specifed in EventContext
// to the buffer specifed in EventInfo
//
LONG MatchFiles(PDOKAN_IO_EVENT IoEvent, PDOKAN_VECTOR DirList) {
ULONG lengthRemaining =
IoEvent->EventContext->Operation.Directory.BufferLength;
PVOID currentBuffer = IoEvent->EventResult->Buffer;
PVOID lastBuffer = currentBuffer;
ULONG index = 0;
BOOL patternCheck = FALSE;
PWCHAR pattern = NULL;
BOOL bufferOverFlow = FALSE;
BOOL caseSensitive = IoEvent->DokanInstance->DokanOptions->Options &
DOKAN_OPTION_CASE_SENSITIVE;
if (IoEvent->EventContext->Operation.Directory.SearchPatternLength > 0) {
pattern = (PWCHAR)((SIZE_T)&IoEvent->EventContext->Operation.Directory
.SearchPatternBase[0] +
(SIZE_T)IoEvent->EventContext->Operation.Directory
.SearchPatternOffset);
}
if (pattern && wcscmp(pattern, L"*") != 0 &&
!IoEvent->DokanInstance->DokanOperations->FindFilesWithPattern) {
patternCheck = TRUE;
}
for (size_t i = 0; i < DokanVector_GetCount(DirList); ++i) {
PDOKAN_FIND_DATA find = (PDOKAN_FIND_DATA)DokanVector_GetItem(DirList, i);
DbgPrintW(L"FileMatch? : %s (%s,%d,%d)\n", find->FindData.cFileName,
(pattern ? pattern : L"null"),
IoEvent->EventContext->Operation.Directory.FileIndex, index);
// pattern is not specified or pattern match is ignore cases
if (!patternCheck ||
DokanIsNameInExpression(pattern, find->FindData.cFileName,
!caseSensitive)) {
if (IoEvent->EventContext->Operation.Directory.FileIndex <= index) {
// index+1 is very important, should use next entry index
ULONG entrySize = DokanFillDirectoryInformation(
IoEvent->EventContext->Operation.Directory.FileInformationClass,
currentBuffer, &lengthRemaining, &find->FindData, index + 1,
IoEvent->DokanInstance);
// buffer is full
if (entrySize == 0) {
bufferOverFlow = TRUE;
break;
}
// pointer of the current last entry
lastBuffer = currentBuffer;
// end if needs to return single entry
if (IoEvent->EventContext->Flags & SL_RETURN_SINGLE_ENTRY) {
DbgPrint(" =>return single entry\n");
index++;
break;
}
DbgPrint(" =>return\n");
// the offset of next entry
((PFILE_BOTH_DIR_INFORMATION)currentBuffer)->NextEntryOffset =
entrySize;
// next buffer position
currentBuffer = (PCHAR)currentBuffer + entrySize;
}
index++;
}
}
// Since next of the last entry doesn't exist, clear next offset
((PFILE_BOTH_DIR_INFORMATION)lastBuffer)->NextEntryOffset = 0;
// acctualy used length of buffer
IoEvent->EventResult->BufferLength =
IoEvent->EventContext->Operation.Directory.BufferLength -
lengthRemaining;
if (index <= IoEvent->EventContext->Operation.Directory.FileIndex) {
if (bufferOverFlow)
return -2; // BUFFER_OVERFLOW
return -1; // NO_MORE_FILES
}
return index;
}
VOID AddMissingCurrentAndParentFolder(PDOKAN_IO_EVENT IoEvent) {
PWCHAR pattern = NULL;
BOOLEAN currentFolder = FALSE, parentFolder = FALSE;
WIN32_FIND_DATAW findData;
FILETIME systime;
PDOKAN_VECTOR dirList = (PDOKAN_VECTOR)IoEvent->DokanFileInfo.ProcessingContext;
assert(dirList);
if (IoEvent->EventContext->Operation.Directory.SearchPatternLength != 0) {
pattern = (PWCHAR)((SIZE_T)&IoEvent->EventContext->Operation.Directory
.SearchPatternBase[0] +
(SIZE_T)IoEvent->EventContext->Operation.Directory
.SearchPatternOffset);
}
if (wcscmp(IoEvent->EventContext->Operation.Directory.DirectoryName,
L"\\") == 0 ||
(pattern != NULL && wcscmp(pattern, L"*") != 0)) {
return;
}
for (size_t i = 0;
(!currentFolder || !parentFolder) && i < DokanVector_GetCount(dirList);
++i) {
PDOKAN_FIND_DATA find = (PDOKAN_FIND_DATA)DokanVector_GetItem(dirList, i);
if (wcscmp(find->FindData.cFileName, L".") == 0) {
currentFolder = TRUE;
}
if (wcscmp(find->FindData.cFileName, L"..") == 0) {
parentFolder = TRUE;
}
}
if (!currentFolder || !parentFolder) {
GetSystemTimeAsFileTime(&systime);
ZeroMemory(&findData, sizeof(WIN32_FIND_DATAW));
findData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
findData.ftCreationTime = systime;
findData.ftLastAccessTime = systime;
findData.ftLastWriteTime = systime;
// Folders times should be the real current and parent folder times...
// TODO: Should it be PushFront ?
if (!parentFolder) {
findData.cFileName[0] = '.';
findData.cFileName[1] = '.';
// NULL written during ZeroMemory()
DokanVector_PushBack(dirList, &findData);
}
if (!currentFolder) {
findData.cFileName[0] = '.';
findData.cFileName[1] = '\0';
DokanVector_PushBack(dirList, &findData);
}
}
}
NTSTATUS WriteDirectoryResults(PDOKAN_IO_EVENT EventInfo,
PDOKAN_VECTOR dirList) {
// If this function is called then so far everything should be good
assert(EventInfo->EventResult->Status == STATUS_SUCCESS);
// Write the file info to the output buffer
int index = MatchFiles(EventInfo, dirList);
DbgPrint("WriteDirectoryResults() New directory index is %d.\n", index);
// there is no matched file
if (index < 0) {
if (index == -1) {
if (EventInfo->EventContext->Operation.Directory.FileIndex == 0) {
DbgPrint(" STATUS_NO_SUCH_FILE\n");
EventInfo->EventResult->Status = STATUS_NO_SUCH_FILE;
} else {
DbgPrint(" STATUS_NO_MORE_FILES\n");
EventInfo->EventResult->Status = STATUS_NO_MORE_FILES;
}
} else if (index == -2) {
DbgPrint(" STATUS_BUFFER_OVERFLOW\n");
EventInfo->EventResult->Status = STATUS_BUFFER_OVERFLOW;
}
EventInfo->EventResult->Operation.Directory.Index =
EventInfo->EventContext->Operation.Directory.FileIndex;
} else {
DbgPrint("index to %d\n", index);
EventInfo->EventResult->Operation.Directory.Index = index;
}
return EventInfo->EventResult->Status;
}
VOID EndFindFilesCommon(PDOKAN_IO_EVENT IoEvent, NTSTATUS Status) {
PDOKAN_VECTOR dirList =
(PDOKAN_VECTOR)IoEvent->DokanFileInfo.ProcessingContext;
PDOKAN_VECTOR oldDirList = NULL;
assert(IoEvent->EventResult->BufferLength == 0);
assert(IoEvent->DokanFileInfo.ProcessingContext);
// STATUS_PENDING should not be passed to this function
if (Status == STATUS_PENDING) {
DbgPrint("Dokan Error: EndFindFilesCommon() failed because STATUS_PENDING "
"was supplied for ResultStatus.\n");
Status = STATUS_INTERNAL_ERROR;
}
if (Status == STATUS_SUCCESS) {
AddMissingCurrentAndParentFolder(IoEvent);
Status = WriteDirectoryResults(IoEvent, dirList);
EnterCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
{
if (IoEvent->DokanOpenInfo->DirList != dirList) {
oldDirList = IoEvent->DokanOpenInfo->DirList;
IoEvent->DokanOpenInfo->DirList = dirList;
} else {
// They should never point to the same object
DbgPrint("Dokan Warning: EndFindFilesCommon() "
"EventInfo->DokanOpenInfo->DirList == dirList\n");
}
if (IoEvent->DokanOpenInfo->DirListSearchPattern) {
free(IoEvent->DokanOpenInfo->DirListSearchPattern);
IoEvent->DokanOpenInfo->DirListSearchPattern = NULL;
}
if (IoEvent->EventContext->Operation.Directory
.SearchPatternLength > 0) {
IoEvent->DokanOpenInfo->DirListSearchPattern =
_wcsdup((PWCHAR)((SIZE_T)&IoEvent->EventContext->Operation.Directory
.SearchPatternBase[0] +
(SIZE_T)IoEvent->EventContext->Operation.Directory
.SearchPatternOffset));
}
}
LeaveCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
if (oldDirList) {
PushDirectoryList(oldDirList);
}
} else {
PushDirectoryList(dirList);
}
IoEvent->DokanFileInfo.ProcessingContext = NULL;
IoEvent->EventResult->Status = Status;
EventCompletion(IoEvent);
}
VOID DispatchDirectoryInformation(PDOKAN_IO_EVENT IoEvent) {
PWCHAR searchPattern = NULL;
NTSTATUS status = STATUS_SUCCESS;
ULONG fileInfoClass =
IoEvent->EventContext->Operation.Directory.FileInformationClass;
BOOL forceScan = FALSE;
DbgPrint(
"###FindFiles file handle = 0x%p, eventID = %04d, event Info = 0x%p\n",
IoEvent->DokanOpenInfo,
IoEvent->DokanOpenInfo != NULL ? IoEvent->DokanOpenInfo->EventId : -1,
IoEvent);
CheckFileName(IoEvent->EventContext->Operation.Directory.DirectoryName);
// check whether this is handled FileInfoClass
if (fileInfoClass != FileDirectoryInformation &&
fileInfoClass != FileFullDirectoryInformation &&
fileInfoClass != FileBothDirectoryInformation &&
fileInfoClass != FileNamesInformation &&
fileInfoClass != FileIdBothDirectoryInformation &&
fileInfoClass != FileIdFullDirectoryInformation &&
fileInfoClass != FileIdExtdDirectoryInformation &&
fileInfoClass != FileIdExtdBothDirectoryInformation) {
DbgPrint("Dokan Information: Unsupported file information class %d\n",
fileInfoClass);
// send directory info to driver
IoEvent->EventResult->BufferLength = 0;
IoEvent->EventResult->Status = STATUS_INVALID_PARAMETER;
EventCompletion(IoEvent);
return;
}
CreateDispatchCommon(IoEvent,
IoEvent->EventContext->Operation.Directory.BufferLength,
/*UseExtraMemoryPool=*/FALSE,
/*ClearNonPoolBuffer=*/TRUE);
IoEvent->EventResult->Operation.Directory.Index =
IoEvent->EventContext->Operation.Directory.FileIndex;
if (IoEvent->EventContext->Operation.Directory.SearchPatternLength > 0) {
searchPattern = (PWCHAR)((SIZE_T)&IoEvent->EventContext->Operation.Directory
.SearchPatternBase[0] +
(SIZE_T)IoEvent->EventContext->Operation.Directory
.SearchPatternOffset);
}
EnterCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
{
if (IoEvent->DokanOpenInfo->DirList == NULL) {
forceScan = TRUE;
} else if (searchPattern && IoEvent->DokanOpenInfo->DirListSearchPattern) {
forceScan = wcscmp(searchPattern,
IoEvent->DokanOpenInfo->DirListSearchPattern) != 0
? TRUE
: FALSE;
} else if (searchPattern) {
forceScan = wcscmp(searchPattern, L"*") != 0 ? TRUE : FALSE;
} else if (IoEvent->DokanOpenInfo->DirListSearchPattern) {
forceScan =
wcscmp(IoEvent->DokanOpenInfo->DirListSearchPattern, L"*") != 0
? TRUE
: FALSE;
}
// In FastFat SL_INDEX_SPECIFIED overrides SL_RESTART_SCAN
forceScan =
(forceScan || (!(IoEvent->EventContext->Flags & SL_INDEX_SPECIFIED) &&
(IoEvent->EventContext->Flags & SL_RESTART_SCAN)))
? TRUE
: FALSE;
if (!forceScan) {
status = WriteDirectoryResults(IoEvent, IoEvent->DokanOpenInfo->DirList);
}
}
LeaveCriticalSection(&IoEvent->DokanOpenInfo->CriticalSection);
if (!forceScan) {
IoEvent->EventResult->Status = status;
EventCompletion(IoEvent);
return;
}
IoEvent->DokanFileInfo.ProcessingContext = PopDirectoryList();
if (!IoEvent->DokanFileInfo.ProcessingContext) {
DbgPrint(
"Dokan Error: Failed to allocate memory for a new directory list.\n");
IoEvent->EventResult->Status = STATUS_NO_MEMORY;
EventCompletion(IoEvent);
return;
}
status = STATUS_NOT_IMPLEMENTED;
// Reminder: FindFilesWithPattern may not be implemented by returning STATUS_NOT_IMPLEMENTED.
if (IoEvent->DokanInstance->DokanOperations->FindFilesWithPattern) {
status = IoEvent->DokanInstance->DokanOperations->FindFilesWithPattern(
IoEvent->EventContext->Operation.Directory.DirectoryName,
searchPattern ? searchPattern : L"*", DokanFillFileData,
&IoEvent->DokanFileInfo);
}
// And if not, try with FindFiles.
if (status == STATUS_NOT_IMPLEMENTED &&
IoEvent->DokanInstance->DokanOperations->FindFiles) {
status = IoEvent->DokanInstance->DokanOperations->FindFiles(
IoEvent->EventContext->Operation.Directory.DirectoryName,
DokanFillFileData, &IoEvent->DokanFileInfo);
}
if (status != STATUS_NOT_IMPLEMENTED) {
EndFindFilesCommon(IoEvent, status);
} else {
// Neither FindFilesWithPattern nor FindFiles are implemented.
IoEvent->EventResult->Status = STATUS_NOT_IMPLEMENTED;
EventCompletion(IoEvent);
}
}
#define DOS_STAR (L'<')
#define DOS_QM (L'>')
#define DOS_DOT (L'"')
BOOL DOKANAPI DokanIsNameInExpression(LPCWSTR Expression, // matching pattern
LPCWSTR Name, // file name
BOOL IgnoreCase) {
ULONG ei = 0;
ULONG ni = 0;
while (Expression[ei] != '\0') {
if (Expression[ei] == L'*') {
ei++;
if (Expression[ei] == '\0')
return TRUE;
while (Name[ni] != '\0') {
if (DokanIsNameInExpression(&Expression[ei], &Name[ni], IgnoreCase))
return TRUE;
ni++;
}
} else if (Expression[ei] == DOS_STAR) {
ULONG p = ni;
ULONG lastDot = 0;
ei++;
while (Name[p] != '\0') {
if (Name[p] == L'.')
lastDot = p;
p++;
}
BOOL endReached = FALSE;
while (!endReached) {
endReached = (Name[ni] == '\0' || ni == lastDot);
if (!endReached) {
if (DokanIsNameInExpression(&Expression[ei], &Name[ni], IgnoreCase))
return TRUE;
ni++;
}
}
} else if (Expression[ei] == DOS_QM) {
ei++;
if (Name[ni] != L'.') {
ni++;
} else {
ULONG p = ni + 1;
while (Name[p] != '\0') {
if (Name[p] == L'.')
break;
p++;
}
if (Name[p] == L'.')
ni++;
}
} else if (Expression[ei] == DOS_DOT) {
ei++;
if (Name[ni] == L'.')
ni++;
} else {
if (Expression[ei] == L'?') {
ei++;
ni++;
} else if (IgnoreCase && towupper(Expression[ei]) == towupper(Name[ni])) {
ei++;
ni++;
} else if (!IgnoreCase && Expression[ei] == Name[ni]) {
ei++;
ni++;
} else {
return FALSE;
}
}
}
if (ei == wcslen(Expression) && ni == wcslen(Name))
return TRUE;
return FALSE;
}