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.

190 lines
3.9 KiB

  1. /*++
  2. Copyright (C) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. writer.cpp
  5. Abstract:
  6. Volume SnapShot Writer for WMI
  7. History:
  8. a-shawnb 06-Nov-00 Created
  9. --*/
  10. #include "precomp.h"
  11. #include "writer.h"
  12. #include <genutils.h> // for EnableAllPrivileges()
  13. #include <malloc.h>
  14. CWbemVssWriter::CWbemVssWriter() : CVssWriter(),
  15. m_pBackupRestore(NULL),
  16. m_Lock(THROW_LOCK,0x80000000 | 500L)
  17. {
  18. }
  19. CWbemVssWriter::~CWbemVssWriter()
  20. {
  21. HRESULT hr = WBEM_S_NO_ERROR;
  22. if (m_pBackupRestore)
  23. {
  24. hr = m_pBackupRestore->Resume();
  25. m_pBackupRestore->Release();
  26. m_pBackupRestore = NULL;
  27. }
  28. }
  29. // {A6AD56C2-B509-4e6c-BB19-49D8F43532F0}
  30. static VSS_ID s_WRITERID = {0xa6ad56c2, 0xb509, 0x4e6c, 0xbb, 0x19, 0x49, 0xd8, 0xf4, 0x35, 0x32, 0xf0};
  31. static LPCWSTR s_WRITERNAME = L"WMI Writer";
  32. HRESULT CWbemVssWriter::Initialize()
  33. {
  34. return CVssWriter::Initialize(s_WRITERID, s_WRITERNAME, VSS_UT_SYSTEMSERVICE, VSS_ST_OTHER);
  35. }
  36. extern HRESULT GetRepositoryDirectory(wchar_t wszRepositoryDirectory[MAX_PATH+1]);
  37. bool STDMETHODCALLTYPE CWbemVssWriter::OnIdentify(IN IVssCreateWriterMetadata *pMetadata)
  38. {
  39. wchar_t wszRepositoryDirectory[MAX_PATH+1];
  40. HRESULT hr = GetRepositoryDirectory(wszRepositoryDirectory);
  41. if (FAILED(hr))
  42. return false;
  43. hr = pMetadata->AddComponent( VSS_CT_FILEGROUP,
  44. NULL,
  45. L"WMI",
  46. L"Windows Managment Instrumentation",
  47. NULL,
  48. 0,
  49. false,
  50. false,
  51. false
  52. );
  53. if (FAILED(hr))
  54. return false;
  55. hr = pMetadata->AddFilesToFileGroup(
  56. NULL,
  57. L"WMI",
  58. wszRepositoryDirectory,
  59. L"*.*",
  60. true,
  61. NULL
  62. );
  63. if (FAILED(hr))
  64. return false;
  65. hr = pMetadata->SetRestoreMethod(
  66. VSS_RME_RESTORE_AT_REBOOT,
  67. NULL,
  68. NULL,
  69. VSS_WRE_NEVER,
  70. true
  71. );
  72. if (FAILED(hr))
  73. return false;
  74. return true;
  75. }
  76. bool STDMETHODCALLTYPE CWbemVssWriter::OnPrepareSnapshot()
  77. {
  78. return true;
  79. }
  80. //
  81. // Doing the Job on this method, we will have a time-out guarantee
  82. // We sync the OnFreeze and the OnAbort/OnThaw calls,
  83. // so that, if a TimeOut occurs, we are not arbitrarly unlocking the repository
  84. //
  85. ///////////////////////////////////////////////////////////////
  86. bool STDMETHODCALLTYPE CWbemVssWriter::OnFreeze()
  87. {
  88. #ifdef _X86_
  89. DWORD * pDW = (DWORD *)_alloca(sizeof(DWORD));
  90. #endif
  91. LockGuard<CriticalSection> lg(m_Lock);
  92. if (!lg.locked())
  93. return false;
  94. // m_pBackupRestore should always be NULL coming into this
  95. if (m_pBackupRestore)
  96. {
  97. return false;
  98. }
  99. HRESULT hr = CoCreateInstance(CLSID_WbemBackupRestore, 0, CLSCTX_LOCAL_SERVER,
  100. IID_IWbemBackupRestoreEx, (LPVOID *) &m_pBackupRestore);
  101. if (SUCCEEDED(hr))
  102. {
  103. if (SUCCEEDED(hr = EnableAllPrivileges(TOKEN_PROCESS)))
  104. {
  105. hr = m_pBackupRestore->Pause();
  106. if (FAILED(hr))
  107. {
  108. m_pBackupRestore->Release();
  109. m_pBackupRestore = NULL;
  110. }
  111. }
  112. }
  113. return (SUCCEEDED(hr));
  114. }
  115. bool STDMETHODCALLTYPE CWbemVssWriter::OnThaw()
  116. {
  117. #ifdef _X86_
  118. DWORD * pDW = (DWORD *)_alloca(sizeof(DWORD));
  119. #endif
  120. LockGuard<CriticalSection> lg(m_Lock);
  121. while (!lg.locked()){
  122. Sleep(20);
  123. lg.acquire();
  124. };
  125. if (!m_pBackupRestore)
  126. {
  127. // if m_pBackupRestore is NULL, then we haven't been
  128. // asked to prepare or we failed our preparation
  129. DebugBreak();
  130. return false;
  131. }
  132. HRESULT hr = m_pBackupRestore->Resume();
  133. m_pBackupRestore->Release();
  134. m_pBackupRestore = NULL;
  135. return (SUCCEEDED(hr));
  136. }
  137. bool STDMETHODCALLTYPE CWbemVssWriter::OnAbort()
  138. {
  139. #ifdef _X86_
  140. DWORD * pDW = (DWORD *)_alloca(sizeof(DWORD));
  141. #endif
  142. LockGuard<CriticalSection> lg(m_Lock);
  143. while (!lg.locked()){
  144. Sleep(20);
  145. lg.acquire();
  146. };
  147. HRESULT hr = WBEM_S_NO_ERROR;
  148. if (m_pBackupRestore)
  149. {
  150. hr = m_pBackupRestore->Resume();
  151. m_pBackupRestore->Release();
  152. m_pBackupRestore = NULL;
  153. }
  154. return (SUCCEEDED(hr));
  155. }