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
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) *
// **************************************************************************
//
#include "shadow.h"
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#ifdef USE_SHADOW_XP
#include "xp/inc/vss.h"
#include "xp/inc/vswriter.h"
#include "xp/inc/vsbackup.h"
#elif defined USE_SHADOW_2003
#include "Server 2003/inc/vss.h"
#include "Server 2003/inc/vswriter.h"
#include "Server 2003/inc/vsbackup.h"
#else
adapt!
#endif
#include <algorithm>
#include <string>
#include <cstdio>
#include <comdef.h>
//typedef GUID VSS_ID;
//IShellItem resource management: better handled with boost::shared_ptr or CComPtr, but we avoid dependency with boost and ATL in this case
template <class T>
class ItemHolder
{
public:
ItemHolder(T* item) : item_(item) {}
~ItemHolder()
{
if (item_)
item_->Release();
}
T* release()
{
T* rv = item_;
item_ = 0;
return rv;
}
T* operator->() const
{
return item_;
}
private:
ItemHolder(const ItemHolder&);
ItemHolder& operator=(const ItemHolder&);
T* item_;
};
void writeString(const wchar_t* input, wchar_t* output, unsigned int outputBufferLen)
{
const size_t newSize = min(wcslen(input) + 1, outputBufferLen); //including null-termination
memcpy(output, input, newSize * sizeof(wchar_t));
output[newSize-1] = 0; //if output buffer is too small...
}
std::wstring numberToHexString(const long number)
{
wchar_t result[100];
swprintf(result, 100, L"0x%08x", number);
return std::wstring(result);
}
void writeErrorMsg(const wchar_t* input, HRESULT hr, wchar_t* output, unsigned int outputBufferLen)
{
std::wstring formattedMsg(input);
formattedMsg += L" (";
formattedMsg += numberToHexString(hr);
formattedMsg += L": ";
formattedMsg += _com_error(hr).ErrorMessage();
formattedMsg += L")";
writeString(formattedMsg.c_str(), output, outputBufferLen);
}
bool Shadow::createShadowCopy(const wchar_t* volumeName,
wchar_t* shadowVolName,
unsigned int shadowBufferLen,
void** backupHandle,
wchar_t* errorMessage,
unsigned int errorBufferLen)
{
//MessageBox(0, L"backup err", L"", 0); */
*backupHandle = NULL;
HRESULT hr = NULL;
IVssBackupComponents* pBackupPtr = NULL;
if (FAILED(hr = CreateVssBackupComponents(&pBackupPtr)))
{
if (hr == E_ACCESSDENIED)
writeErrorMsg(L"The caller does not have sufficient backup privileges or is not an administrator.", hr, errorMessage, errorBufferLen);
else
writeErrorMsg(L"Error calling \"CreateVssBackupComponents\".", hr, errorMessage, errorBufferLen);
return false;
}
ItemHolder<IVssBackupComponents> pBackupComponents(pBackupPtr);
if (FAILED(hr = pBackupComponents->InitializeForBackup()))
{
writeErrorMsg(L"Error calling \"InitializeForBackup\".", hr, errorMessage, errorBufferLen);
return false;
}
if (FAILED(hr = pBackupComponents->SetBackupState(false, false, VSS_BT_FULL)))
{
writeErrorMsg(L"Error calling \"SetBackupState\".", hr, errorMessage, errorBufferLen);
return false;
}
IVssAsync* pWriteMetaData = NULL;
if (FAILED(hr = pBackupComponents->GatherWriterMetadata( &pWriteMetaData )))
{ //this can happen if XP-version of VSS is used on Windows Vista (which needs at least VSS-Server2003 build)
writeErrorMsg(L"Error calling \"GatherWriterMetadata\".", hr, errorMessage, errorBufferLen);
return false;
}
//wait for shadow copy writers to complete
hr = pWriteMetaData->Wait();
if (SUCCEEDED(hr))
pWriteMetaData->QueryStatus(&hr, NULL); //check if the async operation succeeded...
pWriteMetaData->Release();
if (FAILED(hr))
{
writeErrorMsg(L"Error calling \"ppWriteMetaData->Wait\".", hr, errorMessage, errorBufferLen);
return false;
}
VSS_ID snapshotSetId = {0};
if (FAILED(hr = pBackupComponents->StartSnapshotSet( &snapshotSetId )))
{
writeErrorMsg(L"Error calling \"StartSnapshotSet\".", hr, errorMessage, errorBufferLen);
return false;
}
VSS_ID SnapShotId = {0};
if (FAILED(hr = pBackupComponents->AddToSnapshotSet(const_cast<wchar_t*>(volumeName), GUID_NULL, &SnapShotId)))
{
writeErrorMsg(L"Error calling \"AddToSnapshotSet\".", hr, errorMessage, errorBufferLen);
return false;
}
IVssAsync* pPrepare = NULL;
if (FAILED(hr = pBackupComponents->PrepareForBackup( &pPrepare )))
{
writeErrorMsg(L"Error calling \"PrepareForBackup\".", hr, errorMessage, errorBufferLen);
return false;
}
hr = pPrepare->Wait();
if (SUCCEEDED(hr))
pPrepare->QueryStatus(&hr, NULL); //check if the async operation succeeded...
pPrepare->Release();
if (FAILED(hr))
{
writeErrorMsg(L"Error calling \"pPrepare->Wait\".", hr, errorMessage, errorBufferLen);
return false;
}
IVssAsync* pDoShadowCopy = NULL;
if (FAILED(hr = pBackupComponents->DoSnapshotSet(&pDoShadowCopy)))
{
writeErrorMsg(L"Error calling \"DoSnapshotSet\".", hr, errorMessage, errorBufferLen);
return false;
}
hr = pDoShadowCopy->Wait();
if (SUCCEEDED(hr))
pDoShadowCopy->QueryStatus(&hr, NULL); //check if the async operation succeeded...
pDoShadowCopy->Release();
if (FAILED(hr))
{
writeErrorMsg(L"Error calling \"pPrepare->Wait\".", hr, errorMessage, errorBufferLen);
return false;
}
VSS_SNAPSHOT_PROP props;
if (FAILED(hr = pBackupComponents->GetSnapshotProperties( SnapShotId, &props )))
{
writeErrorMsg(L"Error calling \"GetSnapshotProperties\".", hr, errorMessage, errorBufferLen);
return false;
}
//finally: write volume name of newly created shadow copy
writeString(props.m_pwszSnapshotDeviceObject, shadowVolName, shadowBufferLen);
VssFreeSnapshotProperties(&props);
*backupHandle = pBackupComponents.release(); //release ownership
return true;
}
void Shadow::releaseShadowCopy(void* backupHandle)
{
if (backupHandle != NULL)
static_cast<IVssBackupComponents*>(backupHandle)->Release();
}
|