]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/vss.cpp
- Add check for df path for dvd_freespace
[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) 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_dwContext = 0; // VSS_CTX_BACKUP;
76     m_bDuringRestore = false;
77     m_bBackupIsInitialized = false;
78     m_pVssObject = NULL;
79     m_pVectorWriterStates = new vector<int>;
80     m_pVectorWriterInfo = new vector<string>;
81     m_uidCurrentSnapshotSet = GUID_NULL;
82     memset (m_wszUniqueVolumeName,0,sizeof (m_wszUniqueVolumeName));
83     memset (m_szShadowCopyName,0,sizeof (m_szShadowCopyName));
84 }
85
86 // Destructor
87 VSSClient::~VSSClient()
88 {
89    // Release the IVssBackupComponents interface 
90    // WARNING: this must be done BEFORE calling CoUninitialize()
91    if (m_pVssObject) {
92       m_pVssObject->Release();
93       m_pVssObject = NULL;
94    }
95
96    if (m_pVectorWriterStates)
97       delete (m_pVectorWriterStates);
98
99    if (m_pVectorWriterInfo)
100       delete (m_pVectorWriterInfo);
101
102    // Call CoUninitialize if the CoInitialize was performed sucesfully
103    if (m_bCoInitializeCalled)
104       CoUninitialize();
105 }
106
107 BOOL VSSClient::InitializeForBackup()
108 {
109     //return Initialize (VSS_CTX_BACKUP);
110    return Initialize (0);
111 }
112
113
114
115
116 BOOL VSSClient::GetShadowPath (const char* szFilePath, char* szShadowPath, int nBuflen)
117 {
118    if (!m_bBackupIsInitialized)
119       return FALSE;
120
121    /* check for valid pathname -- note, allow simple drive letter too */
122    BOOL bIsValidName;
123    
124    bIsValidName = strlen(szFilePath) >= 2;
125    if (bIsValidName)
126       bIsValidName &= isalpha (szFilePath[0]) &&
127                       szFilePath[1]==':';
128
129    if (bIsValidName) {
130       int nDriveIndex = toupper(szFilePath[0])-'A';
131       if (m_szShadowCopyName[nDriveIndex][0] != 0) {
132          strncpy (szShadowPath, m_szShadowCopyName[nDriveIndex], nBuflen);
133          nBuflen -= (int) strlen (m_szShadowCopyName[nDriveIndex]);
134          strncat (szShadowPath, szFilePath+2,nBuflen);
135
136          return TRUE;
137       }
138    }
139    
140    strncpy (szShadowPath,  szFilePath, nBuflen);
141    return FALSE;   
142 }
143
144
145 const size_t VSSClient::GetWriterCount()
146 {
147    vector<int>* pV = (vector<int>*) m_pVectorWriterStates;
148    return pV->size();
149 }
150
151 const char* VSSClient::GetWriterInfo(size_t nIndex)
152 {
153    vector<string>* pV = (vector<string>*) m_pVectorWriterInfo;   
154    return pV->at(nIndex).c_str();
155 }
156
157
158 const int VSSClient::GetWriterState(size_t nIndex)
159 {
160    vector<int>* pV = (vector<int>*) m_pVectorWriterStates;   
161    return pV->at(nIndex);
162 }