]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/winapi.c
55ea0837887bc432045168c7390ca5847dd6e670
[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 t_SHGetFolderPath       p_SHGetFolderPath = NULL;
89
90 void 
91 InitWinAPIWrapper() 
92 {
93    OSVERSIONINFO osversioninfo = { sizeof(OSVERSIONINFO) };
94
95    // Get the current OS version
96    if (!GetVersionEx(&osversioninfo)) {
97       g_platform_id = 0;
98       g_MinorVersion = 0;
99    } else {
100       g_platform_id = osversioninfo.dwPlatformId;
101       g_MinorVersion = osversioninfo.dwMinorVersion;
102    }
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_CreateDirectoryA = (t_CreateDirectoryA)
110           GetProcAddress(hLib, "CreateDirectoryA");
111
112       /* attribute calls */
113       p_GetFileAttributesA = (t_GetFileAttributesA)
114           GetProcAddress(hLib, "GetFileAttributesA");
115       p_GetFileAttributesExA = (t_GetFileAttributesExA)
116           GetProcAddress(hLib, "GetFileAttributesExA");
117       p_SetFileAttributesA = (t_SetFileAttributesA)
118           GetProcAddress(hLib, "SetFileAttributesA");
119
120       /* process calls */
121       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
122           GetProcAddress(hLib, "SetProcessShutdownParameters");
123
124       /* char conversion calls */
125       p_WideCharToMultiByte = (t_WideCharToMultiByte)
126           GetProcAddress(hLib, "WideCharToMultiByte");
127       p_MultiByteToWideChar = (t_MultiByteToWideChar)
128           GetProcAddress(hLib, "MultiByteToWideChar");
129
130       /* find files */
131       p_FindFirstFileA = (t_FindFirstFileA)
132           GetProcAddress(hLib, "FindFirstFileA"); 
133       p_FindNextFileA = (t_FindNextFileA)
134           GetProcAddress(hLib, "FindNextFileA");
135
136       /* get and set directory */
137       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
138           GetProcAddress(hLib, "GetCurrentDirectoryA");
139       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
140           GetProcAddress(hLib, "SetCurrentDirectoryA");
141
142       if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
143          p_CreateFileW = (t_CreateFileW)
144              GetProcAddress(hLib, "CreateFileW");
145          p_CreateDirectoryW = (t_CreateDirectoryW)
146              GetProcAddress(hLib, "CreateDirectoryW");
147
148          /* backup calls */
149          p_BackupRead = (t_BackupRead)
150              GetProcAddress(hLib, "BackupRead");
151          p_BackupWrite = (t_BackupWrite)
152              GetProcAddress(hLib, "BackupWrite");
153
154          p_GetFileAttributesW = (t_GetFileAttributesW)
155              GetProcAddress(hLib, "GetFileAttributesW");
156          p_GetFileAttributesExW = (t_GetFileAttributesExW)
157              GetProcAddress(hLib, "GetFileAttributesExW");
158          p_SetFileAttributesW = (t_SetFileAttributesW)
159              GetProcAddress(hLib, "SetFileAttributesW");
160          p_FindFirstFileW = (t_FindFirstFileW)
161              GetProcAddress(hLib, "FindFirstFileW");
162          p_FindNextFileW = (t_FindNextFileW)
163              GetProcAddress(hLib, "FindNextFileW");
164          p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
165              GetProcAddress(hLib, "GetCurrentDirectoryW");
166          p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
167              GetProcAddress(hLib, "SetCurrentDirectoryW");
168
169          /* some special stuff we need for VSS
170             but statically linkage doesn't work on Win 9x */
171          p_GetVolumePathNameW = (t_GetVolumePathNameW)
172              GetProcAddress(hLib, "GetVolumePathNameW");
173          p_GetVolumeNameForVolumeMountPointW = (t_GetVolumeNameForVolumeMountPointW)
174              GetProcAddress(hLib, "GetVolumeNameForVolumeMountPointW");
175
176          p_AttachConsole = (t_AttachConsole)
177              GetProcAddress(hLib, "AttachConsole");
178       }
179
180       FreeLibrary(hLib);
181    }
182    
183    if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
184       hLib = LoadLibraryA("MSVCRT.DLL");
185       if (hLib) {
186          /* unlink */
187          p_wunlink = (t_wunlink)
188          GetProcAddress(hLib, "_wunlink");
189          /* wmkdir */
190          p_wmkdir = (t_wmkdir)
191          GetProcAddress(hLib, "_wmkdir");
192
193          FreeLibrary(hLib);
194       }
195
196       hLib = LoadLibraryA("ADVAPI32.DLL");
197       if (hLib) {
198          p_OpenProcessToken = (t_OpenProcessToken)
199             GetProcAddress(hLib, "OpenProcessToken");
200          p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
201             GetProcAddress(hLib, "AdjustTokenPrivileges");
202          p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
203             GetProcAddress(hLib, "LookupPrivilegeValueA");
204          FreeLibrary(hLib);
205       }
206    }
207
208    /* First try in SHFOLDER for older systems */
209    hLib = LoadLibraryA("SHFOLDER.DLL");
210    if (hLib) {
211       p_SHGetFolderPath = (t_SHGetFolderPath)
212          GetProcAddress(hLib, "SHGetFolderPath");
213       FreeLibrary(hLib);
214    }
215
216    /* Now try Shell32.dll for newer systems */
217    hLib = LoadLibraryA("SHELL32.DLL");
218    if (hLib) {
219       p_SHGetFolderPath = (t_SHGetFolderPath)
220          GetProcAddress(hLib, "SHGetFolderPath");
221       FreeLibrary(hLib);
222    }
223
224    atexit(Win32ConvCleanupCache);
225 }