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