ArchiveControlImpl.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef ARCHIVECONTROLIMPL_H_INCLUDED
  18. #define ARCHIVECONTROLIMPL_H_INCLUDED
  19. #include <set>
  20. #include <kopano/zcdefs.h>
  21. #include "operations/operations_fwd.h"
  22. #include "helpers/ArchiveHelper.h"
  23. namespace KC {
  24. class ECConfig;
  25. class ECLogger;
  26. class ECArchiverLogger;
  27. /**
  28. * This class is the entry point to the archiving system. It's responsible for executing the actual archive
  29. * operation.
  30. *
  31. * There are three steps involved in archiving:
  32. * 1. Copying - Copy a message to the archive.
  33. * 2. Stubbing - Stub a message in the primary store and have it reference the archive.
  34. * 3. Deleting - Delete a message from the primary store (but leave the archived copy).
  35. *
  36. * These three steps are executed in the following order: 1, 3, 2. This is done because
  37. * if a message can be deleted from the primary store, there's no need to stub it. So
  38. * by deleting all messages that may be deleted first, we make sure we're not doing more
  39. * work than needed.
  40. *
  41. * The primary way how this is performed is with the use of searchfolders. There are
  42. * three searchfolders for this purpose:
  43. *
  44. * The first searchfolder contains all messages of the right message class that have
  45. * not yet been archived (completely). This means that they do not have a copy in all
  46. * attached archives yet. All messages in this searchfolder are processed in the step
  47. * 1 if they match the restriction for this step.
  48. *
  49. * The second searchfolder contains all messages that have been archived but are not
  50. * yet stubbed. These messages are processed in step 2 if they match the restriction
  51. * for this step.
  52. *
  53. * The third searchfolder contains all message that have been archived, regardless if
  54. * they're stubbed or not. These messages are processed in step 3 if they match the
  55. * restriction for this step.
  56. *
  57. * After a message is processed in a particular step, its state will change, causing
  58. * it to be removed from the searchfolder for that step and be added to the searchfolder
  59. * for the next step(s). A message processed in step 1 will be added to the searchfolder
  60. * for both step 2 and step 3. After step 3 has been processed, the message will be
  61. * removed from the searchfolder for step 3, but not from the searchfolder for step 2.
  62. *
  63. * By going through the three searchfolders and executing the apropriate steps, all
  64. * messages will be processed as they're supposed to.
  65. *
  66. *
  67. * There's a catch though: Searchfolders are asynchronous and the transition of a
  68. * message from one searchfolder to another might take longer than the operation the
  69. * archiver is executing. This causes messages to be unprocces because at the time
  70. * the archiver was looking in the searchfolder the message wasn't there yet. The
  71. * message will be processed on the next run, but it is hard to explain to admins
  72. * that they just have to wait for the next run before something they expect to
  73. * happen really happens.
  74. *
  75. * For this reason there's a second approach to execute the three steps:
  76. * Logically speaking there are just a few possibilities in what steps are executed
  77. * on a message:
  78. * 1. copy
  79. * 2. copy, delete
  80. * 3. copy, stub
  81. * 4. stub
  82. * 5, delete
  83. *
  84. * Option 1, 4 and 5 will be handled fine by the primary approach as they consist of
  85. * one operation only. Option 2 and 3 however depend on the timing of the searchfolders
  86. * for the second step to be executed or not.
  87. *
  88. * Since we know that if a message is copied to all attached archives, it would be
  89. * moved to the searchfolders for the delete and stub steps. Therefore we know that
  90. * if the searchfolders were instant, the message would be processed in these steps
  91. * if it would match the restriction defined for those steps.
  92. *
  93. * The alternate approach is thus to take the following actions at a successful
  94. * completion of step 1:
  95. * 1. Check if deletion is enabled
  96. * 2. If so, check if the message matches the restriction for the deletion step
  97. * 3. If it matched, execute the deletion step and quit
  98. * 4. Check if stubbing is enabled
  99. * 5. If so, check if the message matches the restriction for the stubbing step
  100. * 6. If it matches, execute the stubbing step.
  101. *
  102. * This way necessary steps will be executed on a message, regardless of searchfolder
  103. * timing.
  104. */
  105. class ArchiveControlImpl _kc_final : public ArchiveControl {
  106. public:
  107. static HRESULT Create(ArchiverSessionPtr ptrSession, ECConfig *lpConfig, ECLogger *lpLogger, bool bForceCleanup, ArchiveControlPtr *lpptrArchiveControl);
  108. eResult ArchiveAll(bool bLocalOnly, bool bAutoAttach, unsigned int ulFlags) _kc_override;
  109. eResult Archive(const tstring &strUser, bool bAutoAttach, unsigned int ulFlags) _kc_override;
  110. eResult CleanupAll(bool bLocalOnly) _kc_override;
  111. eResult Cleanup(const tstring &strUser) _kc_override;
  112. ~ArchiveControlImpl();
  113. private:
  114. class ReferenceLessCompare {
  115. public:
  116. typedef std::pair<entryid_t, entryid_t> value_type;
  117. bool operator()(const value_type &lhs, const value_type &rhs) const
  118. {
  119. return lhs.second < rhs.second;
  120. }
  121. };
  122. typedef HRESULT(ArchiveControlImpl::*fnProcess_t)(const tstring&);
  123. typedef std::set<entryid_t> EntryIDSet;
  124. typedef std::set<std::pair<entryid_t, entryid_t>, ReferenceLessCompare> ReferenceSet;
  125. ArchiveControlImpl(ArchiverSessionPtr ptrSession, ECConfig *lpConfig, ECLogger *lpLogger, bool bForceCleanup);
  126. HRESULT Init();
  127. HRESULT DoArchive(const tstring& strUser);
  128. HRESULT DoCleanup(const tstring& strUser);
  129. HRESULT ProcessFolder(MAPIFolderPtr &ptrFolder, operations::ArchiveOperationPtr ptrArchiveOperation);
  130. HRESULT ProcessAll(bool bLocalOnly, fnProcess_t fnProcess);
  131. HRESULT PurgeArchives(const ObjectEntryList &lstArchives);
  132. HRESULT PurgeArchiveFolder(MsgStorePtr &ptrArchive, const entryid_t &folderEntryID, const LPSRestriction lpRestriction);
  133. HRESULT CleanupArchive(const SObjectEntry &archiveEntry, IMsgStore* lpUserStore, LPSRestriction lpRestriction);
  134. HRESULT GetAllReferences(LPMDB lpUserStore, LPGUID lpArchiveGuid, EntryIDSet *lpMsgReferences);
  135. HRESULT AppendAllReferences(LPMAPIFOLDER lpRoot, LPGUID lpArchiveGuid, EntryIDSet *lpMsgReferences);
  136. HRESULT GetAllEntries(helpers::ArchiveHelperPtr, LPMAPIFOLDER arc, LPSRestriction, EntryIDSet *entries);
  137. HRESULT AppendAllEntries(LPMAPIFOLDER lpArchive, LPSRestriction lpRestriction, EntryIDSet *lpMsgEntries);
  138. HRESULT CleanupHierarchy(helpers::ArchiveHelperPtr, LPMAPIFOLDER arc_root, LPMDB user_store);
  139. HRESULT MoveAndDetachMessages(helpers::ArchiveHelperPtr, LPMAPIFOLDER arc_folder, const EntryIDSet &);
  140. HRESULT MoveAndDetachFolder(helpers::ArchiveHelperPtr, LPMAPIFOLDER arc_folder);
  141. HRESULT DeleteMessages(LPMAPIFOLDER lpArchiveFolder, const EntryIDSet &setEIDs);
  142. HRESULT DeleteFolder(LPMAPIFOLDER lpArchiveFolder);
  143. HRESULT AppendFolderEntries(LPMAPIFOLDER lpBase, EntryIDSet *lpEntries);
  144. HRESULT CheckSafeCleanupSettings();
  145. HRESULT purgesoftdeletedmessages(const tstring& strUser);
  146. tstring getfoldername(LPMAPIFOLDER folder);
  147. HRESULT purgesoftdeleteditems(LPMAPIFOLDER folder, const tstring& strUser);
  148. enum eCleanupAction { caDelete, caStore, caNone };
  149. ArchiverSessionPtr m_ptrSession;
  150. ECConfig *m_lpConfig = nullptr;
  151. ECArchiverLogger *m_lpLogger = nullptr;
  152. FILETIME m_ftCurrent = {0, 0};
  153. bool m_bArchiveEnable = true;
  154. int m_ulArchiveAfter = 30;
  155. bool m_bDeleteEnable = false;
  156. bool m_bDeleteUnread = false;
  157. int m_ulDeleteAfter = 0;
  158. bool m_bStubEnable = false;
  159. bool m_bStubUnread = false;
  160. int m_ulStubAfter = 0;
  161. bool m_bPurgeEnable = false;
  162. int m_ulPurgeAfter = 2555;
  163. eCleanupAction m_cleanupAction;
  164. bool m_bCleanupFollowPurgeAfter = false;
  165. bool m_bForceCleanup;
  166. PROPMAP_DECL()
  167. PROPMAP_DEF_NAMED_ID(ARCHIVE_STORE_ENTRYIDS)
  168. PROPMAP_DEF_NAMED_ID(ARCHIVE_ITEM_ENTRYIDS)
  169. PROPMAP_DEF_NAMED_ID(ORIGINAL_SOURCEKEY)
  170. PROPMAP_DEF_NAMED_ID(STUBBED)
  171. PROPMAP_DEF_NAMED_ID(DIRTY)
  172. };
  173. } /* namespace */
  174. #endif // !defined ARCHIVECONTROLIMPL_H_INCLUDED