-
Notifications
You must be signed in to change notification settings - Fork 51
/
CBackupState.h
309 lines (258 loc) · 11.6 KB
/
CBackupState.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright (c) 2011 Wangdera Corporation ([email protected])
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
#include "CComException.h"
#include "OutputWriter.h"
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);
OutputWriter::WriteLine(message);
CComPtr<IXMLDOMAttribute> pAttribute;
CHECK_HRESULT(document->createAttribute(CComBSTR(name), &pAttribute));
OutputWriter::WriteLine(TEXT("Setting value of attribute."));
CHECK_HRESULT(pAttribute->put_text(CComBSTR(value)));
OutputWriter::WriteLine(TEXT("Retrieving attributes from parent element."));
CComPtr<IXMLDOMNamedNodeMap> pAttributes;
CHECK_HRESULT(parent->get_attributes(&pAttributes));
OutputWriter::WriteLine(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);
OutputWriter::WriteLine(message);
CComPtr<IXMLDOMNode> pNode;
HRESULT hr = document->selectSingleNode(CComBSTR(xpath), &pNode);
if (hr == S_FALSE)
{
OutputWriter::WriteLine(TEXT("Unable to find matching node."));
return FALSE;
}
else
{
// Cheap way of throwing an exception with the details filled in
CHECK_HRESULT(hr);
}
OutputWriter::WriteLine(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);
OutputWriter::WriteLine(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;
OutputWriter::WriteLine(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);
OutputWriter::WriteLine(message);
OutputWriter::WriteLine(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);
OutputWriter::WriteLine(message);
}
else
{
OutputWriter::WriteLine(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);
OutputWriter::WriteLine(message);
}
else
{
OutputWriter::WriteLine(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);
OutputWriter::WriteLine(message, VERBOSITY_THRESHOLD_NORMAL);
OutputWriter::WriteLine(TEXT("Backup document:"));
OutputWriter::WriteLine(backupDocument);
OutputWriter::WriteLine(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)));
OutputWriter::WriteLine(TEXT("Turning off validation"));
CHECK_HRESULT(backupDocumentDom->put_validateOnParse(VARIANT_FALSE));
OutputWriter::WriteLine(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;
OutputWriter::WriteLine(TEXT("Retrieving parse error"));
CHECK_HRESULT(backupDocumentDom->get_parseError(&parseError));
CComBSTR bstrReason;
OutputWriter::WriteLine(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"));
}
OutputWriter::WriteLine(TEXT("Creating state element."));
CComPtr<IXMLDOMElement> pStateElement;
CHECK_HRESULT(backupDocumentDom->createElement(CComBSTR(TEXT("hoboCopyState")),
&pStateElement));
if (_hasLastFullBackup)
{
OutputWriter::WriteLine(TEXT("Creating lastFullBackup attribute"));
CString dateTime;
Utilities::FormatDateTime(&_lastFullBackup, TEXT("T"), false, dateTime);
AppendAttribute(backupDocumentDom, pStateElement, TEXT("lastFullBackup"), dateTime);
}
else
{
OutputWriter::WriteLine(TEXT("No last full backup time was present."));
}
if (_hasLastIncrementalBackup)
{
OutputWriter::WriteLine(TEXT("Creating lastIncrementalBackup attribute"));
CString dateTime;
Utilities::FormatDateTime(&_lastIncrementalBackup, TEXT("T"), false, dateTime);
AppendAttribute(backupDocumentDom, pStateElement, TEXT("lastIncrementalBackup"), dateTime);
}
else
{
OutputWriter::WriteLine(TEXT("No last incremental backup time was present."));
}
OutputWriter::WriteLine(TEXT("Retrieving reference to backup document element."));
CComPtr<IXMLDOMElement> pBackupDocumentElement;
CHECK_HRESULT(backupDocumentDom->get_documentElement(&pBackupDocumentElement));
CComPtr<IXMLDOMNode> pThrowawayNode;
OutputWriter::WriteLine(TEXT("Removing backup document element."));
CHECK_HRESULT(backupDocumentDom->removeChild(pBackupDocumentElement, &pThrowawayNode));
pThrowawayNode.Release();
OutputWriter::WriteLine(TEXT("Adding backup document element as child of state element."));
CHECK_HRESULT(pStateElement->appendChild(pBackupDocumentElement, &pThrowawayNode));
pThrowawayNode.Release();
OutputWriter::WriteLine(TEXT("Adding state element as document element."));
CHECK_HRESULT(backupDocumentDom->appendChild(pStateElement, &pThrowawayNode));
pThrowawayNode.Release();
OutputWriter::WriteLine(TEXT("Saving backup document."));
CHECK_HRESULT(backupDocumentDom->save(CComVariant(path)));
OutputWriter::WriteLine(TEXT("Successfully wrote state file"), VERBOSITY_THRESHOLD_NORMAL);
}
};