]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/vss.cpp
small vss improvement (more complete shutdown and startup after each backup instead...
[bacula/bacula] / bacula / src / win32 / compat / vss.cpp
1
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) 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
11 //  version 2 as amended with additional clauses defined in the
12 //  file LICENSE in the main source directory.
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 
17 //  the file LICENSE for additional details.
18 //
19 //  
20 // Author          : Thorsten Engel
21 // Created On      : Fri May 06 21:44:00 2005
22
23
24 #include <stdio.h>
25 #include <basetsd.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #include <process.h>
29 #include <direct.h>
30 #include <winsock2.h>
31 #include <windows.h>
32 #include <wincon.h>
33 #include <winbase.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <conio.h>
38 #include <process.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <time.h>
42 #include <signal.h>
43 #include <malloc.h>
44 #include <setjmp.h>
45 #include <direct.h>
46 #include <ctype.h>
47 #include <fcntl.h>
48 #include <io.h>
49
50
51 // STL includes
52 #include <vector>
53 #include <algorithm>
54 #include <string>
55 #include <fstream>
56 using namespace std;   
57
58 #include <atlcomcli.h>
59 #include <objbase.h>
60
61
62 // Used for safe string manipulation
63 #include <strsafe.h>
64 #include "vss.h"
65
66
67 #pragma comment(lib,"atlsd.lib")
68
69
70
71 // Constructor
72 VSSClient::VSSClient()
73 {
74     m_bCoInitializeCalled = false;
75     m_bCoInitializeSecurityCalled = false;
76     m_dwContext = 0; // VSS_CTX_BACKUP;
77     m_bDuringRestore = false;
78     m_bBackupIsInitialized = false;
79     m_pVssObject = NULL;
80     m_pVectorWriterStates = new vector<int>;
81     m_pVectorWriterInfo = new vector<string>;
82     m_uidCurrentSnapshotSet = GUID_NULL;
83     memset(m_wszUniqueVolumeName,0, sizeof(m_wszUniqueVolumeName));
84     memset(m_szShadowCopyName,0, sizeof(m_szShadowCopyName));
85 }
86
87 // Destructor
88 VSSClient::~VSSClient()
89 {
90    // Release the IVssBackupComponents interface 
91    // WARNING: this must be done BEFORE calling CoUninitialize()
92    if (m_pVssObject) {
93       m_pVssObject->Release();
94       m_pVssObject = NULL;
95    }
96
97    if (m_pVectorWriterStates)
98       delete (m_pVectorWriterStates);
99
100    if (m_pVectorWriterInfo)
101       delete (m_pVectorWriterInfo);
102
103    // Call CoUninitialize if the CoInitialize was performed sucesfully
104    if (m_bCoInitializeCalled)
105       CoUninitialize();
106 }
107
108 BOOL VSSClient::InitializeForBackup()
109 {
110     //return Initialize (VSS_CTX_BACKUP);
111    return Initialize(0);
112 }
113
114
115
116
117 BOOL VSSClient::GetShadowPath(const char *szFilePath, char *szShadowPath, int nBuflen)
118 {
119    if (!m_bBackupIsInitialized)
120       return FALSE;
121
122    /* check for valid pathname */
123    BOOL bIsValidName;
124    
125    bIsValidName = strlen(szFilePath) > 3;
126    if (bIsValidName)
127       bIsValidName &= isalpha (szFilePath[0]) &&
128                       szFilePath[1]==':' && 
129                       szFilePath[2] == '\\';
130
131    if (bIsValidName) {
132       int nDriveIndex = toupper(szFilePath[0])-'A';
133       if (m_szShadowCopyName[nDriveIndex][0] != 0) {
134          strncpy(szShadowPath, m_szShadowCopyName[nDriveIndex], nBuflen);
135          nBuflen -= (int)strlen(m_szShadowCopyName[nDriveIndex]);
136          strncat(szShadowPath, szFilePath+2, nBuflen);
137          return TRUE;
138       }
139    }
140    
141    strncpy(szShadowPath,  szFilePath, nBuflen);
142    errno = EINVAL;
143    return FALSE;   
144 }
145
146
147 const size_t VSSClient::GetWriterCount()
148 {
149    vector<int>* pV = (vector<int>*) m_pVectorWriterStates;
150    return pV->size();
151 }
152
153 const char* VSSClient::GetWriterInfo(size_t nIndex)
154 {
155    vector<string>* pV = (vector<string>*) m_pVectorWriterInfo;   
156    return pV->at(nIndex).c_str();
157 }
158
159
160 const int VSSClient::GetWriterState(size_t nIndex)
161 {
162    vector<int>* pV = (vector<int>*) m_pVectorWriterStates;   
163    return pV->at(nIndex);
164 }