2 Bacula® - The Network Backup Solution
4 Copyright (C) 2005-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 // vss.cpp -- Interface to Volume Shadow Copies (VSS)
31 // Copyright transferred from MATRIX-Computer GmbH to
32 // Kern Sibbald by express permission.
34 // Author : Thorsten Engel
35 // Created On : Fri May 06 21:44:00 2005
56 * Kludges to get Vista code to compile.
61 #define __RPC_unique_pointer
63 #define __RPC__out_ecount_part(x, y)
64 #define __RPC__deref_inout_opt
67 #if !defined(ENABLE_NLS)
68 #define setlocale(p, d)
72 // Used for safe string manipulation
76 BOOL VSSPathConvert(const char *szFilePath, char *szShadowPath, int nBuflen);
77 BOOL VSSPathConvertW(const wchar_t *szFilePath, wchar_t *szShadowPath, int nBuflen);
80 class IXMLDOMDocument;
83 /* Reduce compiler warnings from Windows vss code */
87 #pragma message("compile VSS for Windows XP")
88 #define VSSClientGeneric VSSClientXP
90 #include "inc/WinXP/vss.h"
91 #include "inc/WinXP/vswriter.h"
92 #include "inc/WinXP/vsbackup.h"
97 #pragma message("compile VSS for Windows 2003")
98 #define VSSClientGeneric VSSClient2003
100 #include "inc/Win2003/vss.h"
101 #include "inc/Win2003/vswriter.h"
102 #include "inc/Win2003/vsbackup.h"
106 #pragma message("compile VSS for Vista")
107 #define VSSClientGeneric VSSClientVista
109 #include "inc/Win2003/vss.h"
110 #include "inc/Win2003/vswriter.h"
111 #include "inc/Win2003/vsbackup.h"
115 typedef HRESULT (STDAPICALLTYPE* t_CreateVssBackupComponents)(OUT IVssBackupComponents **);
116 typedef void (APIENTRY* t_VssFreeSnapshotProperties)(IN VSS_SNAPSHOT_PROP*);
118 static t_CreateVssBackupComponents p_CreateVssBackupComponents = NULL;
119 static t_VssFreeSnapshotProperties p_VssFreeSnapshotProperties = NULL;
121 #define VSSVBACK_ENTRY "?CreateVssBackupComponents@@YGJPAPAVIVssBackupComponents@@@Z"
128 * some helper functions
133 // Append a backslash to the current string
134 inline wstring AppendBackslash(wstring str)
136 if (str.length() == 0)
137 return wstring(L"\\");
138 if (str[str.length() - 1] == L'\\')
140 return str.append(L"\\");
143 // Get the unique volume name for the given path
144 inline wstring GetUniqueVolumeNameForPath(wstring path)
146 if (path.length() <= 0) {
150 // Add the backslash termination, if needed
151 path = AppendBackslash(path);
153 // Get the root path of the volume
154 wchar_t volumeRootPath[MAX_PATH];
155 wchar_t volumeName[MAX_PATH];
156 wchar_t volumeUniqueName[MAX_PATH];
158 if (!p_GetVolumePathNameW || !p_GetVolumePathNameW((LPCWSTR)path.c_str(), volumeRootPath, MAX_PATH))
161 // Get the volume name alias (might be different from the unique volume name in rare cases)
162 if (!p_GetVolumeNameForVolumeMountPointW || !p_GetVolumeNameForVolumeMountPointW(volumeRootPath, volumeName, MAX_PATH))
165 // Get the unique volume name
166 if (!p_GetVolumeNameForVolumeMountPointW(volumeName, volumeUniqueName, MAX_PATH))
169 return volumeUniqueName;
173 // Helper macro for quick treatment of case statements for error codes
174 #define GEN_MERGE(A, B) A##B
175 #define GEN_MAKE_W(A) GEN_MERGE(L, A)
177 #define CHECK_CASE_FOR_CONSTANT(value) \
178 case value: return (GEN_MAKE_W(#value));
181 // Convert a writer status into a string
182 inline const wchar_t* GetStringFromWriterStatus(VSS_WRITER_STATE eWriterStatus)
184 switch (eWriterStatus) {
185 CHECK_CASE_FOR_CONSTANT(VSS_WS_STABLE);
186 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_FREEZE);
187 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_THAW);
188 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_POST_SNAPSHOT);
189 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_BACKUP_COMPLETE);
190 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_IDENTIFY);
191 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_PREPARE_BACKUP);
192 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_PREPARE_SNAPSHOT);
193 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_FREEZE);
194 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_THAW);
195 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_POST_SNAPSHOT);
196 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_BACKUP_COMPLETE);
197 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_PRE_RESTORE);
198 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_POST_RESTORE);
201 return L"Error or Undefined";
207 VSSClientGeneric::VSSClientGeneric()
209 m_hLib = LoadLibraryA("VSSAPI.DLL");
211 p_CreateVssBackupComponents = (t_CreateVssBackupComponents)
212 GetProcAddress(m_hLib, VSSVBACK_ENTRY);
214 p_VssFreeSnapshotProperties = (t_VssFreeSnapshotProperties)
215 GetProcAddress(m_hLib, "VssFreeSnapshotProperties");
222 VSSClientGeneric::~VSSClientGeneric()
228 // Initialize the COM infrastructure and the internal pointers
229 BOOL VSSClientGeneric::Initialize(DWORD dwContext, BOOL bDuringRestore)
231 if (!(p_CreateVssBackupComponents && p_VssFreeSnapshotProperties)) {
232 Dmsg2(0, "VSSClientGeneric::Initialize: p_CreateVssBackupComponents = 0x%08X, p_VssFreeSnapshotProperties = 0x%08X\n", p_CreateVssBackupComponents, p_VssFreeSnapshotProperties);
239 if (!m_bCoInitializeCalled) {
240 hr = CoInitialize(NULL);
242 Dmsg1(0, "VSSClientGeneric::Initialize: CoInitialize returned 0x%08X\n", hr);
243 errno = b_errno_win32;
246 m_bCoInitializeCalled = true;
249 // Initialize COM security
250 if (!m_bCoInitializeSecurityCalled) {
252 CoInitializeSecurity(
253 NULL, // Allow *all* VSS writers to communicate back!
254 -1, // Default COM authentication service
255 NULL, // Default COM authorization service
256 NULL, // reserved parameter
257 RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // Strongest COM authentication level
258 RPC_C_IMP_LEVEL_IDENTIFY, // Minimal impersonation abilities
259 NULL, // Default COM authentication settings
260 EOAC_NONE, // No special options
261 NULL // Reserved parameter
265 Dmsg1(0, "VSSClientGeneric::Initialize: CoInitializeSecurity returned 0x%08X\n", hr);
266 errno = b_errno_win32;
269 m_bCoInitializeSecurityCalled = true;
272 // Release the IVssBackupComponents interface
274 m_pVssObject->Release();
278 // Create the internal backup components object
279 hr = p_CreateVssBackupComponents((IVssBackupComponents**) &m_pVssObject);
281 Dmsg1(0, "VSSClientGeneric::Initialize: CreateVssBackupComponents returned 0x%08X\n", hr);
282 errno = b_errno_win32;
286 #if defined(B_VSS_W2K3) || defined(B_VSS_VISTA)
287 if (dwContext != VSS_CTX_BACKUP) {
288 hr = ((IVssBackupComponents*) m_pVssObject)->SetContext(dwContext);
290 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->SetContext returned 0x%08X\n", hr);
291 errno = b_errno_win32;
297 if (!bDuringRestore) {
298 // 1. InitializeForBackup
299 hr = ((IVssBackupComponents*) m_pVssObject)->InitializeForBackup();
301 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->InitializeForBackup returned 0x%08X\n", hr);
302 errno = b_errno_win32;
307 hr = ((IVssBackupComponents*) m_pVssObject)->SetBackupState(true, true, VSS_BT_FULL, false);
309 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->SetBackupState returned 0x%08X\n", hr);
310 errno = b_errno_win32;
314 CComPtr<IVssAsync> pAsync1;
315 // 3. GatherWriterMetaData
316 hr = ((IVssBackupComponents*) m_pVssObject)->GatherWriterMetadata(&pAsync1.p);
318 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->GatherWriterMetadata returned 0x%08X\n", hr);
319 errno = b_errno_win32;
322 // Waits for the async operation to finish and checks the result
323 WaitAndCheckForAsyncOperation(pAsync1.p);
326 // We are during restore now?
327 m_bDuringRestore = bDuringRestore;
330 m_dwContext = dwContext;
336 BOOL VSSClientGeneric::WaitAndCheckForAsyncOperation(IVssAsync* pAsync)
338 // Wait until the async operation finishes
339 // unfortunately we can't use a timeout here yet.
340 // the interface would allow it on W2k3,
341 // but it is not implemented yet....
345 // Check the result of the asynchronous operation
346 HRESULT hrReturned = S_OK;
348 int timeout = 600; // 10 minutes....
352 if (hrReturned != S_OK)
356 hr = pAsync->QueryStatus(&hrReturned, NULL);
360 } while ((timeout-- > 0) && (hrReturned == VSS_S_ASYNC_PENDING));
362 if (hrReturned == VSS_S_ASYNC_FINISHED)
367 // Check if the async operation succeeded...
368 if(hrReturned != VSS_S_ASYNC_FINISHED) {
369 wchar_t *pwszBuffer = NULL;
370 /* I don't see the usefulness of the following -- KES */
372 FORMAT_MESSAGE_ALLOCATE_BUFFER
373 | FORMAT_MESSAGE_FROM_SYSTEM
374 | FORMAT_MESSAGE_IGNORE_INSERTS,
376 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
377 (LPWSTR)&pwszBuffer, 0, NULL);
379 LocalFree(pwszBuffer);
380 errno = b_errno_win32;
387 BOOL VSSClientGeneric::CreateSnapshots(char* szDriveLetters)
389 /* szDriveLetters contains all drive letters in uppercase */
390 /* if a drive can not being added, it's converted to lowercase in szDriveLetters */
391 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vss/base/ivssbackupcomponents_startsnapshotset.asp */
393 if (!m_pVssObject || m_bBackupIsInitialized) {
398 m_uidCurrentSnapshotSet = GUID_NULL;
400 IVssBackupComponents *pVss = (IVssBackupComponents*)m_pVssObject;
402 /* startSnapshotSet */
404 pVss->StartSnapshotSet(&m_uidCurrentSnapshotSet);
406 /* AddToSnapshotSet */
414 CComPtr<IVssAsync> pAsync1;
415 CComPtr<IVssAsync> pAsync2;
418 for (size_t i=0; i < strlen (szDriveLetters); i++) {
419 szDrive[0] = szDriveLetters[i];
420 volume = GetUniqueVolumeNameForPath(szDrive);
421 // store uniquevolumname
422 if (SUCCEEDED(pVss->AddToSnapshotSet((LPWSTR)volume.c_str(), GUID_NULL, &pid))) {
423 wcsncpy (m_wszUniqueVolumeName[szDriveLetters[i]-'A'], (LPWSTR) volume.c_str(), MAX_PATH);
425 szDriveLetters[i] = tolower (szDriveLetters[i]);
429 /* PrepareForBackup */
430 if (FAILED(pVss->PrepareForBackup(&pAsync1.p))) {
431 errno = b_errno_win32;
435 // Waits for the async operation to finish and checks the result
436 WaitAndCheckForAsyncOperation(pAsync1.p);
438 /* get latest info about writer status */
439 if (!CheckWriterStatus()) {
440 errno = b_errno_win32;
445 if (FAILED(pVss->DoSnapshotSet(&pAsync2.p))) {
446 errno = b_errno_win32;
450 // Waits for the async operation to finish and checks the result
451 WaitAndCheckForAsyncOperation(pAsync2.p);
453 /* query snapshot info */
454 QuerySnapshotSet(m_uidCurrentSnapshotSet);
456 SetVSSPathConvert(VSSPathConvert, VSSPathConvertW);
458 m_bBackupIsInitialized = true;
463 BOOL VSSClientGeneric::CloseBackup()
469 IVssBackupComponents* pVss = (IVssBackupComponents*) m_pVssObject;
470 CComPtr<IVssAsync> pAsync;
472 SetVSSPathConvert(NULL, NULL);
474 m_bBackupIsInitialized = false;
476 if (SUCCEEDED(pVss->BackupComplete(&pAsync.p))) {
477 // Waits for the async operation to finish and checks the result
478 WaitAndCheckForAsyncOperation(pAsync.p);
481 errno = b_errno_win32;
485 /* get latest info about writer status */
488 if (m_uidCurrentSnapshotSet != GUID_NULL) {
489 VSS_ID idNonDeletedSnapshotID = GUID_NULL;
492 pVss->DeleteSnapshots(
493 m_uidCurrentSnapshotSet,
494 VSS_OBJECT_SNAPSHOT_SET,
497 &idNonDeletedSnapshotID);
499 m_uidCurrentSnapshotSet = GUID_NULL;
506 // Call CoUninitialize if the CoInitialize was performed sucesfully
507 if (m_bCoInitializeCalled) {
509 m_bCoInitializeCalled = false;
515 // Query all the shadow copies in the given set
516 void VSSClientGeneric::QuerySnapshotSet(GUID snapshotSetID)
518 if (!(p_CreateVssBackupComponents && p_VssFreeSnapshotProperties)) {
523 memset (m_szShadowCopyName,0,sizeof (m_szShadowCopyName));
525 if (snapshotSetID == GUID_NULL || m_pVssObject == NULL) {
530 IVssBackupComponents* pVss = (IVssBackupComponents*) m_pVssObject;
532 // Get list all shadow copies.
533 CComPtr<IVssEnumObject> pIEnumSnapshots;
534 HRESULT hr = pVss->Query( GUID_NULL,
537 (IVssEnumObject**)(&pIEnumSnapshots) );
539 // If there are no shadow copies, just return
541 errno = b_errno_win32;
545 // Enumerate all shadow copies.
546 VSS_OBJECT_PROP Prop;
547 VSS_SNAPSHOT_PROP& Snap = Prop.Obj.Snap;
550 // Get the next element
552 hr = (pIEnumSnapshots.p)->Next( 1, &Prop, &ulFetched );
554 // We reached the end of list
558 // Print the shadow copy (if not filtered out)
559 if (Snap.m_SnapshotSetId == snapshotSetID) {
560 for (int ch='A'-'A';ch<='Z'-'A';ch++) {
561 if (wcscmp(Snap.m_pwszOriginalVolumeName, m_wszUniqueVolumeName[ch]) == 0) {
562 wcsncpy(m_szShadowCopyName[ch],Snap.m_pwszSnapshotDeviceObject, MAX_PATH-1);
567 p_VssFreeSnapshotProperties(&Snap);
572 // Check the status for all selected writers
573 BOOL VSSClientGeneric::CheckWriterStatus()
576 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vss/base/ivssbackupcomponents_startsnapshotset.asp
578 IVssBackupComponents* pVss = (IVssBackupComponents*) m_pVssObject;
581 // Gather writer status to detect potential errors
582 CComPtr<IVssAsync> pAsync;
584 HRESULT hr = pVss->GatherWriterStatus(&pAsync.p);
586 errno = b_errno_win32;
590 // Waits for the async operation to finish and checks the result
591 WaitAndCheckForAsyncOperation(pAsync.p);
593 unsigned cWriters = 0;
595 hr = pVss->GetWriterStatusCount(&cWriters);
597 errno = b_errno_win32;
603 // Enumerate each writer
604 for (unsigned iWriter = 0; iWriter < cWriters; iWriter++) {
605 VSS_ID idInstance = GUID_NULL;
606 VSS_ID idWriter= GUID_NULL;
607 VSS_WRITER_STATE eWriterStatus = VSS_WS_UNKNOWN;
608 CComBSTR bstrWriterName;
609 HRESULT hrWriterFailure = S_OK;
612 hr = pVss->GetWriterStatus(iWriter,
623 switch(eWriterStatus) {
624 case VSS_WS_FAILED_AT_IDENTIFY:
625 case VSS_WS_FAILED_AT_PREPARE_BACKUP:
626 case VSS_WS_FAILED_AT_PREPARE_SNAPSHOT:
627 case VSS_WS_FAILED_AT_FREEZE:
628 case VSS_WS_FAILED_AT_THAW:
629 case VSS_WS_FAILED_AT_POST_SNAPSHOT:
630 case VSS_WS_FAILED_AT_BACKUP_COMPLETE:
631 case VSS_WS_FAILED_AT_PRE_RESTORE:
632 case VSS_WS_FAILED_AT_POST_RESTORE:
633 #if defined(B_VSS_W2K3) || defined(B_VSS_VISTA)
634 case VSS_WS_FAILED_AT_BACKUPSHUTDOWN:
645 /* store text info */
648 bstrncpy(str, "\"", sizeof(str));
649 wchar_2_UTF8(szBuf, bstrWriterName.p, sizeof(szBuf));
650 bstrncat(str, szBuf, sizeof(str));
651 bstrncat(str, "\", State: 0x", sizeof(str));
652 itoa(eWriterStatus, szBuf, sizeof(szBuf));
653 bstrncat(str, szBuf, sizeof(str));
654 bstrncat(str, " (", sizeof(str));
655 wchar_2_UTF8(szBuf, GetStringFromWriterStatus(eWriterStatus), sizeof(szBuf));
656 bstrncat(str, szBuf, sizeof(str));
657 bstrncat(str, ")", sizeof(str));
659 AppendWriterInfo(nState, (const char *)str);
662 hr = pVss->FreeWriterStatus();
665 errno = b_errno_win32;
673 #endif /* WIN32_VSS */