]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/winapi.c
- Add code to ensure that reserved but unused volumes
[bacula/bacula] / bacula / src / lib / winapi.c
1 /*
2  * Windows APIs that are different for each system.
3  *   We use pointers to the entry points so that a
4  *   single binary will run on all Windows systems.
5  *
6  *     Kern Sibbald MMIII
7  */
8 /*
9    Copyright (C) 2000-2005 Kern Sibbald
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29
30 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
31
32 #include "winapi.h"
33
34 #ifdef WIN32_VSS
35 #include "vss.h"   
36 #endif
37
38 // init with win9x, but maybe set to NT in InitWinAPI
39 DWORD  g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
40 #ifdef WIN32_VSS
41 /* preset VSSClient to NULL */
42 VSSClient* g_pVSSClient = NULL;
43 #endif
44
45
46 /* API Pointers */
47
48 t_OpenProcessToken      p_OpenProcessToken = NULL;
49 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
50 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
51
52 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
53
54 t_CreateFileA   p_CreateFileA = NULL;
55 t_CreateFileW   p_CreateFileW = NULL;
56
57 t_wunlink p_wunlink = NULL;
58 t_wmkdir p_wmkdir = NULL;
59 t_wopen p_wopen = NULL;
60
61 t_GetFileAttributesA    p_GetFileAttributesA = NULL;
62 t_GetFileAttributesW    p_GetFileAttributesW = NULL;
63
64 t_GetFileAttributesExA  p_GetFileAttributesExA = NULL;
65 t_GetFileAttributesExW  p_GetFileAttributesExW = NULL;
66
67 t_SetFileAttributesA    p_SetFileAttributesA = NULL;
68 t_SetFileAttributesW    p_SetFileAttributesW = NULL;
69 t_BackupRead            p_BackupRead = NULL;
70 t_BackupWrite           p_BackupWrite = NULL;
71 t_WideCharToMultiByte p_WideCharToMultiByte = NULL;
72 t_MultiByteToWideChar p_MultiByteToWideChar = NULL;
73
74 t_FindFirstFileA p_FindFirstFileA = NULL;
75 t_FindFirstFileW p_FindFirstFileW = NULL;
76
77 t_FindNextFileA p_FindNextFileA = NULL;
78 t_FindNextFileW p_FindNextFileW = NULL;
79
80 t_SetCurrentDirectoryA p_SetCurrentDirectoryA = NULL;
81 t_SetCurrentDirectoryW p_SetCurrentDirectoryW = NULL;
82
83 t_GetCurrentDirectoryA p_GetCurrentDirectoryA = NULL;
84 t_GetCurrentDirectoryW p_GetCurrentDirectoryW = NULL;
85
86 #ifdef WIN32_VSS
87 void 
88 VSSCleanup()
89 {
90    if (g_pVSSClient)
91       delete (g_pVSSClient);
92 }
93 #endif
94
95 void 
96 InitWinAPIWrapper() 
97 {
98    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
99    if (hLib) {
100       /* create file calls */
101       p_CreateFileA = (t_CreateFileA)
102           GetProcAddress(hLib, "CreateFileA");
103       p_CreateFileW = (t_CreateFileW)
104           GetProcAddress(hLib, "CreateFileW");      
105
106       /* attribute calls */
107       p_GetFileAttributesA = (t_GetFileAttributesA)
108           GetProcAddress(hLib, "GetFileAttributesA");
109       p_GetFileAttributesW = (t_GetFileAttributesW)
110           GetProcAddress(hLib, "GetFileAttributesW");
111       p_GetFileAttributesExA = (t_GetFileAttributesExA)
112           GetProcAddress(hLib, "GetFileAttributesExA");
113       p_GetFileAttributesExW = (t_GetFileAttributesExW)
114           GetProcAddress(hLib, "GetFileAttributesExW");
115       p_SetFileAttributesA = (t_SetFileAttributesA)
116           GetProcAddress(hLib, "SetFileAttributesA");
117       p_SetFileAttributesW = (t_SetFileAttributesW)
118           GetProcAddress(hLib, "SetFileAttributesW");
119       /* process calls */
120       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
121           GetProcAddress(hLib, "SetProcessShutdownParameters");
122       /* backup calls */
123       p_BackupRead = (t_BackupRead)
124           GetProcAddress(hLib, "BackupRead");
125       p_BackupWrite = (t_BackupWrite)
126           GetProcAddress(hLib, "BackupWrite");
127       /* char conversion calls */
128       p_WideCharToMultiByte = (t_WideCharToMultiByte)
129           GetProcAddress(hLib, "WideCharToMultiByte");
130       p_MultiByteToWideChar = (t_MultiByteToWideChar)
131           GetProcAddress(hLib, "MultiByteToWideChar");
132
133       /* find files */
134       p_FindFirstFileA = (t_FindFirstFileA)
135           GetProcAddress(hLib, "FindFirstFileA"); 
136       p_FindFirstFileW = (t_FindFirstFileW)
137           GetProcAddress(hLib, "FindFirstFileW");       
138       p_FindNextFileA = (t_FindNextFileA)
139           GetProcAddress(hLib, "FindNextFileA");
140       p_FindNextFileW = (t_FindNextFileW)
141           GetProcAddress(hLib, "FindNextFileW");
142       /* set and get directory */
143       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
144           GetProcAddress(hLib, "SetCurrentDirectoryA");
145       p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
146           GetProcAddress(hLib, "SetCurrentDirectoryW");       
147       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
148           GetProcAddress(hLib, "GetCurrentDirectoryA");
149       p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
150           GetProcAddress(hLib, "GetCurrentDirectoryW");      
151       FreeLibrary(hLib);
152    }
153    
154    hLib = LoadLibraryA("MSVCRT.DLL");
155    if (hLib) {
156       /* unlink */
157       p_wunlink = (t_wunlink)
158       GetProcAddress(hLib, "_wunlink");
159       /* wmkdir */
160       p_wmkdir = (t_wmkdir)
161       GetProcAddress(hLib, "_wmkdir");
162       /* wopen */
163       p_wopen = (t_wopen)
164       GetProcAddress(hLib, "_wopen");
165         
166       FreeLibrary(hLib);
167    }
168    
169    hLib = LoadLibraryA("ADVAPI32.DLL");
170    if (hLib) {
171       p_OpenProcessToken = (t_OpenProcessToken)
172          GetProcAddress(hLib, "OpenProcessToken");
173       p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
174          GetProcAddress(hLib, "AdjustTokenPrivileges");
175       p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
176          GetProcAddress(hLib, "LookupPrivilegeValueA");
177       FreeLibrary(hLib);
178    }
179
180    // do we run on win 9x ???
181    OSVERSIONINFO osversioninfo;   
182    osversioninfo.dwOSVersionInfoSize = sizeof(osversioninfo);
183
184    DWORD dwMinorVersion;
185
186    // Get the current OS version
187    if (!GetVersionEx(&osversioninfo)) {
188       g_platform_id = 0;
189       dwMinorVersion = 0;
190    } else {
191       g_platform_id = osversioninfo.dwPlatformId;
192       dwMinorVersion = osversioninfo.dwMinorVersion;
193    }
194
195    /* deinitialize some routines on win95 - they're not implemented well */
196    if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
197       p_BackupRead = NULL;
198       p_BackupWrite = NULL;
199
200       p_CreateFileW = NULL;          
201       p_GetFileAttributesW = NULL;          
202       p_GetFileAttributesExW = NULL;
203           
204       p_SetFileAttributesW = NULL;
205                 
206       p_FindFirstFileW = NULL;
207       p_FindNextFileW = NULL;
208       p_SetCurrentDirectoryW = NULL;
209       p_GetCurrentDirectoryW = NULL;
210
211       p_wunlink = NULL;
212       p_wmkdir = NULL;
213       p_wopen = NULL;
214    }   
215
216    /* decide which vss class to initialize */
217 #ifdef WIN32_VSS
218    switch (dwMinorVersion) {
219       case 1: 
220          g_pVSSClient = new VSSClientXP();
221          atexit(VSSCleanup);
222          break;
223       case 2: 
224          g_pVSSClient = new VSSClient2003();
225          atexit(VSSCleanup);
226          break;
227    }
228 #endif
229 }
230
231 #endif