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.

279 lines
6.4 KiB

  1. //
  2. // FSCTL_ENUM_USN_DATA dumper..
  3. //
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <windows.h>
  8. #include <winioctl.h>
  9. #include <winbase.h>
  10. #include <wtypes.h>
  11. #include <winver.h>
  12. #define OUT_BUFF_SIZE 0x1000
  13. #define GLE_EXIT printf("gle=%ld\n",GetLastError()); \
  14. fflush(stdout); \
  15. ExitProcess(1);
  16. char *Days[] =
  17. {
  18. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  19. };
  20. char *Months[] =
  21. {
  22. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  23. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  24. };
  25. CHAR *
  26. FileTimeToString(FILETIME *FileTime)
  27. {
  28. FILETIME LocalFileTime;
  29. SYSTEMTIME SystemTime;
  30. static char Buffer[32] = "-none-";
  31. if (FileTime->dwHighDateTime != 0 || FileTime->dwLowDateTime != 0)
  32. {
  33. if (!FileTimeToLocalFileTime(FileTime, &LocalFileTime) ||
  34. !FileTimeToSystemTime(&LocalFileTime, &SystemTime))
  35. {
  36. return("Time???");
  37. }
  38. sprintf(
  39. Buffer,
  40. "%s %s %2d, %4d %02d:%02d:%02d",
  41. Days[SystemTime.wDayOfWeek],
  42. Months[SystemTime.wMonth - 1],
  43. SystemTime.wDay,
  44. SystemTime.wYear,
  45. SystemTime.wHour,
  46. SystemTime.wMinute,
  47. SystemTime.wSecond);
  48. }
  49. return(Buffer);
  50. }
  51. //
  52. // return uppper 32bits of a 64bit number
  53. //
  54. ULONG HiPart(ULONGLONG n) {
  55. return (ULONG) (n >> 32);
  56. }
  57. //
  58. // return lower 32bits of a 64bit number
  59. //
  60. ULONG LoPart(ULONGLONG n) {
  61. return (ULONG) (n);
  62. }
  63. //
  64. // returns a zero terminated wide char string
  65. //
  66. WCHAR *
  67. GetSZWideString( WCHAR* WideString, USHORT Length) {
  68. WCHAR* pResult;
  69. pResult = (WCHAR*) calloc( Length + 2, 1); //+2 for null termination chars
  70. if (NULL == pResult) {
  71. printf("calloc failed: GetSZWideString()\n");
  72. fflush(stdout);
  73. ExitProcess(1);
  74. }
  75. CopyMemory( (PVOID) pResult, (CONST VOID *) WideString, (DWORD) Length);
  76. return ( pResult );
  77. }
  78. //
  79. // globals
  80. //
  81. HANDLE ghVol;
  82. //
  83. // ********** MAIN ***********
  84. //
  85. void __cdecl main(int argc, char* argv[]) {
  86. DWORD dwRc=0; // return byte count
  87. DWORD gle; // GetLastError() code
  88. BOOL fSuccess;
  89. BOOL fMoreFiles;
  90. HANDLE hVol2;
  91. MFT_ENUM_DATA MftEnumData;
  92. CHAR OutBuff[0x10000];
  93. PUSN_RECORD pUsnRecord;
  94. ULONGLONG NextFileRefNum = 0;
  95. CHAR szVolStr[MAX_PATH];
  96. CHAR fn[MAX_PATH];
  97. WCHAR* pFileName;
  98. if ( argc < 2 ) {
  99. printf("\nUsage is \"%s drive:\"\n",argv[0]);
  100. fflush(stdout);
  101. ExitProcess(1);
  102. }
  103. sprintf(szVolStr, "\\\\.\\%s", argv[1]);
  104. //
  105. // open volume handle
  106. //
  107. ghVol = CreateFileA(szVolStr,
  108. GENERIC_READ | GENERIC_WRITE,
  109. FILE_SHARE_READ|FILE_SHARE_WRITE,
  110. NULL,
  111. OPEN_EXISTING,
  112. 0,
  113. NULL);
  114. if ( INVALID_HANDLE_VALUE == ghVol ) {
  115. GLE_EXIT;
  116. }
  117. MftEnumData.LowUsn = 0;
  118. MftEnumData.HighUsn = 0x0FFFFFFFFFFFFFFF;
  119. printf("l=%I64Xh, h=%I64Xh\n", MftEnumData.LowUsn, MftEnumData.HighUsn);
  120. //
  121. // enum mft
  122. //
  123. fMoreFiles = TRUE;
  124. while( fMoreFiles ) {
  125. MftEnumData.StartFileReferenceNumber = NextFileRefNum;
  126. ZeroMemory(OutBuff, 0x1000);
  127. fSuccess = DeviceIoControl( ghVol,
  128. FSCTL_ENUM_USN_DATA,
  129. &MftEnumData,
  130. sizeof(MFT_ENUM_DATA),
  131. OutBuff,
  132. OUT_BUFF_SIZE,
  133. &dwRc,
  134. NULL);
  135. if ( ! fSuccess ) {
  136. gle = GetLastError();
  137. //
  138. // this error is OK
  139. //
  140. if (ERROR_NO_MORE_FILES == gle) {
  141. fMoreFiles = FALSE;
  142. } else {
  143. printf("fsctl_enum_usn data gle=%ld\n",gle);
  144. fflush(stdout);
  145. ExitProcess(1);
  146. }
  147. }
  148. //
  149. // run thru mft records..
  150. //
  151. if ( dwRc ) {
  152. printf("\n%ld bytes Returned\n", dwRc);
  153. pUsnRecord = (PUSN_RECORD) (OutBuff + sizeof(ULONGLONG));
  154. dwRc -= sizeof(ULONGLONG);
  155. if ( ! dwRc ) {
  156. fMoreFiles = FALSE;
  157. }
  158. NextFileRefNum = *(ULONGLONG*) OutBuff;
  159. printf("\nNextFileRef: %08lX-%08lXh\n\n", HiPart(NextFileRefNum), LoPart(NextFileRefNum));
  160. }
  161. if ( fMoreFiles ) {
  162. while ( dwRc ) {
  163. // do some printing..
  164. printf("\n reclen: %Xh", pUsnRecord->RecordLength);
  165. printf("\n Major ver: %d", pUsnRecord->MajorVersion);
  166. printf("\n Minor ver: %d", pUsnRecord->MinorVersion);
  167. printf("\n fileref: %08lX-%08lXh", HiPart(pUsnRecord->FileReferenceNumber),
  168. LoPart(pUsnRecord->FileReferenceNumber));
  169. printf("\n parentref: %08lX-%08lXh", HiPart(pUsnRecord->ParentFileReferenceNumber),
  170. LoPart(pUsnRecord->ParentFileReferenceNumber));
  171. printf("\n usn: %08lX-%08lXh", HiPart(pUsnRecord->Usn), LoPart(pUsnRecord->Usn));
  172. printf("\n timestamp: %s", FileTimeToString((FILETIME*)&pUsnRecord->TimeStamp));
  173. printf("\n reason: %Xh",pUsnRecord->Reason);
  174. printf("\n sourceinfo: %Xh",pUsnRecord->SourceInfo);
  175. printf("\n security-id: %Xh",pUsnRecord->SecurityId);
  176. printf("\n attributes: %Xh", pUsnRecord->FileAttributes);
  177. printf("\n filename len: %Xh", pUsnRecord->FileNameLength);
  178. printf("\n filename offset: %Xh", pUsnRecord->FileNameOffset);
  179. pFileName = GetSZWideString( (WCHAR*) pUsnRecord->FileName, pUsnRecord->FileNameLength);
  180. printf("\n filename: %S", pFileName);
  181. free(pFileName);
  182. printf("\n\n---------------------------------------\n");
  183. if (pUsnRecord->RecordLength <= dwRc) {
  184. dwRc -= pUsnRecord->RecordLength;
  185. pUsnRecord = (PUSN_RECORD)((PCHAR) pUsnRecord + pUsnRecord->RecordLength);
  186. } else {
  187. printf("Invalid dwRc");
  188. fflush(stdout);
  189. ExitProcess(1);
  190. }
  191. }
  192. }
  193. }
  194. }