]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/vss.cpp
added VSS toggling by enable_vss
[bacula/bacula] / bacula / src / win32 / compat / vss.cpp
1 //                              -*- Mode: C++ -*-
2 // vss.cpp -- Interface to Volume Shadow Copies (VSS)
3 //
4 // Copyright transferred from MATRIX-Computer GmbH to
5 //   Kern Sibbald by express permission.
6 //
7 // Copyright (C) 2004-2005 Kern Sibbald
8 //
9 //   This program is free software; you can redistribute it and/or
10 //   modify it under the terms of the GNU General Public License as
11 //   published by the Free Software Foundation; either version 2 of
12 //   the License, or (at your option) any later version.
13 //
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 GNU
17 //   General Public License for more details.
18 //
19 //   You should have received a copy of the GNU General Public
20 //   License along with this program; if not, write to the Free
21 //   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 //   MA 02111-1307, USA.
23 //
24 // Author          : Thorsten Engel
25 // Created On      : Fri May 06 21:44:00 2006
26
27
28 #include <stdio.h>
29 #include <basetsd.h>
30 #include <stdarg.h>
31 #include <sys/types.h>
32 #include <process.h>
33 #include <direct.h>
34 #include <winsock2.h>
35 #include <windows.h>
36 #include <wincon.h>
37 #include <winbase.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <conio.h>
42 #include <process.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <time.h>
46 #include <signal.h>
47 #include <malloc.h>
48 #include <setjmp.h>
49 #include <direct.h>
50 #include <ctype.h>
51 #include <fcntl.h>
52 #include <io.h>
53
54
55 // STL includes
56 #include <vector>
57 #include <algorithm>
58 #include <string>
59 #include <fstream>
60 using namespace std;   
61
62 #include <atlcomcli.h>
63 #include <objbase.h>
64
65
66 // Used for safe string manipulation
67 #include <strsafe.h>
68 #include "vss.h"
69
70
71 #pragma comment(lib,"atlsd.lib")
72
73
74
75 // Constructor
76 VSSClient::VSSClient()
77 {
78     m_bCoInitializeCalled = false;
79     m_dwContext = 0; // VSS_CTX_BACKUP;
80     m_bDuringRestore = false;
81     m_bBackupIsInitialized = false;
82     m_pVssObject = NULL;
83     m_pVectorWriterStates = new vector<int>;
84     m_pVectorWriterInfo = new vector<string>;
85     m_uidCurrentSnapshotSet = GUID_NULL;
86     memset (m_wszUniqueVolumeName,0,sizeof (m_wszUniqueVolumeName));
87     memset (m_szShadowCopyName,0,sizeof (m_szShadowCopyName));
88 }
89
90 // Destructor
91 VSSClient::~VSSClient()
92 {
93    // Release the IVssBackupComponents interface 
94    // WARNING: this must be done BEFORE calling CoUninitialize()
95    if (m_pVssObject) {
96       m_pVssObject->Release();
97       m_pVssObject = NULL;
98    }
99
100    if (m_pVectorWriterStates)
101       delete (m_pVectorWriterStates);
102
103    if (m_pVectorWriterInfo)
104       delete (m_pVectorWriterInfo);
105
106    // Call CoUninitialize if the CoInitialize was performed sucesfully
107    if (m_bCoInitializeCalled)
108       CoUninitialize();
109 }
110
111 BOOL VSSClient::InitializeForBackup()
112 {
113     //return Initialize (VSS_CTX_BACKUP);
114    return Initialize (0);
115 }
116
117
118
119
120 BOOL VSSClient::GetShadowPath (const char* szFilePath, char* szShadowPath, int nBuflen)
121 {
122    if (!m_bBackupIsInitialized)
123       return FALSE;
124
125    /* check for valid pathname */
126    BOOL bIsValidName;
127    
128    bIsValidName = strlen (szFilePath) > 3;
129    if (bIsValidName)
130       bIsValidName &= isalpha (szFilePath[0]) &&
131                       szFilePath[1]==':' && 
132                       szFilePath[2]=='\\';
133
134    if (bIsValidName) {
135       int nDriveIndex = toupper(szFilePath[0])-'A';
136       if (m_szShadowCopyName[nDriveIndex][0] != 0) {
137          strncpy (szShadowPath, m_szShadowCopyName[nDriveIndex], nBuflen);
138          nBuflen -= (int) strlen (m_szShadowCopyName[nDriveIndex]);
139          strncat (szShadowPath, szFilePath+2,nBuflen);
140
141          return TRUE;
142       }
143    }
144    
145    strncpy (szShadowPath,  szFilePath, nBuflen);
146    return FALSE;   
147 }
148
149
150 const size_t VSSClient::GetWriterCount()
151 {
152    vector<int>* pV = (vector<int>*) m_pVectorWriterStates;
153    return pV->size();
154 }
155
156 const char* VSSClient::GetWriterInfo(size_t nIndex)
157 {
158    vector<string>* pV = (vector<string>*) m_pVectorWriterInfo;   
159    return pV->at(nIndex).c_str();
160 }
161
162
163 const int VSSClient::GetWriterState(size_t nIndex)
164 {
165    vector<int>* pV = (vector<int>*) m_pVectorWriterStates;   
166    return pV->at(nIndex);
167 }