]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/vss.cpp
improved vss support (goal: only 1 binary, still problems on w2k3, xp seems to work)
[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     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    // Call CoUninitialize if the CoInitialize was performed sucesfully
98    if (m_bCoInitializeCalled)
99       CoUninitialize();
100 }
101
102 BOOL VSSClient::InitializeForBackup()
103 {
104     //return Initialize (VSS_CTX_BACKUP);
105    return Initialize (0);
106 }
107
108
109
110
111 BOOL VSSClient::GetShadowPath (const char* szFilePath, char* szShadowPath, int nBuflen)
112 {
113    if (!m_bBackupIsInitialized)
114       return FALSE;
115
116    /* check for valid pathname */
117    BOOL bIsValidName;
118    
119    bIsValidName = strlen (szFilePath) > 3;
120    if (bIsValidName)
121       bIsValidName &= isalpha (szFilePath[0]) &&
122                       szFilePath[1]==':' && 
123                       szFilePath[2]=='\\';
124
125    if (bIsValidName) {
126       int nDriveIndex = toupper(szFilePath[0])-'A';
127       if (m_szShadowCopyName[nDriveIndex][0] != 0) {
128          strncpy (szShadowPath, m_szShadowCopyName[nDriveIndex], nBuflen);
129          nBuflen -= (int) strlen (m_szShadowCopyName[nDriveIndex]);
130          strncat (szShadowPath, szFilePath+2,nBuflen);
131
132          return TRUE;
133       }
134    }
135    
136    strncpy (szShadowPath,  szFilePath, nBuflen);
137    return FALSE;   
138 }