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