Source code of Windows XP (NT5)
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.

241 lines
5.1 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 1999 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * restmap.cpp
  7. *
  8. * Abstract:
  9. * This file contains the implementation of RestoreMap apis.
  10. *
  11. * Revision History:
  12. * Kanwaljit Marok (kmarok) 06/22/1999
  13. * created
  14. *
  15. *
  16. ******************************************************************************/
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <windows.h>
  21. #include "restmap.h"
  22. #include "reslist.h"
  23. #include "enumlogs.h"
  24. #include "utils.h"
  25. #ifdef THIS_FILE
  26. #undef THIS_FILE
  27. #endif
  28. static char __szTraceSourceFile[] = __FILE__;
  29. #define THIS_FILE __szTraceSourceFile
  30. #include "dbgtrace.h"
  31. //
  32. // CreateRestoreMap : Creates a restore map file for a given drive and restore point number
  33. // Appends the restore map to file hFile
  34. //
  35. DWORD
  36. CreateRestoreMap(LPWSTR pszDrive,
  37. DWORD dwRPNum,
  38. HANDLE hFile)
  39. {
  40. CRestoreList resList;
  41. DWORD dwRc;
  42. WCHAR szPath[MAX_PATH];
  43. BOOL fRet = FALSE;
  44. // enumerate backward, skipping current restore point
  45. CChangeLogEntryEnum cle_enum(pszDrive, FALSE, dwRPNum, TRUE);
  46. CChangeLogEntry cle;
  47. dwRc = cle_enum.FindFirstChangeLogEntry(cle);
  48. while (dwRc == ERROR_SUCCESS)
  49. {
  50. fRet = resList.AddMergeElement(
  51. pszDrive,
  52. cle.GetType(),
  53. cle.GetAttributes(),
  54. cle.GetFlags(),
  55. cle.GetTemp(),
  56. cle.GetPath1(),
  57. cle.GetPath2(),
  58. cle.GetAcl(),
  59. cle.GetAclSize(),
  60. cle.GetAclInline());
  61. if (! fRet)
  62. {
  63. dwRc = ERROR_INTERNAL_ERROR;
  64. goto Exit;
  65. }
  66. dwRc = cle_enum.FindNextChangeLogEntry(cle);
  67. }
  68. // if there was an error, then we're done
  69. if (dwRc != ERROR_NO_MORE_ITEMS)
  70. goto Exit;
  71. // if there are no entries to restore, then we're done
  72. if (! fRet)
  73. goto Exit;
  74. //
  75. // Generate the restore map in the specified file.
  76. //
  77. if (! resList.GenerateRestoreMap(hFile))
  78. {
  79. dwRc = ERROR_INTERNAL_ERROR;
  80. goto Exit;
  81. }
  82. dwRc = ERROR_SUCCESS;
  83. Exit:
  84. cle_enum.FindClose();
  85. return dwRc;
  86. }
  87. //
  88. // AppendRestoreMapEntry : writes the restore map entry to file
  89. //
  90. BOOL
  91. AppendRestoreMapEntry(
  92. HANDLE hFile,
  93. DWORD dwOperation,
  94. DWORD dwAttribute,
  95. LPWSTR pTmpFile,
  96. LPWSTR pPathSrc,
  97. LPWSTR pPathDes,
  98. BYTE *pbAcl,
  99. DWORD cbAcl,
  100. BOOL fAclInline)
  101. {
  102. BOOL fRet = FALSE;
  103. INT cbSrc = 0, cbDes = 0, cbTemp = 0;
  104. RestoreMapEntry *pMapEnt = NULL;
  105. DWORD dwRead, dwSize;
  106. if (! pPathSrc) // something wrong
  107. goto done;
  108. cbSrc = STRSIZE(pPathSrc);
  109. // only one of pPathDes, pTmpFile and pbAcl will be non-NULL, if at all
  110. if (pTmpFile)
  111. cbTemp = STRSIZE(pTmpFile);
  112. if (pPathDes)
  113. cbDes = STRSIZE(pPathDes);
  114. dwSize = sizeof(RestoreMapEntry) - sizeof(BYTE) + cbSrc + cbTemp + cbDes + cbAcl;
  115. pMapEnt = (RestoreMapEntry *) HEAP_ALLOC(dwSize);
  116. if (! pMapEnt)
  117. goto done;
  118. pMapEnt->m_dwSize = dwSize;
  119. pMapEnt->m_dwOperation = dwOperation;
  120. pMapEnt->m_dwAttribute = dwAttribute;
  121. pMapEnt->m_cbAcl = cbAcl;
  122. pMapEnt->m_fAclInline = fAclInline;
  123. memcpy(pMapEnt->m_bData, pPathSrc, cbSrc);
  124. if (pTmpFile)
  125. memcpy((BYTE *) pMapEnt->m_bData + cbSrc, pTmpFile, cbTemp);
  126. if (pPathDes)
  127. memcpy((BYTE *) pMapEnt->m_bData + cbSrc + cbTemp, pPathDes, cbDes);
  128. if (pbAcl)
  129. memcpy((BYTE *) pMapEnt->m_bData + cbSrc + cbTemp + cbDes , pbAcl, cbAcl);
  130. fRet = WriteFile( hFile, pMapEnt, pMapEnt->m_dwSize, &dwRead, NULL );
  131. HEAP_FREE(pMapEnt);
  132. done:
  133. return fRet;
  134. }
  135. // reads a restore map entry from a given file
  136. DWORD
  137. ReadRestoreMapEntry(
  138. HANDLE hFile,
  139. RestoreMapEntry **pprme)
  140. {
  141. DWORD dwRead, dwErr = ERROR_SUCCESS, dwSize;
  142. if (*pprme)
  143. HEAP_FREE(*pprme);
  144. if (! ReadFile(hFile, &dwSize, sizeof(DWORD), &dwRead, NULL))
  145. {
  146. dwErr = GetLastError();
  147. goto Err;
  148. }
  149. if (dwRead == 0 || dwSize == 0)
  150. {
  151. dwErr = ERROR_NO_MORE_ITEMS;
  152. goto Err;
  153. }
  154. *pprme = (RestoreMapEntry *) HEAP_ALLOC(dwSize);
  155. if (! *pprme)
  156. {
  157. dwErr = ERROR_INTERNAL_ERROR;
  158. goto Err;
  159. }
  160. (*pprme)->m_dwSize = dwSize;
  161. if (! ReadFile(hFile,
  162. (PVOID) ((BYTE *) (*pprme) + sizeof(DWORD)),
  163. (*pprme)->m_dwSize - sizeof(DWORD),
  164. &dwRead,
  165. NULL) ||
  166. dwRead != (*pprme)->m_dwSize - sizeof(DWORD) )
  167. {
  168. dwErr = GetLastError();
  169. goto Err;
  170. }
  171. Err:
  172. return dwErr;
  173. }
  174. PVOID
  175. GetOptional(
  176. RestoreMapEntry *prme)
  177. {
  178. DWORD cbData = STRSIZE((LPWSTR) prme->m_bData);
  179. DWORD dwBasicSize = sizeof(RestoreMapEntry) + cbData - sizeof(BYTE);
  180. if (prme->m_dwSize > dwBasicSize)
  181. return (PVOID) (prme->m_bData + cbData);
  182. else
  183. return NULL;
  184. }
  185. void
  186. FreeRestoreMapEntry(
  187. RestoreMapEntry *prme)
  188. {
  189. HEAP_FREE(prme);
  190. }