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.

200 lines
4.6 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sfc.h>
  5. PROTECTED_FILE_DATA pd;
  6. DWORD CachedFiles;
  7. DWORD NonCachedFiles;
  8. DWORD TotalFiles;
  9. WCHAR BackupDir[256];
  10. void
  11. ProcessProtectedFile(
  12. int FileNumber
  13. )
  14. {
  15. int action;
  16. PWSTR s;
  17. WCHAR buf[512];
  18. //
  19. // get the file name
  20. //
  21. ZeroMemory( &pd, sizeof(pd) );
  22. pd.FileNumber = (DWORD)FileNumber;
  23. if (!SfcGetNextProtectedFile(NULL,&pd)) {
  24. return;
  25. }
  26. //
  27. // if the file doesn't exist then there is nothing do to
  28. //
  29. if (GetFileAttributes( pd.FileName ) == 0xffffffff) {
  30. return;
  31. }
  32. //
  33. // backup the file before we mess with it
  34. //
  35. wcscpy( buf, BackupDir );
  36. wcscat( buf, &pd.FileName[2] );
  37. s = wcsrchr( buf, L'\\' );
  38. *s = 0;
  39. CreateDirectory( buf, NULL );
  40. *s = L'\\';
  41. CopyFile( pd.FileName, buf, FALSE );
  42. //
  43. // now do something
  44. //
  45. action = rand() % 3;
  46. switch (action) {
  47. case 0:
  48. //
  49. // delete the file
  50. //
  51. DeleteFile( pd.FileName );
  52. break;
  53. case 1:
  54. //
  55. // rename the file
  56. //
  57. wcscpy( buf, pd.FileName );
  58. wcscat( buf, L".sfc" );
  59. MoveFileEx( pd.FileName, buf, MOVEFILE_REPLACE_EXISTING );
  60. break;
  61. case 2:
  62. //
  63. // move the file
  64. //
  65. wcscpy( buf, L"c:\\temp\\sfctemp" );
  66. wcscat( buf, &pd.FileName[2] );
  67. MoveFileEx( pd.FileName, buf, MOVEFILE_REPLACE_EXISTING );
  68. break;
  69. case 3:
  70. //
  71. // change the file attributes
  72. //
  73. SetFileAttributes( pd.FileName, GetFileAttributes( pd.FileName ) );
  74. break;
  75. default:
  76. //
  77. // should not get here....
  78. //
  79. return;
  80. }
  81. }
  82. int __cdecl wmain( int argc, WCHAR *argv[] )
  83. {
  84. LONG rc;
  85. HKEY hKey;
  86. PWSTR s;
  87. WCHAR buf[512];
  88. DWORD sz;
  89. WCHAR CacheDir[512];
  90. int rnum;
  91. DWORD FileCount = (DWORD)-1;
  92. HANDLE SfcDebugBreakEvent;
  93. if (argc == 2) {
  94. if (_wcsicmp( argv[1], L"break" ) == 0) {
  95. SfcDebugBreakEvent = OpenEvent( EVENT_MODIFY_STATE, FALSE, L"SfcDebugBreakEvent" );
  96. if (SfcDebugBreakEvent) {
  97. SetEvent( SfcDebugBreakEvent );
  98. } else {
  99. wprintf( L"could not open the break event, ec=%d\n", GetLastError() );
  100. }
  101. return 0;
  102. } else {
  103. FileCount = _wtoi( argv[1] );
  104. }
  105. }
  106. rc = RegOpenKey(
  107. HKEY_LOCAL_MACHINE,
  108. L"software\\microsoft\\windows nt\\currentversion\\winlogon",
  109. &hKey
  110. );
  111. if (rc != ERROR_SUCCESS) {
  112. return 0;
  113. }
  114. sz = sizeof(buf);
  115. rc = RegQueryValueEx(
  116. hKey,
  117. L"SFCDllCacheDir",
  118. NULL,
  119. NULL,
  120. (LPBYTE)buf,
  121. &sz
  122. );
  123. if (rc != ERROR_SUCCESS) {
  124. wcscpy( buf, L"%systemroot%\\system32\\dllcache\\" );
  125. }
  126. RegCloseKey( hKey );
  127. if (buf[wcslen(buf)-1] != L'\\') {
  128. wcscat( buf, L"\\" );
  129. }
  130. rc = ExpandEnvironmentStrings( buf, CacheDir, sizeof(CacheDir)/sizeof(WCHAR) );
  131. if (!rc) {
  132. return 0;
  133. }
  134. wcscpy( BackupDir, L"c:\\temp\\sfcsave" );
  135. while (SfcGetNextProtectedFile(NULL,&pd)) {
  136. s = wcsrchr( pd.FileName, L'\\' );
  137. if (!s) {
  138. return 0;
  139. }
  140. s += 1;
  141. wcscpy( buf, CacheDir );
  142. wcscat( buf, s );
  143. if (GetFileAttributes( buf ) != 0xffffffff) {
  144. CachedFiles += 1;
  145. }
  146. }
  147. ZeroMemory( &pd, sizeof(pd) );
  148. pd.FileNumber = 0xffffffff;
  149. SfcGetNextProtectedFile(NULL,&pd);
  150. TotalFiles = pd.FileNumber;
  151. NonCachedFiles = TotalFiles - CachedFiles;
  152. wprintf( L"cached files = %d\n", CachedFiles );
  153. wprintf( L"non-cached files = %d\n", NonCachedFiles );
  154. wprintf( L"total files = %d\n", TotalFiles );
  155. srand( (unsigned int)GetTickCount() );
  156. while(1) {
  157. rnum = rand();
  158. if (rnum&1) {
  159. rnum = rand() % CachedFiles;
  160. } else {
  161. rnum = rand() % TotalFiles;
  162. if (rnum < (int)CachedFiles) {
  163. rnum = (rnum + CachedFiles) % TotalFiles;
  164. }
  165. }
  166. ProcessProtectedFile( rnum );
  167. if (FileCount != (DWORD)-1) {
  168. FileCount -= 1;
  169. if (FileCount == 0) {
  170. break;
  171. }
  172. }
  173. }
  174. return 0;
  175. }