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.

193 lines
7.2 KiB

  1. /*
  2. **++
  3. **
  4. ** Copyright (c) 2002 Microsoft Corporation
  5. **
  6. **
  7. ** Module Name:
  8. **
  9. ** swriter.h
  10. **
  11. **
  12. ** Abstract:
  13. **
  14. ** Test program to to register a Writer with various properties
  15. **
  16. ** Author:
  17. **
  18. ** Reuven Lax [reuvenl] 04-June-2002
  19. **
  20. **
  21. **
  22. ** Revision History:
  23. **
  24. **--
  25. */
  26. #ifndef _SWRITER_H_
  27. #define _SWRITER_H_
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // Includes
  30. #include <vector>
  31. #include <stack>
  32. #include <functional>
  33. #include <string>
  34. #include "writerconfig.h"
  35. #include "utility.h"
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // Declarations and Definitions
  38. // {5AFFB034-969F-4919-8875-88F830D0EF89}
  39. static const VSS_ID TestWriterId =
  40. { 0x5affb034, 0x969f, 0x4919, { 0x88, 0x75, 0x88, 0xf8, 0x30, 0xd0, 0xef, 0x89 } };
  41. static const wchar_t* const TestWriterName = L"TestVssWriter";
  42. using std::vector;
  43. ///////////////////////////////////////////////////////////////////////////////
  44. // TestWriter class
  45. class TestWriter : public CVssWriter {
  46. private:
  47. // member variables
  48. vector<Component> m_selectedComponents;
  49. vector<Component> m_selectedRestoreComponents;
  50. vector<wstring> m_toDelete;
  51. std::stack<wstring> m_directoriesToRemove;
  52. long m_failures[Utility::NumEvents];
  53. // closure to encapsulate calls to verifyFileAtLocation and record error messages
  54. class VerifyFileAtLocation : public std::binary_function<const TargetedFile, const File, void> {
  55. private:
  56. const vector<File>& m_excluded;
  57. bool m_verifyAlternateLocation;
  58. mutable IVssComponent* m_pComponent; // necessary due to bug in STL
  59. wstring verifyFileAtLocation(const File& file, const TargetedFile& location) const;
  60. bool verifyAlternateLocation(const TargetedFile& writerAlt) const;
  61. void saveErrorMessage(const wstring& message) const;
  62. public:
  63. VerifyFileAtLocation(const vector<File>& excludeFiles, IVssComponent* pComponent,
  64. bool verifyAlternateLocation) :
  65. m_excluded(excludeFiles), m_pComponent(pComponent),
  66. m_verifyAlternateLocation(verifyAlternateLocation)
  67. {}
  68. // The function operator. Verifies the file, and records any error message
  69. void operator()(const TargetedFile location, const File file) const {
  70. saveErrorMessage(verifyFileAtLocation(file, location));
  71. }
  72. };
  73. // static helper functions
  74. // filter out elements in a source container that match a specific condition. Place these elements
  75. // into a target container.
  76. template <class SourceIterator, class TargetIterator, class Condition>
  77. static void buildContainer_if(SourceIterator begin, SourceIterator end, TargetIterator output, Condition cond) {
  78. SourceIterator current = std::find_if(begin, end, cond);
  79. while (current != end) {
  80. *output++ = *current++;
  81. current = std::find_if(current, end, cond);
  82. }
  83. }
  84. // build a list of all files in this component and in all non-selectable subcomponents
  85. template<class TargetIterator>
  86. static void __cdecl buildComponentFiles(Component component, TargetIterator output) {
  87. WriterConfiguration* config = WriterConfiguration::instance();
  88. buildComponentFilesHelper(component, output);
  89. // build a list of all subcomponents
  90. vector<Component> subcomponents;
  91. buildContainer_if(config->components().begin(),
  92. config->components().end(),
  93. std::back_inserter(subcomponents),
  94. std::bind2nd(std::ptr_fun(isSubcomponent), component));
  95. // add all files in all non-selectable subcomponents to the output
  96. std::pointer_to_binary_function<Component, std::back_insert_iterator<vector<TargetedFile> >, void>
  97. ptrFun(buildComponentFilesHelper);
  98. std::for_each(subcomponents.begin(),
  99. subcomponents.end(),
  100. std::bind2nd(ptrFun, output));
  101. }
  102. template<class TargetIterator>
  103. static void __cdecl buildComponentFilesHelper(Component component, TargetIterator output) {
  104. // add all the files in the current component
  105. Component::ComponentFileList::iterator currentCompFile = component.m_files.begin();
  106. while (currentCompFile != component.m_files.end())
  107. *output++ = *currentCompFile++;
  108. }
  109. static bool __cdecl isSubcomponent(ComponentBase sub, ComponentBase super);
  110. static bool __cdecl isSupercomponent(ComponentBase super, ComponentBase sub) {
  111. return isSubcomponent(sub, super);
  112. }
  113. // return whether a component is selectable for backup
  114. static bool __cdecl isComponentSelectable(Component component) {
  115. return component.m_selectable;
  116. }
  117. static bool __cdecl addableComponent(Component toAdd);
  118. // Returns whether a filespec is a wildcard or an exact filespec.
  119. static bool isExact(const wstring& file) { return file.find_first_of(L"*?") == wstring::npos; }
  120. static bool __cdecl targetMatches(File target, File file);
  121. static bool wildcardMatches(const wstring& first, const wstring& second);
  122. // non-static helper functions
  123. void enterEvent(Utility::Events event);
  124. void addComponent(const Component& component, IVssCreateWriterMetadata* pMetadata);
  125. void spitFiles(const TargetedFile& file);
  126. wstring getName(IVssComponent* pComponent);
  127. wstring getPath(IVssComponent* pComponent);
  128. void writeBackupMetadata(IVssComponent* pComponent);
  129. bool verifyBackupMetadata(IVssComponent* pComponent);
  130. void writeRestoreMetadata(IVssComponent* pComponent);
  131. bool verifyRestoreMetadata(IVssComponent* pComponent);
  132. bool checkPathAffected(const TargetedFile& file);
  133. void cleanupFiles();
  134. void updateNewTargets(IVssComponent* pComponent, Component& writerComponent);
  135. void verifyFilesRestored(IVssComponent* pComponent, const Component& writerComponent);
  136. // returns the private metadata string that the writer stores in the document
  137. wstring metadata(IVssComponent* pComponent, const wstring& suffix) {
  138. return getPath(pComponent) + L"\\" + getName(pComponent) + suffix;
  139. }
  140. bool inSequence(Utility::Events event) {
  141. return event != Utility::Identify && event != Utility::BackupComplete &&
  142. event != Utility::BackupShutdown;
  143. }
  144. public:
  145. TestWriter() { memset(m_failures, 0, sizeof(m_failures)); }
  146. virtual ~TestWriter() { Uninitialize(); }
  147. HRESULT STDMETHODCALLTYPE Initialize();
  148. HRESULT STDMETHODCALLTYPE Uninitialize() { return Unsubscribe(); }
  149. bool STDMETHODCALLTYPE OnIdentify(IN IVssCreateWriterMetadata *pMetadata);
  150. bool STDMETHODCALLTYPE OnPrepareBackup(IN IVssWriterComponents *pComponents);
  151. bool STDMETHODCALLTYPE OnPrepareSnapshot();
  152. bool STDMETHODCALLTYPE OnFreeze();
  153. bool STDMETHODCALLTYPE OnThaw();
  154. bool STDMETHODCALLTYPE OnPostSnapshot(IN IVssWriterComponents *pComponents);
  155. bool STDMETHODCALLTYPE OnAbort();
  156. bool STDMETHODCALLTYPE OnBackupComplete(IN IVssWriterComponents *pComponents);
  157. bool STDMETHODCALLTYPE OnBackupShutdown(IN VSS_ID SnapshotSetId);
  158. bool STDMETHODCALLTYPE OnPreRestore(IN IVssWriterComponents *pComponents);
  159. bool STDMETHODCALLTYPE OnPostRestore(IN IVssWriterComponents *pComponents);
  160. };
  161. #endif