]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/winapi.c
0de12dc822cacb470db4544bd7e7de9ce5889760
[bacula/bacula] / bacula / src / win32 / compat / 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    Bacula® - The Network Backup Solution
10
11    Copyright (C) 2003-2006 Free Software Foundation Europe e.V.
12
13    The main author of Bacula is Kern Sibbald, with contributions from
14    many others, a complete list can be found in the file AUTHORS.
15    This program is Free Software; you can redistribute it and/or
16    modify it under the terms of version two of the GNU General Public
17    License as published by the Free Software Foundation plus additions
18    that are listed in the file LICENSE.
19
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28    02110-1301, USA.
29
30    Bacula® is a registered trademark of John Walker.
31    The licensor of Bacula is the Free Software Foundation Europe
32    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
33    Switzerland, email:ftf@fsfeurope.org.
34 */
35
36 #include "bacula.h"
37
38 // init with win9x, but maybe set to NT in InitWinAPI
39 DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
40 DWORD g_MinorVersion = 0;
41
42 /* API Pointers */
43
44 t_OpenProcessToken      p_OpenProcessToken = NULL;
45 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
46 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
47
48 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
49
50 t_CreateFileA           p_CreateFileA = NULL;
51 t_CreateFileW           p_CreateFileW = NULL;
52 t_CreateDirectoryA      p_CreateDirectoryA;
53 t_CreateDirectoryW      p_CreateDirectoryW;
54
55 t_wunlink               p_wunlink = NULL;
56 t_wmkdir                p_wmkdir = 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_AttachConsole         p_AttachConsole = 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 void 
89 InitWinAPIWrapper() 
90 {
91    OSVERSIONINFO osversioninfo = { sizeof(OSVERSIONINFO) };
92
93    // Get the current OS version
94    if (!GetVersionEx(&osversioninfo)) {
95       g_platform_id = 0;
96       g_MinorVersion = 0;
97    } else {
98       g_platform_id = osversioninfo.dwPlatformId;
99       g_MinorVersion = osversioninfo.dwMinorVersion;
100    }
101
102    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
103    if (hLib) {
104       /* create file calls */
105       p_CreateFileA = (t_CreateFileA)
106           GetProcAddress(hLib, "CreateFileA");
107       p_CreateDirectoryA = (t_CreateDirectoryA)
108           GetProcAddress(hLib, "CreateDirectoryA");
109
110       /* attribute calls */
111       p_GetFileAttributesA = (t_GetFileAttributesA)
112           GetProcAddress(hLib, "GetFileAttributesA");
113       p_GetFileAttributesExA = (t_GetFileAttributesExA)
114           GetProcAddress(hLib, "GetFileAttributesExA");
115       p_SetFileAttributesA = (t_SetFileAttributesA)
116           GetProcAddress(hLib, "SetFileAttributesA");
117
118       /* process calls */
119       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
120           GetProcAddress(hLib, "SetProcessShutdownParameters");
121
122       /* char conversion calls */
123       p_WideCharToMultiByte = (t_WideCharToMultiByte)
124           GetProcAddress(hLib, "WideCharToMultiByte");
125       p_MultiByteToWideChar = (t_MultiByteToWideChar)
126           GetProcAddress(hLib, "MultiByteToWideChar");
127
128       /* find files */
129       p_FindFirstFileA = (t_FindFirstFileA)
130           GetProcAddress(hLib, "FindFirstFileA"); 
131       p_FindNextFileA = (t_FindNextFileA)
132           GetProcAddress(hLib, "FindNextFileA");
133
134       /* get and set directory */
135       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
136           GetProcAddress(hLib, "GetCurrentDirectoryA");
137       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
138           GetProcAddress(hLib, "SetCurrentDirectoryA");
139
140       if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
141          p_CreateFileW = (t_CreateFileW)
142              GetProcAddress(hLib, "CreateFileW");
143          p_CreateDirectoryW = (t_CreateDirectoryW)
144              GetProcAddress(hLib, "CreateDirectoryW");
145
146          /* backup calls */
147          p_BackupRead = (t_BackupRead)
148              GetProcAddress(hLib, "BackupRead");
149          p_BackupWrite = (t_BackupWrite)
150              GetProcAddress(hLib, "BackupWrite");
151
152          p_GetFileAttributesW = (t_GetFileAttributesW)
153              GetProcAddress(hLib, "GetFileAttributesW");
154          p_GetFileAttributesExW = (t_GetFileAttributesExW)
155              GetProcAddress(hLib, "GetFileAttributesExW");
156          p_SetFileAttributesW = (t_SetFileAttributesW)
157              GetProcAddress(hLib, "SetFileAttributesW");
158          p_FindFirstFileW = (t_FindFirstFileW)
159              GetProcAddress(hLib, "FindFirstFileW");
160          p_FindNextFileW = (t_FindNextFileW)
161              GetProcAddress(hLib, "FindNextFileW");
162          p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
163              GetProcAddress(hLib, "GetCurrentDirectoryW");
164          p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
165              GetProcAddress(hLib, "SetCurrentDirectoryW");
166
167          /* some special stuff we need for VSS
168             but statically linkage doesn't work on Win 9x */
169          p_GetVolumePathNameW = (t_GetVolumePathNameW)
170              GetProcAddress(hLib, "GetVolumePathNameW");
171          p_GetVolumeNameForVolumeMountPointW = (t_GetVolumeNameForVolumeMountPointW)
172              GetProcAddress(hLib, "GetVolumeNameForVolumeMountPointW");
173
174          p_AttachConsole = (t_AttachConsole)
175              GetProcAddress(hLib, "AttachConsole");
176       }
177
178       FreeLibrary(hLib);
179    }
180    
181    if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
182       hLib = LoadLibraryA("MSVCRT.DLL");
183       if (hLib) {
184          /* unlink */
185          p_wunlink = (t_wunlink)
186          GetProcAddress(hLib, "_wunlink");
187          /* wmkdir */
188          p_wmkdir = (t_wmkdir)
189          GetProcAddress(hLib, "_wmkdir");
190
191          FreeLibrary(hLib);
192       }
193
194       hLib = LoadLibraryA("ADVAPI32.DLL");
195       if (hLib) {
196          p_OpenProcessToken = (t_OpenProcessToken)
197             GetProcAddress(hLib, "OpenProcessToken");
198          p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
199             GetProcAddress(hLib, "AdjustTokenPrivileges");
200          p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
201             GetProcAddress(hLib, "LookupPrivilegeValueA");
202          FreeLibrary(hLib);
203       }
204    }
205
206    atexit(Win32ConvCleanupCache);
207 }