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
33 VSSClient *g_pVSSClient;
39 delete (g_pVSSClient);
45 /* decide which vss class to initialize */
46 switch (g_MinorVersion) {
48 g_pVSSClient = new VSSClientXP();
52 g_pVSSClient = new VSSClient2003();
59 VSSPathConvert(const char *szFilePath, char *szShadowPath, int nBuflen)
61 return g_pVSSClient->GetShadowPath(szFilePath, szShadowPath, nBuflen);
65 VSSPathConvertW(const wchar_t *szFilePath, wchar_t *szShadowPath, int nBuflen)
67 return g_pVSSClient->GetShadowPathW(szFilePath, szShadowPath, nBuflen);
71 VSSClient::VSSClient()
73 m_bCoInitializeCalled = false;
74 m_bCoInitializeSecurityCalled = false;
75 m_dwContext = 0; // VSS_CTX_BACKUP;
76 m_bDuringRestore = false;
77 m_bBackupIsInitialized = false;
79 m_pAlistWriterState = New (alist(10, not_owned_by_alist));
80 m_pAlistWriterInfoText = New (alist(10, owned_by_alist));
81 m_uidCurrentSnapshotSet = GUID_NULL;
82 memset(m_wszUniqueVolumeName, 0, sizeof(m_wszUniqueVolumeName));
83 memset(m_szShadowCopyName, 0, sizeof(m_szShadowCopyName));
87 VSSClient::~VSSClient()
89 // Release the IVssBackupComponents interface
90 // WARNING: this must be done BEFORE calling CoUninitialize()
92 m_pVssObject->Release();
97 delete (alist*) m_pAlistWriterState;
98 delete (alist*) m_pAlistWriterInfoText;
100 // Call CoUninitialize if the CoInitialize was performed successfully
101 if (m_bCoInitializeCalled)
105 BOOL VSSClient::InitializeForBackup()
107 //return Initialize (VSS_CTX_BACKUP);
108 return Initialize(0);
114 BOOL VSSClient::GetShadowPath(const char *szFilePath, char *szShadowPath, int nBuflen)
116 if (!m_bBackupIsInitialized)
119 /* check for valid pathname */
122 bIsValidName = strlen(szFilePath) > 3;
124 bIsValidName &= isalpha (szFilePath[0]) &&
125 szFilePath[1]==':' &&
126 szFilePath[2] == '\\';
129 int nDriveIndex = toupper(szFilePath[0])-'A';
130 if (m_szShadowCopyName[nDriveIndex][0] != 0) {
132 if (WideCharToMultiByte(CP_UTF8,0,m_szShadowCopyName[nDriveIndex],-1,szShadowPath,nBuflen-1,NULL,NULL)) {
133 nBuflen -= (int)strlen(szShadowPath);
134 strncat(szShadowPath, szFilePath+2, nBuflen);
140 strncpy(szShadowPath, szFilePath, nBuflen);
145 BOOL VSSClient::GetShadowPathW(const wchar_t *szFilePath, wchar_t *szShadowPath, int nBuflen)
147 if (!m_bBackupIsInitialized)
150 /* check for valid pathname */
153 bIsValidName = wcslen(szFilePath) > 3;
155 bIsValidName &= iswalpha (szFilePath[0]) &&
156 szFilePath[1]==':' &&
157 szFilePath[2] == '\\';
160 int nDriveIndex = towupper(szFilePath[0])-'A';
161 if (m_szShadowCopyName[nDriveIndex][0] != 0) {
162 wcsncpy(szShadowPath, m_szShadowCopyName[nDriveIndex], nBuflen);
163 nBuflen -= (int)wcslen(m_szShadowCopyName[nDriveIndex]);
164 wcsncat(szShadowPath, szFilePath+2, nBuflen);
169 wcsncpy(szShadowPath, szFilePath, nBuflen);
175 const size_t VSSClient::GetWriterCount()
177 alist* pV = (alist*) m_pAlistWriterInfoText;
181 const char* VSSClient::GetWriterInfo(int nIndex)
183 alist* pV = (alist*) m_pAlistWriterInfoText;
184 return (char*) pV->get(nIndex);
188 const int VSSClient::GetWriterState(int nIndex)
190 alist* pV = (alist*) m_pAlistWriterState;
191 return (int) pV->get(nIndex);
194 void VSSClient::AppendWriterInfo(int nState, const char* pszInfo)
196 alist* pT = (alist*) m_pAlistWriterInfoText;
197 alist* pS = (alist*) m_pAlistWriterState;
199 pT->push (bstrdup(pszInfo));
200 pS->push ((void*) nState);
203 void VSSClient::DestroyWriterInfo()
205 alist* pT = (alist*) m_pAlistWriterInfoText;
206 alist* pS = (alist*) m_pAlistWriterState;