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.

280 lines
7.5 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * chglog.cpp
  7. *
  8. * Abstract:
  9. * Tool for enumerating the change log - forward/reverse
  10. *
  11. * Revision History:
  12. *
  13. * Brijesh Krishnaswami (brijeshk) 04/09/2000
  14. * created
  15. *
  16. * SHeffner
  17. * Just grabbed the code, and put it into SRDiag.
  18. *
  19. * Weiyou Cui (weiyouc) 02-May-2001
  20. * Rewritten
  21. *
  22. *****************************************************************************/
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Common Includes
  26. //
  27. //----------------------------------------------------------------------------
  28. #include "srheader.hxx"
  29. #include <srapi.h>
  30. #include <shellapi.h>
  31. #include <enumlogs.h>
  32. #include <srrpcapi.h>
  33. //+---------------------------------------------------------------------------
  34. //
  35. // Function proto types
  36. //
  37. //----------------------------------------------------------------------------
  38. LPWSTR GetEventString(DWORD EventId);
  39. HRESULT EnumLog(LPTSTR ptszFileName, LPTSTR ptszDrive);
  40. //+---------------------------------------------------------------------------
  41. //
  42. // Some structures
  43. //
  44. //----------------------------------------------------------------------------
  45. struct _EVENT_STR_MAP
  46. {
  47. DWORD EventId;
  48. LPWSTR pEventStr;
  49. } EventMap[] =
  50. {
  51. {SrEventInvalid , L"INVALID" },
  52. {SrEventStreamChange, L"FILE-MODIFY" },
  53. {SrEventAclChange, L"ACL-CHANGE" },
  54. {SrEventAttribChange, L"ATTR-CHANGE" },
  55. {SrEventStreamOverwrite,L"FILE-MODIFY" },
  56. {SrEventFileDelete, L"FILE-DELETE" },
  57. {SrEventFileCreate, L"FILE-CREATE" },
  58. {SrEventFileRename, L"FILE-RENAME" },
  59. {SrEventDirectoryCreate,L"DIR-CREATE" },
  60. {SrEventDirectoryRename,L"DIR-RENAME" },
  61. {SrEventDirectoryDelete,L"DIR-DELETE" },
  62. {SrEventMountCreate, L"MNT-CREATE" },
  63. {SrEventMountDelete, L"MNT-DELETE" },
  64. {SrEventVolumeError, L"VOLUME-ERROR" }
  65. };
  66. //+---------------------------------------------------------------------------
  67. //
  68. // Function: GetChgLogOnDrives
  69. //
  70. // Synopsis: Dumps the change log into the file specified
  71. //
  72. // Arguments: [ptszLogFile] -- log file name
  73. //
  74. // Returns: HRESULT
  75. //
  76. // History: 9/21/00 SHeffner created
  77. //
  78. // 02-May-2001 WeiyouC Rewritten
  79. //
  80. //----------------------------------------------------------------------------
  81. HRESULT GetChgLogOnDrives(LPTSTR ptszLogFile)
  82. {
  83. HRESULT hr = S_OK;
  84. DWORD dwLength = MAX_PATH;
  85. HANDLE hVolume = INVALID_HANDLE_VALUE;
  86. TCHAR tszVolume[MAX_PATH];
  87. DH_VDATEPTRIN(ptszLogFile, TCHAR);
  88. //
  89. // Walk through all of the volume's on the system, and then validate that
  90. // this is a fixed drive. Once we have a valid drive then pass this volume
  91. // to the enumeration routine for changelog.
  92. //
  93. hVolume = FindFirstVolume(tszVolume, dwLength);
  94. if (INVALID_HANDLE_VALUE != hVolume)
  95. {
  96. do
  97. {
  98. dwLength = MAX_PATH;
  99. if (DRIVE_FIXED == GetDriveType(tszVolume))
  100. {
  101. hr = EnumLog(ptszLogFile, tszVolume);
  102. DH_HRCHECK_ABORT(hr, TEXT("EnumLog"));
  103. }
  104. } while (FindNextVolume(hVolume, tszVolume, dwLength) );
  105. }
  106. if (INVALID_HANDLE_VALUE != hVolume)
  107. {
  108. FindVolumeClose(hVolume);
  109. }
  110. ErrReturn:
  111. return hr;
  112. }
  113. //+---------------------------------------------------------------------------
  114. //
  115. // Function: GetEventString
  116. //
  117. // Synopsis: Transulates the EventString from the event ID
  118. //
  119. // Arguments: [dwEventID] -- DWord for the event code
  120. //
  121. // Returns: Pointer to maped string to the event coded
  122. //
  123. // History: 9/21/00 SHeffner Copied from Brijesh
  124. //
  125. // 02-May-2001 WeiyouC Rewritten
  126. //
  127. //----------------------------------------------------------------------------
  128. LPWSTR GetEventString(DWORD dwEventID)
  129. {
  130. LPWSTR pwStr = L"NOT-FOUND";
  131. for (int i = 0; i < sizeof(EventMap)/sizeof(_EVENT_STR_MAP); i++)
  132. {
  133. if (EventMap[i].EventId == dwEventID )
  134. {
  135. pwStr = EventMap[i].pEventStr;
  136. }
  137. }
  138. return pwStr;
  139. }
  140. //+---------------------------------------------------------------------------
  141. //
  142. // Function: EnumLog
  143. //
  144. // Synopsis: Enumerate the change log for the Volume
  145. //
  146. // Arguments: [ptszLogFile] -- log file name
  147. // [ptszDrive] -- drive name
  148. //
  149. // Returns: HRESULT
  150. //
  151. // History: 9/21/00 SHeffner
  152. // Grabbed from Brijesh. Tweaked to get the rest of
  153. // the fields
  154. //
  155. // 02-May-2001 WeiyouC
  156. // Rewritten
  157. //
  158. //----------------------------------------------------------------------------
  159. HRESULT EnumLog(LPTSTR ptszLogFile, LPTSTR ptszDrive)
  160. {
  161. HRESULT hr = S_OK;
  162. DWORD dwTargetRPNum = 0;
  163. HGLOBAL hMem = NULL;
  164. FILE* fpLog = NULL;
  165. DWORD dwLength = 0;
  166. BOOL fOK = FALSE;
  167. CChangeLogEntry cle;
  168. CChangeLogEntryEnum ChangeLog(ptszDrive, TRUE, dwTargetRPNum, TRUE);
  169. TCHAR tszMount[MAX_PATH];
  170. DH_VDATEPTRIN(ptszLogFile, TCHAR);
  171. DH_VDATEPTRIN(ptszDrive, TCHAR);
  172. //
  173. // Open up our logging file
  174. //
  175. fpLog = _tfopen(ptszLogFile, TEXT("a"));
  176. DH_ABORTIF(NULL == fpLog,
  177. E_FAIL,
  178. TEXT("_tfopen"));
  179. //
  180. // Write header for our Section so that we can see what
  181. // volume that we are enumerating
  182. //
  183. fOK = GetVolumePathNamesForVolumeName(ptszDrive,
  184. tszMount,
  185. MAX_PATH,
  186. &dwLength);
  187. DH_ABORTIF(!fOK,
  188. HRESULT_FROM_WIN32(GetLastError()),
  189. TEXT("GetVolumePathNamesForVolumeName"));
  190. fprintf(fpLog,
  191. "\nChangeLog Enumeration for Drive [%S] Volume %S\n\n",
  192. tszMount,
  193. ptszDrive);
  194. //
  195. // Calling the ChangeLogenumeration functions,
  196. // specifying the drive. Forward through log,
  197. // RP Number start 0, and switch??
  198. //
  199. if (ERROR_SUCCESS == ChangeLog.FindFirstChangeLogEntry(cle))
  200. {
  201. do
  202. {
  203. fprintf(fpLog,
  204. "RPDir=%S, "
  205. "Drive=%S, "
  206. "SeqNum=%I64ld, "
  207. "EventString=%S, "
  208. "Flags=%lu, "
  209. "Attr=%lu, "
  210. "Acl=%S, "
  211. "AclSize=%lu, "
  212. "AclInline=%lu, "
  213. "Process=%S, "
  214. "ProcName=%S, "
  215. "Path1=%S, "
  216. "Path2=%S, "
  217. "Temp=%S\n",
  218. cle.GetRPDir(),
  219. tszMount,
  220. cle.GetSequenceNum(),
  221. GetEventString(cle.GetType()),
  222. cle.GetFlags(),
  223. cle.GetAttributes(),
  224. cle.GetAcl() ? L"Yes" : L"No",
  225. cle.GetAclSize(),
  226. cle.GetAclInline(),
  227. cle.GetProcess() ? cle.GetProcess() : L"NULL",
  228. cle.GetProcName() ? cle.GetProcName() : L"NULL",
  229. cle.GetPath1() ? cle.GetPath1() : L"NULL",
  230. cle.GetPath2() ? cle.GetPath2() : L"NULL",
  231. cle.GetTemp() ? cle.GetTemp() : L"NULL");
  232. } while (ERROR_SUCCESS == ChangeLog.FindNextChangeLogEntry(cle));
  233. ChangeLog.FindClose();
  234. }
  235. else
  236. {
  237. fprintf(fpLog, "No change log entries\n");
  238. }
  239. ErrReturn:
  240. if (NULL != fpLog)
  241. {
  242. fclose(fpLog);
  243. }
  244. if (NULL != hMem)
  245. {
  246. GlobalFree(hMem);
  247. }
  248. return hr;
  249. }