Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.4 KiB

  1. //
  2. // Simple wrapper around GetFullPathname and FindFirstFile/FindNextFile and DeleteFile
  3. // that converts to \\? form.
  4. //
  5. #include "nt.h"
  6. #include "ntrtl.h"
  7. #include "nturtl.h"
  8. #include "windows.h"
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #pragma warning(disable:4511)
  14. #pragma warning(disable:4512)
  15. #include "yvals.h"
  16. #pragma warning(disable:4663)
  17. #pragma warning(disable:4018)
  18. #include <vector>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #define NUMBER_OF(x) (sizeof(x)/sizeof((x)[0]))
  22. #define FusionpGetLastWin32Error GetLastError
  23. #define FusionpSetLastWin32Error SetLastError
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #include "fusionhandle.h"
  27. BOOL FusionpConvertToBigPath(PCWSTR Path, SIZE_T BufferSize, PWSTR Buffer);
  28. BOOL FusionpSkipBigPathRoot(PCWSTR s, OUT SIZE_T*);
  29. BOOL FusionpAreWeInOSSetupMode(BOOL* pfIsInSetup) { *pfIsInSetup = FALSE; return TRUE; }
  30. extern "C"
  31. {
  32. BOOL WINAPI SxsDllMain(HINSTANCE hInst, DWORD dwReason, PVOID pvReserved);
  33. void __cdecl wmainCRTStartup();
  34. BOOL FusionpInitializeHeap(HINSTANCE hInstance);
  35. VOID FusionpUninitializeHeap();
  36. };
  37. void ExeEntry()
  38. {
  39. if (!::FusionpInitializeHeap(GetModuleHandleW(NULL)))
  40. goto Exit;
  41. ::wmainCRTStartup();
  42. Exit:
  43. FusionpUninitializeHeap();
  44. }
  45. FILE* g_pLogFile;
  46. const static WCHAR g_pszImage[] = L"del_bigpath";
  47. void
  48. ReportFailure(
  49. const char* szFormat,
  50. ...
  51. )
  52. {
  53. const DWORD dwLastError = ::GetLastError();
  54. va_list ap;
  55. char rgchBuffer[4096] = { 0 };
  56. WCHAR rgchWin32Error[4096] = { 0 };
  57. va_start(ap, szFormat);
  58. ::_vsnprintf(rgchBuffer, NUMBER_OF(rgchBuffer) - 1, szFormat, ap);
  59. va_end(ap);
  60. if (dwLastError != 0)
  61. {
  62. if (!::FormatMessageW(
  63. FORMAT_MESSAGE_FROM_SYSTEM,
  64. NULL,
  65. dwLastError,
  66. 0,
  67. rgchWin32Error,
  68. NUMBER_OF(rgchWin32Error) - 1,
  69. &ap))
  70. {
  71. const DWORD dwLastError2 = ::GetLastError();
  72. ::_snwprintf(rgchWin32Error, NUMBER_OF(rgchWin32Error) - 1, L"Error formatting Win32 error %lu\nError from FormatMessage is %lu", dwLastError, dwLastError2);
  73. }
  74. }
  75. ::fprintf(stderr, "%ls: %s\n%ls\n", g_pszImage, rgchBuffer, rgchWin32Error);
  76. if (g_pLogFile != NULL)
  77. {
  78. ::fprintf(g_pLogFile, "%ls: %s\n%ls\n", g_pszImage, rgchBuffer, rgchWin32Error);
  79. }
  80. }
  81. extern "C" int __cdecl wmain(int argc, wchar_t** argv)
  82. {
  83. int iReturnStatus = EXIT_FAILURE;
  84. std::vector<WCHAR> arg1;
  85. WIN32_FIND_DATAW wfd;
  86. CFindFile FindFileHandle;
  87. SIZE_T BigPathRootLength = 0;
  88. SIZE_T NonwildcardLength = 0;
  89. SIZE_T AllButLastPathElementlength = 0;
  90. SIZE_T LastPathElementLength = 0;
  91. if (argc != 2)
  92. {
  93. ::fprintf(stderr,
  94. "%ls: Usage:\n"
  95. " %ls <file-to-delete-wildcards-ok>\n",
  96. argv[0], argv[0]);
  97. goto Exit;
  98. }
  99. arg1.resize(1 + (1UL << 15));
  100. arg1[0] = 0;
  101. if (!FusionpConvertToBigPath(argv[1], arg1.size(), &arg1[0]))
  102. {
  103. ::ReportFailure("FusionpConvertToBigPath\n");
  104. goto Exit;
  105. }
  106. if (!FusionpSkipBigPathRoot(&arg1[0], &BigPathRootLength))
  107. {
  108. ::ReportFailure("FusionpSkipBigPathRoot\n");
  109. goto Exit;
  110. }
  111. arg1.resize(1 + ::wcslen(&arg1[0]));
  112. NonwildcardLength = wcscspn(&arg1[BigPathRootLength], L"*?");
  113. LastPathElementLength = StringReverseComplementSpan(&arg1[BigPathRootLength], &arg1[arg1.size() - 1], L"\\/");
  114. AllButLastPathElementlength = arg1.size() - 1 - BigPathRootLength - LastPathElementLength;
  115. //AllButLastPathElementlength -= (AllButLastPathElementlength != 0);
  116. //::printf("%ls\n", &arg1[0]);
  117. //::printf("BigPathRootLength %Id\n", BigPathRootLength);
  118. //::printf("NonwildcardLength %Id\n", NonwildcardLength);
  119. //::printf("LastPathElementLength %Id\n", LastPathElementLength);
  120. //::printf("AllButLastPathElementlength %Id\n", AllButLastPathElementlength);
  121. if (NonwildcardLength + BigPathRootLength == arg1.size() - 1)
  122. {
  123. //::printf("arg1 contains no wildcards\n");
  124. //::TerminateProcess(GetCurrentProcess(), __LINE__);
  125. if (!DeleteFileW(&arg1[0]))
  126. {
  127. ::ReportFailure("DeleteFileW(%ls)\n", &arg1[0]);
  128. goto Exit;
  129. }
  130. ::printf("DeleteFileW(%ls)\n", &arg1[0]);
  131. goto Success;
  132. }
  133. if (NonwildcardLength < AllButLastPathElementlength)
  134. {
  135. ::SetLastError(0);
  136. ::ReportFailure("wildcards in nonleaf\n");
  137. goto Exit;
  138. }
  139. //::TerminateProcess(GetCurrentProcess(), __LINE__);
  140. if (!FindFileHandle.Win32FindFirstFile(&arg1[0], &wfd))
  141. {
  142. ::ReportFailure("FindFirstFileW(%ls)\n", &arg1[0]);
  143. goto Exit;
  144. }
  145. do
  146. {
  147. if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  148. {
  149. continue;
  150. }
  151. arg1.resize(AllButLastPathElementlength + BigPathRootLength);
  152. //arg1.push_back('\\');
  153. arg1.insert(arg1.end(), wfd.cFileName, wfd.cFileName + ::wcslen(wfd.cFileName) + 1);
  154. if (!DeleteFileW(&arg1[0]))
  155. {
  156. ::ReportFailure("DeleteFileW(%ls)\n", &arg1[0]);
  157. continue;
  158. }
  159. ::printf("DeleteFileW(%ls)\n", &arg1[0]);
  160. } while(::FindNextFileW(FindFileHandle, &wfd));
  161. Success:
  162. iReturnStatus = EXIT_SUCCESS;
  163. Exit:
  164. return iReturnStatus;
  165. }