-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileRoutines.cpp
1450 lines (1340 loc) · 42.6 KB
/
FileRoutines.cpp
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the LICENSE file distributed with
* this work for additional information regarding copyright ownership.
*
* Also, if exists, check the Licenses directory for information about
* third-party modules.
*
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FileRoutines.h"
#include <WaitableObjects.h>
#include <ShlObj.h>
#include <fileapi.h>
#include <Debug.h>
//-----------------------------------------------------------
#define CREATE_RETRIES_COUNT 600
#define CREATE_RETRIES_DELAY_MS 15
#define DELETE_RETRIES_COUNT 400
#define DELETE_RETRIES_DELAY_MS 15
#ifndef FILE_SUPERSEDE
#define FILE_SUPERSEDE 0x00000000
#endif //FILE_SUPERSEDE
#ifndef FILE_OPEN
#define FILE_OPEN 0x00000001
#endif //FILE_OPEN
#ifndef FILE_CREATE
#define FILE_CREATE 0x00000002
#endif //FILE_CREATE
#ifndef FILE_SYNCHRONOUS_IO_NONALERT
#define FILE_SYNCHRONOUS_IO_NONALERT 0x00000020
#endif //FILE_SYNCHRONOUS_IO_NONALERT
#ifndef FILE_NON_DIRECTORY_FILE
#define FILE_NON_DIRECTORY_FILE 0x00000040
#endif //FILE_NON_DIRECTORY_FILE
#ifndef OBJ_CASE_INSENSITIVE
#define OBJ_CASE_INSENSITIVE 0x00000040
#endif //OBJ_CASE_INSENSITIVE
#ifndef STATUS_OBJECT_TYPE_MISMATCH
#define STATUS_OBJECT_TYPE_MISMATCH ((NTSTATUS)0xC0000024L)
#endif //STATUS_OBJECT_TYPE_MISMATCH
#ifndef STATUS_OBJECT_NAME_INVALID
#define STATUS_OBJECT_NAME_INVALID ((NTSTATUS)0xC0000033L)
#endif //STATUS_OBJECT_NAME_INVALID
#ifndef STATUS_OBJECT_PATH_NOT_FOUND
#define STATUS_OBJECT_PATH_NOT_FOUND ((NTSTATUS)0xC000003AL)
#endif //STATUS_OBJECT_PATH_NOT_FOUND
#define ___KF_FLAG_CREATE 0x00008000
//-----------------------------------------------------------
typedef HRESULT (__stdcall *lpfnSHGetKnownFolderPath)(_In_ const GUID &rfid, _In_ DWORD dwFlags,
_In_opt_ HANDLE hToken, _Out_ PWSTR *ppszPath);
typedef HRESULT (__stdcall *lpfnSHGetFolderPathW)(_Reserved_ HWND hwnd, _In_ int csidl, _In_opt_ HANDLE hToken,
_In_ DWORD dwFlags, _Out_writes_(MAX_PATH) LPWSTR pszPath);
typedef VOID (__stdcall *lpfnCoTaskMemFree)(_In_opt_ LPVOID pv);
//-----------------------------------------------------------
static HRESULT _GetKnownFolderFolderPath(_In_ int csIdl, _In_ const GUID &sGuid, _In_ BOOL bCreate,
_Out_ MX::CStringW &cStrDestW);
//-----------------------------------------------------------
namespace MX {
namespace FileRoutines {
HRESULT GetAppFileName(_Out_ CStringW &cStrDestW)
{
DWORD dwSize, dwLen;
HRESULT hRes;
for (dwSize = 256; dwSize <= 32768; dwSize <<= 1)
{
if (cStrDestW.EnsureBuffer((SIZE_T)dwSize + 4) == FALSE)
return E_OUTOFMEMORY;
dwLen = ::GetModuleFileNameW(NULL, (LPWSTR)cStrDestW, dwSize);
if (dwLen == 0)
{
hRes = MX_HRESULT_FROM_LASTERROR();
return (FAILED(hRes)) ? hRes : E_FAIL;
}
if (dwLen < dwSize - 2)
break;
}
if (dwSize > 32768)
return E_OUTOFMEMORY;
((LPWSTR)cStrDestW)[dwLen] = 0;
cStrDestW.Refresh();
NormalizePath(cStrDestW);
return ConvertToLongPath(cStrDestW);
}
HRESULT GetAppFolderPath(_Out_ CStringW &cStrDestW)
{
HRESULT hRes;
hRes = GetAppFileName(cStrDestW);
if (SUCCEEDED(hRes))
{
LPCWSTR sW = (LPWSTR)cStrDestW;
SIZE_T nLen = cStrDestW.GetLength();
while (nLen > 0 && sW[nLen - 1] != L'\\')
nLen--;
cStrDestW.Delete(nLen, (SIZE_T)-1);
}
return hRes;
}
HRESULT GetProcessFileName(_In_ DWORD dwPid, _Out_ CStringW &cStrDestW)
{
HANDLE hProc;
HRESULT hRes;
hProc = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPid);
if (hProc != NULL)
{
hRes = GetProcessFileName(hProc, cStrDestW);
::CloseHandle(hProc);
}
else
{
hRes = MX_HRESULT_FROM_LASTERROR();
cStrDestW.Empty();
}
//done
return hRes;
}
HRESULT GetProcessFileName(_In_ HANDLE hProc, _Out_ CStringW &cStrDestW)
{
PMX_UNICODE_STRING FileName = NULL;
ULONG RetLength;
NTSTATUS nNtStatus;
HRESULT hRes;
if (hProc == NULL || hProc == INVALID_HANDLE_VALUE)
{
cStrDestW.Empty();
return E_INVALIDARG;
}
FileName = (PMX_UNICODE_STRING)MX_MALLOC(sizeof(MX_UNICODE_STRING) + 256);
if (FileName == NULL)
return E_OUTOFMEMORY;
FileName->Buffer = (PWCH)(FileName + 1);
FileName->Length = 0;
FileName->MaximumLength = 256;
RetLength = 0;
nNtStatus = ::MxNtQueryInformationProcess(hProc, MxProcessImageFileName, FileName,
(ULONG)sizeof(MX_UNICODE_STRING) + 256, &RetLength);
if (nNtStatus == STATUS_INFO_LENGTH_MISMATCH || nNtStatus == STATUS_BUFFER_TOO_SMALL ||
nNtStatus == STATUS_BUFFER_OVERFLOW)
{
MX_FREE(FileName);
if (RetLength >= sizeof(MX_UNICODE_STRING))
{
FileName = (PMX_UNICODE_STRING)MX_MALLOC((SIZE_T)RetLength);
if (FileName == NULL)
return E_OUTOFMEMORY;
FileName->Buffer = (PWCH)(FileName + 1);
FileName->Length = 0;
FileName->MaximumLength = (USHORT)(RetLength - (ULONG)sizeof(MX_UNICODE_STRING));
RetLength = 0;
nNtStatus = ::MxNtQueryInformationProcess(hProc, MxProcessImageFileName, FileName, RetLength, &RetLength);
}
else
{
nNtStatus = STATUS_BUFFER_OVERFLOW;
}
}
if (nNtStatus < 0)
{
MX_FREE(FileName);
cStrDestW.Empty();
return MX_HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
}
if (cStrDestW.CopyN(FileName->Buffer, (SIZE_T)(FileName->Length / 2)) == FALSE)
{
MX_FREE(FileName);
return E_OUTOFMEMORY;
}
MX_FREE(FileName);
hRes = ConvertToLongPath(cStrDestW);
if (SUCCEEDED(hRes))
hRes = ConvertToWin32(cStrDestW);
if (hRes == E_OUTOFMEMORY)
{
cStrDestW.Empty();
return hRes;
}
//done
return S_OK;
}
HRESULT GetCommonAppDataFolderPath(_Out_ CStringW &cStrDestW)
{
static const GUID __FOLDERID_ProgramData = {
0x62AB5D82, 0xFDC1, 0x4DC3, { 0xA9, 0xDD, 0x07, 0x0D, 0x1D, 0x49, 0x5D, 0x97 }
};
return _GetKnownFolderFolderPath(CSIDL_COMMON_APPDATA, __FOLDERID_ProgramData, TRUE, cStrDestW);
}
HRESULT GetProgramFilesFolderPath(_Out_ CStringW &cStrDestW)
{
static const GUID __FOLDERID_ProgramFiles = {
0x905E63B6, 0xC1BF, 0x494E, { 0xB2, 0x9C, 0x65, 0xB7, 0x32, 0xD3, 0xD2, 0x1A }
};
return _GetKnownFolderFolderPath(CSIDL_PROGRAM_FILES, __FOLDERID_ProgramFiles, TRUE, cStrDestW);
}
#if defined(_M_X64)
HRESULT GetProgramFilesX86FolderPath(_Out_ CStringW &cStrDestW)
{
static const GUID __FOLDERID_ProgramFilesX86 = {
0x7C5A40EF, 0xA0FB, 0x4BFC, { 0x87, 0x4A, 0xC0, 0xF2, 0xE0, 0xB9, 0xFA, 0x8E }
};
return _GetKnownFolderFolderPath(CSIDL_PROGRAM_FILESX86, __FOLDERID_ProgramFilesX86, TRUE, cStrDestW);
}
#endif //_M_X64
HRESULT GetWindowsPath(_Out_ CStringW &cStrDestW)
{
DWORD dwSize, dwLen;
LPWSTR sW;
HRESULT hRes;
for (dwSize = 256; dwSize <= 32768; dwSize <<= 1)
{
if (cStrDestW.EnsureBuffer((SIZE_T)dwSize) == FALSE)
{
cStrDestW.Empty();
return E_OUTOFMEMORY;
}
dwLen = ::GetWindowsDirectoryW((LPWSTR)cStrDestW, dwSize-2);
if (dwLen == 0)
{
hRes = MX_HRESULT_FROM_LASTERROR();
cStrDestW.Empty();
return (FAILED(hRes)) ? hRes : E_FAIL;
}
if (dwLen < dwSize-4)
break;
}
if (dwSize > 32768)
{
cStrDestW.Empty();
return MX_E_BufferOverflow;
}
sW = (LPWSTR)cStrDestW;
if (dwLen == 0 || (sW[dwLen-1] != L'/' && sW[dwLen-1] != L'\\'))
sW[dwLen++] = L'\\';
sW[dwLen] = 0;
cStrDestW.Refresh();
NormalizePath(cStrDestW);
return S_OK;
}
HRESULT GetWindowsSystemPath(_Out_ CStringW &cStrDestW)
{
DWORD dwSize, dwLen;
LPWSTR sW;
HRESULT hRes;
for (dwSize = 256; dwSize <= 32768; dwSize <<= 1)
{
if (cStrDestW.EnsureBuffer((SIZE_T)dwSize) == FALSE)
{
cStrDestW.Empty();
return E_OUTOFMEMORY;
}
dwLen = ::GetSystemDirectoryW((LPWSTR)cStrDestW, dwSize-2);
if (dwLen == 0)
{
hRes = MX_HRESULT_FROM_LASTERROR();
cStrDestW.Empty();
return (FAILED(hRes)) ? hRes : E_FAIL;
}
if (dwLen < dwSize-4)
break;
}
if (dwSize > 32768)
{
cStrDestW.Empty();
return MX_E_BufferOverflow;
}
sW = (LPWSTR)cStrDestW;
if (dwLen == 0 || (sW[dwLen-1] != L'/' && sW[dwLen-1] != L'\\'))
sW[dwLen++] = L'\\';
sW[dwLen] = 0;
cStrDestW.Refresh();
NormalizePath(cStrDestW);
return S_OK;
}
#if defined(_M_X64)
HRESULT GetWindowsSysWow64Path(_Out_ CStringW &cStrDestW)
{
static const GUID __FOLDERID_SystemX86 = {
0xD65231B0, 0xB2F1, 0x4857, { 0xA4, 0xCE, 0xA8, 0xE7, 0xC6, 0xEA, 0x7D, 0x27 }
};
return _GetKnownFolderFolderPath(CSIDL_SYSTEMX86, __FOLDERID_SystemX86, TRUE, cStrDestW);
}
#endif //_M_X64
HRESULT _GetTempPath(_Out_ CStringW &cStrDestW)
{
DWORD dwSize, dwLen;
LPWSTR sW;
HRESULT hRes;
for (dwSize = 256; dwSize <= 32768; dwSize <<= 1)
{
if (cStrDestW.EnsureBuffer((SIZE_T)dwSize) == FALSE)
{
cStrDestW.Empty();
return E_OUTOFMEMORY;
}
dwLen = ::GetTempPathW(dwSize-2, (LPWSTR)cStrDestW);
if (dwLen == 0)
{
hRes = MX_HRESULT_FROM_LASTERROR();
cStrDestW.Empty();
return (FAILED(hRes)) ? hRes : E_FAIL;
}
if (dwLen < dwSize-4)
break;
}
if (dwSize > 32768)
{
cStrDestW.Empty();
return MX_E_BufferOverflow;
}
sW = (LPWSTR)cStrDestW;
if (dwLen == 0 || (sW[dwLen-1] != L'/' && sW[dwLen-1] != L'\\'))
sW[dwLen++] = L'\\';
sW[dwLen] = 0;
cStrDestW.Refresh();
NormalizePath(cStrDestW);
return ConvertToLongPath(cStrDestW);
}
HRESULT CreateDirectoryRecursive(_In_ LPCWSTR szFolderNameW)
{
CStringW cStrTempW;
LARGE_INTEGER liTime;
WCHAR chOrigW, *sW;
SIZE_T i, nOfs;
HRESULT hRes;
if (szFolderNameW == NULL)
return E_POINTER;
if (StrLenW(szFolderNameW) < 3)
return E_INVALIDARG;
if (szFolderNameW[0] != L'\\')
{
if (szFolderNameW[1] != L':' || (szFolderNameW[2] != L'\\' && szFolderNameW[2] != L'/'))
return E_INVALIDARG;
}
else
{
if ((szFolderNameW[0] != L'\\' && szFolderNameW[0] != L'/') ||
(szFolderNameW[1] != L'\\' && szFolderNameW[1] != L'/') ||
szFolderNameW[2] == L'\\' || szFolderNameW[2] == L'/')
{
return E_INVALIDARG;
}
}
if (cStrTempW.Copy(szFolderNameW) == FALSE)
return E_OUTOFMEMORY;
//if the path is NOT a network share, append long path mode
sW = (LPWSTR)cStrTempW;
if (sW[0] != L'\\')
{
if (cStrTempW.Insert(L"\\\\?\\", 0) == FALSE)
return E_OUTOFMEMORY;
sW = (LPWSTR)cStrTempW;
}
if (cStrTempW.GetLength() > 7 && sW[cStrTempW.GetLength()-1] == L'\\')
{
cStrTempW.Delete(cStrTempW.GetLength() - 1, 1);
sW = (LPWSTR)cStrTempW;
}
//first check if the directory can be created directly or already exists
if (::CreateDirectoryW(sW, NULL) != FALSE)
return S_OK;
hRes = MX_HRESULT_FROM_LASTERROR();
if (hRes == MX_HRESULT_FROM_WIN32(ERROR_FILE_EXISTS) || hRes == MX_E_AlreadyExists)
return S_OK;
//skip until \\computer\share\first-folder(\\?) or \\?\drive:\first-folder(\\?)
for (nOfs=0; sW[nOfs] == L'\\'; nOfs++);
//advance until third backslash
for (i=0; sW[nOfs] != 0; nOfs++)
{
if (sW[nOfs] == L'\\')
{
if ((++i) == 3)
break;
}
}
//create each directory
while (sW[nOfs] != 0)
{
while (sW[nOfs] != 0 && sW[nOfs] != L'\\')
nOfs++;
chOrigW = sW[nOfs];
sW[nOfs] = 0;
if (::CreateDirectoryW(sW, NULL) == FALSE)
{
hRes = S_OK;
for (i=CREATE_RETRIES_COUNT; i>0; i--)
{
liTime.QuadPart = -(LONGLONG)MX_MILLISECONDS_TO_100NS(CREATE_RETRIES_DELAY_MS);
::MxNtDelayExecution(FALSE, &liTime);
if (::CreateDirectoryW(sW, NULL) != FALSE)
{
hRes = S_OK;
break;
}
hRes = MX_HRESULT_FROM_LASTERROR();
if (hRes == MX_HRESULT_FROM_WIN32(ERROR_FILE_EXISTS) || hRes == MX_E_AlreadyExists)
{
hRes = S_OK;
break;
}
}
if (FAILED(hRes))
return hRes;
}
sW[nOfs] = chOrigW;
if (chOrigW != 0)
nOfs++;
}
return S_OK;
}
HRESULT RemoveDirectoryRecursive(_In_ LPCWSTR szFolderNameW, _In_opt_ FileRoutines::eDelayedDelete nDD)
{
CStringW cStrTempW;
LARGE_INTEGER liTime;
SIZE_T i, nBaseLen;
HANDLE hFind;
WIN32_FIND_DATAW sFfData;
HRESULT hRes;
if (szFolderNameW == NULL)
return E_POINTER;
if (*szFolderNameW == 0)
return E_INVALIDARG;
//transverse folder
if (cStrTempW.Copy(szFolderNameW) == FALSE)
return E_OUTOFMEMORY;
nBaseLen = cStrTempW.GetLength();
if (nBaseLen == 0)
return E_INVALIDARG;
if (((LPWSTR)cStrTempW)[nBaseLen-1] != L'\\')
{
if (cStrTempW.Concat(L"\\") == FALSE)
return E_OUTOFMEMORY;
nBaseLen++;
}
if (cStrTempW.Concat(L"*") == FALSE)
return E_OUTOFMEMORY;
hFind = ::FindFirstFileW((LPWSTR)cStrTempW, &sFfData);
if (hFind == NULL || hFind == INVALID_HANDLE_VALUE)
{
hRes = MX_HRESULT_FROM_LASTERROR();
return (hRes == MX_E_FileNotFound || hRes == MX_E_PathNotFound) ? S_OK : hRes;
}
do
{
if ((sFfData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 || sFfData.cFileName[0] != L'.' ||
(sFfData.cFileName[1] != 0 && (sFfData.cFileName[1] != L'.' || sFfData.cFileName[2] != 0)))
{
cStrTempW.Delete(nBaseLen, (SIZE_T)-1);
if (cStrTempW.Concat(sFfData.cFileName) != FALSE)
{
if ((sFfData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
hRes = RemoveDirectoryRecursive((LPCWSTR)cStrTempW, nDD);
else
hRes = _DeleteFile((LPCWSTR)cStrTempW, nDD);
}
else
{
hRes = E_OUTOFMEMORY;
}
if (FAILED(hRes))
{
::FindClose(hFind);
return hRes;
}
}
}
while (::FindNextFileW(hFind, &sFfData) != FALSE);
::FindClose(hFind);
//remove directory
cStrTempW.Delete(nBaseLen-1, (SIZE_T)-1); //remove trailing slash
if (nDD == eDelayedDelete::WaitUntilReboot)
{
if (::MoveFileExW((LPCWSTR)cStrTempW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
}
else if (::RemoveDirectoryW((LPWSTR)cStrTempW) == FALSE)
{
::SetFileAttributesW((LPCWSTR)cStrTempW, FILE_ATTRIBUTE_NORMAL|FILE_ATTRIBUTE_DIRECTORY);
for (i = DELETE_RETRIES_COUNT; i > 0; i--)
{
liTime.QuadPart = -(LONGLONG)MX_MILLISECONDS_TO_100NS(DELETE_RETRIES_DELAY_MS);
::MxNtDelayExecution(FALSE, &liTime);
if (::RemoveDirectoryW((LPWSTR)cStrTempW) != FALSE)
break;
hRes = MX_HRESULT_FROM_LASTERROR();
if (hRes == MX_E_FileNotFound || hRes == MX_E_PathNotFound)
break;
if (hRes != MX_HRESULT_FROM_WIN32(ERROR_DIR_NOT_EMPTY) &&
hRes != E_ACCESSDENIED &&
hRes != MX_HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION) &&
hRes != MX_HRESULT_FROM_WIN32(ERROR_LOCK_VIOLATION))
{
return hRes;
}
if (i == 1)
{
if (nDD == eDelayedDelete::DeleteOnRebootOnFailure)
{
if (::MoveFileExW((LPWSTR)cStrTempW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
break;
}
return hRes;
}
}
}
return S_OK;
}
HRESULT _DeleteFile(_In_ LPCWSTR szFileNameW, _In_opt_ eDelayedDelete nDD)
{
LARGE_INTEGER liTime;
DWORD dw;
HRESULT hRes;
if (szFileNameW == NULL)
return E_POINTER;
if (*szFileNameW == 0)
return E_INVALIDARG;
dw = ::GetFileAttributesW(szFileNameW);
if (dw != INVALID_FILE_ATTRIBUTES && (dw & FILE_ATTRIBUTE_DIRECTORY) != 0)
return S_OK;
if (nDD == eDelayedDelete::WaitUntilReboot)
{
if (::MoveFileExW(szFileNameW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
return S_OK;
}
if (::DeleteFileW(szFileNameW) == FALSE)
{
::SetFileAttributesW(szFileNameW, FILE_ATTRIBUTE_NORMAL);
for (dw = DELETE_RETRIES_COUNT; dw > 0; dw--)
{
liTime.QuadPart = -(LONGLONG)MX_MILLISECONDS_TO_100NS(DELETE_RETRIES_DELAY_MS);
::MxNtDelayExecution(FALSE, &liTime);
if (::DeleteFileW(szFileNameW) != FALSE)
break;
hRes = MX_HRESULT_FROM_LASTERROR();
if (hRes == MX_E_PathNotFound || hRes == MX_E_FileNotFound)
break;
if (hRes != E_ACCESSDENIED && hRes != MX_HRESULT_FROM_WIN32(ERROR_SHARING_VIOLATION) &&
hRes != MX_HRESULT_FROM_WIN32(ERROR_LOCK_VIOLATION))
{
return hRes;
}
if (dw == 1)
{
if (nDD == eDelayedDelete::DeleteOnRebootOnFailure)
{
if (::MoveFileExW(szFileNameW, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) == FALSE)
return MX_HRESULT_FROM_LASTERROR();
break;
}
return hRes;
}
}
}
return S_OK;
}
HRESULT DeleteDirectoryFiles(_In_ LPCWSTR szFolderNameW, _In_opt_ eDelayedDelete nDD)
{
CStringW cStrTempW;
SIZE_T nBaseLen;
HANDLE hFind;
WIN32_FIND_DATAW sFfData;
HRESULT hRes;
if (szFolderNameW == NULL)
return E_POINTER;
if (*szFolderNameW == 0)
return E_INVALIDARG;
//trasverse folder
if (cStrTempW.Copy(szFolderNameW) == FALSE)
return E_OUTOFMEMORY;
nBaseLen = cStrTempW.GetLength();
if (nBaseLen == 0)
return E_INVALIDARG;
if (((LPWSTR)cStrTempW)[nBaseLen - 1] != L'\\')
{
if (cStrTempW.Concat(L"\\") == FALSE)
return E_OUTOFMEMORY;
nBaseLen++;
}
if (cStrTempW.Concat(L"*") == FALSE)
return E_OUTOFMEMORY;
hFind = ::FindFirstFileW((LPWSTR)cStrTempW, &sFfData);
if (hFind == NULL || hFind == INVALID_HANDLE_VALUE)
{
hRes = MX_HRESULT_FROM_LASTERROR();
return (hRes == MX_E_FileNotFound || hRes == MX_E_PathNotFound) ? S_OK : hRes;
}
do
{
if ((sFfData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
cStrTempW.Delete(nBaseLen, (SIZE_T)-1);
if (cStrTempW.Concat(sFfData.cFileName) != FALSE)
hRes = _DeleteFile((LPCWSTR)cStrTempW, nDD);
else
hRes = E_OUTOFMEMORY;
if (FAILED(hRes))
{
::FindClose(hFind);
return hRes;
}
}
}
while (::FindNextFileW(hFind, &sFfData) != FALSE);
::FindClose(hFind);
return S_OK;
}
VOID NormalizePath(_Inout_ CStringW &cStrPathW)
{
LPWSTR sW;
SIZE_T i;
sW = (LPWSTR)cStrPathW;
if (sW[0] == L'\\' && sW[1] == L'\\')
sW += 2;
//remove double slashes and convert forward slashes to back slashes
while (*sW != 0)
{
for (i=0; sW[i] == L'/' || sW[i] == L'\\'; i++);
if (i > 0)
{
if (i > 1)
cStrPathW.Delete((SIZE_T)(sW - (LPWSTR)cStrPathW), i-1);
*sW = L'\\';
}
sW++;
}
//remove "." && ".."
sW = (LPWSTR)cStrPathW;
while (*sW != 0)
{
if (sW[0] == L'\\' && sW[1] == L'.')
{
if (sW[2] == L'\\' || sW[2] == 0)
{
//remove "\.\"
i = (SIZE_T)(sW - (LPWSTR)cStrPathW);
cStrPathW.Delete(i, 2);
sW = (LPWSTR)cStrPathW + i;
continue;
}
else if (sW[2] == L'.' && (sW[3] == L'\\' || sW[3] == 0))
{
LPWSTR szStartW, szPrevSlashW;
szStartW = (LPWSTR)cStrPathW;
if (szStartW[0] == L'\\' && szStartW[1] == L'\\')
szStartW += 2;
else if (((szStartW[0] >= L'A' && szStartW[0] <= L'Z') || (szStartW[0] >= L'a' && szStartW[0] <= L'z')) &&
szStartW[1] == L':' && szStartW[2] == L'\\')
szStartW += 3;
szPrevSlashW = sW-1;
while (szPrevSlashW > szStartW && *(szPrevSlashW-1) != L'\\')
szPrevSlashW--;
//remove from szPrevSlashW to sW+4
i = (SIZE_T)(szPrevSlashW - (LPWSTR)cStrPathW);
cStrPathW.Delete(i, (SIZE_T)(sW-szPrevSlashW) + 4);
if (i > 0)
i--;
sW = (LPWSTR)cStrPathW + i;
continue;
}
}
sW++;
}
//convert short path to long if not a network folder
if (((LPCWSTR)cStrPathW)[0] != L'\\')
{
CStringW cStrTempW;
SIZE_T nLen, nSize;
for (nSize = 256; nSize <= 32768 && nSize < cStrTempW.GetLength(); nSize <<= 1);
while (nSize <= 32768)
{
if (cStrTempW.EnsureBuffer(nSize + 4) == FALSE)
break;
nLen = (SIZE_T)::GetLongPathNameW((LPCWSTR)cStrPathW, (LPWSTR)cStrTempW, (DWORD)nSize);
if (nLen == 0)
break; //couldn't convert (i.e. access denied to a parent folder), use original temp path
if (nLen < nSize)
{
((LPWSTR)cStrTempW)[nLen] = 0;
cStrTempW.Refresh();
//----
cStrPathW.Attach(cStrTempW.Detach());
break;
}
nSize <<= 1;
}
}
//done
return;
}
HRESULT ConvertToLongPath(_Inout_ CStringW &cStrPathW)
{
CStringW cStrTempW;
LPCWSTR sW;
SIZE_T i, nStartOfs, nOfs, nPrefixLen = 0;
HANDLE hFindFile;
WIN32_FIND_DATAW sFindDataW;
WCHAR chOrigW;
sW = (LPCWSTR)cStrPathW;
if (sW[0] == L'\\' &&
(sW[1] == L'\\' || sW[1] == L'?') &&
(sW[2] == L'.' || sW[2] == L'?') &&
sW[3] == L'\\')
{
//nt path provided
if ((sW[0] == L'U' || sW[0] == L'u') &&
(sW[1] == L'N' || sW[1] == L'n') &&
(sW[2] == L'C' || sW[2] == L'c') &&
sW[3] == L'\\')
{
return S_OK; //skip network folders
}
if (cStrTempW.Copy(L"\\\\?\\") == FALSE || cStrTempW.Concat(sW+4) == FALSE)
return E_OUTOFMEMORY;
}
else if ((sW[0] >= L'A' && sW[0] <= L'Z') || (sW[0] >= L'a' && sW[0] <= L'z') &&
sW[1] == L':')
{
if (cStrTempW.Copy(L"\\\\?\\") == FALSE || cStrTempW.Concat(sW) == FALSE)
return E_OUTOFMEMORY;
nPrefixLen = 4;
}
else
{
//skip other path types (like network folders)
return S_OK;
}
//look for the fourth backslash as starting offset
sW = (LPCWSTR)cStrTempW;
for (i = nOfs = 0; sW[nOfs] !=0; nOfs++)
{
if (sW[nOfs] == L'\\')
{
if ((++i) == 4)
{
nOfs++;
break;
}
}
}
//process
while (sW[nOfs] != 0)
{
//skip slashes
while (sW[nOfs] == L'\\')
nOfs++;
//advance until component end
nStartOfs = nOfs;
while (sW[nOfs] != 0 && sW[nOfs] != L'\\')
nOfs++;
//find file
if (nOfs > nStartOfs)
{
chOrigW = sW[nOfs];
((LPWSTR)sW)[nOfs] = 0;
hFindFile = ::FindFirstFileW(sW, &sFindDataW);
((LPWSTR)sW)[nOfs] = chOrigW;
if (hFindFile == INVALID_HANDLE_VALUE)
break;
::FindClose(hFindFile);
if (sFindDataW.cFileName[0] != 0)
{
cStrTempW.Delete(nStartOfs, nOfs - nStartOfs);
if (cStrTempW.Insert(sFindDataW.cFileName, nStartOfs) == FALSE)
return E_OUTOFMEMORY;
nOfs = nStartOfs + StrLenW(sFindDataW.cFileName);
sW = (LPCWSTR)cStrTempW;
}
}
}
//done
cStrTempW.Delete(0, nPrefixLen);
cStrPathW.Attach(cStrTempW.Detach());
return S_OK;
}
HRESULT ConvertToNative(_Inout_ CStringW &cStrPathW)
{
CStringW cStrTempW;
LPCWSTR sW;
HRESULT hRes;
sW = (LPCWSTR)cStrPathW;
//try symbolic link path
if (sW[0] == L'\\' &&
(sW[1] == L'\\' || sW[1] == L'?') &&
(sW[2] == L'.' || sW[2] == L'?') &&
sW[3] == L'\\')
{
if (cStrTempW.Format(L"\\??\\%s", sW+4) == FALSE)
return E_OUTOFMEMORY;
hRes = ResolveSymbolicLink(cStrTempW);
if (SUCCEEDED(hRes))
cStrPathW.Attach(cStrTempW.Detach());
return hRes;
}
//try DOS volume/path
if (((sW[0] >= L'A' && sW[0] <= L'Z') || (sW[0] >= L'a' && sW[0] <= L'z')) &&
sW[1] == L':')
{
if (cStrTempW.Format(L"\\??\\%s", sW) == FALSE)
return E_OUTOFMEMORY;
hRes = ResolveSymbolicLink(cStrTempW);
if (SUCCEEDED(hRes))
cStrPathW.Attach(cStrTempW.Detach());
return hRes;
}
//try network paths
if (sW[0] == L'\\' && sW[1] == L'\\')
{
if (cStrPathW.Insert(L"Device\\Mup", 1) == FALSE)
return E_OUTOFMEMORY;
return S_OK;
}
//done
return S_OK;
}
HRESULT ConvertToWin32(_Inout_ CStringW &cStrPathW)
{
CStringW cStrTempW;
MX_PROCESS_DEVICEMAP_INFORMATION sPdmi;
SIZE_T i, nLen;
WCHAR szDriveW[4], szVolumeNameW[512];
LPCWSTR sW;
NTSTATUS nNtStatus;
HRESULT hRes;
//first resolve symlink
sW = (LPCWSTR)cStrPathW;
if (sW[0] == L'\\' &&
(sW[1] == L'\\' || sW[1] == L'?') &&
(sW[2] == L'.' || sW[2] == L'?') &&
sW[3] == L'\\')
{
if (cStrTempW.Format(L"\\??\\%s", sW+4) == FALSE)
return E_OUTOFMEMORY;
hRes = ResolveSymbolicLink(cStrTempW);
if (SUCCEEDED(hRes))
cStrPathW.Attach(cStrTempW.Detach());
}
//network path?
if (StrNCompareW(sW, L"\\Device\\LanmanRedirector\\", 25, TRUE) == 0)
{
cStrPathW.Delete(1, 23);
return S_OK;
}
if (StrNCompareW(sW, L"\\Device\\Mup\\", 12, TRUE) == 0)
{
cStrPathW.Delete(1, 10);
return S_OK;
}
//can convert to DOS volume?
if (StrNCompareW(sW, L"\\Device\\", 8, TRUE) == 0)
{
nNtStatus = ::MxNtQueryInformationProcess(MX_CURRENTPROCESS, MxProcessDeviceMap, &(sPdmi.Query),
(ULONG)sizeof(sPdmi.Query), NULL);
if (!NT_SUCCESS(nNtStatus))
return MX_HRESULT_FROM_WIN32(::MxRtlNtStatusToDosError(nNtStatus));
szDriveW[1] = L':';
szDriveW[2] = 0;
for (i=0; i<26; i++)
{
if ((sPdmi.Query.DriveMap & (1UL << i)) != 0)
{
szDriveW[0] = (WCHAR)(L'A' + i);
nLen = (SIZE_T)::QueryDosDeviceW(szDriveW, szVolumeNameW, MX_ARRAYLEN(szVolumeNameW)-1);
if (nLen > 0)
{
szVolumeNameW[nLen] = 0;
nLen = StrLenW(szVolumeNameW);
if (StrNCompareW(sW, szVolumeNameW, nLen, TRUE) == 0 && (sW[nLen] == 0 || sW[nLen] == L'\\'))
{
//insert drive letter (before deletion to preserve input on error)
i = 2;
if (cStrPathW.GetLength() == 0 || *sW != L'\\')
szDriveW[i++] = L'\\';
if (cStrPathW.InsertN(szDriveW, 0, i) == FALSE)
return E_OUTOFMEMORY;
//delete NT data
cStrPathW.Delete(i, nLen);
break;
}
}
}
}
//if a drive letter was not found, convert "\Device\" to "\\?\"
if (i >= 26)
{
if (cStrPathW.InsertN(L"\\?", 1, 2) == FALSE)
return E_OUTOFMEMORY;
cStrPathW.Delete(3, 6);
}
}
//done
return S_OK;
}
HRESULT DeviceName2DosName(_Inout_ CStringW &cStrPathW)
{
WCHAR szNameW[1024], szDriveW[3], *sW;
DWORD dwDrivesMask;
SIZE_T nNameLen;
HRESULT hRes;
sW = (LPWSTR)cStrPathW;
//try network shares first
if (sW[0] == L'\\')
{
if (StrNCompareW(sW, L"\\Device\\MUP\\", 12, TRUE) == 0)
{
cStrPathW.Delete(1, 10);
return S_OK;
}
if (StrNCompareW(sW, L"\\Device\\LanmanRedirector\\", 25, TRUE) == 0)
{
cStrPathW.Delete(1, 23);
return S_OK;
}
if (StrNCompareW(sW, L"\\\\?\\", 4) == 0 || StrNCompareW(sW, L"\\??\\", 4) == 0)
{
cStrPathW.Delete(0, 4);
}
}
//translate path in device form to drive letters
dwDrivesMask = ::GetLogicalDrives();
szDriveW[1] = L':';
szDriveW[2] = 0;
sW = (LPWSTR)cStrPathW;
hRes = S_OK;
for (szDriveW[0] = L'A'; szDriveW[0] <= L'Z'; szDriveW[0]++, dwDrivesMask >>= 1)