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.

477 lines
12 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. logfile.cpp
  5. Abstract:
  6. This file contains the implementation of CRestoreLogFile class and
  7. ::CreateRestoreLogFile.
  8. Revision History:
  9. Seong Kook Khang (SKKhang) 06/21/00
  10. created
  11. ******************************************************************************/
  12. #include "stdwin.h"
  13. #include "rstrcore.h"
  14. #include "resource.h"
  15. static LPCWSTR s_cszLogFile = L"%SystemRoot%\\system32\\restore\\rstrlog.dat";
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CRestoreLogFile construction / destruction
  18. CRestoreLogFile::CRestoreLogFile()
  19. {
  20. m_szLogFile[0] = L'\0';
  21. m_hfLog = INVALID_HANDLE_VALUE;
  22. }
  23. CRestoreLogFile::~CRestoreLogFile()
  24. {
  25. }
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CRestoreLogFile - methods
  28. BOOL
  29. CRestoreLogFile::Open()
  30. {
  31. TraceFunctEnter("CRestoreLogFile::Open");
  32. BOOL fRet = FALSE;
  33. LPCWSTR cszErr;
  34. SRstrLogHdrBase sHdrBase;
  35. DWORD dwRes;
  36. if ( m_hfLog != INVALID_HANDLE_VALUE )
  37. {
  38. ErrorTrace(0, "File is already opened...");
  39. goto Exit;
  40. }
  41. if ( !Init() )
  42. goto Exit;
  43. m_hfLog = ::CreateFile( m_szLogFile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
  44. if ( m_hfLog == INVALID_HANDLE_VALUE )
  45. {
  46. cszErr = ::GetSysErrStr();
  47. ErrorTrace(0, "::CreateFile failed - %ls", cszErr);
  48. goto Exit;
  49. }
  50. READFILE_AND_VALIDATE( m_hfLog, &sHdrBase, sizeof(SRstrLogHdrBase), dwRes, Exit );
  51. if ( ( sHdrBase.dwSig1 != RSTRLOG_SIGNATURE1 ) ||
  52. ( sHdrBase.dwSig2 != RSTRLOG_SIGNATURE2 ) ||
  53. ( sHdrBase.dwVer != RSTRLOG_VERSION ) )
  54. {
  55. ErrorTrace(0, "Invalid restore log file header signature...");
  56. goto Exit;
  57. }
  58. fRet = TRUE;
  59. Exit:
  60. TraceFunctLeave();
  61. return( fRet );
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. BOOL
  65. CRestoreLogFile::Close()
  66. {
  67. TraceFunctEnter("CRestoreLogFile::Close");
  68. BOOL fRet = TRUE;
  69. if ( m_hfLog != INVALID_HANDLE_VALUE )
  70. {
  71. fRet = ::CloseHandle( m_hfLog );
  72. if ( fRet )
  73. m_hfLog = INVALID_HANDLE_VALUE;
  74. }
  75. TraceFunctLeave();
  76. return( fRet );
  77. }
  78. /////////////////////////////////////////////////////////////////////////////
  79. BOOL
  80. CRestoreLogFile::ReadHeader( SRstrLogHdrV3 *pRPInfo, CRDIArray &aryDrv )
  81. {
  82. TraceFunctEnter("CRestoreLogFile::ReadHeader");
  83. BOOL fRet = FALSE;
  84. SRstrLogHdrBase sHdr;
  85. DWORD dwRes;
  86. DWORD i;
  87. CRstrDriveInfo *pRDI = NULL;
  88. if ( m_hfLog == INVALID_HANDLE_VALUE )
  89. {
  90. ErrorTrace(0, "File is not opened...");
  91. goto Exit;
  92. }
  93. READFILE_AND_VALIDATE( m_hfLog, pRPInfo, sizeof(SRstrLogHdrV3), dwRes, Exit );
  94. // read drive table information
  95. for ( i = 0; i < pRPInfo->dwDrives; i++ )
  96. {
  97. if ( !CreateAndLoadDriveInfoInstance( m_hfLog, &pRDI ) )
  98. goto Exit;
  99. if ( !aryDrv.AddItem( pRDI ) )
  100. goto Exit;
  101. pRDI = NULL;
  102. }
  103. fRet = TRUE;
  104. Exit:
  105. if ( !fRet )
  106. {
  107. aryDrv.DeleteAll();
  108. SAFE_RELEASE(pRDI);
  109. }
  110. TraceFunctLeave();
  111. return( fRet );
  112. }
  113. /////////////////////////////////////////////////////////////////////////////
  114. BOOL
  115. CRestoreLogFile::AppendHeader( SRstrLogHdrV3Ex *pExtInfo )
  116. {
  117. TraceFunctEnter("CRestoreLogFile::AppendHeader");
  118. BOOL fRet = FALSE;
  119. DWORD dwRes;
  120. if ( m_hfLog == INVALID_HANDLE_VALUE )
  121. {
  122. ErrorTrace(0, "File is not opened...");
  123. goto Exit;
  124. }
  125. // Assuming ReadHeader has been called to move file pointer to a proper location.
  126. // Review if explicit setting of file pointer would be necessary.
  127. WRITEFILE_AND_VALIDATE( m_hfLog, pExtInfo, sizeof(SRstrLogHdrV3Ex), dwRes, Exit );
  128. fRet = TRUE;
  129. Exit:
  130. TraceFunctLeave();
  131. return( fRet );
  132. }
  133. /////////////////////////////////////////////////////////////////////////////
  134. BOOL
  135. CRestoreLogFile::WriteEntry( DWORD dwID, CRestoreMapEntry *pEnt, LPCWSTR cszMount )
  136. {
  137. TraceFunctEnter("CRestoreLogFile::WriteEntry");
  138. BOOL fRet = FALSE;
  139. BYTE *pBuf = NULL;
  140. SRstrEntryHdr *pEntHdr;
  141. DWORD dwSize;
  142. DWORD dwRes;
  143. WCHAR szTemp[SR_MAX_FILENAME_LENGTH];
  144. DWORD cbBuf;
  145. if ( m_hfLog == INVALID_HANDLE_VALUE )
  146. {
  147. ErrorTrace(0, "File is not opened...");
  148. goto Exit;
  149. }
  150. cbBuf = sizeof(SRstrEntryHdr);
  151. if (pEnt->GetPath1())
  152. cbBuf += (lstrlen(pEnt->GetPath1())+1)*sizeof(WCHAR);
  153. if (pEnt->GetPath2())
  154. cbBuf += (lstrlen(pEnt->GetPath2())+1)*sizeof(WCHAR);
  155. if (pEnt->GetAltPath())
  156. cbBuf += (lstrlen(pEnt->GetAltPath())+1)*sizeof(WCHAR);
  157. pBuf = (BYTE *) SRMemAlloc(cbBuf);
  158. if (! pBuf)
  159. {
  160. trace(0, "! SRMemAlloc for pBuf");
  161. goto Exit;
  162. }
  163. pEntHdr = (SRstrEntryHdr*) pBuf;
  164. pEntHdr->dwID = dwID;
  165. pEntHdr->dwOpr = pEnt->GetOpCode();
  166. pEntHdr->llSeq = pEnt->GetSeqNum();
  167. pEntHdr->dwRes = pEnt->GetResult();
  168. pEntHdr->dwErr = pEnt->GetError();
  169. dwSize = sizeof(SRstrEntryHdr);
  170. // change \\?\Volume{Guid}\path to <Mountpoint>\Path
  171. wsprintf(szTemp, L"%s\\%s", cszMount, PathFindNextComponent(pEnt->GetPath1()+4));
  172. dwSize += ::StrCpyAlign4( pBuf+dwSize, szTemp );
  173. if (pEnt->GetPath2())
  174. wsprintf(szTemp, L"%s\\%s", cszMount, PathFindNextComponent(pEnt->GetPath2()+4));
  175. dwSize += ::StrCpyAlign4( pBuf+dwSize, pEnt->GetPath2() ? szTemp : NULL );
  176. if (pEnt->GetAltPath())
  177. wsprintf(szTemp, L"%s\\%s", cszMount, PathFindNextComponent(pEnt->GetAltPath()+4));
  178. dwSize += ::StrCpyAlign4( pBuf+dwSize, pEnt->GetAltPath() ? szTemp : NULL );
  179. WRITEFILE_AND_VALIDATE( m_hfLog, pBuf, dwSize, dwRes, Exit );
  180. fRet = TRUE;
  181. Exit:
  182. if (pBuf)
  183. SRMemFree(pBuf);
  184. TraceFunctLeave();
  185. return( fRet );
  186. }
  187. /////////////////////////////////////////////////////////////////////////////
  188. BOOL
  189. CRestoreLogFile::WriteCollisionEntry( LPCWSTR cszSrc, LPCWSTR cszDst, LPCWSTR cszMount )
  190. {
  191. TraceFunctEnter("CRestoreLogFile::WriteCollisionEntry");
  192. BOOL fRet = FALSE;
  193. BYTE *pBuf = NULL;
  194. SRstrEntryHdr *pEntHdr;
  195. DWORD dwSize;
  196. DWORD dwRes;
  197. WCHAR szTemp[SR_MAX_FILENAME_LENGTH];
  198. DWORD cbBuf;
  199. if ( m_hfLog == INVALID_HANDLE_VALUE )
  200. {
  201. ErrorTrace(0, "File is not opened...");
  202. goto Exit;
  203. }
  204. cbBuf = sizeof(SRstrEntryHdr);
  205. if (cszSrc)
  206. cbBuf += (lstrlen(cszSrc)+1)*sizeof(WCHAR);
  207. if (cszDst)
  208. cbBuf += (lstrlen(cszDst)+1)*sizeof(WCHAR);
  209. pBuf = (BYTE *) SRMemAlloc(cbBuf);
  210. if (! pBuf)
  211. {
  212. trace(0, "! SRMemAlloc for pBuf");
  213. goto Exit;
  214. }
  215. pEntHdr = (SRstrEntryHdr*) pBuf;
  216. pEntHdr->dwID = RSTRLOGID_COLLISION;
  217. pEntHdr->dwOpr = 0;
  218. pEntHdr->dwRes = 0;
  219. pEntHdr->dwErr = 0;
  220. dwSize = sizeof(SRstrEntryHdr);
  221. wsprintf(szTemp, L"%s\\%s", cszMount, PathFindNextComponent(cszSrc+4));
  222. dwSize += ::StrCpyAlign4( pBuf+dwSize, szTemp );
  223. wsprintf(szTemp, L"%s\\%s", cszMount, PathFindNextComponent(cszDst+4));
  224. dwSize += ::StrCpyAlign4( pBuf+dwSize, szTemp );
  225. dwSize += ::StrCpyAlign4( pBuf+dwSize, NULL );
  226. WRITEFILE_AND_VALIDATE( m_hfLog, pBuf, dwSize, dwRes, Exit );
  227. fRet = TRUE;
  228. Exit:
  229. if (pBuf)
  230. SRMemFree(pBuf);
  231. TraceFunctLeave();
  232. return( fRet );
  233. }
  234. /////////////////////////////////////////////////////////////////////////////
  235. BOOL
  236. CRestoreLogFile::WriteMarker( DWORD dwMarker, DWORD dwErr )
  237. {
  238. TraceFunctEnter("CRestoreLogFile::WriteMarker");
  239. BOOL fRet = FALSE;
  240. BYTE szBuf[MAX_PATH];
  241. SRstrEntryHdr *pEntHdr = (SRstrEntryHdr*)szBuf;
  242. DWORD dwSize;
  243. DWORD dwRes;
  244. if ( m_hfLog == INVALID_HANDLE_VALUE )
  245. {
  246. ErrorTrace(0, "File is not opened...");
  247. goto Exit;
  248. }
  249. pEntHdr->dwID = dwMarker;
  250. pEntHdr->dwOpr = 0;
  251. pEntHdr->dwRes = 0;
  252. pEntHdr->dwErr = dwErr;
  253. dwSize = sizeof(SRstrEntryHdr);
  254. WRITEFILE_AND_VALIDATE( m_hfLog, szBuf, dwSize, dwRes, Exit );
  255. fRet = TRUE;
  256. Exit:
  257. TraceFunctLeave();
  258. return( fRet );
  259. }
  260. /////////////////////////////////////////////////////////////////////////////
  261. BOOL
  262. CRestoreLogFile::IsValid()
  263. {
  264. TraceFunctEnter("CRestoreLogFile::IsValid");
  265. TraceFunctLeave();
  266. return( TRUE );
  267. }
  268. /////////////////////////////////////////////////////////////////////////////
  269. BOOL
  270. CRestoreLogFile::Release()
  271. {
  272. TraceFunctEnter("CRestoreLogFile::Release");
  273. Close();
  274. delete this;
  275. TraceFunctLeave();
  276. return( TRUE );
  277. }
  278. /////////////////////////////////////////////////////////////////////////////
  279. // CRestoreLogFile operations
  280. BOOL
  281. CRestoreLogFile::Init()
  282. {
  283. TraceFunctEnter("CRestoreLogFile::Init");
  284. BOOL fRet = FALSE;
  285. // Construct internal file pathes
  286. if ( ::ExpandEnvironmentStrings( s_cszLogFile, m_szLogFile, MAX_PATH ) == 0 )
  287. {
  288. LPCWSTR cszErr = ::GetSysErrStr();
  289. ErrorTrace(0, "::ExpandEnvironmentString failed - %ls", cszErr);
  290. goto Exit;
  291. }
  292. fRet = TRUE;
  293. Exit:
  294. TraceFunctLeave();
  295. return( fRet );
  296. }
  297. /////////////////////////////////////////////////////////////////////////////
  298. //
  299. // CreateRestoreLogFile function
  300. //
  301. /////////////////////////////////////////////////////////////////////////////
  302. BOOL
  303. CreateRestoreLogFile( SRstrLogHdrV3 *pRPInfo, CRDIArray &aryDrv )
  304. {
  305. TraceFunctEnter("CreateRestoreLogFile");
  306. BOOL fRet = FALSE;
  307. LPCWSTR cszErr;
  308. WCHAR szLogFile[MAX_PATH];
  309. HANDLE hfLog = INVALID_HANDLE_VALUE;
  310. SRstrLogHdrBase sHdr;
  311. DWORD dwRes;
  312. DWORD i;
  313. // Construct internal file pathes
  314. if ( ::ExpandEnvironmentStrings( s_cszLogFile, szLogFile, MAX_PATH ) == 0 )
  315. {
  316. cszErr = ::GetSysErrStr();
  317. ErrorTrace(0, "::ExpandEnvironmentString failed - %ls", cszErr);
  318. goto Exit;
  319. }
  320. // create log file, write header
  321. hfLog = ::CreateFile( szLogFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL );
  322. if ( hfLog == INVALID_HANDLE_VALUE )
  323. {
  324. cszErr = ::GetSysErrStr();
  325. ErrorTrace(0, "::CreateFile failed - %ls", cszErr);
  326. goto Exit;
  327. }
  328. sHdr.dwSig1 = RSTRLOG_SIGNATURE1;
  329. sHdr.dwSig2 = RSTRLOG_SIGNATURE2;
  330. sHdr.dwVer = RSTRLOG_VERSION;
  331. WRITEFILE_AND_VALIDATE( hfLog, &sHdr, sizeof(SRstrLogHdrBase), dwRes, Exit );
  332. WRITEFILE_AND_VALIDATE( hfLog, pRPInfo, sizeof(SRstrLogHdrV3), dwRes, Exit );
  333. // write drive table information
  334. for ( i = 0; i < pRPInfo->dwDrives; i++ )
  335. {
  336. if ( !aryDrv[i]->SaveToLog( hfLog ) )
  337. goto Exit;
  338. }
  339. fRet = TRUE;
  340. Exit:
  341. if ( hfLog != INVALID_HANDLE_VALUE )
  342. ::CloseHandle( hfLog );
  343. TraceFunctLeave();
  344. return( fRet );
  345. }
  346. /////////////////////////////////////////////////////////////////////////////
  347. //
  348. // OpenRestoreLogFile function
  349. //
  350. /////////////////////////////////////////////////////////////////////////////
  351. BOOL
  352. OpenRestoreLogFile( CRestoreLogFile **ppLogFile )
  353. {
  354. TraceFunctEnter("OpenRestoreLogFile");
  355. BOOL fRet = FALSE;
  356. CRestoreLogFile *pLogFile=NULL;
  357. if ( ppLogFile == NULL )
  358. {
  359. ErrorTrace(0, "Invalid parameter, ppLogFile is NULL.");
  360. goto Exit;
  361. }
  362. *ppLogFile = NULL;
  363. pLogFile = new CRestoreLogFile;
  364. if ( pLogFile == NULL )
  365. {
  366. ErrorTrace(0, "Insufficient memory...");
  367. goto Exit;
  368. }
  369. if ( !pLogFile->Open() )
  370. goto Exit;
  371. *ppLogFile = pLogFile;
  372. fRet = TRUE;
  373. Exit:
  374. if ( !fRet )
  375. SAFE_RELEASE(pLogFile);
  376. TraceFunctLeave();
  377. return( fRet );
  378. }
  379. // end of file