-
Notifications
You must be signed in to change notification settings - Fork 256
/
vss.cc
266 lines (220 loc) · 6.35 KB
/
vss.cc
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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2005-2010 Free Software Foundation Europe e.V.
Copyright (C) 2014-2015 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
Copyright transferred from MATRIX-Computer GmbH to
Kern Sibbald by express permission.
Author : Thorsten Engel
Created On : Fri May 06 21:44:00 2005
*/
/* @file
Interface to Volume Shadow Copies (VSS)
*/
#ifdef WIN32_VSS
#include "include/bareos.h"
#include "filed/filed.h"
#include "ms_atl.h"
#include <objbase.h>
namespace filedaemon {
/*
* { b5946137-7b9f-4925-af80-51abd60b20d5 }
*/
static const GUID VSS_SWPRV_ProviderID = {
0xb5946137, 0x7b9f, 0x4925, { 0xaf, 0x80, 0x51, 0xab, 0xd6, 0x0b, 0x20, 0xd5 }
};
static bool VSSPathConvert(const char *szFilePath, char *szShadowPath, int nBuflen)
{
JobControlRecord *jcr = get_jcr_from_tsd();
if (jcr && jcr->pVSSClient) {
return jcr->pVSSClient->GetShadowPath(szFilePath, szShadowPath, nBuflen);
}
return false;
}
static bool VSSPathConvertW(const wchar_t *szFilePath, wchar_t *szShadowPath, int nBuflen)
{
JobControlRecord *jcr = get_jcr_from_tsd();
if (jcr && jcr->pVSSClient) {
return jcr->pVSSClient->GetShadowPathW(szFilePath, szShadowPath, nBuflen);
}
return false;
}
void VSSInit(JobControlRecord *jcr)
{
/*
* Decide which vss class to initialize
*/
if (g_MajorVersion == 5) {
switch (g_MinorVersion) {
case 1:
jcr->pVSSClient = new VSSClientXP();
break;
case 2:
jcr->pVSSClient = new VSSClient2003();
break;
}
/*
* Vista or Longhorn or later
*/
} else if (g_MajorVersion >= 6) {
jcr->pVSSClient = new VSSClientVista();
}
/*
* Setup the callback functions.
*/
if (!SetVSSPathConvert(VSSPathConvert, VSSPathConvertW)) {
Jmsg(jcr, M_FATAL, 0, "Failed to setup VSS Path Conversion callbacks.\n");
}
}
/*
* Constructor
*/
VSSClient::VSSClient()
{
memset(this, 0, sizeof(VSSClient));
pAlistWriterState_ = New(alist(10, not_owned_by_alist));
pAlistWriterInfoText_ = New(alist(10, owned_by_alist));
uidCurrentSnapshotSet_ = GUID_NULL;
}
/*
* Destructor
*/
VSSClient::~VSSClient()
{
/*
* Release the IVssBackupComponents interface
* WARNING: this must be done BEFORE calling CoUninitialize()
*/
if (pVssObject_) {
// pVssObject_->Release();
pVssObject_ = NULL;
}
DestroyWriterInfo();
delete pAlistWriterState_;
delete pAlistWriterInfoText_;
/*
* Call CoUninitialize if the CoInitialize was performed successfully
*/
if (bCoInitializeCalled_) {
CoUninitialize();
}
}
bool VSSClient::InitializeForBackup(JobControlRecord *jcr)
{
jcr_ = jcr;
GeneratePluginEvent(jcr, bEventVssInitializeForBackup);
return Initialize(0);
}
bool VSSClient::InitializeForRestore(JobControlRecord *jcr)
{
metadata_ = NULL;
jcr_ = jcr;
GeneratePluginEvent(jcr, bEventVssInitializeForRestore);
return Initialize(0, true /*=>Restore*/);
}
bool VSSClient::GetShadowPath(const char *szFilePath, char *szShadowPath, int nBuflen)
{
if (!bBackupIsInitialized_)
return false;
/*
* Check for valid pathname
*/
bool bIsValidName;
bIsValidName = strlen(szFilePath) > 3;
if (bIsValidName)
bIsValidName &= isalpha (szFilePath[0]) &&
szFilePath[1]==':' &&
szFilePath[2] == '\\';
if (bIsValidName) {
int nDriveIndex = toupper(szFilePath[0])-'A';
if (szShadowCopyName_[nDriveIndex][0] != 0) {
if (WideCharToMultiByte(CP_UTF8,0,szShadowCopyName_[nDriveIndex],-1,szShadowPath,nBuflen-1,NULL,NULL)) {
nBuflen -= (int)strlen(szShadowPath);
bstrncat(szShadowPath, szFilePath+2, nBuflen);
return true;
}
}
}
bstrncpy(szShadowPath, szFilePath, nBuflen);
errno = EINVAL;
return false;
}
bool VSSClient::GetShadowPathW(const wchar_t *szFilePath, wchar_t *szShadowPath, int nBuflen)
{
if (!bBackupIsInitialized_)
return false;
/*
* Check for valid pathname
*/
bool bIsValidName;
bIsValidName = wcslen(szFilePath) > 3;
if (bIsValidName)
bIsValidName &= iswalpha (szFilePath[0]) &&
szFilePath[1]==':' &&
szFilePath[2] == '\\';
if (bIsValidName) {
int nDriveIndex = towupper(szFilePath[0])-'A';
if (szShadowCopyName_[nDriveIndex][0] != 0) {
wcsncpy(szShadowPath, szShadowCopyName_[nDriveIndex], nBuflen);
nBuflen -= (int)wcslen(szShadowCopyName_[nDriveIndex]);
wcsncat(szShadowPath, szFilePath+2, nBuflen);
return true;
}
}
wcsncpy(szShadowPath, szFilePath, nBuflen);
errno = EINVAL;
return false;
}
const size_t VSSClient::GetWriterCount()
{
return pAlistWriterInfoText_->size();
}
const char* VSSClient::GetWriterInfo(int nIndex)
{
return (char*)pAlistWriterInfoText_->get(nIndex);
}
const int VSSClient::GetWriterState(int nIndex)
{
void *item = pAlistWriterState_->get(nIndex);
/*
* Eliminate compiler warnings
*/
#ifdef HAVE_VSS64
return (int64_t)(char *)item;
#else
return (int)(char *)item;
#endif
}
void VSSClient::AppendWriterInfo(int nState, const char* pszInfo)
{
pAlistWriterInfoText_->push(bstrdup(pszInfo));
pAlistWriterState_->push((void*)nState);
}
/*
* Note, this is called at the end of every job, so release all
* the items in the alists, but do not delete the alist.
*/
void VSSClient::DestroyWriterInfo()
{
while (!pAlistWriterInfoText_->empty()) {
free(pAlistWriterInfoText_->pop());
}
while (!pAlistWriterState_->empty()) {
pAlistWriterState_->pop();
}
}
#endif
} /* namespace filedaemon */