-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
12,895 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright (c) 2006 Wangdera Corporation (hobocopy@wangdera.com) | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <stdafx.h> | ||
|
||
#include "CBackupState.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
/* | ||
Copyright (c) 2006 Wangdera Corporation (hobocopy@wangdera.com) | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
class CBackupState | ||
{ | ||
private: | ||
bool _hasLastFullBackup; | ||
bool _hasLastIncrementalBackup; | ||
SYSTEMTIME _lastFullBackup; | ||
SYSTEMTIME _lastIncrementalBackup; | ||
|
||
void AppendAttribute(CComPtr<IXMLDOMDocument> document, CComPtr<IXMLDOMElement> parent, | ||
LPCTSTR name, LPCTSTR value) | ||
{ | ||
CString message; | ||
message.AppendFormat(TEXT("Creating attribute %s"), name); | ||
InstrumentationHelper::Log(message); | ||
CComPtr<IXMLDOMAttribute> pAttribute; | ||
CHECK_HRESULT(document->createAttribute(CComBSTR(name), &pAttribute)); | ||
|
||
InstrumentationHelper::Log(TEXT("Setting value of attribute.")); | ||
CHECK_HRESULT(pAttribute->put_text(CComBSTR(value))); | ||
|
||
InstrumentationHelper::Log(TEXT("Retrieving attributes from parent element.")); | ||
CComPtr<IXMLDOMNamedNodeMap> pAttributes; | ||
CHECK_HRESULT(parent->get_attributes(&pAttributes)); | ||
|
||
InstrumentationHelper::Log(TEXT("Adding attribute to parent.")); | ||
CComPtr<IXMLDOMNode> pThrowawayNode; | ||
CHECK_HRESULT(pAttributes->setNamedItem(pAttribute, &pThrowawayNode)); | ||
} | ||
|
||
bool SelectDateTimeValue(CComPtr<IXMLDOMDocument> document, | ||
LPCTSTR xpath, LPSYSTEMTIME pTime) | ||
{ | ||
CString message(TEXT("Selecting node for xpath ")); | ||
message.Append(xpath); | ||
InstrumentationHelper::Log(message); | ||
CComPtr<IXMLDOMNode> pNode; | ||
HRESULT hr = document->selectSingleNode(CComBSTR(xpath), &pNode); | ||
|
||
if (hr == S_FALSE) | ||
{ | ||
InstrumentationHelper::Log(TEXT("Unable to find matching node.")); | ||
return FALSE; | ||
} | ||
else | ||
{ | ||
// Cheap way of throwing an exception with the details filled in | ||
CHECK_HRESULT(hr); | ||
} | ||
|
||
InstrumentationHelper::Log(TEXT("Retrieving text value of node")); | ||
CComBSTR bstrLastFullBackup; | ||
CHECK_HRESULT(pNode->get_text(&bstrLastFullBackup)); | ||
|
||
CString lastFullBackup(bstrLastFullBackup); | ||
|
||
Utilities::ParseDateTime(lastFullBackup, TEXT("T"), pTime); | ||
|
||
CString message2(TEXT("Time value was: ")); | ||
CString dateTime; | ||
Utilities::FormatDateTime(pTime, TEXT(" "), false, dateTime); | ||
message2.Append(dateTime); | ||
InstrumentationHelper::Log(message2); | ||
|
||
return true; | ||
} | ||
|
||
public: | ||
CBackupState::CBackupState(void) | ||
{ | ||
_hasLastFullBackup = false; | ||
_hasLastIncrementalBackup = false; | ||
} | ||
|
||
LPSYSTEMTIME get_LastFullBackupTime() | ||
{ | ||
if (_hasLastFullBackup) | ||
{ | ||
return &_lastFullBackup; | ||
} | ||
else | ||
{ | ||
return NULL; | ||
} | ||
} | ||
|
||
void set_LastFullBackupTime(LPSYSTEMTIME value) | ||
{ | ||
_hasLastFullBackup = true; | ||
_lastFullBackup = *value; | ||
} | ||
|
||
LPSYSTEMTIME get_LastIncrementalBackupTime() | ||
{ | ||
if (_hasLastIncrementalBackup) | ||
{ | ||
return &_lastIncrementalBackup; | ||
} | ||
else | ||
{ | ||
return NULL; | ||
} | ||
} | ||
|
||
void set_LastIncrementalBackupTime(LPSYSTEMTIME value) | ||
{ | ||
_hasLastIncrementalBackup = true; | ||
_lastIncrementalBackup = *value; | ||
} | ||
|
||
void Load(LPCTSTR path) | ||
{ | ||
_hasLastFullBackup = false; | ||
_hasLastIncrementalBackup = false; | ||
|
||
InstrumentationHelper::Log(TEXT("Creating DOM document object.")); | ||
CComPtr<IXMLDOMDocument> stateDocument; | ||
CHECK_HRESULT(stateDocument.CoCreateInstance(__uuidof(DOMDocument30))); | ||
|
||
CString message; | ||
message.AppendFormat(TEXT("Loading state file from %s."), path); | ||
InstrumentationHelper::Log(message); | ||
|
||
InstrumentationHelper::Log(TEXT("Turning off validation")); | ||
CHECK_HRESULT(stateDocument->put_validateOnParse(VARIANT_FALSE)); | ||
|
||
VARIANT_BOOL isSuccessful; | ||
HRESULT hr = stateDocument->load(CComVariant(path), &isSuccessful); | ||
|
||
if (FAILED(hr)) | ||
{ | ||
// A cheap way of throwing an exception with the details filled in | ||
CHECK_HRESULT(hr); | ||
} | ||
else if (hr == S_FALSE || isSuccessful == VARIANT_FALSE) | ||
{ | ||
CComPtr<IXMLDOMParseError> pError; | ||
CHECK_HRESULT(stateDocument->get_parseError(&pError)); | ||
CComBSTR bstrReason; | ||
CHECK_HRESULT(pError->get_reason(&bstrReason)); | ||
CString reason(bstrReason); | ||
CString message; | ||
message.AppendFormat(TEXT("Failed to load state file from %s. Reason: %s."), path, (LPCTSTR) reason); | ||
throw new CHoboCopyException(message); | ||
} | ||
|
||
_hasLastFullBackup = SelectDateTimeValue(stateDocument, TEXT("/hoboCopyState/@lastFullBackup"), &_lastFullBackup); | ||
|
||
if (_hasLastFullBackup) | ||
{ | ||
CString message; | ||
CString dateTime; | ||
Utilities::FormatDateTime(&_lastFullBackup, TEXT(" "), false, dateTime); | ||
message.AppendFormat(TEXT("Last full backup time read as %s"), dateTime); | ||
InstrumentationHelper::Log(message); | ||
} | ||
else | ||
{ | ||
InstrumentationHelper::Log(TEXT("Backup file did not have last full backup time recorded.")); | ||
} | ||
|
||
_hasLastIncrementalBackup = SelectDateTimeValue(stateDocument, TEXT("/hoboCopyState/@lastIncrementalBackup"), &_lastIncrementalBackup); | ||
|
||
if (_hasLastIncrementalBackup) | ||
{ | ||
CString message; | ||
CString dateTime; | ||
Utilities::FormatDateTime(&_lastIncrementalBackup, TEXT(" "), false, dateTime); | ||
message.AppendFormat(TEXT("Last incremental backup time read as %s"), dateTime); | ||
InstrumentationHelper::Log(message); | ||
} | ||
else | ||
{ | ||
InstrumentationHelper::Log(TEXT("Backup file did not have last incremental backup time recorded.")); | ||
} | ||
} | ||
|
||
void Save(LPCTSTR path, BSTR backupDocument) | ||
{ | ||
if (!_hasLastFullBackup) | ||
{ | ||
throw new CHoboCopyException(TEXT("Could not locate last full backup time.")); | ||
} | ||
|
||
CString message; | ||
message.AppendFormat(TEXT("Saving backup document to state file %s"), path); | ||
InstrumentationHelper::Log(message, LOG_LEVEL_INFO); | ||
|
||
InstrumentationHelper::Log(TEXT("Backup document:")); | ||
InstrumentationHelper::Log(backupDocument); | ||
|
||
InstrumentationHelper::Log(TEXT("Creating DOM document object.")); | ||
CComPtr<IXMLDOMDocument> backupDocumentDom; | ||
//IXMLDOMDocument* backupDocumentDom; | ||
//CHECK_HRESULT(::CoCreateInstance(CLSID_DOMDocument30, NULL, CLSCTX_INPROC, IID_IXMLDOMDocument, (LPVOID*) &backupDocumentDom) | ||
CHECK_HRESULT(backupDocumentDom.CoCreateInstance(__uuidof(DOMDocument30))); | ||
|
||
InstrumentationHelper::Log(TEXT("Turning off validation")); | ||
CHECK_HRESULT(backupDocumentDom->put_validateOnParse(VARIANT_FALSE)); | ||
|
||
InstrumentationHelper::Log(TEXT("Loading backup document into DOM.")); | ||
VARIANT_BOOL worked; | ||
HRESULT hr = backupDocumentDom->loadXML(backupDocument, &worked); | ||
|
||
if (FAILED(hr)) | ||
{ | ||
CString message; | ||
message.Format(TEXT("loadXML failed with HRESULT 0x%x"), hr); | ||
throw new CHoboCopyException(message); | ||
} | ||
else if (hr == S_FALSE) | ||
{ | ||
CComPtr<IXMLDOMParseError> parseError; | ||
InstrumentationHelper::Log(TEXT("Retrieving parse error")); | ||
CHECK_HRESULT(backupDocumentDom->get_parseError(&parseError)); | ||
CComBSTR bstrReason; | ||
InstrumentationHelper::Log(TEXT("Retrieving reason")); | ||
CHECK_HRESULT(parseError->get_reason(&bstrReason)); | ||
CString message; | ||
message.Format(TEXT("loadXML failed to parse: %s"), bstrReason); | ||
throw new CHoboCopyException(message); | ||
} | ||
|
||
if (worked == VARIANT_FALSE) | ||
{ | ||
throw new CHoboCopyException(TEXT("IXMLDOMDocument::loadXML() failed")); | ||
} | ||
|
||
InstrumentationHelper::Log(TEXT("Creating state element.")); | ||
CComPtr<IXMLDOMElement> pStateElement; | ||
CHECK_HRESULT(backupDocumentDom->createElement(CComBSTR(TEXT("hoboCopyState")), | ||
&pStateElement)); | ||
|
||
if (_hasLastFullBackup) | ||
{ | ||
InstrumentationHelper::Log(TEXT("Creating lastFullBackup attribute")); | ||
CString dateTime; | ||
Utilities::FormatDateTime(&_lastFullBackup, TEXT("T"), false, dateTime); | ||
AppendAttribute(backupDocumentDom, pStateElement, TEXT("lastFullBackup"), dateTime); | ||
} | ||
else | ||
{ | ||
InstrumentationHelper::Log(TEXT("No last full backup time was present.")); | ||
} | ||
|
||
if (_hasLastIncrementalBackup) | ||
{ | ||
InstrumentationHelper::Log(TEXT("Creating lastIncrementalBackup attribute")); | ||
CString dateTime; | ||
Utilities::FormatDateTime(&_lastIncrementalBackup, TEXT("T"), false, dateTime); | ||
AppendAttribute(backupDocumentDom, pStateElement, TEXT("lastIncrementalBackup"), dateTime); | ||
} | ||
else | ||
{ | ||
InstrumentationHelper::Log(TEXT("No last incremental backup time was present.")); | ||
} | ||
|
||
InstrumentationHelper::Log(TEXT("Retrieving reference to backup document element.")); | ||
CComPtr<IXMLDOMElement> pBackupDocumentElement; | ||
CHECK_HRESULT(backupDocumentDom->get_documentElement(&pBackupDocumentElement)); | ||
|
||
CComPtr<IXMLDOMNode> pThrowawayNode; | ||
InstrumentationHelper::Log(TEXT("Removing backup document element.")); | ||
CHECK_HRESULT(backupDocumentDom->removeChild(pBackupDocumentElement, &pThrowawayNode)); | ||
pThrowawayNode.Release(); | ||
|
||
InstrumentationHelper::Log(TEXT("Adding backup document element as child of state element.")); | ||
CHECK_HRESULT(pStateElement->appendChild(pBackupDocumentElement, &pThrowawayNode)); | ||
pThrowawayNode.Release(); | ||
|
||
InstrumentationHelper::Log(TEXT("Adding state element as document element.")); | ||
CHECK_HRESULT(backupDocumentDom->appendChild(pStateElement, &pThrowawayNode)); | ||
pThrowawayNode.Release(); | ||
|
||
InstrumentationHelper::Log(TEXT("Saving backup document.")); | ||
CHECK_HRESULT(backupDocumentDom->save(CComVariant(path))); | ||
|
||
InstrumentationHelper::Log(TEXT("Successfully wrote state file"), LOG_LEVEL_INFO); | ||
|
||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright (c) 2006 Wangdera Corporation (hobocopy@wangdera.com) | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include "stdafx.h" | ||
|
||
#include "CComException.h" |
Oops, something went wrong.