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
|
// **************************************************************************
// * 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) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#include "shadow.h"
#include <algorithm>
#include <string>
#include <zen/com_ptr.h>
#include <zen/com_error.h>
#include <zen/scope_guard.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"
#elif defined USE_SHADOW_WINDOWS7
#include <vss.h> //
#include <vswriter.h> //part of Windows SDK for Windows 7
#include <vsbackup.h> //
#pragma comment(lib, "VssApi.lib")
#else
#error adapt!
#endif
using namespace zen;
struct shadow::ShadowData
{
ShadowData(const ComPtr<IVssBackupComponents>& backupComp,
const std::wstring& shadowVolume) : backupComp_(backupComp), shadowVolume_(shadowVolume) {}
ComPtr<IVssBackupComponents> backupComp_;
std::wstring shadowVolume_;
};
namespace
{
std::wstring formatVssError(HRESULT hr) //at least the one's from IVssBackupComponents::AddToSnapshotSet; return empty if no format found
{
switch (hr)
{
case VSS_E_BAD_STATE:
return L"VSS_E_BAD_STATE";
case VSS_E_MAXIMUM_NUMBER_OF_VOLUMES_REACHED:
return L"VSS_E_MAXIMUM_NUMBER_OF_VOLUMES_REACHED";
case VSS_E_MAXIMUM_NUMBER_OF_SNAPSHOTS_REACHED:
return L"VSS_E_MAXIMUM_NUMBER_OF_SNAPSHOTS_REACHED";
case VSS_E_OBJECT_NOT_FOUND:
return L"VSS_E_OBJECT_NOT_FOUND";
case VSS_E_PROVIDER_NOT_REGISTERED:
return L"VSS_E_PROVIDER_NOT_REGISTERED";
case VSS_E_PROVIDER_VETO:
return L"VSS_E_PROVIDER_VETO";
case VSS_E_VOLUME_NOT_SUPPORTED:
return L"VSS_E_VOLUME_NOT_SUPPORTED";
case VSS_E_VOLUME_NOT_SUPPORTED_BY_PROVIDER:
return L"VSS_E_VOLUME_NOT_SUPPORTED_BY_PROVIDER";
case VSS_E_UNEXPECTED_PROVIDER_ERROR:
return L"VSS_E_UNEXPECTED_PROVIDER_ERROR";
default:
return std::wstring();
}
}
inline
void copyString(const std::wstring& input, wchar_t* buffer, size_t bufferSize)
{
if (bufferSize > 0)
{
//size_t endPos = input.copy(buffer, bufferSize - 1);
//buffer[endPos] = 0;
const size_t maxSize = std::min(input.length(), bufferSize - 1);
std::copy(input.begin(), input.begin() + maxSize, buffer);
buffer[maxSize] = 0;
}
}
shadow::ShadowData createShadowCopy(const wchar_t* volumeName) //throw ComError
{
ComPtr<IVssBackupComponents> backupComp;
{
HRESULT hr = ::CreateVssBackupComponents(backupComp.init());
if (FAILED(hr))
{
if (hr == E_ACCESSDENIED)
throw ComError(L"The caller does not have sufficient backup privileges or is not an administrator.", hr);
throw ComError(L"Error calling \"CreateVssBackupComponents\".", hr);
}
}
ZEN_CHECK_COM(backupComp->InitializeForBackup()); //throw ComError
ZEN_CHECK_COM(backupComp->SetBackupState(false, false, VSS_BT_FULL)); //throw ComError
auto waitForComFuture = [](IVssAsync& fut)
{
ZEN_CHECK_COM(fut.Wait());
HRESULT hr = S_OK;
ZEN_CHECK_COM(fut.QueryStatus(&hr, nullptr)); //check if the async operation succeeded...
if (FAILED(hr))
throw ComError(L"Error calling \"fut->QueryStatus\".", hr);
};
ComPtr<IVssAsync> gatherAsync;
ZEN_CHECK_COM(backupComp->GatherWriterMetadata(gatherAsync.init()));
waitForComFuture(*gatherAsync); //failure can happen if XP-version of VSS is used on Windows Vista (which needs at least VSS-Server2003 build)
VSS_ID snapshotSetId = {};
ZEN_CHECK_COM(backupComp->StartSnapshotSet(&snapshotSetId));
ScopeGuard guardSnapShot = makeGuard([&] { backupComp->AbortBackup(); });
//Quote: "This method must be called if a backup operation terminates after the creation of a
//shadow copy set with "StartSnapshotSet" and before "DoSnapshotSet" returns."
VSS_ID SnapShotId = {};
{
HRESULT hr = backupComp->AddToSnapshotSet(const_cast<wchar_t*>(volumeName), GUID_NULL, &SnapShotId);
if (FAILED(hr))
{
if (hr == VSS_E_VOLUME_NOT_SUPPORTED)
throw ComError(L"Volume Shadow Copy Service is not supported on this volume!");
const std::wstring vssError = formatVssError(hr);
if (!vssError.empty())
throw ComError(L"Error calling \"backupComp->AddToSnapshotSet\": " + vssError);
else
throw ComError(L"Error calling \"backupComp->AddToSnapshotSet\".", hr);
}
}
ComPtr<IVssAsync> prepareAsync;
ZEN_CHECK_COM(backupComp->PrepareForBackup(prepareAsync.init()));
waitForComFuture(*prepareAsync);
ComPtr<IVssAsync> snapshotAsync;
ZEN_CHECK_COM(backupComp->DoSnapshotSet(snapshotAsync.init()));
guardSnapShot.dismiss();
waitForComFuture(*snapshotAsync);
VSS_SNAPSHOT_PROP props = {};
ZEN_CHECK_COM(backupComp->GetSnapshotProperties(SnapShotId, &props));
ZEN_ON_SCOPE_EXIT(::VssFreeSnapshotProperties(&props));
//finally: write volume name of newly created shadow copy
return shadow::ShadowData(backupComp, props.m_pwszSnapshotDeviceObject);
}
}
shadow::ShadowHandle shadow::createShadowCopy(const wchar_t* volumeName,
wchar_t* errorMessage,
unsigned int errorBufferLen)
{
try
{
ShadowData result = ::createShadowCopy(volumeName); //shadow handle owned by caller! throw ComError
return new ShadowData(result);
}
catch (const zen::ComError& e)
{
copyString(e.toString(), errorMessage, errorBufferLen);
return nullptr;
}
}
void shadow::getShadowVolume(shadow::ShadowHandle handle,
wchar_t* buffer,
unsigned int bufferLen)
{
copyString(handle->shadowVolume_, buffer, bufferLen);
}
void shadow::releaseShadowCopy(ShadowHandle handle)
{
delete handle;
}
|