]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/winapi.c
Tweak Win32 build
[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) 2003-2006 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
13    version 2 as amended with additional clauses defined in the
14    file LICENSE in the main source directory.
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 
19    the file LICENSE for additional details.
20
21  */
22
23 #include "bacula.h"
24
25 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
26
27 #include "winapi.h"
28
29 #ifdef WIN32_VSS
30 #include "vss.h"   
31 #endif
32
33 // init with win9x, but maybe set to NT in InitWinAPI
34 DWORD  g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
35 #ifdef WIN32_VSS
36 /* preset VSSClient to NULL */
37 VSSClient *g_pVSSClient = NULL;
38 #endif
39
40
41 /* API Pointers */
42
43 t_OpenProcessToken      p_OpenProcessToken = NULL;
44 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
45 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
46
47 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
48
49 t_CreateFileA   p_CreateFileA = NULL;
50 t_CreateFileW   p_CreateFileW = NULL;
51 t_CreateDirectoryA   p_CreateDirectoryA;
52 t_CreateDirectoryW   p_CreateDirectoryW;
53
54 t_wunlink p_wunlink = NULL;
55 t_wmkdir p_wmkdir = NULL;
56 t_wopen p_wopen = NULL;
57
58 t_GetFileAttributesA    p_GetFileAttributesA = NULL;
59 t_GetFileAttributesW    p_GetFileAttributesW = NULL;
60
61 t_GetFileAttributesExA  p_GetFileAttributesExA = NULL;
62 t_GetFileAttributesExW  p_GetFileAttributesExW = NULL;
63
64 t_SetFileAttributesA    p_SetFileAttributesA = NULL;
65 t_SetFileAttributesW    p_SetFileAttributesW = NULL;
66 t_BackupRead            p_BackupRead = NULL;
67 t_BackupWrite           p_BackupWrite = NULL;
68 t_WideCharToMultiByte p_WideCharToMultiByte = NULL;
69 t_MultiByteToWideChar p_MultiByteToWideChar = NULL;
70
71 t_FindFirstFileA p_FindFirstFileA = NULL;
72 t_FindFirstFileW p_FindFirstFileW = NULL;
73
74 t_FindNextFileA p_FindNextFileA = NULL;
75 t_FindNextFileW p_FindNextFileW = NULL;
76
77 t_SetCurrentDirectoryA p_SetCurrentDirectoryA = NULL;
78 t_SetCurrentDirectoryW p_SetCurrentDirectoryW = NULL;
79
80 t_GetCurrentDirectoryA p_GetCurrentDirectoryA = NULL;
81 t_GetCurrentDirectoryW p_GetCurrentDirectoryW = NULL;
82
83 t_GetVolumePathNameW p_GetVolumePathNameW = NULL;
84 t_GetVolumeNameForVolumeMountPointW p_GetVolumeNameForVolumeMountPointW = 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       p_CreateDirectoryA = (t_CreateDirectoryA)
107           GetProcAddress(hLib, "CreateDirectoryA");
108       p_CreateDirectoryW = (t_CreateDirectoryW)
109           GetProcAddress(hLib, "CreateDirectoryW");      
110
111       /* attribute calls */
112       p_GetFileAttributesA = (t_GetFileAttributesA)
113           GetProcAddress(hLib, "GetFileAttributesA");
114       p_GetFileAttributesW = (t_GetFileAttributesW)
115           GetProcAddress(hLib, "GetFileAttributesW");
116       p_GetFileAttributesExA = (t_GetFileAttributesExA)
117           GetProcAddress(hLib, "GetFileAttributesExA");
118       p_GetFileAttributesExW = (t_GetFileAttributesExW)
119           GetProcAddress(hLib, "GetFileAttributesExW");
120       p_SetFileAttributesA = (t_SetFileAttributesA)
121           GetProcAddress(hLib, "SetFileAttributesA");
122       p_SetFileAttributesW = (t_SetFileAttributesW)
123           GetProcAddress(hLib, "SetFileAttributesW");
124       /* process calls */
125       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
126           GetProcAddress(hLib, "SetProcessShutdownParameters");
127       /* backup calls */
128       p_BackupRead = (t_BackupRead)
129           GetProcAddress(hLib, "BackupRead");
130       p_BackupWrite = (t_BackupWrite)
131           GetProcAddress(hLib, "BackupWrite");
132       /* char conversion calls */
133       p_WideCharToMultiByte = (t_WideCharToMultiByte)
134           GetProcAddress(hLib, "WideCharToMultiByte");
135       p_MultiByteToWideChar = (t_MultiByteToWideChar)
136           GetProcAddress(hLib, "MultiByteToWideChar");
137
138       /* find files */
139       p_FindFirstFileA = (t_FindFirstFileA)
140           GetProcAddress(hLib, "FindFirstFileA"); 
141       p_FindFirstFileW = (t_FindFirstFileW)
142           GetProcAddress(hLib, "FindFirstFileW");       
143       p_FindNextFileA = (t_FindNextFileA)
144           GetProcAddress(hLib, "FindNextFileA");
145       p_FindNextFileW = (t_FindNextFileW)
146           GetProcAddress(hLib, "FindNextFileW");
147       /* set and get directory */
148       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
149           GetProcAddress(hLib, "SetCurrentDirectoryA");
150       p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
151           GetProcAddress(hLib, "SetCurrentDirectoryW");       
152       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
153           GetProcAddress(hLib, "GetCurrentDirectoryA");
154       p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
155           GetProcAddress(hLib, "GetCurrentDirectoryW");      
156
157       /* some special stuff we need for VSS
158          but statically linkage doesn't work on Win 9x */
159       p_GetVolumePathNameW = (t_GetVolumePathNameW)
160           GetProcAddress(hLib, "GetVolumePathNameW");
161       p_GetVolumeNameForVolumeMountPointW = (t_GetVolumeNameForVolumeMountPointW)
162           GetProcAddress(hLib, "GetVolumeNameForVolumeMountPointW");
163     
164       FreeLibrary(hLib);
165    }
166    
167    hLib = LoadLibraryA("MSVCRT.DLL");
168    if (hLib) {
169       /* unlink */
170       p_wunlink = (t_wunlink)
171       GetProcAddress(hLib, "_wunlink");
172       /* wmkdir */
173       p_wmkdir = (t_wmkdir)
174       GetProcAddress(hLib, "_wmkdir");
175       /* wopen */
176       p_wopen = (t_wopen)
177       GetProcAddress(hLib, "_wopen");
178         
179       FreeLibrary(hLib);
180    }
181    
182    hLib = LoadLibraryA("ADVAPI32.DLL");
183    if (hLib) {
184       p_OpenProcessToken = (t_OpenProcessToken)
185          GetProcAddress(hLib, "OpenProcessToken");
186       p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
187          GetProcAddress(hLib, "AdjustTokenPrivileges");
188       p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
189          GetProcAddress(hLib, "LookupPrivilegeValueA");
190       FreeLibrary(hLib);
191    }
192
193    // do we run on win 9x ???
194    OSVERSIONINFO osversioninfo;   
195    osversioninfo.dwOSVersionInfoSize = sizeof(osversioninfo);
196
197    DWORD dwMinorVersion;
198
199    // Get the current OS version
200    if (!GetVersionEx(&osversioninfo)) {
201       g_platform_id = 0;
202       dwMinorVersion = 0;
203    } else {
204       g_platform_id = osversioninfo.dwPlatformId;
205       dwMinorVersion = osversioninfo.dwMinorVersion;
206    }
207
208    /* deinitialize some routines on win95 - they're not implemented well */
209    if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
210       p_BackupRead = NULL;
211       p_BackupWrite = NULL;
212
213       p_CreateFileW = NULL;          
214       p_GetFileAttributesW = NULL;          
215       p_GetFileAttributesExW = NULL;
216           
217       p_SetFileAttributesW = NULL;
218                 
219       p_FindFirstFileW = NULL;
220       p_FindNextFileW = NULL;
221       p_SetCurrentDirectoryW = NULL;
222       p_GetCurrentDirectoryW = NULL;
223
224       p_wunlink = NULL;
225       p_wmkdir = NULL;
226       p_wopen = NULL;
227
228       p_GetVolumePathNameW = NULL;
229       p_GetVolumeNameForVolumeMountPointW = NULL;
230    }   
231
232    /* decide which vss class to initialize */
233 #ifdef WIN32_VSS
234    switch (dwMinorVersion) {
235       case 1: 
236          g_pVSSClient = new VSSClientXP();
237          atexit(VSSCleanup);
238          break;
239       case 2: 
240          g_pVSSClient = new VSSClient2003();
241          atexit(VSSCleanup);
242          break;
243    }
244 #endif /* WIN32_VSS */
245 }
246
247 #endif