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