2 // compat.cpp -- compatibilty layer to make bacula-fd run
3 // natively under windows
5 // Copyright transferred from Raider Solutions, Inc to
6 // Kern Sibbald and John Walker by express permission.
8 // Copyright (C) 2004-2006 Kern Sibbald
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // version 2 as amended with additional clauses defined in the
13 // file LICENSE in the main source directory.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // the file LICENSE for additional details.
20 // Author : Christopher S. Hull
21 // Created On : Sat Jan 31 15:55:00 2004
35 #include "../../lib/winapi.h"
40 #define b_errno_win32 (1<<29)
43 /* to allow the usage of the original version in this file here */
47 #define USE_WIN32_COMPAT_IO 1
48 #define USE_WIN32_32KPATHCONVERSION 1
50 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
51 extern DWORD g_platform_id;
52 extern int enable_vss;
54 // from MicroSoft SDK (KES) is the diff between Jan 1 1601 and Jan 1 1970
56 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000ULL
58 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000I64
61 #define WIN32_FILETIME_SCALE 10000000 // 100ns/second
63 void conv_unix_to_win32_path(const char *name, char *win32_name, DWORD dwSize)
65 const char *fname = name;
66 char *tname = win32_name;
68 /* Check for Unix separator and convert to Win32 */
69 if (name[0] == '/' && name[1] == '/') { /* double slash? */
70 name++; /* yes, skip first one */
73 *win32_name++ = '\\'; /* convert char */
74 /* If Win32 separated that is "quoted", remove quote */
75 } else if (*name == '\\' && name[1] == '\\') {
77 name++; /* skip first \ */
79 *win32_name++ = *name; /* copy character */
83 /* Strip any trailing slash, if we stored something */
84 /* but leave "c:\" with backslash (root directory case */
85 if (*fname != 0 && win32_name[-1] == '\\' && strlen (fname) != 3) {
92 /* here we convert to VSS specific file name which
93 can get longer because VSS will make something like
94 \\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\bacula\\uninstall.exe
95 from c:\bacula\uninstall.exe
97 if (g_pVSSClient && enable_vss && g_pVSSClient->IsInitialized()) {
98 POOLMEM *pszBuf = get_pool_memory (PM_FNAME);
99 pszBuf = check_pool_memory_size(pszBuf, dwSize);
100 bstrncpy(pszBuf, tname, strlen(tname)+1);
101 g_pVSSClient->GetShadowPath(pszBuf, tname, dwSize);
102 free_pool_memory(pszBuf);
108 make_wchar_win32_path(POOLMEM* pszUCSPath, BOOL* pBIsRawPath /*= NULL*/)
110 /* created 02/27/2006 Thorsten Engel
112 This function expects an UCS-encoded standard wchar_t in pszUCSPath and
113 will complete the input path to an absolue path of the form \\?\c:\path\file
115 With this trick, it is possible to have 32K characters long paths.
117 Optionally one can use pBIsRawPath to determine id pszUCSPath contains a path
118 to a raw windows partition */
121 *pBIsRawPath = FALSE;
123 if (!p_GetCurrentDirectoryW)
126 wchar_t * name = (wchar_t *) pszUCSPath;
128 /* if it has already the desired form, exit without changes */
129 if (wcslen(name) > 3 && wcsncmp (name, L"\\\\?\\", 4) == 0)
132 POOLMEM* pwszBuf = get_pool_memory (PM_FNAME);
133 POOLMEM* pwszCurDirBuf = get_pool_memory (PM_FNAME);
134 DWORD dwCurDirPathSize = 0;
136 /* get buffer with enough size (name+max 6. wchars+1 null terminator */
137 DWORD dwBufCharsNeeded = (wcslen(name)+7);
138 pwszBuf = check_pool_memory_size(pwszBuf, dwBufCharsNeeded*sizeof(wchar_t));
140 /* add \\?\ to support 32K long filepaths
141 it is important to make absolute paths, so we add drive and
142 current path if necessary */
144 BOOL bAddDrive = TRUE;
145 BOOL bAddCurrentPath = TRUE;
146 BOOL bAddPrefix = TRUE;
148 /* does path begin with drive? if yes, it is absolute */
149 if (wcslen(name) >= 3 && (iswalpha (*name) && *(name+1) == ':'
150 && (*(name+2) == '\\' || *(name+2) == '/'))) {
152 bAddCurrentPath = FALSE;
155 /* is path absolute? */
156 if (*name == '/' || *name == '\\')
157 bAddCurrentPath = FALSE;
159 /* is path relative to itself?, if yes, skip ./ */
160 if (wcslen(name) > 2 && ((wcsncmp (name, L"./", 2) == 0) || (wcsncmp (name, L".\\", 2) == 0))) {
164 /* is path of form '//./'? */
165 if (wcslen(name) > 3 && ((wcsncmp (name, L"//./", 4) == 0) || (wcsncmp (name, L"\\\\.\\", 4) == 0))) {
167 bAddCurrentPath = FALSE;
173 int nParseOffset = 0;
175 /* add 4 bytes header */
178 wcscpy ((wchar_t *) pwszBuf,L"\\\\?\\");
181 /* get current path if needed */
182 if (bAddDrive || bAddCurrentPath) {
183 dwCurDirPathSize = p_GetCurrentDirectoryW(0, NULL);
184 if (dwCurDirPathSize > 0) {
185 /* get directory into own buffer as it may either return c:\... or \\?\C:\.... */
186 pwszCurDirBuf = check_pool_memory_size(pwszCurDirBuf, (dwCurDirPathSize+1)*sizeof(wchar_t));
187 p_GetCurrentDirectoryW(dwCurDirPathSize,(wchar_t *)pwszCurDirBuf);
191 /* we have no info for doing so */
193 bAddCurrentPath = FALSE;
198 /* add drive if needed */
199 if (bAddDrive && !bAddCurrentPath) {
202 if (dwCurDirPathSize > 3 && wcsncmp((LPCWSTR)pwszCurDirBuf, L"\\\\?\\", 4) == 0)
203 /* copy drive character */
204 wcsncpy((wchar_t *) szDrive, (LPCWSTR)pwszCurDirBuf+4,2);
206 /* copy drive character */
207 wcsncpy((wchar_t *) szDrive, (LPCWSTR)pwszCurDirBuf,2);
211 wcscat((wchar_t *) pwszBuf, szDrive);
215 /* add path if needed */
216 if (bAddCurrentPath) {
217 /* the 1 add. character is for the eventually added backslash */
218 dwBufCharsNeeded += dwCurDirPathSize+1;
219 pwszBuf = check_pool_memory_size(pwszBuf, dwBufCharsNeeded*sizeof(wchar_t));
220 /* get directory into own buffer as it may either return c:\... or \\?\C:\.... */
222 if (dwCurDirPathSize > 3 && wcsncmp ((LPCWSTR)pwszCurDirBuf, L"\\\\?\\", 4) == 0)
223 /* copy complete string */
224 wcscpy((wchar_t *) pwszBuf, (LPCWSTR)pwszCurDirBuf);
227 wcscat((wchar_t *) pwszBuf, (LPCWSTR)pwszCurDirBuf);
229 nParseOffset = wcslen ((LPCWSTR) pwszBuf);
231 /* check if path ends with backslash, if not, add one */
232 if (*((wchar_t *) pwszBuf+nParseOffset-1) != L'\\') {
233 wcscat((wchar_t *) pwszBuf, L"\\");
239 wchar_t * win32_name = (wchar_t *)pwszBuf+nParseOffset;
242 /* Check for Unix separator and convert to Win32 */
244 *win32_name++ = '\\'; /* convert char */
245 /* If Win32 separated that is "quoted", remove quote */
246 /* HELPME (Thorsten Engel): I don't understand the following part
247 and it removes a backslash from e.g. "\\.\c:" which I need for
248 RAW device access. So I took it out */
249 /* } else if (*name == '\\' && name[1] == '\\') {
250 *win32_name++ = '\\';
251 name++; */ /* skip first \ */
253 *win32_name++ = *name; /* copy character */
258 /* null terminate string */
262 /* here we convert to VSS specific file name which
263 can get longer because VSS will make something like
264 \\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\bacula\\uninstall.exe
265 from c:\bacula\uninstall.exe
267 if (g_pVSSClient && enable_vss && g_pVSSClient->IsInitialized()) {
268 /* is output buffer large enough? */
269 pwszBuf = check_pool_memory_size(pwszBuf, (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t));
270 /* create temp. buffer */
271 POOLMEM* pszBuf = get_pool_memory(PM_FNAME);
272 pszBuf = check_pool_memory_size(pszBuf, (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t));
277 wcsncpy((wchar_t *) pszBuf, (wchar_t *) pwszBuf+nParseOffset, wcslen((wchar_t *)pwszBuf)+1-nParseOffset);
278 g_pVSSClient->GetShadowPathW((wchar_t *)pszBuf,(wchar_t *)pwszBuf,dwBufCharsNeeded+MAX_PATH);
279 free_pool_memory(pszBuf);
283 free_pool_memory (pszUCSPath);
284 free_pool_memory (pwszCurDirBuf);
290 wchar_2_UTF8(char *pszUTF, const wchar_t *pszUCS, int cchChar)
292 /* the return value is the number of bytes written to the buffer.
293 The number includes the byte for the null terminator. */
295 if (p_WideCharToMultiByte) {
296 int nRet = p_WideCharToMultiByte(CP_UTF8,0,pszUCS,-1,pszUTF,cchChar,NULL,NULL);
305 UTF8_2_wchar(POOLMEM **ppszUCS, const char *pszUTF)
307 /* the return value is the number of wide characters written to the buffer. */
308 /* convert null terminated string from utf-8 to ucs2, enlarge buffer if necessary */
310 if (p_MultiByteToWideChar) {
311 /* strlen of UTF8 +1 is enough */
312 DWORD cchSize = (strlen(pszUTF)+1);
313 *ppszUCS = check_pool_memory_size(*ppszUCS, cchSize*sizeof (wchar_t));
315 int nRet = p_MultiByteToWideChar(CP_UTF8, 0, pszUTF, -1, (LPWSTR) *ppszUCS,cchSize);
325 wchar_win32_path(const char *name, wchar_t *win32_name)
327 const char *fname = name;
329 /* Check for Unix separator and convert to Win32 */
331 *win32_name++ = '\\'; /* convert char */
332 /* If Win32 separated that is "quoted", remove quote */
333 } else if (*name == '\\' && name[1] == '\\') {
334 *win32_name++ = '\\';
335 name++; /* skip first \ */
337 *win32_name++ = *name; /* copy character */
341 /* Strip any trailing slash, if we stored something */
342 if (*fname != 0 && win32_name[-1] == '\\') {
350 make_win32_path_UTF8_2_wchar(POOLMEM **pszUCS, const char *pszUTF, BOOL* pBIsRawPath /*= NULL*/)
352 /* helper to convert from utf-8 to UCS-2 and to complete a path for 32K path syntax */
353 int nRet = UTF8_2_wchar(pszUCS, pszUTF);
355 #ifdef USE_WIN32_32KPATHCONVERSION
356 /* add \\?\ to support 32K long filepaths */
357 *pszUCS = make_wchar_win32_path(*pszUCS, pBIsRawPath);
360 *pBIsRawPath = FALSE;
373 int chmod(const char *, mode_t)
378 int chown(const char *k, uid_t, gid_t)
383 int lchown(const char *k, uid_t, gid_t)
389 bool fstype(const char *fname, char *fs, int fslen)
391 return true; /* accept anything */
403 srandom(unsigned int seed)
407 // /////////////////////////////////////////////////////////////////
408 // convert from Windows concept of time to Unix concept of time
409 // /////////////////////////////////////////////////////////////////
411 cvt_utime_to_ftime(const time_t &time, FILETIME &wintime)
413 uint64_t mstime = time;
414 mstime *= WIN32_FILETIME_SCALE;
415 mstime += WIN32_FILETIME_ADJUST;
418 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffUL);
420 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
422 wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
426 cvt_ftime_to_utime(const FILETIME &time)
428 uint64_t mstime = time.dwHighDateTime;
430 mstime |= time.dwLowDateTime;
432 mstime -= WIN32_FILETIME_ADJUST;
433 mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
435 return (time_t) (mstime & 0xffffffff);
443 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
444 FORMAT_MESSAGE_FROM_SYSTEM |
445 FORMAT_MESSAGE_IGNORE_INSERTS,
448 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
453 /* Strip any \r or \n */
454 char *rval = (char *) lpMsgBuf;
455 char *cp = strchr(rval, '\r');
459 cp = strchr(rval, '\n');
468 statDir(const char *file, struct stat *sb)
470 WIN32_FIND_DATAW info_w; // window's file info
471 WIN32_FIND_DATAA info_a; // window's file info
473 // cache some common vars to make code more transparent
474 DWORD* pdwFileAttributes;
475 DWORD* pnFileSizeHigh;
476 DWORD* pnFileSizeLow;
477 FILETIME* pftLastAccessTime;
478 FILETIME* pftLastWriteTime;
479 FILETIME* pftCreationTime;
481 if (file[1] == ':' && file[2] == 0) {
482 d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
483 sb->st_mode = S_IFDIR;
484 sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
491 HANDLE h = INVALID_HANDLE_VALUE;
493 // use unicode or ascii
494 if (p_FindFirstFileW) {
495 POOLMEM* pwszBuf = get_pool_memory (PM_FNAME);
496 make_win32_path_UTF8_2_wchar(&pwszBuf, file);
498 h = p_FindFirstFileW((LPCWSTR) pwszBuf, &info_w);
499 free_pool_memory(pwszBuf);
501 pdwFileAttributes = &info_w.dwFileAttributes;
502 pnFileSizeHigh = &info_w.nFileSizeHigh;
503 pnFileSizeLow = &info_w.nFileSizeLow;
504 pftLastAccessTime = &info_w.ftLastAccessTime;
505 pftLastWriteTime = &info_w.ftLastWriteTime;
506 pftCreationTime = &info_w.ftCreationTime;
508 else if (p_FindFirstFileA) {
509 h = p_FindFirstFileA(file, &info_a);
511 pdwFileAttributes = &info_a.dwFileAttributes;
512 pnFileSizeHigh = &info_a.nFileSizeHigh;
513 pnFileSizeLow = &info_a.nFileSizeLow;
514 pftLastAccessTime = &info_a.ftLastAccessTime;
515 pftLastWriteTime = &info_a.ftLastWriteTime;
516 pftCreationTime = &info_a.ftCreationTime;
519 if (h == INVALID_HANDLE_VALUE) {
520 const char *err = errorString();
521 d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err);
522 LocalFree((void *)err);
523 errno = b_errno_win32;
527 sb->st_mode = 0777; /* start with everything */
528 if (*pdwFileAttributes & FILE_ATTRIBUTE_READONLY)
529 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
530 if (*pdwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
531 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
532 if (*pdwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
533 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
534 sb->st_mode |= S_IFDIR;
536 sb->st_size = *pnFileSizeHigh;
538 sb->st_size |= *pnFileSizeLow;
539 sb->st_blksize = 4096;
540 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
542 sb->st_atime = cvt_ftime_to_utime(*pftLastAccessTime);
543 sb->st_mtime = cvt_ftime_to_utime(*pftLastWriteTime);
544 sb->st_ctime = cvt_ftime_to_utime(*pftCreationTime);
551 stat2(const char *file, struct stat *sb)
553 BY_HANDLE_FILE_INFORMATION info;
557 conv_unix_to_win32_path(file, tmpbuf, 1024);
559 DWORD attr = (DWORD)-1;
561 if (p_GetFileAttributesW) {
562 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
563 make_win32_path_UTF8_2_wchar(&pwszBuf, tmpbuf);
565 attr = p_GetFileAttributesW((LPCWSTR) pwszBuf);
566 free_pool_memory(pwszBuf);
567 } else if (p_GetFileAttributesA) {
568 attr = p_GetFileAttributesA(tmpbuf);
572 const char *err = errorString();
573 d_msg(__FILE__, __LINE__, 99,
574 "GetFileAttributes(%s): %s\n", tmpbuf, err);
575 LocalFree((void *)err);
576 errno = b_errno_win32;
580 if (attr & FILE_ATTRIBUTE_DIRECTORY)
581 return statDir(tmpbuf, sb);
583 h = CreateFileA(tmpbuf, GENERIC_READ,
584 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
586 if (h == INVALID_HANDLE_VALUE) {
587 const char *err = errorString();
588 d_msg(__FILE__, __LINE__, 99,
589 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
590 LocalFree((void *)err);
592 errno = b_errno_win32;
596 if (!GetFileInformationByHandle(h, &info)) {
597 const char *err = errorString();
598 d_msg(__FILE__, __LINE__, 99,
599 "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
600 LocalFree((void *)err);
602 errno = b_errno_win32;
606 sb->st_dev = info.dwVolumeSerialNumber;
607 sb->st_ino = info.nFileIndexHigh;
609 sb->st_ino |= info.nFileIndexLow;
610 sb->st_nlink = (short)info.nNumberOfLinks;
611 if (sb->st_nlink > 1) {
612 d_msg(__FILE__, __LINE__, 99, "st_nlink=%d\n", sb->st_nlink);
615 sb->st_mode = 0777; /* start with everything */
616 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
617 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
618 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
619 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
620 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
621 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
622 sb->st_mode |= S_IFREG;
624 sb->st_size = info.nFileSizeHigh;
626 sb->st_size |= info.nFileSizeLow;
627 sb->st_blksize = 4096;
628 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
629 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
630 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
631 sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime);
639 stat(const char *file, struct stat *sb)
641 WIN32_FILE_ATTRIBUTE_DATA data;
645 memset(sb, 0, sizeof(*sb));
647 /* why not allow win 95 to use p_GetFileAttributesExA ?
648 * this function allows _some_ open files to be stat'ed
649 * if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
650 * return stat2(file, sb);
654 if (p_GetFileAttributesExW) {
655 /* dynamically allocate enough space for UCS2 filename */
656 POOLMEM* pwszBuf = get_pool_memory (PM_FNAME);
657 make_win32_path_UTF8_2_wchar(&pwszBuf, file);
659 BOOL b = p_GetFileAttributesExW((LPCWSTR) pwszBuf, GetFileExInfoStandard, &data);
660 free_pool_memory(pwszBuf);
663 return stat2(file, sb);
665 } else if (p_GetFileAttributesExA) {
666 if (!p_GetFileAttributesExA(file, GetFileExInfoStandard, &data)) {
667 return stat2(file, sb);
670 return stat2(file, sb);
673 sb->st_mode = 0777; /* start with everything */
674 if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
675 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
677 if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
678 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
680 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
681 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
683 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
684 sb->st_mode |= S_IFDIR;
686 sb->st_mode |= S_IFREG;
690 sb->st_size = data.nFileSizeHigh;
692 sb->st_size |= data.nFileSizeLow;
693 sb->st_blksize = 4096;
694 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
695 sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
696 sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
697 sb->st_ctime = cvt_ftime_to_utime(data.ftCreationTime);
702 lstat(const char *file, struct stat *sb)
704 return stat(file, sb);
720 execvp(const char *, char *[]) {
741 waitpid(int, int*, int)
748 readlink(const char *, char *, int)
757 strcasecmp(const char *s1, const char *s2)
759 register int ch1, ch2;
762 return 0; /* strings are equal if same object. */
772 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
779 strncasecmp(const char *s1, const char *s2, int len)
781 register int ch1, ch2;
784 return 0; /* strings are equal if same object. */
794 if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break;
801 gettimeofday(struct timeval *tv, struct timezone *)
811 if (!SystemTimeToFileTime(&now, &tmp)) {
812 errno = b_errno_win32;
816 int64_t _100nsec = tmp.dwHighDateTime;
818 _100nsec |= tmp.dwLowDateTime;
819 _100nsec -= WIN32_FILETIME_ADJUST;
821 tv->tv_sec =(long) (_100nsec / 10000000);
822 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
827 /* For apcupsd this is in src/lib/wincompat.c */
829 extern "C" void syslog(int type, const char *fmt, ...)
831 /*#ifndef HAVE_CONSOLE
832 MessageBox(NULL, msg, "Bacula", MB_OK);
849 // implement opendir/readdir/closedir on top of window's API
853 WIN32_FIND_DATAA data_a; // window's file info (ansii version)
854 WIN32_FIND_DATAW data_w; // window's file info (wchar version)
855 const char *spec; // the directory we're traversing
856 HANDLE dirh; // the search handle
857 BOOL valid_a; // the info in data_a field is valid
858 BOOL valid_w; // the info in data_w field is valid
859 UINT32 offset; // pseudo offset for d_off
863 opendir(const char *path)
865 /* enough space for VSS !*/
866 int max_len = strlen(path) + MAX_PATH;
873 rval = (_dir *)malloc(sizeof(_dir));
874 memset (rval, 0, sizeof (_dir));
875 if (rval == NULL) return NULL;
876 char *tspec = (char *)malloc(max_len);
877 if (tspec == NULL) return NULL;
879 if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
881 /* will append \\?\ at front itself */
882 conv_unix_to_win32_path(path, tspec, max_len-4);
884 /* allow path to be 32767 bytes */
890 conv_unix_to_win32_path(path, tspec+4, max_len-4);
893 conv_unix_to_win32_path(path, tspec, max_len);
896 // add backslash only if there is none yet (think of c:\)
897 if (tspec[strlen(tspec)-1] != '\\')
898 bstrncat(tspec, "\\*", max_len);
900 bstrncat(tspec, "*", max_len);
904 // convert to wchar_t
905 if (p_FindFirstFileW) {
906 POOLMEM* pwcBuf = get_pool_memory(PM_FNAME);;
907 make_win32_path_UTF8_2_wchar(&pwcBuf,rval->spec);
909 rval->dirh = p_FindFirstFileW((LPCWSTR)pwcBuf, &rval->data_w);
911 free_pool_memory(pwcBuf);
913 if (rval->dirh != INVALID_HANDLE_VALUE)
915 } else if (p_FindFirstFileA) {
916 rval->dirh = p_FindFirstFileA(rval->spec, &rval->data_a);
918 if (rval->dirh != INVALID_HANDLE_VALUE)
923 d_msg(__FILE__, __LINE__,
924 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
925 path, rval->spec, rval->dirh);
928 if (rval->dirh == INVALID_HANDLE_VALUE)
932 d_msg(__FILE__, __LINE__,
933 99, "\tFirstFile=%s\n", rval->data_w.cFileName);
936 d_msg(__FILE__, __LINE__,
937 99, "\tFirstFile=%s\n", rval->data_a.cFileName);
942 free((void *)rval->spec);
944 errno = b_errno_win32;
951 _dir *dp = (_dir *)dirp;
953 free((void *)dp->spec);
959 typedef struct _WIN32_FIND_DATA {
960 DWORD dwFileAttributes;
961 FILETIME ftCreationTime;
962 FILETIME ftLastAccessTime;
963 FILETIME ftLastWriteTime;
968 TCHAR cFileName[MAX_PATH];
969 TCHAR cAlternateFileName[14];
970 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
974 copyin(struct dirent &dp, const char *fname)
978 char *cp = dp.d_name;
988 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
990 _dir *dp = (_dir *)dirp;
991 if (dp->valid_w || dp->valid_a) {
992 entry->d_off = dp->offset;
996 char szBuf[MAX_PATH_UTF8+1];
997 wchar_2_UTF8(szBuf,dp->data_w.cFileName);
998 dp->offset += copyin(*entry, szBuf);
999 } else if (dp->valid_a) { // copy ansi (only 1 will be valid)
1000 dp->offset += copyin(*entry, dp->data_a.cFileName);
1003 *result = entry; /* return entry address */
1004 d_msg(__FILE__, __LINE__,
1005 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
1006 dirp, entry->d_name, entry->d_reclen, entry->d_off);
1008 // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
1009 errno = b_errno_win32;
1013 // get next file, try unicode first
1014 if (p_FindNextFileW)
1015 dp->valid_w = p_FindNextFileW(dp->dirh, &dp->data_w);
1016 else if (p_FindNextFileA)
1017 dp->valid_a = p_FindNextFileA(dp->dirh, &dp->data_a);
1019 dp->valid_a = FALSE;
1020 dp->valid_w = FALSE;
1027 * Dotted IP address to network address
1033 inet_aton(const char *a, struct in_addr *inp)
1036 uint32_t acc = 0, tmp = 0;
1039 if (!isdigit(*cp)) { /* first char must be digit */
1040 return 0; /* error */
1044 tmp = (tmp * 10) + (*cp -'0');
1045 } else if (*cp == '.' || *cp == 0) {
1047 return 0; /* error */
1049 acc = (acc << 8) + tmp;
1053 return 0; /* error */
1055 } while (*cp++ != 0);
1056 if (dotc != 4) { /* want 3 .'s plus EOS */
1057 return 0; /* error */
1059 inp->s_addr = htonl(acc); /* store addr in network format */
1064 nanosleep(const struct timespec *req, struct timespec *rem)
1067 rem->tv_sec = rem->tv_nsec = 0;
1068 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
1073 init_signals(void terminate(int sig))
1079 init_stack_dump(void)
1086 pathconf(const char *path, int name)
1090 if (strncmp(path, "\\\\?\\", 4) == 0)
1102 WORD wVersionRequested = MAKEWORD( 1, 1);
1105 int err = WSAStartup(wVersionRequested, &wsaData);
1109 printf("Can not start Windows Sockets\n");
1119 win32_chdir(const char *dir)
1121 if (p_SetCurrentDirectoryW) {
1122 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1123 make_win32_path_UTF8_2_wchar(&pwszBuf, dir);
1125 BOOL b=p_SetCurrentDirectoryW((LPCWSTR)pwszBuf);
1127 free_pool_memory(pwszBuf);
1130 errno = b_errno_win32;
1134 else if (p_SetCurrentDirectoryA) {
1135 if (0 == p_SetCurrentDirectoryA(dir)) {
1136 errno = b_errno_win32;
1146 win32_mkdir(const char *dir)
1149 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1150 make_win32_path_UTF8_2_wchar(&pwszBuf, dir);
1152 int n = p_wmkdir((LPCWSTR)pwszBuf);
1153 free_pool_memory(pwszBuf);
1162 win32_getcwd(char *buf, int maxlen)
1166 if (p_GetCurrentDirectoryW) {
1167 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1168 pwszBuf = check_pool_memory_size (pwszBuf, maxlen*sizeof(wchar_t));
1170 n = p_GetCurrentDirectoryW(maxlen, (LPWSTR) pwszBuf);
1172 n = wchar_2_UTF8 (buf, (wchar_t *)pwszBuf, maxlen)-1;
1173 free_pool_memory(pwszBuf);
1175 } else if (p_GetCurrentDirectoryA)
1176 n = p_GetCurrentDirectoryA(maxlen, buf);
1178 if (n == 0 || n > maxlen) return NULL;
1180 if (n+1 > maxlen) return NULL;
1189 win32_fputs(const char *string, FILE *stream)
1191 /* we use WriteConsoleA / WriteConsoleA
1192 so we can be sure that unicode support works on win32.
1193 with fallback if something fails
1196 HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
1197 if (hOut && (hOut != INVALID_HANDLE_VALUE) && p_WideCharToMultiByte &&
1198 p_MultiByteToWideChar && (stream == stdout)) {
1200 POOLMEM* pwszBuf = get_pool_memory(PM_MESSAGE);
1202 DWORD dwCharsWritten;
1205 dwChars = UTF8_2_wchar(&pwszBuf, string);
1207 /* try WriteConsoleW */
1208 if (WriteConsoleW (hOut, pwszBuf, dwChars-1, &dwCharsWritten, NULL)) {
1209 free_pool_memory(pwszBuf);
1210 return dwCharsWritten;
1213 /* convert to local codepage and try WriteConsoleA */
1214 POOLMEM* pszBuf = get_pool_memory(PM_MESSAGE);
1215 pszBuf = check_pool_memory_size(pszBuf, dwChars+1);
1217 dwChars = p_WideCharToMultiByte(GetConsoleOutputCP(),0,(LPCWSTR) pwszBuf,-1,pszBuf,dwChars,NULL,NULL);
1218 free_pool_memory(pwszBuf);
1220 if (WriteConsoleA (hOut, pszBuf, dwChars-1, &dwCharsWritten, NULL)) {
1221 free_pool_memory(pszBuf);
1222 return dwCharsWritten;
1226 return fputs(string, stream);
1230 win32_cgets (char* buffer, int len)
1232 /* we use console ReadConsoleA / ReadConsoleW to be able to read unicode
1233 from the win32 console and fallback if seomething fails */
1235 HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
1236 if (hIn && (hIn != INVALID_HANDLE_VALUE) && p_WideCharToMultiByte && p_MultiByteToWideChar) {
1238 wchar_t wszBuf[1024];
1241 /* nt and unicode conversion */
1242 if (ReadConsoleW (hIn, wszBuf, 1024, &dwRead, NULL)) {
1244 /* null terminate at end */
1245 if (wszBuf[dwRead-1] == L'\n') {
1246 wszBuf[dwRead-1] = L'\0';
1250 if (wszBuf[dwRead-1] == L'\r') {
1251 wszBuf[dwRead-1] = L'\0';
1255 wchar_2_UTF8(buffer, wszBuf, len);
1259 /* win 9x and unicode conversion */
1260 if (ReadConsoleA (hIn, szBuf, 1024, &dwRead, NULL)) {
1262 /* null terminate at end */
1263 if (szBuf[dwRead-1] == L'\n') {
1264 szBuf[dwRead-1] = L'\0';
1268 if (szBuf[dwRead-1] == L'\r') {
1269 szBuf[dwRead-1] = L'\0';
1273 /* convert from ansii to wchar_t */
1274 p_MultiByteToWideChar(GetConsoleCP(), 0, szBuf, -1, wszBuf,1024);
1275 /* convert from wchar_t to UTF-8 */
1276 if (wchar_2_UTF8(buffer, wszBuf, len))
1282 if (fgets(buffer, len, stdin))
1289 win32_unlink(const char *filename)
1293 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1294 make_win32_path_UTF8_2_wchar(&pwszBuf, filename);
1296 nRetCode = _wunlink((LPCWSTR) pwszBuf);
1298 /* special case if file is readonly,
1299 we retry but unset attribute before */
1300 if (nRetCode == -1 && errno == EACCES && p_SetFileAttributesW && p_GetFileAttributesW) {
1301 DWORD dwAttr = p_GetFileAttributesW((LPCWSTR)pwszBuf);
1302 if (dwAttr != INVALID_FILE_ATTRIBUTES) {
1303 if (p_SetFileAttributesW((LPCWSTR)pwszBuf, dwAttr & ~FILE_ATTRIBUTE_READONLY)) {
1304 nRetCode = _wunlink((LPCWSTR) pwszBuf);
1305 /* reset to original if it didn't help */
1307 p_SetFileAttributesW((LPCWSTR)pwszBuf, dwAttr);
1311 free_pool_memory(pwszBuf);
1313 nRetCode = _unlink(filename);
1315 /* special case if file is readonly,
1316 we retry but unset attribute before */
1317 if (nRetCode == -1 && errno == EACCES && p_SetFileAttributesA && p_GetFileAttributesA) {
1318 DWORD dwAttr = p_GetFileAttributesA(filename);
1319 if (dwAttr != INVALID_FILE_ATTRIBUTES) {
1320 if (p_SetFileAttributesA(filename, dwAttr & ~FILE_ATTRIBUTE_READONLY)) {
1321 nRetCode = _unlink(filename);
1322 /* reset to original if it didn't help */
1324 p_SetFileAttributesA(filename, dwAttr);
1333 #include "mswinver.h"
1335 char WIN_VERSION_LONG[64];
1336 char WIN_VERSION[32];
1337 char WIN_RAWVERSION[32];
1344 static winver INIT; // cause constructor to be called before main()
1347 winver::winver(void)
1349 const char *version = "";
1350 const char *platform = "";
1351 OSVERSIONINFO osvinfo;
1352 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
1354 // Get the current OS version
1355 if (!GetVersionEx(&osvinfo)) {
1356 version = "Unknown";
1357 platform = "Unknown";
1359 const int ver = _mkversion(osvinfo.dwPlatformId,
1360 osvinfo.dwMajorVersion,
1361 osvinfo.dwMinorVersion);
1362 snprintf(WIN_RAWVERSION, sizeof(WIN_RAWVERSION), "Windows %#08x", ver);
1365 case MS_WINDOWS_95: (version = "Windows 95"); break;
1366 case MS_WINDOWS_98: (version = "Windows 98"); break;
1367 case MS_WINDOWS_ME: (version = "Windows ME"); break;
1368 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
1369 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
1370 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
1371 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
1372 default: version = WIN_RAWVERSION; break;
1375 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
1376 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
1377 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
1380 HANDLE h = CreateFile("G:\\foobar", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
1384 BPIPE *b = open_bpipe("ls -l", 10, "r");
1386 while (!feof(b->rfd)) {
1387 fgets(buf, sizeof(buf), b->rfd);
1393 BOOL CreateChildProcess(VOID);
1394 VOID WriteToPipe(VOID);
1395 VOID ReadFromPipe(VOID);
1396 VOID ErrorExit(LPCSTR);
1397 VOID ErrMsg(LPTSTR, BOOL);
1400 * Check for a quoted path, if an absolute path name is given and it contains
1401 * spaces it will need to be quoted. i.e. "c:/Program Files/foo/bar.exe"
1402 * CreateProcess() says the best way to ensure proper results with executables
1403 * with spaces in path or filename is to quote the string.
1406 getArgv0(const char *cmdline)
1411 for (cp = cmdline; *cp; cp++)
1416 if (!inquote && isspace(*cp))
1421 int len = cp - cmdline;
1422 char *rval = (char *)malloc(len+1);
1436 * OK, so it would seem CreateProcess only handles true executables:
1437 * .com or .exe files.
1438 * So test to see whether we're getting a .bat file and if so grab
1439 * $COMSPEC value and pass batch file to it.
1442 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
1444 static const char *comspec = NULL;
1445 PROCESS_INFORMATION piProcInfo;
1446 STARTUPINFOA siStartInfo;
1447 BOOL bFuncRetn = FALSE;
1449 if (comspec == NULL) {
1450 comspec = getenv("COMSPEC");
1452 if (comspec == NULL) // should never happen
1453 return INVALID_HANDLE_VALUE;
1455 // Set up members of the PROCESS_INFORMATION structure.
1456 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
1458 // Set up members of the STARTUPINFO structure.
1460 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
1461 siStartInfo.cb = sizeof(STARTUPINFO);
1462 // setup new process to use supplied handles for stdin,stdout,stderr
1463 // if supplied handles are not used the send a copy of our STD_HANDLE
1465 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
1467 if (in != INVALID_HANDLE_VALUE)
1468 siStartInfo.hStdInput = in;
1470 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
1472 if (out != INVALID_HANDLE_VALUE)
1473 siStartInfo.hStdOutput = out;
1475 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1476 if (err != INVALID_HANDLE_VALUE)
1477 siStartInfo.hStdError = err;
1479 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
1480 // Create the child process.
1483 int cmdLen = strlen(cmdline) + strlen(comspec) + 16;
1485 char *cmdLine = (char *)alloca(cmdLen);
1487 bstrncpy(exeFile, comspec, sizeof(exeFile));
1488 bstrncpy(cmdLine, comspec, cmdLen);
1489 bstrncat(cmdLine, " /c ", cmdLen);
1490 bstrncat(cmdLine, cmdline, cmdLen);
1492 // try to execute program
1493 bFuncRetn = CreateProcessA(exeFile,
1494 cmdLine, // command line
1495 NULL, // process security attributes
1496 NULL, // primary thread security attributes
1497 TRUE, // handles are inherited
1498 0, // creation flags
1499 NULL, // use parent's environment
1500 NULL, // use parent's current directory
1501 &siStartInfo, // STARTUPINFO pointer
1502 &piProcInfo); // receives PROCESS_INFORMATION
1504 if (bFuncRetn == 0) {
1505 ErrorExit("CreateProcess failed\n");
1506 const char *err = errorString();
1507 d_msg(__FILE__, __LINE__, 99,
1508 "CreateProcess(%s, %s, ...)=%s\n", exeFile, cmdLine, err);
1509 LocalFree((void *)err);
1510 return INVALID_HANDLE_VALUE;
1512 // we don't need a handle on the process primary thread so we close
1514 CloseHandle(piProcInfo.hThread);
1516 return piProcInfo.hProcess;
1521 ErrorExit (LPCSTR lpszMessage)
1523 d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
1528 typedef struct s_bpipe {
1530 time_t worker_stime;
1539 CloseIfValid(HANDLE handle)
1541 if (handle != INVALID_HANDLE_VALUE)
1542 CloseHandle(handle);
1547 open_bpipe(char *prog, int wait, const char *mode)
1549 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
1550 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
1553 SECURITY_ATTRIBUTES saAttr;
1557 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
1558 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
1559 hInputFile = INVALID_HANDLE_VALUE;
1561 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
1562 memset((void *)bpipe, 0, sizeof(BPIPE));
1564 int mode_read = (mode[0] == 'r');
1565 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
1568 // Set the bInheritHandle flag so pipe handles are inherited.
1570 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1571 saAttr.bInheritHandle = TRUE;
1572 saAttr.lpSecurityDescriptor = NULL;
1576 // Create a pipe for the child process's STDOUT.
1577 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
1578 ErrorExit("Stdout pipe creation failed\n");
1581 // Create noninheritable read handle and close the inheritable read
1584 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
1585 GetCurrentProcess(), &hChildStdoutRdDup , 0,
1587 DUPLICATE_SAME_ACCESS);
1589 ErrorExit("DuplicateHandle failed");
1593 CloseHandle(hChildStdoutRd);
1594 hChildStdoutRd = INVALID_HANDLE_VALUE;
1599 // Create a pipe for the child process's STDIN.
1601 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
1602 ErrorExit("Stdin pipe creation failed\n");
1606 // Duplicate the write handle to the pipe so it is not inherited.
1607 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
1608 GetCurrentProcess(), &hChildStdinWrDup,
1610 FALSE, // not inherited
1611 DUPLICATE_SAME_ACCESS);
1613 ErrorExit("DuplicateHandle failed");
1617 CloseHandle(hChildStdinWr);
1618 hChildStdinWr = INVALID_HANDLE_VALUE;
1620 // spawn program with redirected handles as appropriate
1621 bpipe->worker_pid = (pid_t)
1622 CreateChildProcess(prog, // commandline
1623 hChildStdinRd, // stdin HANDLE
1624 hChildStdoutWr, // stdout HANDLE
1625 hChildStdoutWr); // stderr HANDLE
1627 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1631 bpipe->worker_stime = time(NULL);
1634 CloseHandle(hChildStdoutWr); // close our write side so when
1635 // process terminates we can
1637 // ugly but convert WIN32 HANDLE to FILE*
1638 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1640 bpipe->rfd = _fdopen(rfd, "r");
1644 CloseHandle(hChildStdinRd); // close our read side so as not
1645 // to interfre with child's copy
1646 // ugly but convert WIN32 HANDLE to FILE*
1647 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1649 bpipe->wfd = _fdopen(wfd, "w");
1654 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1661 CloseIfValid(hChildStdoutRd);
1662 CloseIfValid(hChildStdoutRdDup);
1663 CloseIfValid(hChildStdinWr);
1664 CloseIfValid(hChildStdinWrDup);
1666 free((void *) bpipe);
1667 errno = b_errno_win32; /* do GetLastError() for error code */
1674 kill(int pid, int signal)
1677 if (!TerminateProcess((HANDLE)pid, (UINT) signal)) {
1679 errno = b_errno_win32;
1681 CloseHandle((HANDLE)pid);
1688 close_bpipe(BPIPE *bpipe)
1691 int32_t remaining_wait = bpipe->wait;
1693 if (remaining_wait == 0) { /* wait indefinitely */
1694 remaining_wait = INT32_MAX;
1698 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) {
1699 const char *err = errorString();
1700 rval = b_errno_win32;
1701 d_msg(__FILE__, __LINE__, 0,
1702 "GetExitCode error %s\n", err);
1703 LocalFree((void *)err);
1706 if (exitCode == STILL_ACTIVE) {
1707 if (remaining_wait <= 0) {
1708 rval = ETIME; /* timed out */
1711 bmicrosleep(1, 0); /* wait one second */
1713 } else if (exitCode != 0) {
1714 /* Truncate exit code as it doesn't seem to be correct */
1715 rval = (exitCode & 0xFF) | b_errno_exit;
1718 break; /* Shouldn't get here */
1722 if (bpipe->timer_id) {
1723 stop_child_timer(bpipe->timer_id);
1725 if (bpipe->rfd) fclose(bpipe->rfd);
1726 if (bpipe->wfd) fclose(bpipe->wfd);
1727 free((void *)bpipe);
1732 close_wpipe(BPIPE *bpipe)
1738 if (fclose(bpipe->wfd) != 0) {
1746 #include "findlib/find.h"
1749 utime(const char *fname, struct utimbuf *times)
1754 conv_unix_to_win32_path(fname, tmpbuf, 5000);
1756 cvt_utime_to_ftime(times->actime, acc);
1757 cvt_utime_to_ftime(times->modtime, mod);
1759 HANDLE h = INVALID_HANDLE_VALUE;
1761 if (p_CreateFileW) {
1762 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1763 make_win32_path_UTF8_2_wchar(&pwszBuf, tmpbuf);
1765 h = p_CreateFileW((LPCWSTR)pwszBuf,
1766 FILE_WRITE_ATTRIBUTES,
1767 FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
1770 FILE_FLAG_BACKUP_SEMANTICS, // required for directories
1773 free_pool_memory(pwszBuf);
1774 } else if (p_CreateFileA) {
1775 h = p_CreateFileA(tmpbuf,
1776 FILE_WRITE_ATTRIBUTES,
1777 FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
1780 FILE_FLAG_BACKUP_SEMANTICS, // required for directories
1784 if (h == INVALID_HANDLE_VALUE) {
1785 const char *err = errorString();
1786 d_msg(__FILE__, __LINE__, 99,
1787 "Cannot open file \"%s\" for utime(): ERR=%s", tmpbuf, err);
1788 LocalFree((void *)err);
1789 errno = b_errno_win32;
1793 int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
1796 errno = b_errno_win32;
1801 #if USE_WIN32_COMPAT_IO
1804 open(const char *file, int flags, int mode)
1807 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1808 make_win32_path_UTF8_2_wchar(&pwszBuf, file);
1810 int nRet = p_wopen((LPCWSTR) pwszBuf, flags|_O_BINARY, mode);
1811 free_pool_memory(pwszBuf);
1816 return _open(file, flags|_O_BINARY, mode);
1820 * Note, this works only for a file. If you want
1821 * to close a socket, use closesocket().
1822 * Bacula has been modified in src/lib/bnet.c
1823 * to use closesocket().
1832 #ifndef HAVE_WXCONSOLE
1834 read(int fd, void *buf, ssize_t len)
1836 return _read(fd, buf, (size_t)len);
1840 write(int fd, const void *buf, ssize_t len)
1842 return _write(fd, buf, (size_t)len);
1848 lseek(int fd, off_t offset, int whence)
1850 return (off_t)_lseeki64(fd, offset, whence);
1854 dup2(int fd1, int fd2)
1856 return _dup2(fd1, fd2);
1861 open(const char *file, int flags, int mode)
1864 DWORD shareMode = 0;
1867 HANDLE foo = INVALID_HANDLE_VALUE;
1868 const char *remap = file;
1870 if (flags & O_WRONLY) access = GENERIC_WRITE;
1871 else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
1872 else access = GENERIC_READ;
1874 if (flags & O_CREAT) create = CREATE_NEW;
1875 else create = OPEN_EXISTING;
1877 if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
1879 if (!(flags & O_EXCL))
1880 shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
1882 if (flags & O_APPEND) {
1883 printf("open...APPEND not implemented yet.");
1887 if (p_CreateFileW) {
1888 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1889 make_win32_path_UTF8_2_wchar(pwszBuf, file);
1891 foo = p_CreateFileW((LPCWSTR) pwszBuf, access, shareMode, NULL, create, msflags, NULL);
1892 free_pool_memory(pwszBuf);
1893 } else if (p_CreateFileA)
1894 foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
1896 if (INVALID_HANDLE_VALUE == foo) {
1897 errno = b_errno_win32;
1908 if (!CloseHandle((HANDLE)fd)) {
1909 errno = b_errno_win32;
1917 write(int fd, const void *data, ssize_t len)
1921 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
1922 if (status) return bwrite;
1923 errno = b_errno_win32;
1929 read(int fd, void *data, ssize_t len)
1934 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
1935 if (status) return bread;
1936 errno = b_errno_win32;
1941 lseek(int fd, off_t offset, int whence)
1947 method = FILE_BEGIN;
1950 method = FILE_CURRENT;
1960 if ((val=SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method)) == INVALID_SET_FILE_POINTER) {
1961 errno = b_errno_win32;
1964 /* ***FIXME*** I doubt this works right */
1981 /* syslog function, added by Nicolas Boichat */
1983 void openlog(const char *ident, int option, int facility) {}
1986 /* Temp kludges ***FIXME**** */
1988 unsigned int alarm(unsigned int seconds)