2 // vss.cpp -- Interface to Volume Shadow Copies (VSS)
4 // Copyright transferred from MATRIX-Computer GmbH to
5 // Kern Sibbald by express permission.
7 // Copyright (C) 2005-2006 Kern Sibbald
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // version 2 as amended with additional clauses defined in the
12 // file LICENSE in the main source directory.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // the file LICENSE for additional details.
20 // Author : Thorsten Engel
21 // Created On : Fri May 06 21:44:00 2005
41 #if !defined(ENABLE_NLS)
42 #define setlocale(p, d)
46 // Used for safe string manipulation
50 BOOL VSSPathConvert(const char *szFilePath, char *szShadowPath, int nBuflen);
51 BOOL VSSPathConvertW(const wchar_t *szFilePath, wchar_t *szShadowPath, int nBuflen);
54 class IXMLDOMDocument;
58 #pragma message("compile VSS for Windows XP")
59 #define VSSClientGeneric VSSClientXP
61 #include "inc/WinXP/vss.h"
62 #include "inc/WinXP/vswriter.h"
63 #include "inc/WinXP/vsbackup.h"
66 typedef HRESULT (STDAPICALLTYPE* t_CreateVssBackupComponents)(OUT IVssBackupComponents **);
67 typedef void (APIENTRY* t_VssFreeSnapshotProperties)(IN VSS_SNAPSHOT_PROP*);
69 static t_CreateVssBackupComponents p_CreateVssBackupComponents = NULL;
70 static t_VssFreeSnapshotProperties p_VssFreeSnapshotProperties = NULL;
72 #define VSSVBACK_ENTRY "?CreateVssBackupComponents@@YGJPAPAVIVssBackupComponents@@@Z"
76 #pragma message("compile VSS for Windows 2003")
77 #define VSSClientGeneric VSSClient2003
79 #include "inc/Win2003/vss.h"
80 #include "inc/Win2003/vswriter.h"
81 #include "inc/Win2003/vsbackup.h"
84 typedef HRESULT (STDAPICALLTYPE* t_CreateVssBackupComponents)(OUT IVssBackupComponents **);
85 typedef void (APIENTRY* t_VssFreeSnapshotProperties)(IN VSS_SNAPSHOT_PROP*);
87 static t_CreateVssBackupComponents p_CreateVssBackupComponents = NULL;
88 static t_VssFreeSnapshotProperties p_VssFreeSnapshotProperties = NULL;
90 #define VSSVBACK_ENTRY "?CreateVssBackupComponents@@YGJPAPAVIVssBackupComponents@@@Z"
97 * some helper functions
102 // Append a backslash to the current string
103 inline wstring AppendBackslash(wstring str)
105 if (str.length() == 0)
106 return wstring(L"\\");
107 if (str[str.length() - 1] == L'\\')
109 return str.append(L"\\");
112 // Get the unique volume name for the given path
113 inline wstring GetUniqueVolumeNameForPath(wstring path)
115 if (path.length() <= 0) {
119 // Add the backslash termination, if needed
120 path = AppendBackslash(path);
122 // Get the root path of the volume
123 wchar_t volumeRootPath[MAX_PATH];
124 wchar_t volumeName[MAX_PATH];
125 wchar_t volumeUniqueName[MAX_PATH];
127 if (!p_GetVolumePathNameW || !p_GetVolumePathNameW((LPCWSTR)path.c_str(), volumeRootPath, MAX_PATH))
130 // Get the volume name alias (might be different from the unique volume name in rare cases)
131 if (!p_GetVolumeNameForVolumeMountPointW || !p_GetVolumeNameForVolumeMountPointW(volumeRootPath, volumeName, MAX_PATH))
134 // Get the unique volume name
135 if (!p_GetVolumeNameForVolumeMountPointW(volumeName, volumeUniqueName, MAX_PATH))
138 return volumeUniqueName;
142 // Helper macro for quick treatment of case statements for error codes
143 #define GEN_MERGE(A, B) A##B
144 #define GEN_MAKE_W(A) GEN_MERGE(L, A)
146 #define CHECK_CASE_FOR_CONSTANT(value) \
147 case value: return (GEN_MAKE_W(#value));
150 // Convert a writer status into a string
151 inline const wchar_t* GetStringFromWriterStatus(VSS_WRITER_STATE eWriterStatus)
153 switch (eWriterStatus) {
154 CHECK_CASE_FOR_CONSTANT(VSS_WS_STABLE);
155 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_FREEZE);
156 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_THAW);
157 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_POST_SNAPSHOT);
158 CHECK_CASE_FOR_CONSTANT(VSS_WS_WAITING_FOR_BACKUP_COMPLETE);
159 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_IDENTIFY);
160 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_PREPARE_BACKUP);
161 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_PREPARE_SNAPSHOT);
162 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_FREEZE);
163 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_THAW);
164 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_POST_SNAPSHOT);
165 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_BACKUP_COMPLETE);
166 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_PRE_RESTORE);
167 CHECK_CASE_FOR_CONSTANT(VSS_WS_FAILED_AT_POST_RESTORE);
170 return L"Error or Undefined";
176 VSSClientGeneric::VSSClientGeneric()
178 m_hLib = LoadLibraryA("VSSAPI.DLL");
180 p_CreateVssBackupComponents = (t_CreateVssBackupComponents)
181 GetProcAddress(m_hLib, VSSVBACK_ENTRY);
183 p_VssFreeSnapshotProperties = (t_VssFreeSnapshotProperties)
184 GetProcAddress(m_hLib, "VssFreeSnapshotProperties");
191 VSSClientGeneric::~VSSClientGeneric()
197 // Initialize the COM infrastructure and the internal pointers
198 BOOL VSSClientGeneric::Initialize(DWORD dwContext, BOOL bDuringRestore)
200 if (!(p_CreateVssBackupComponents && p_VssFreeSnapshotProperties)) {
201 Dmsg2(0, "VSSClientGeneric::Initialize: p_CreateVssBackupComponents = 0x%08X, p_VssFreeSnapshotProperties = 0x%08X\n", p_CreateVssBackupComponents, p_VssFreeSnapshotProperties);
208 if (!m_bCoInitializeCalled) {
209 hr = CoInitialize(NULL);
211 Dmsg1(0, "VSSClientGeneric::Initialize: CoInitialize returned 0x%08X\n", hr);
212 errno = b_errno_win32;
215 m_bCoInitializeCalled = true;
218 // Initialize COM security
219 if (!m_bCoInitializeSecurityCalled) {
221 CoInitializeSecurity(
222 NULL, // Allow *all* VSS writers to communicate back!
223 -1, // Default COM authentication service
224 NULL, // Default COM authorization service
225 NULL, // reserved parameter
226 RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // Strongest COM authentication level
227 RPC_C_IMP_LEVEL_IDENTIFY, // Minimal impersonation abilities
228 NULL, // Default COM authentication settings
229 EOAC_NONE, // No special options
230 NULL // Reserved parameter
234 Dmsg1(0, "VSSClientGeneric::Initialize: CoInitializeSecurity returned 0x%08X\n", hr);
235 errno = b_errno_win32;
238 m_bCoInitializeSecurityCalled = true;
241 // Release the IVssBackupComponents interface
243 m_pVssObject->Release();
247 // Create the internal backup components object
248 hr = p_CreateVssBackupComponents((IVssBackupComponents**) &m_pVssObject);
250 Dmsg1(0, "VSSClientGeneric::Initialize: CreateVssBackupComponents returned 0x%08X\n", hr);
251 errno = b_errno_win32;
256 if (dwContext != VSS_CTX_BACKUP) {
257 hr = ((IVssBackupComponents*) m_pVssObject)->SetContext(dwContext);
259 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->SetContext returned 0x%08X\n", hr);
260 errno = b_errno_win32;
266 if (!bDuringRestore) {
267 // 1. InitializeForBackup
268 hr = ((IVssBackupComponents*) m_pVssObject)->InitializeForBackup();
270 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->InitializeForBackup returned 0x%08X\n", hr);
271 errno = b_errno_win32;
276 hr = ((IVssBackupComponents*) m_pVssObject)->SetBackupState(true, true, VSS_BT_FULL, false);
278 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->SetBackupState returned 0x%08X\n", hr);
279 errno = b_errno_win32;
283 CComPtr<IVssAsync> pAsync1;
284 // 3. GatherWriterMetaData
285 hr = ((IVssBackupComponents*) m_pVssObject)->GatherWriterMetadata(&pAsync1.p);
287 Dmsg1(0, "VSSClientGeneric::Initialize: IVssBackupComponents->GatherWriterMetadata returned 0x%08X\n", hr);
288 errno = b_errno_win32;
291 // Waits for the async operation to finish and checks the result
292 WaitAndCheckForAsyncOperation(pAsync1.p);
295 // We are during restore now?
296 m_bDuringRestore = bDuringRestore;
299 m_dwContext = dwContext;
305 BOOL VSSClientGeneric::WaitAndCheckForAsyncOperation(IVssAsync* pAsync)
307 // Wait until the async operation finishes
308 // unfortunately we can't use a timeout here yet.
309 // the interface would allow it on W2k3,
310 // but it is not implemented yet....
314 // Check the result of the asynchronous operation
315 HRESULT hrReturned = S_OK;
317 int timeout = 600; // 10 minutes....
321 if (hrReturned != S_OK)
325 hr = pAsync->QueryStatus(&hrReturned, NULL);
329 } while ((timeout-- > 0) && (hrReturned == VSS_S_ASYNC_PENDING));
331 if (hrReturned == VSS_S_ASYNC_FINISHED)
336 // Check if the async operation succeeded...
337 if(hrReturned != VSS_S_ASYNC_FINISHED) {
338 wchar_t *pwszBuffer = NULL;
339 DWORD dwRet = ::FormatMessageW(
340 FORMAT_MESSAGE_ALLOCATE_BUFFER
341 | FORMAT_MESSAGE_FROM_SYSTEM
342 | FORMAT_MESSAGE_IGNORE_INSERTS,
344 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
345 (LPWSTR)&pwszBuffer, 0, NULL);
348 LocalFree(pwszBuffer);
350 errno = b_errno_win32;
357 BOOL VSSClientGeneric::CreateSnapshots(char* szDriveLetters)
359 /* szDriveLetters contains all drive letters in uppercase */
360 /* if a drive can not being added, it's converted to lowercase in szDriveLetters */
361 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vss/base/ivssbackupcomponents_startsnapshotset.asp */
363 if (!m_pVssObject || m_bBackupIsInitialized) {
368 m_uidCurrentSnapshotSet = GUID_NULL;
370 IVssBackupComponents *pVss = (IVssBackupComponents*)m_pVssObject;
372 /* startSnapshotSet */
374 pVss->StartSnapshotSet(&m_uidCurrentSnapshotSet);
376 /* AddToSnapshotSet */
384 CComPtr<IVssAsync> pAsync1;
385 CComPtr<IVssAsync> pAsync2;
388 for (size_t i=0; i < strlen (szDriveLetters); i++) {
389 szDrive[0] = szDriveLetters[i];
390 volume = GetUniqueVolumeNameForPath(szDrive);
391 // store uniquevolumname
392 if (SUCCEEDED(pVss->AddToSnapshotSet((LPWSTR)volume.c_str(), GUID_NULL, &pid))) {
393 wcsncpy (m_wszUniqueVolumeName[szDriveLetters[i]-'A'], (LPWSTR) volume.c_str(), MAX_PATH);
395 szDriveLetters[i] = tolower (szDriveLetters[i]);
399 /* PrepareForBackup */
400 if (FAILED(pVss->PrepareForBackup(&pAsync1.p))) {
401 errno = b_errno_win32;
405 // Waits for the async operation to finish and checks the result
406 WaitAndCheckForAsyncOperation(pAsync1.p);
408 /* get latest info about writer status */
409 if (!CheckWriterStatus()) {
410 errno = b_errno_win32;
415 if (FAILED(pVss->DoSnapshotSet(&pAsync2.p))) {
416 errno = b_errno_win32;
420 // Waits for the async operation to finish and checks the result
421 WaitAndCheckForAsyncOperation(pAsync2.p);
423 /* query snapshot info */
424 QuerySnapshotSet(m_uidCurrentSnapshotSet);
426 SetVSSPathConvert(VSSPathConvert, VSSPathConvertW);
428 m_bBackupIsInitialized = true;
433 BOOL VSSClientGeneric::CloseBackup()
439 IVssBackupComponents* pVss = (IVssBackupComponents*) m_pVssObject;
440 CComPtr<IVssAsync> pAsync;
442 SetVSSPathConvert(NULL, NULL);
444 m_bBackupIsInitialized = false;
446 if (SUCCEEDED(pVss->BackupComplete(&pAsync.p))) {
447 // Waits for the async operation to finish and checks the result
448 WaitAndCheckForAsyncOperation(pAsync.p);
451 errno = b_errno_win32;
455 /* get latest info about writer status */
458 if (m_uidCurrentSnapshotSet != GUID_NULL) {
459 VSS_ID idNonDeletedSnapshotID = GUID_NULL;
462 pVss->DeleteSnapshots(
463 m_uidCurrentSnapshotSet,
464 VSS_OBJECT_SNAPSHOT_SET,
467 &idNonDeletedSnapshotID);
469 m_uidCurrentSnapshotSet = GUID_NULL;
476 // Call CoUninitialize if the CoInitialize was performed sucesfully
477 if (m_bCoInitializeCalled) {
479 m_bCoInitializeCalled = false;
485 // Query all the shadow copies in the given set
486 void VSSClientGeneric::QuerySnapshotSet(GUID snapshotSetID)
488 if (!(p_CreateVssBackupComponents && p_VssFreeSnapshotProperties)) {
493 memset (m_szShadowCopyName,0,sizeof (m_szShadowCopyName));
495 if (snapshotSetID == GUID_NULL || m_pVssObject == NULL) {
500 IVssBackupComponents* pVss = (IVssBackupComponents*) m_pVssObject;
502 // Get list all shadow copies.
503 CComPtr<IVssEnumObject> pIEnumSnapshots;
504 HRESULT hr = pVss->Query( GUID_NULL,
507 (IVssEnumObject**)(&pIEnumSnapshots) );
509 // If there are no shadow copies, just return
511 errno = b_errno_win32;
515 // Enumerate all shadow copies.
516 VSS_OBJECT_PROP Prop;
517 VSS_SNAPSHOT_PROP& Snap = Prop.Obj.Snap;
520 // Get the next element
522 hr = (pIEnumSnapshots.p)->Next( 1, &Prop, &ulFetched );
524 // We reached the end of list
528 // Print the shadow copy (if not filtered out)
529 if (Snap.m_SnapshotSetId == snapshotSetID) {
530 for (char ch='A'-'A';ch<='Z'-'A';ch++) {
531 if (wcscmp(Snap.m_pwszOriginalVolumeName, m_wszUniqueVolumeName[ch]) == 0) {
532 wcsncpy (m_szShadowCopyName[ch],Snap.m_pwszSnapshotDeviceObject, MAX_PATH-1);
537 p_VssFreeSnapshotProperties(&Snap);
542 // Check the status for all selected writers
543 BOOL VSSClientGeneric::CheckWriterStatus()
546 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vss/base/ivssbackupcomponents_startsnapshotset.asp
548 IVssBackupComponents* pVss = (IVssBackupComponents*) m_pVssObject;
551 // Gather writer status to detect potential errors
552 CComPtr<IVssAsync> pAsync;
554 HRESULT hr = pVss->GatherWriterStatus(&pAsync.p);
556 errno = b_errno_win32;
560 // Waits for the async operation to finish and checks the result
561 WaitAndCheckForAsyncOperation(pAsync.p);
563 unsigned cWriters = 0;
565 hr = pVss->GetWriterStatusCount(&cWriters);
567 errno = b_errno_win32;
573 // Enumerate each writer
574 for (unsigned iWriter = 0; iWriter < cWriters; iWriter++) {
575 VSS_ID idInstance = GUID_NULL;
576 VSS_ID idWriter= GUID_NULL;
577 VSS_WRITER_STATE eWriterStatus = VSS_WS_UNKNOWN;
578 CComBSTR bstrWriterName;
579 HRESULT hrWriterFailure = S_OK;
582 hr = pVss->GetWriterStatus(iWriter,
593 switch(eWriterStatus) {
594 case VSS_WS_FAILED_AT_IDENTIFY:
595 case VSS_WS_FAILED_AT_PREPARE_BACKUP:
596 case VSS_WS_FAILED_AT_PREPARE_SNAPSHOT:
597 case VSS_WS_FAILED_AT_FREEZE:
598 case VSS_WS_FAILED_AT_THAW:
599 case VSS_WS_FAILED_AT_POST_SNAPSHOT:
600 case VSS_WS_FAILED_AT_BACKUP_COMPLETE:
601 case VSS_WS_FAILED_AT_PRE_RESTORE:
602 case VSS_WS_FAILED_AT_POST_RESTORE:
604 case VSS_WS_FAILED_AT_BACKUPSHUTDOWN:
615 /* store text info */
618 bstrncpy(str, "\"", sizeof(str));
619 wchar_2_UTF8(szBuf, bstrWriterName.p, sizeof(szBuf));
620 bstrncat(str, szBuf, sizeof(str));
621 bstrncat(str, "\", State: 0x", sizeof(str));
622 itoa(eWriterStatus, szBuf, sizeof(szBuf));
623 bstrncat(str, szBuf, sizeof(str));
624 bstrncat(str, " (", sizeof(str));
625 wchar_2_UTF8(szBuf, GetStringFromWriterStatus(eWriterStatus), sizeof(szBuf));
626 bstrncat(str, szBuf, sizeof(str));
627 bstrncat(str, ")", sizeof(str));
629 AppendWriterInfo(nState, (const char *)str);
632 hr = pVss->FreeWriterStatus();
635 errno = b_errno_win32;
643 #endif /* WIN32_VSS */