]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/compat.h
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / compat / compat.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2018 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*                               -*- Mode: C -*-
20  * compat.h --
21  */
22 // Copyright transferred from Raider Solutions, Inc to
23 //   Kern Sibbald and John Walker by express permission.
24 //
25 /*
26  * Author          : Christopher S. Hull
27  * Created On      : Fri Jan 30 13:00:51 2004
28  */
29
30 #include <winnt.h>
31 #include <ws2ipdef.h>
32
33 #if !defined(__COMPAT_H_)
34 #define __COMPAT_H_
35 #if !defined(_STAT_H)
36 #define _STAT_H         /* don't pull in MinGW stat.h */
37 #endif
38
39 #ifndef _STAT_DEFINED
40 #define _STAT_DEFINED 1 /* don't pull in MinGW struct stat from wchar.h */
41 #endif
42
43 #if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC8+
44 #pragma warning(disable : 4996) // Either disable all deprecation warnings,
45 // #define _CRT_SECURE_NO_DEPRECATE // Or just turn off warnings about the newly deprecated CRT functions.
46 #elif !defined(HAVE_MINGW) && !defined(HAVE_WXCONSOLE)
47 #define __STDC__ 1
48 #endif
49
50 #include <malloc.h>
51
52 #ifdef HAVE_MINGW_W64
53 /* Was defined in pthread.h before */
54 # define localtime_r( _clock, _result ) \
55         ( *(_result) = *localtime( (_clock) ), \
56           (_result) )
57
58 # define strtok_r( _s, _sep, _lasts ) \
59         ( *(_lasts) = strtok( (_s), (_sep) ) )
60
61 # define asctime_r( _tm, _buf ) \
62         ( strcpy( (_buf), asctime( (_tm) ) ), \
63           (_buf) )
64
65 # define ctime_r( _clock, _buf ) \
66         ( strcpy( (_buf), ctime( (_clock) ) ),  \
67           (_buf) )
68
69 # define gmtime_r( _clock, _result ) \
70         ( *(_result) = *gmtime( (_clock) ), \
71           (_result) )
72
73 #define rand_r( _seed ) \
74         ( _seed == _seed? rand() : rand() )
75
76 #endif
77 #ifdef MINGW64
78 #include <direct.h>
79 #include <ntdef.h>
80 #define _declspec __declspec
81 #endif
82
83 #include <winioctl.h>
84
85 #ifdef _WIN64
86 # define GWL_USERDATA  GWLP_USERDATA
87 #endif
88
89 #ifndef INT64
90 #define INT64 long long int
91 #endif
92
93 typedef UINT64 u_int64_t;
94 typedef UINT64 uint64_t;
95 typedef INT64 int64_t;
96 typedef UINT32 uint32_t;
97 typedef INT64 intmax_t;
98 typedef unsigned char uint8_t;
99 typedef unsigned short uint16_t;
100 typedef signed short int16_t;
101 typedef signed char int8_t;
102 typedef int __daddr_t;
103
104 #if !defined(HAVE_MINGW)
105 typedef long int32_t;
106 typedef float float32_t;
107 typedef double float64_t;
108 #endif
109
110 #if !defined(_MSC_VER) || (_MSC_VER < 1400) // VC8+
111 #ifndef _TIME_T_DEFINED
112 #define _TIME_T_DEFINED
113 typedef long time_t;
114 #endif
115 #endif
116
117 #if __STDC__ && !defined(HAVE_MINGW)
118 typedef _dev_t dev_t;
119 #if !defined(HAVE_WXCONSOLE)
120 typedef __int64 ino_t;
121 #endif
122 #endif
123
124 typedef UINT32 u_int32_t;
125 typedef unsigned char u_int8_t;
126 typedef unsigned short u_int16_t;
127
128 #if !defined(HAVE_MINGW)
129 #undef uint32_t
130 #endif
131
132 void sleep(int);
133
134 typedef UINT32 key_t;
135
136 #if defined(HAVE_MINGW)
137 #if !defined(uid_t)
138 typedef UINT32 uid_t;
139 typedef UINT32 gid_t;
140 #endif
141 #else
142 typedef UINT32 uid_t;
143 typedef UINT32 gid_t;
144 typedef UINT32 mode_t;
145 typedef INT32  ssize_t;
146 typedef UINT32 size_t;
147 #define HAVE_SSIZE_T 1
148
149 #endif /* HAVE_MINGW */
150
151 struct dirent {
152     uint64_t    d_ino;
153     uint32_t    d_off;
154     uint16_t    d_reclen;
155     char        d_name[256];
156 };
157 typedef void DIR;
158
159
160 #if !defined(__cplusplus)
161 #if !defined(true)
162 #define true 1
163 #endif
164 #if !defined(false)
165 #define false 0
166 #endif
167 #endif
168
169 #ifndef _TIMEZONE_DEFINED /* also in sys/time.h */
170 #define _TIMEZONE_DEFINED
171 struct timezone {
172     int foo;
173 };
174 #endif
175
176 int strcasecmp(const char*, const char *);
177 int gettimeofday(struct timeval *, struct timezone *);
178
179 #if !defined(EETXTBUSY)
180 #define EETXTBUSY 26
181 #endif
182
183 #if !defined(ETIMEDOUT)
184 #define ETIMEDOUT 55
185 #endif
186
187 #if !defined(ENOMEDIUM)
188 #define ENOMEDIUM 123
189 #endif
190
191 #if !defined(ENODATA)
192 #define ENODATA 61
193 #endif
194
195 /*
196  * Stat packet that we use for Windows
197  */
198 struct stat
199 {
200     _dev_t      st_dev;
201     uint64_t    st_ino;
202     uint16_t    st_mode;
203     int16_t     st_nlink;
204     uint32_t    st_uid;
205     uint32_t    st_gid;
206     _dev_t      st_rdev;
207     uint64_t    st_size;
208     time_t      st_atime;
209     time_t      st_mtime;
210     time_t      st_ctime;
211     uint32_t    st_blksize;
212     uint64_t    st_blocks;
213     uint32_t    st_fattrs;  /* Windows file attributes */
214 };
215
216 #ifndef SOCK_CLOEXEC
217 #define SOCK_CLOEXEC 0x00000000
218 #endif
219
220 #undef  S_IFMT
221 #define S_IFMT         0170000         /* file type mask */
222 #undef  S_IFDIR
223 #define S_IFDIR        0040000         /* directory */
224 #define S_IFCHR        0020000         /* character special */
225 #define S_IFBLK        0060000         /* block special */
226 #define S_IFIFO        0010000         /* pipe */
227 #undef  S_IFREG
228 #define S_IFREG        0100000         /* regular */
229 #define S_IREAD        0000400         /* read permission, owner */
230 #define S_IWRITE       0000200         /* write permission, owner */
231 #define S_IEXEC        0000100         /* execute/search permission, owner */
232
233 #define S_IRUSR         S_IREAD
234 #define S_IWUSR         S_IWRITE
235 #define S_IXUSR         S_IEXEC
236 #define S_ISREG(x)  (((x) & S_IFMT) == S_IFREG)
237 #define S_ISDIR(x)  (((x) & S_IFMT) == S_IFDIR)
238 #define S_ISCHR(x) 0
239 #define S_ISBLK(x)  (((x) & S_IFMT) == S_IFBLK)
240 #define S_ISFIFO(x) 0
241
242 #define S_IRGRP         000040
243 #define S_IWGRP         000020
244 #define S_IXGRP         000010
245
246 #define S_IROTH         00004
247 #define S_IWOTH         00002
248 #define S_IXOTH         00001
249
250 #define S_IRWXO         000007
251 #define S_IRWXG         000070
252 #define S_ISUID         004000
253 #define S_ISGID         002000
254 #define S_ISVTX         001000
255 #define S_ISSOCK(x) 0
256 #define S_ISLNK(x)      0
257
258 #if __STDC__
259 #define O_RDONLY _O_RDONLY
260 #define O_WRONLY _O_WRONLY
261 #define O_RDWR   _O_RDWR
262 #define O_CREAT  _O_CREAT
263 #define O_TRUNC  _O_TRUNC
264
265 #define isascii __isascii
266 #define toascii __toascii
267 #define iscsymf __iscsymf
268 #define iscsym  __iscsym
269 #endif
270
271 typedef  BOOL (*t_pVSSPathConvert)(const char *szFilePath, char *szShadowPath, int nBuflen);
272 typedef  BOOL (*t_pVSSPathConvertW)(const wchar_t  *szFilePath, wchar_t  *szShadowPath, int nBuflen);
273 /* To know if we can use the VSSPathConvert function */
274 typedef  BOOL (*t_pVSSPathConverter)();
275
276 void SetVSSPathConvert(t_pVSSPathConverter pPathConverter, t_pVSSPathConvert pPathConvert, t_pVSSPathConvertW pPathConvertW);
277
278 int lchown(const char *, uid_t uid, gid_t gid);
279 int chown(const char *, uid_t uid, gid_t gid);
280 #if !defined(HAVE_MINGW)
281 int chmod(const char *, mode_t mode);
282 #endif
283 #define O_NONBLOCK   04000
284 #define F_GETFL      3
285 #define F_SETFL      4
286
287 #ifndef MINGW64
288 #define open   _open
289 #endif
290
291 int fcntl(int fd, int cmd, long arg);
292 int fstat(intptr_t fd, struct stat *sb);
293
294 int inet_aton(const char *cp, struct in_addr *inp);
295 int binet_pton(int af, const char *src, void *dst);
296 int kill(pid_t pid, int signo);
297 int pipe(int []);
298 int fork();
299 int waitpid(int, int *, int);
300
301 #if !defined(HAVE_MINGW)
302 #define strncasecmp strnicmp
303 //int strncasecmp(const char*, const char *, int);
304 int utime(const char *filename, struct utimbuf *buf);
305 #define vsnprintf _vsnprintf
306 #define snprintf _snprintf
307 #endif //HAVE_MINGW
308
309
310 #define WNOHANG 0
311 #define WIFEXITED(x) 0
312 #define WEXITSTATUS(x) x
313 #define WIFSIGNALED(x) 0
314 #define WTERMSIG(x) x
315 #define SIGKILL 9
316 #define SIGUSR2 9999
317
318 #define HAVE_OLD_SOCKOPT
319
320 struct timespec;
321 int readdir(unsigned int fd, struct dirent *dirp, unsigned int count);
322 int nanosleep(const struct timespec*, struct timespec *);
323 long int random(void);
324 void srandom(unsigned int seed);
325 int lstat(const char *, struct stat *);
326 int stat(const char *file, struct stat *sb);
327 long pathconf(const char *, int);
328 int readlink(const char *, char *, int);
329 int symlink(const char *path1, const char *path2);
330 #define _PC_PATH_MAX 1
331 #define _PC_NAME_MAX 2
332
333 int geteuid();
334
335 DIR *opendir(const char *name);
336 int closedir(DIR *dir);
337
338 struct passwd {
339     char *foo;
340 };
341
342 struct group {
343     char *foo;
344 };
345
346 struct passwd *getpwuid(uid_t);
347 struct group *getgrgid(uid_t);
348
349 #ifdef xxx_needed
350 struct sigaction {
351     int sa_flags;
352     void (*sa_handler)(int);
353 };
354 #define sigfillset(x)
355 #define sigaction(a, b, c)
356 #endif
357
358 #define mkdir(p, m) win32_mkdir(p)
359 #define unlink win32_unlink
360 #define chdir win32_chdir
361 #define chmod win32_chmod
362 extern void syslog(int type, const char *fmt, ...);
363 #if !defined(LOG_DAEMON)
364 #define LOG_DAEMON 0
365 #endif
366
367 #if !defined(HAVE_MINGW)
368 #define R_OK 04
369 #define W_OK 02
370 int stat(const char *, struct stat *);
371 #if defined(__cplusplus)
372 #define access _access
373 extern "C" _CRTIMP int __cdecl _access(const char *, int);
374 int execvp(const char *, char *[]);
375 extern "C" void *  __cdecl _alloca(size_t);
376 #endif
377 #endif //HAVE_MINGW
378
379 #define getpid _getpid
380
381 #define getppid() 0
382 #define gethostid() 0
383 #define getuid() 0
384 #define getgid() 0
385
386 #define getcwd win32_getcwd
387 #define chdir win32_chdir
388 #define chmod win32_chmod
389 #define fputs win32_fputs
390 char *win32_getcwd(char *buf, int maxlen);
391 int win32_chdir(const char *buf);
392 int win32_mkdir(const char *buf);
393 int win32_fputs(const char *string, FILE *stream);
394 int win32_unlink(const char *filename);
395 int win32_chmod(const char *, mode_t);
396
397
398 char* win32_cgets (char* buffer, int len);
399
400 int WSA_Init(void);
401 void Win32ConvCleanupCache();
402
403 #if defined(HAVE_MINGW)
404 void closelog();
405 void openlog(const char *ident, int option, int facility);
406 #endif //HAVE_MINGW
407
408 typedef DWORD EXECUTION_STATE;
409 #ifndef ES_CONTINUOUS
410 #define ES_CONTINUOUS            0x80000000
411 #endif
412 #ifndef ES_SYSTEM_REQUIRED
413 #define ES_SYSTEM_REQUIRED       0x00000001
414 #endif
415 #ifndef ES_DISPLAY_REQUIRED
416 #define ES_DISPLAY_REQUIRED      0x00000002
417 #endif
418 #ifndef ES_USER_PRESENT
419 # define ES_USER_PRESENT          0x00000004
420 #endif
421
422 WINBASEAPI EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE esFlags);
423
424
425 extern void LogErrorMsg(const char *message);
426
427 #if !defined(INVALID_FILE_ATTRIBUTES)
428 #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
429 #endif
430
431 /*
432  * Since the FileAttributes flag in Windows does not hold the
433  *  dedup bit, we simplify the code by defining our own
434  *  bit.
435  */
436 #define FILE_ATTRIBUTE_DEDUP 0x80000000
437 #define CREATE_FOR_EXPORT 0 /* default in OpenEncryptedFileRaw */
438
439 #if defined(_MSC_VER)
440 inline unsigned long ffs(unsigned long word)
441 {
442    unsigned long  index;
443
444    if (_BitScanForward(&index, word) != 0)
445       return index + 1;
446    else
447       return 0;
448 }
449
450 #else
451 #define  ffs   __builtin_ffs
452 #endif
453
454
455 int win32_ftruncate(int fd, int64_t length);
456 int mkstemp(char *t);
457
458 void malloc_trim(int);
459 uint64_t get_memory_info(char *buf, int buflen);
460
461 #undef ftruncate
462 #define ftruncate win32_ftruncate
463
464 #ifndef O_DIRECTORY
465 #define O_DIRECTORY 0100000000
466 #endif
467 #define O_ENCRYPTED 0200000000
468
469 #endif /* __COMPAT_H_ */