]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/winapi.c
improved vss support (goal: only 1 binary, still problems on w2k3, xp seems to work)
[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 /* preset VSSClient to NULL */
41 VSSClient* g_pVSSClient = NULL;
42
43
44 /* API Pointers */
45
46 t_OpenProcessToken      p_OpenProcessToken = NULL;
47 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
48 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
49
50 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
51
52 t_CreateFileA   p_CreateFileA = NULL;
53 t_CreateFileW   p_CreateFileW = NULL;
54
55 t_wunlink p_wunlink = NULL;
56 t_wmkdir p_wmkdir = NULL;
57 t_wopen p_wopen = NULL;
58
59 t_GetFileAttributesA    p_GetFileAttributesA = NULL;
60 t_GetFileAttributesW    p_GetFileAttributesW = NULL;
61
62 t_GetFileAttributesExA  p_GetFileAttributesExA = NULL;
63 t_GetFileAttributesExW  p_GetFileAttributesExW = NULL;
64
65 t_SetFileAttributesA    p_SetFileAttributesA = NULL;
66 t_SetFileAttributesW    p_SetFileAttributesW = NULL;
67 t_BackupRead            p_BackupRead = NULL;
68 t_BackupWrite           p_BackupWrite = NULL;
69 t_WideCharToMultiByte p_WideCharToMultiByte = NULL;
70 t_MultiByteToWideChar p_MultiByteToWideChar = NULL;
71
72 t_FindFirstFileA p_FindFirstFileA = NULL;
73 t_FindFirstFileW p_FindFirstFileW = NULL;
74
75 t_FindNextFileA p_FindNextFileA = NULL;
76 t_FindNextFileW p_FindNextFileW = NULL;
77
78 t_SetCurrentDirectoryA p_SetCurrentDirectoryA = NULL;
79 t_SetCurrentDirectoryW p_SetCurrentDirectoryW = NULL;
80
81 t_GetCurrentDirectoryA p_GetCurrentDirectoryA = NULL;
82 t_GetCurrentDirectoryW p_GetCurrentDirectoryW = NULL;
83
84 #ifdef WIN32_VSS
85 void 
86 VSSCleanup()
87 {
88    if (g_pVSSClient)
89       delete (g_pVSSClient);
90 }
91 #endif
92
93 void 
94 InitWinAPIWrapper() 
95 {
96    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
97    if (hLib) {
98       /* create file calls */
99       p_CreateFileA = (t_CreateFileA)
100           GetProcAddress(hLib, "CreateFileA");
101       p_CreateFileW = (t_CreateFileW)
102           GetProcAddress(hLib, "CreateFileW");      
103
104       /* attribute calls */
105       p_GetFileAttributesA = (t_GetFileAttributesA)
106           GetProcAddress(hLib, "GetFileAttributesA");
107       p_GetFileAttributesW = (t_GetFileAttributesW)
108           GetProcAddress(hLib, "GetFileAttributesW");
109       p_GetFileAttributesExA = (t_GetFileAttributesExA)
110           GetProcAddress(hLib, "GetFileAttributesExA");
111       p_GetFileAttributesExW = (t_GetFileAttributesExW)
112           GetProcAddress(hLib, "GetFileAttributesExW");
113       p_SetFileAttributesA = (t_SetFileAttributesA)
114           GetProcAddress(hLib, "SetFileAttributesA");
115       p_SetFileAttributesW = (t_SetFileAttributesW)
116           GetProcAddress(hLib, "SetFileAttributesW");
117       /* process calls */
118       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
119           GetProcAddress(hLib, "SetProcessShutdownParameters");
120       /* backup calls */
121       p_BackupRead = (t_BackupRead)
122           GetProcAddress(hLib, "BackupRead");
123       p_BackupWrite = (t_BackupWrite)
124           GetProcAddress(hLib, "BackupWrite");
125       /* char conversion calls */
126       p_WideCharToMultiByte = (t_WideCharToMultiByte)
127           GetProcAddress(hLib, "WideCharToMultiByte");
128       p_MultiByteToWideChar = (t_MultiByteToWideChar)
129           GetProcAddress(hLib, "MultiByteToWideChar");
130
131       /* find files */
132       p_FindFirstFileA = (t_FindFirstFileA)
133           GetProcAddress(hLib, "FindFirstFileA"); 
134       p_FindFirstFileW = (t_FindFirstFileW)
135           GetProcAddress(hLib, "FindFirstFileW");       
136       p_FindNextFileA = (t_FindNextFileA)
137           GetProcAddress(hLib, "FindNextFileA");
138       p_FindNextFileW = (t_FindNextFileW)
139           GetProcAddress(hLib, "FindNextFileW");
140       /* set and get directory */
141       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
142           GetProcAddress(hLib, "SetCurrentDirectoryA");
143       p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
144           GetProcAddress(hLib, "SetCurrentDirectoryW");       
145       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
146           GetProcAddress(hLib, "GetCurrentDirectoryA");
147       p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
148           GetProcAddress(hLib, "GetCurrentDirectoryW");      
149       FreeLibrary(hLib);
150    }
151    
152    hLib = LoadLibraryA("MSVCRT.DLL");
153    if (hLib) {
154       /* unlink */
155       p_wunlink = (t_wunlink)
156       GetProcAddress(hLib, "_wunlink");
157       /* wmkdir */
158       p_wmkdir = (t_wmkdir)
159       GetProcAddress(hLib, "_wmkdir");
160       /* wopen */
161       p_wopen = (t_wopen)
162       GetProcAddress(hLib, "_wopen");
163         
164       FreeLibrary(hLib);
165    }
166    
167    hLib = LoadLibraryA("ADVAPI32.DLL");
168    if (hLib) {
169       p_OpenProcessToken = (t_OpenProcessToken)
170          GetProcAddress(hLib, "OpenProcessToken");
171       p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
172          GetProcAddress(hLib, "AdjustTokenPrivileges");
173       p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
174          GetProcAddress(hLib, "LookupPrivilegeValueA");
175       FreeLibrary(hLib);
176    }
177
178    // do we run on win 9x ???
179    OSVERSIONINFO osversioninfo;   
180    osversioninfo.dwOSVersionInfoSize = sizeof(osversioninfo);
181
182    DWORD dwMinorVersion;
183
184    // Get the current OS version
185    if (!GetVersionEx(&osversioninfo)) {
186       g_platform_id = 0;
187       dwMinorVersion = 0;
188    } else {
189       g_platform_id = osversioninfo.dwPlatformId;
190       dwMinorVersion = osversioninfo.dwMinorVersion;
191    }
192
193    /* deinitialize some routines on win95 - they're not implemented well */
194    if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
195       p_BackupRead = NULL;
196       p_BackupWrite = NULL;
197
198       p_CreateFileW = NULL;          
199       p_GetFileAttributesW = NULL;          
200       p_GetFileAttributesExW = NULL;
201           
202       p_SetFileAttributesW = NULL;
203                 
204       p_FindFirstFileW = NULL;
205       p_FindNextFileW = NULL;
206       p_SetCurrentDirectoryW = NULL;
207       p_GetCurrentDirectoryW = NULL;
208
209       p_wunlink = NULL;
210       p_wmkdir = NULL;
211       p_wopen = NULL;
212    }   
213
214    /* decide which vss class to initialize */
215 #ifdef WIN32_VSS
216    switch (dwMinorVersion) {
217       case 1: 
218          g_pVSSClient = new VSSClientXP();
219          atexit(VSSCleanup);
220          break;
221       case 2: 
222          g_pVSSClient = new VSSClient2003();
223          atexit(VSSCleanup);
224          break;
225    }
226 #endif
227 }
228
229 #endif