]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/winapi.c
- Add Scratch to PoolType in PostgreSQL make...tables and do not
[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) 2000-2005 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 as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
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 GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29
30 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
31
32 #include "winapi.h"
33
34 // init with win9x, but maybe set to NT in InitWinAPI
35 DWORD  g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
36
37
38 /* API Pointers */
39
40 t_OpenProcessToken      p_OpenProcessToken = NULL;
41 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
42 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
43
44 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
45
46 t_CreateFileA   p_CreateFileA = NULL;
47 t_CreateFileW   p_CreateFileW = NULL;
48
49 t_wunlink p_wunlink = NULL;
50 t_wmkdir p_wmkdir = NULL;
51 t_wopen p_wopen = NULL;
52
53 t_GetFileAttributesA    p_GetFileAttributesA = NULL;
54 t_GetFileAttributesW    p_GetFileAttributesW = NULL;
55
56 t_GetFileAttributesExA  p_GetFileAttributesExA = NULL;
57 t_GetFileAttributesExW  p_GetFileAttributesExW = NULL;
58
59 t_SetFileAttributesA    p_SetFileAttributesA = NULL;
60 t_SetFileAttributesW    p_SetFileAttributesW = NULL;
61 t_BackupRead            p_BackupRead = NULL;
62 t_BackupWrite           p_BackupWrite = NULL;
63 t_WideCharToMultiByte p_WideCharToMultiByte = NULL;
64 t_MultiByteToWideChar p_MultiByteToWideChar = NULL;
65
66 t_FindFirstFileA p_FindFirstFileA = NULL;
67 t_FindFirstFileW p_FindFirstFileW = NULL;
68
69 t_FindNextFileA p_FindNextFileA = NULL;
70 t_FindNextFileW p_FindNextFileW = NULL;
71
72 t_SetCurrentDirectoryA p_SetCurrentDirectoryA = NULL;
73 t_SetCurrentDirectoryW p_SetCurrentDirectoryW = NULL;
74
75 t_GetCurrentDirectoryA p_GetCurrentDirectoryA = NULL;
76 t_GetCurrentDirectoryW p_GetCurrentDirectoryW = NULL;
77
78
79 void 
80 InitWinAPIWrapper() 
81 {
82    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
83    if (hLib) {
84       /* create file calls */
85       p_CreateFileA = (t_CreateFileA)
86           GetProcAddress(hLib, "CreateFileA");
87       p_CreateFileW = (t_CreateFileW)
88           GetProcAddress(hLib, "CreateFileW");      
89
90       /* attribute calls */
91       p_GetFileAttributesA = (t_GetFileAttributesA)
92           GetProcAddress(hLib, "GetFileAttributesA");
93       p_GetFileAttributesW = (t_GetFileAttributesW)
94           GetProcAddress(hLib, "GetFileAttributesW");
95       p_GetFileAttributesExA = (t_GetFileAttributesExA)
96           GetProcAddress(hLib, "GetFileAttributesExA");
97       p_GetFileAttributesExW = (t_GetFileAttributesExW)
98           GetProcAddress(hLib, "GetFileAttributesExW");
99       p_SetFileAttributesA = (t_SetFileAttributesA)
100           GetProcAddress(hLib, "SetFileAttributesA");
101       p_SetFileAttributesW = (t_SetFileAttributesW)
102           GetProcAddress(hLib, "SetFileAttributesW");
103       /* process calls */
104       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
105           GetProcAddress(hLib, "SetProcessShutdownParameters");
106       /* backup calls */
107       p_BackupRead = (t_BackupRead)
108           GetProcAddress(hLib, "BackupRead");
109       p_BackupWrite = (t_BackupWrite)
110           GetProcAddress(hLib, "BackupWrite");
111       /* char conversion calls */
112       p_WideCharToMultiByte = (t_WideCharToMultiByte)
113           GetProcAddress(hLib, "WideCharToMultiByte");
114       p_MultiByteToWideChar = (t_MultiByteToWideChar)
115           GetProcAddress(hLib, "MultiByteToWideChar");
116
117       /* find files */
118       p_FindFirstFileA = (t_FindFirstFileA)
119           GetProcAddress(hLib, "FindFirstFileA"); 
120       p_FindFirstFileW = (t_FindFirstFileW)
121           GetProcAddress(hLib, "FindFirstFileW");       
122       p_FindNextFileA = (t_FindNextFileA)
123           GetProcAddress(hLib, "FindNextFileA");
124       p_FindNextFileW = (t_FindNextFileW)
125           GetProcAddress(hLib, "FindNextFileW");
126       /* set and get directory */
127       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
128           GetProcAddress(hLib, "SetCurrentDirectoryA");
129       p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
130           GetProcAddress(hLib, "SetCurrentDirectoryW");       
131       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
132           GetProcAddress(hLib, "GetCurrentDirectoryA");
133       p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
134           GetProcAddress(hLib, "GetCurrentDirectoryW");      
135       FreeLibrary(hLib);
136    }
137    
138    hLib = LoadLibraryA("MSVCRT.DLL");
139    if (hLib) {
140       /* unlink */
141       p_wunlink = (t_wunlink)
142       GetProcAddress(hLib, "_wunlink");
143       /* wmkdir */
144       p_wmkdir = (t_wmkdir)
145       GetProcAddress(hLib, "_wmkdir");
146       /* wopen */
147       p_wopen = (t_wopen)
148       GetProcAddress(hLib, "_wopen");
149         
150       FreeLibrary(hLib);
151    }
152    
153    hLib = LoadLibraryA("ADVAPI32.DLL");
154    if (hLib) {
155       p_OpenProcessToken = (t_OpenProcessToken)
156          GetProcAddress(hLib, "OpenProcessToken");
157       p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
158          GetProcAddress(hLib, "AdjustTokenPrivileges");
159       p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
160          GetProcAddress(hLib, "LookupPrivilegeValueA");
161       FreeLibrary(hLib);
162    }
163
164    // do we run on win 9x ???
165    OSVERSIONINFO osversioninfo;
166    osversioninfo.dwOSVersionInfoSize = sizeof(osversioninfo);
167
168    // Get the current OS version
169    if (!GetVersionEx(&osversioninfo)) {
170       g_platform_id = 0;
171    } else {
172       g_platform_id = osversioninfo.dwPlatformId;
173    }
174
175    if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
176       p_BackupRead = NULL;
177       p_BackupWrite = NULL;
178
179       p_CreateFileW = NULL;          
180       p_GetFileAttributesW = NULL;          
181       p_GetFileAttributesExW = NULL;
182           
183       p_SetFileAttributesW = NULL;
184                 
185       p_FindFirstFileW = NULL;
186       p_FindNextFileW = NULL;
187       p_SetCurrentDirectoryW = NULL;
188       p_GetCurrentDirectoryW = NULL;
189
190       p_wunlink = NULL;
191       p_wmkdir = NULL;
192       p_wopen = NULL;
193    }   
194 }
195 #endif