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
28 #define b_errno_win32 (1<<29)
31 /* UTF-8 to UCS2 path conversion is expensive,
32 so we cache the conversion. During backup the
33 conversion is called 3 times (lstat, attribs, open),
34 by using the cache this is reduced to 1 time */
36 static POOLMEM *g_pWin32ConvUTF8Cache = get_pool_memory (PM_FNAME);
37 static POOLMEM *g_pWin32ConvUCS2Cache = get_pool_memory (PM_FNAME);
38 static DWORD g_dwWin32ConvUTF8strlen = 0;
39 static pthread_mutex_t Win32Convmutex = PTHREAD_MUTEX_INITIALIZER;
41 static t_pVSSPathConvert g_pVSSPathConvert;
42 static t_pVSSPathConvertW g_pVSSPathConvertW;
44 void SetVSSPathConvert(t_pVSSPathConvert pPathConvert, t_pVSSPathConvertW pPathConvertW)
46 g_pVSSPathConvert = pPathConvert;
47 g_pVSSPathConvertW = pPathConvertW;
50 void Win32ConvCleanupCache()
52 if (g_pWin32ConvUTF8Cache) {
53 free_pool_memory(g_pWin32ConvUTF8Cache);
54 g_pWin32ConvUTF8Cache = NULL;
57 if (g_pWin32ConvUCS2Cache) {
58 free_pool_memory(g_pWin32ConvUCS2Cache);
59 g_pWin32ConvUCS2Cache = NULL;
62 g_dwWin32ConvUTF8strlen = 0;
66 /* to allow the usage of the original version in this file here */
70 //#define USE_WIN32_COMPAT_IO 1
71 #define USE_WIN32_32KPATHCONVERSION 1
73 extern DWORD g_platform_id;
74 extern DWORD g_MinorVersion;
76 // from MicroSoft SDK (KES) is the diff between Jan 1 1601 and Jan 1 1970
78 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000ULL
80 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000I64
83 #define WIN32_FILETIME_SCALE 10000000 // 100ns/second
85 void conv_unix_to_win32_path(const char *name, char *win32_name, DWORD dwSize)
87 const char *fname = name;
88 char *tname = win32_name;
90 Dmsg0(100, "Enter convert_unix_to_win32_path\n");
92 if ((name[0] == '/' || name[0] == '\\') &&
93 (name[1] == '/' || name[1] == '\\') &&
95 (name[3] == '/' || name[3] == '\\')) {
100 *win32_name++ = '\\';
103 } else if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS &&
104 g_pVSSPathConvert == NULL) {
105 /* allow path to be 32767 bytes */
106 *win32_name++ = '\\';
107 *win32_name++ = '\\';
109 *win32_name++ = '\\';
113 /* Check for Unix separator and convert to Win32 */
114 if (name[0] == '/' && name[1] == '/') { /* double slash? */
115 name++; /* yes, skip first one */
118 *win32_name++ = '\\'; /* convert char */
119 /* If Win32 separated that is "quoted", remove quote */
120 } else if (*name == '\\' && name[1] == '\\') {
121 *win32_name++ = '\\';
122 name++; /* skip first \ */
124 *win32_name++ = *name; /* copy character */
128 /* Strip any trailing slash, if we stored something */
129 /* but leave "c:\" with backslash (root directory case */
130 if (*fname != 0 && win32_name[-1] == '\\' && strlen (fname) != 3) {
136 /* here we convert to VSS specific file name which
137 can get longer because VSS will make something like
138 \\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\bacula\\uninstall.exe
139 from c:\bacula\uninstall.exe
141 Dmsg1(100, "path=%s\n", tname);
142 if (g_pVSSPathConvert != NULL) {
143 POOLMEM *pszBuf = get_pool_memory (PM_FNAME);
144 pszBuf = check_pool_memory_size(pszBuf, dwSize);
145 bstrncpy(pszBuf, tname, strlen(tname)+1);
146 g_pVSSPathConvert(pszBuf, tname, dwSize);
147 free_pool_memory(pszBuf);
150 Dmsg1(100, "Leave cvt_u_to_win32_path path=%s\n", tname);
153 /* Conversion of a Unix filename to a Win32 filename */
154 void unix_name_to_win32(POOLMEM **win32_name, char *name)
156 /* One extra byte should suffice, but we double it */
157 /* add MAX_PATH bytes for VSS shadow copy name */
158 DWORD dwSize = 2*strlen(name)+MAX_PATH;
159 *win32_name = check_pool_memory_size(*win32_name, dwSize);
160 conv_unix_to_win32_path(name, *win32_name, dwSize);
164 make_wchar_win32_path(POOLMEM *pszUCSPath, BOOL *pBIsRawPath /*= NULL*/)
166 /* created 02/27/2006 Thorsten Engel
168 * This function expects an UCS-encoded standard wchar_t in pszUCSPath and
169 * will complete the input path to an absolue path of the form \\?\c:\path\file
171 * With this trick, it is possible to have 32K characters long paths.
173 * Optionally one can use pBIsRawPath to determine id pszUCSPath contains a path
174 * to a raw windows partition.
177 Dmsg0(100, "Enter wchar_win32_path\n");
179 *pBIsRawPath = FALSE; /* Initialize, set later */
182 if (!p_GetCurrentDirectoryW) {
183 Dmsg0(100, "Leave wchar_win32_path no change \n");
187 wchar_t *name = (wchar_t *)pszUCSPath;
189 /* if it has already the desired form, exit without changes */
190 if (wcslen(name) > 3 && wcsncmp(name, L"\\\\?\\", 4) == 0) {
191 Dmsg0(100, "Leave wchar_win32_path no change \n");
195 POOLMEM *pwszBuf = get_pool_memory(PM_FNAME);
196 POOLMEM *pwszCurDirBuf = get_pool_memory(PM_FNAME);
197 DWORD dwCurDirPathSize = 0;
199 /* get buffer with enough size (name+max 6. wchars+1 null terminator */
200 DWORD dwBufCharsNeeded = (wcslen(name)+7);
201 pwszBuf = check_pool_memory_size(pwszBuf, dwBufCharsNeeded*sizeof(wchar_t));
203 /* add \\?\ to support 32K long filepaths
204 it is important to make absolute paths, so we add drive and
205 current path if necessary */
207 BOOL bAddDrive = TRUE;
208 BOOL bAddCurrentPath = TRUE;
209 BOOL bAddPrefix = TRUE;
211 /* does path begin with drive? if yes, it is absolute */
212 if (wcslen(name) >= 3 && (iswalpha (*name) && *(name+1) == ':'
213 && (*(name+2) == '\\' || *(name+2) == '/'))) {
215 bAddCurrentPath = FALSE;
218 /* is path absolute? */
219 if (*name == '/' || *name == '\\')
220 bAddCurrentPath = FALSE;
222 /* is path relative to itself?, if yes, skip ./ */
223 if (wcslen(name) > 2 && ((wcsncmp(name, L"./", 2) == 0) || (wcsncmp(name, L".\\", 2) == 0))) {
227 /* is path of form '//./'? */
228 if (wcslen(name) > 3 && ((wcsncmp(name, L"//./", 4) == 0) || (wcsncmp(name, L"\\\\.\\", 4) == 0))) {
230 bAddCurrentPath = FALSE;
237 int nParseOffset = 0;
239 /* add 4 bytes header */
242 wcscpy((wchar_t *)pwszBuf, L"\\\\?\\");
245 /* get current path if needed */
246 if (bAddDrive || bAddCurrentPath) {
247 dwCurDirPathSize = p_GetCurrentDirectoryW(0, NULL);
248 if (dwCurDirPathSize > 0) {
249 /* get directory into own buffer as it may either return c:\... or \\?\C:\.... */
250 pwszCurDirBuf = check_pool_memory_size(pwszCurDirBuf, (dwCurDirPathSize+1)*sizeof(wchar_t));
251 p_GetCurrentDirectoryW(dwCurDirPathSize,(wchar_t *)pwszCurDirBuf);
253 /* we have no info for doing so */
255 bAddCurrentPath = FALSE;
260 /* add drive if needed */
261 if (bAddDrive && !bAddCurrentPath) {
264 if (dwCurDirPathSize > 3 && wcsncmp((LPCWSTR)pwszCurDirBuf, L"\\\\?\\", 4) == 0) {
265 /* copy drive character */
266 wcsncpy((wchar_t *)szDrive, (LPCWSTR)pwszCurDirBuf+4, 2);
268 /* copy drive character */
269 wcsncpy((wchar_t *)szDrive, (LPCWSTR)pwszCurDirBuf, 2);
274 wcscat((wchar_t *)pwszBuf, szDrive);
278 /* add path if needed */
279 if (bAddCurrentPath) {
280 /* the 1 add. character is for the eventually added backslash */
281 dwBufCharsNeeded += dwCurDirPathSize+1;
282 pwszBuf = check_pool_memory_size(pwszBuf, dwBufCharsNeeded*sizeof(wchar_t));
283 /* get directory into own buffer as it may either return c:\... or \\?\C:\.... */
285 if (dwCurDirPathSize > 3 && wcsncmp((LPCWSTR)pwszCurDirBuf, L"\\\\?\\", 4) == 0) {
286 /* copy complete string */
287 wcscpy((wchar_t *)pwszBuf, (LPCWSTR)pwszCurDirBuf);
290 wcscat((wchar_t *)pwszBuf, (LPCWSTR)pwszCurDirBuf);
293 nParseOffset = wcslen((LPCWSTR) pwszBuf);
295 /* check if path ends with backslash, if not, add one */
296 if (*((wchar_t *)pwszBuf+nParseOffset-1) != L'\\') {
297 wcscat((wchar_t *)pwszBuf, L"\\");
303 wchar_t *win32_name = (wchar_t *)pwszBuf+nParseOffset;
306 /* Check for Unix separator and convert to Win32 */
308 *win32_name++ = '\\'; /* convert char */
309 /* If Win32 separated that is "quoted", remove quote */
310 /* HELPME (Thorsten Engel): I don't understand the following part
311 * and it removes a backslash from e.g. "\\.\c:" which I need for
312 * RAW device access. So I took it out.
315 } else if (*name == '\\' && name[1] == '\\') {
316 *win32_name++ = '\\';
317 name++; /* skip first \ */
320 *win32_name++ = *name; /* copy character */
325 /* null terminate string */
328 /* here we convert to VSS specific file name which
329 * can get longer because VSS will make something like
330 * \\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\bacula\\uninstall.exe
331 * from c:\bacula\uninstall.exe
333 if (g_pVSSPathConvertW != NULL) {
334 /* is output buffer large enough? */
335 pwszBuf = check_pool_memory_size(pwszBuf, (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t));
336 /* create temp. buffer */
337 POOLMEM* pszBuf = get_pool_memory(PM_FNAME);
338 pszBuf = check_pool_memory_size(pszBuf, (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t));
343 wcsncpy((wchar_t *)pszBuf, (wchar_t *)pwszBuf+nParseOffset, wcslen((wchar_t *)pwszBuf)+1-nParseOffset);
344 g_pVSSPathConvertW((wchar_t *)pszBuf, (wchar_t *)pwszBuf, dwBufCharsNeeded+MAX_PATH);
345 free_pool_memory(pszBuf);
348 free_pool_memory(pszUCSPath);
349 free_pool_memory(pwszCurDirBuf);
351 Dmsg1(100, "Leave wchar_win32_path=%s\n", pwszBuf);
356 wchar_2_UTF8(char *pszUTF, const wchar_t *pszUCS, int cchChar)
358 /* the return value is the number of bytes written to the buffer.
359 The number includes the byte for the null terminator. */
361 if (p_WideCharToMultiByte) {
362 int nRet = p_WideCharToMultiByte(CP_UTF8,0,pszUCS,-1,pszUTF,cchChar,NULL,NULL);
371 UTF8_2_wchar(POOLMEM **ppszUCS, const char *pszUTF)
373 /* the return value is the number of wide characters written to the buffer. */
374 /* convert null terminated string from utf-8 to ucs2, enlarge buffer if necessary */
376 if (p_MultiByteToWideChar) {
377 /* strlen of UTF8 +1 is enough */
378 DWORD cchSize = (strlen(pszUTF)+1);
379 *ppszUCS = check_pool_memory_size(*ppszUCS, cchSize*sizeof (wchar_t));
381 int nRet = p_MultiByteToWideChar(CP_UTF8, 0, pszUTF, -1, (LPWSTR) *ppszUCS,cchSize);
391 wchar_win32_path(const char *name, wchar_t *win32_name)
393 const char *fname = name;
395 /* Check for Unix separator and convert to Win32 */
397 *win32_name++ = '\\'; /* convert char */
398 /* If Win32 separated that is "quoted", remove quote */
399 } else if (*name == '\\' && name[1] == '\\') {
400 *win32_name++ = '\\';
401 name++; /* skip first \ */
403 *win32_name++ = *name; /* copy character */
407 /* Strip any trailing slash, if we stored something */
408 if (*fname != 0 && win32_name[-1] == '\\') {
416 make_win32_path_UTF8_2_wchar(POOLMEM **pszUCS, const char *pszUTF, BOOL* pBIsRawPath /*= NULL*/)
419 /* if we find the utf8 string in cache, we use the cached ucs2 version.
420 we compare the stringlength first (quick check) and then compare the content.
422 if (g_dwWin32ConvUTF8strlen == strlen(pszUTF)) {
423 if (bstrcmp(pszUTF, g_pWin32ConvUTF8Cache)) {
424 int32_t nBufSize = sizeof_pool_memory(g_pWin32ConvUCS2Cache);
425 *pszUCS = check_pool_memory_size(*pszUCS, nBufSize);
426 wcscpy((LPWSTR) *pszUCS, (LPWSTR) g_pWin32ConvUCS2Cache);
428 return nBufSize / sizeof (WCHAR);
432 /* helper to convert from utf-8 to UCS-2 and to complete a path for 32K path syntax */
433 int nRet = UTF8_2_wchar(pszUCS, pszUTF);
435 #ifdef USE_WIN32_32KPATHCONVERSION
436 /* add \\?\ to support 32K long filepaths */
437 *pszUCS = make_wchar_win32_path(*pszUCS, pBIsRawPath);
440 *pBIsRawPath = FALSE;
444 g_pWin32ConvUCS2Cache = check_pool_memory_size(g_pWin32ConvUCS2Cache, sizeof_pool_memory(*pszUCS));
445 wcscpy((LPWSTR) g_pWin32ConvUCS2Cache, (LPWSTR) *pszUCS);
447 g_dwWin32ConvUTF8strlen = strlen(pszUTF);
448 g_pWin32ConvUTF8Cache = check_pool_memory_size(g_pWin32ConvUTF8Cache, g_dwWin32ConvUTF8strlen+1);
449 bstrncpy(g_pWin32ConvUTF8Cache, pszUTF, g_dwWin32ConvUTF8strlen+1);
455 #if !defined(_MSC_VER) || (_MSC_VER < 1400) // VC8+
462 int fcntl(int fd, int cmd)
467 int chmod(const char *, mode_t)
472 int chown(const char *k, uid_t, gid_t)
477 int lchown(const char *k, uid_t, gid_t)
489 srandom(unsigned int seed)
493 // /////////////////////////////////////////////////////////////////
494 // convert from Windows concept of time to Unix concept of time
495 // /////////////////////////////////////////////////////////////////
497 cvt_utime_to_ftime(const time_t &time, FILETIME &wintime)
499 uint64_t mstime = time;
500 mstime *= WIN32_FILETIME_SCALE;
501 mstime += WIN32_FILETIME_ADJUST;
503 #if defined(_MSC_VER)
504 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
506 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffUL);
508 wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
512 cvt_ftime_to_utime(const FILETIME &time)
514 uint64_t mstime = time.dwHighDateTime;
516 mstime |= time.dwLowDateTime;
518 mstime -= WIN32_FILETIME_ADJUST;
519 mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
521 return (time_t) (mstime & 0xffffffff);
529 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
530 FORMAT_MESSAGE_FROM_SYSTEM |
531 FORMAT_MESSAGE_IGNORE_INSERTS,
534 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
539 /* Strip any \r or \n */
540 char *rval = (char *) lpMsgBuf;
541 char *cp = strchr(rval, '\r');
545 cp = strchr(rval, '\n');
554 statDir(const char *file, struct stat *sb)
556 WIN32_FIND_DATAW info_w; // window's file info
557 WIN32_FIND_DATAA info_a; // window's file info
559 // cache some common vars to make code more transparent
560 DWORD* pdwFileAttributes;
561 DWORD* pnFileSizeHigh;
562 DWORD* pnFileSizeLow;
563 FILETIME* pftLastAccessTime;
564 FILETIME* pftLastWriteTime;
565 FILETIME* pftCreationTime;
567 if (file[1] == ':' && file[2] == 0) {
568 Dmsg1(99, "faking ROOT attrs(%s).\n", file);
569 sb->st_mode = S_IFDIR;
570 sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
577 HANDLE h = INVALID_HANDLE_VALUE;
579 // use unicode or ascii
580 if (p_FindFirstFileW) {
581 POOLMEM* pwszBuf = get_pool_memory (PM_FNAME);
582 make_win32_path_UTF8_2_wchar(&pwszBuf, file);
584 h = p_FindFirstFileW((LPCWSTR) pwszBuf, &info_w);
585 free_pool_memory(pwszBuf);
587 pdwFileAttributes = &info_w.dwFileAttributes;
588 pnFileSizeHigh = &info_w.nFileSizeHigh;
589 pnFileSizeLow = &info_w.nFileSizeLow;
590 pftLastAccessTime = &info_w.ftLastAccessTime;
591 pftLastWriteTime = &info_w.ftLastWriteTime;
592 pftCreationTime = &info_w.ftCreationTime;
594 else if (p_FindFirstFileA) {
595 h = p_FindFirstFileA(file, &info_a);
597 pdwFileAttributes = &info_a.dwFileAttributes;
598 pnFileSizeHigh = &info_a.nFileSizeHigh;
599 pnFileSizeLow = &info_a.nFileSizeLow;
600 pftLastAccessTime = &info_a.ftLastAccessTime;
601 pftLastWriteTime = &info_a.ftLastWriteTime;
602 pftCreationTime = &info_a.ftCreationTime;
605 if (h == INVALID_HANDLE_VALUE) {
606 const char *err = errorString();
607 Dmsg2(99, "FindFirstFile(%s):%s\n", file, err);
608 LocalFree((void *)err);
609 errno = b_errno_win32;
613 sb->st_mode = 0777; /* start with everything */
614 if (*pdwFileAttributes & FILE_ATTRIBUTE_READONLY)
615 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
616 if (*pdwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
617 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
618 if (*pdwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
619 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
620 sb->st_mode |= S_IFDIR;
622 sb->st_size = *pnFileSizeHigh;
624 sb->st_size |= *pnFileSizeLow;
625 sb->st_blksize = 4096;
626 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
628 sb->st_atime = cvt_ftime_to_utime(*pftLastAccessTime);
629 sb->st_mtime = cvt_ftime_to_utime(*pftLastWriteTime);
630 sb->st_ctime = cvt_ftime_to_utime(*pftCreationTime);
637 fstat(int fd, struct stat *sb)
639 BY_HANDLE_FILE_INFORMATION info;
642 if (!GetFileInformationByHandle((HANDLE)fd, &info)) {
643 const char *err = errorString();
644 Dmsg2(99, "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
645 LocalFree((void *)err);
646 errno = b_errno_win32;
650 sb->st_dev = info.dwVolumeSerialNumber;
651 sb->st_ino = info.nFileIndexHigh;
653 sb->st_ino |= info.nFileIndexLow;
654 sb->st_nlink = (short)info.nNumberOfLinks;
655 if (sb->st_nlink > 1) {
656 Dmsg1(99, "st_nlink=%d\n", sb->st_nlink);
659 sb->st_mode = 0777; /* start with everything */
660 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
661 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
662 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
663 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
664 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
665 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
666 sb->st_mode |= S_IFREG;
668 sb->st_size = info.nFileSizeHigh;
670 sb->st_size |= info.nFileSizeLow;
671 sb->st_blksize = 4096;
672 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
673 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
674 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
675 sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime);
681 stat2(const char *file, struct stat *sb)
686 conv_unix_to_win32_path(file, tmpbuf, 1024);
688 DWORD attr = (DWORD)-1;
690 if (p_GetFileAttributesW) {
691 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
692 make_win32_path_UTF8_2_wchar(&pwszBuf, tmpbuf);
694 attr = p_GetFileAttributesW((LPCWSTR) pwszBuf);
695 free_pool_memory(pwszBuf);
696 } else if (p_GetFileAttributesA) {
697 attr = p_GetFileAttributesA(tmpbuf);
700 if (attr == (DWORD)-1) {
701 const char *err = errorString();
702 Dmsg2(99, "GetFileAttributes(%s): %s\n", tmpbuf, err);
703 LocalFree((void *)err);
704 errno = b_errno_win32;
708 if (attr & FILE_ATTRIBUTE_DIRECTORY)
709 return statDir(tmpbuf, sb);
711 h = CreateFileA(tmpbuf, GENERIC_READ,
712 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
714 if (h == INVALID_HANDLE_VALUE) {
715 const char *err = errorString();
716 Dmsg2(99, "Cannot open file for stat (%s):%s\n", tmpbuf, err);
717 LocalFree((void *)err);
718 errno = b_errno_win32;
722 rval = fstat((int)h, sb);
729 stat(const char *file, struct stat *sb)
731 WIN32_FILE_ATTRIBUTE_DATA data;
735 memset(sb, 0, sizeof(*sb));
737 /* why not allow win 95 to use p_GetFileAttributesExA ?
738 * this function allows _some_ open files to be stat'ed
739 * if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
740 * return stat2(file, sb);
744 if (p_GetFileAttributesExW) {
745 /* dynamically allocate enough space for UCS2 filename */
746 POOLMEM* pwszBuf = get_pool_memory (PM_FNAME);
747 make_win32_path_UTF8_2_wchar(&pwszBuf, file);
749 BOOL b = p_GetFileAttributesExW((LPCWSTR) pwszBuf, GetFileExInfoStandard, &data);
750 free_pool_memory(pwszBuf);
753 return stat2(file, sb);
755 } else if (p_GetFileAttributesExA) {
756 if (!p_GetFileAttributesExA(file, GetFileExInfoStandard, &data)) {
757 return stat2(file, sb);
760 return stat2(file, sb);
763 sb->st_mode = 0777; /* start with everything */
764 if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
765 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
767 if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
768 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
770 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
771 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
773 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
774 sb->st_mode |= S_IFDIR;
776 sb->st_mode |= S_IFREG;
780 sb->st_size = data.nFileSizeHigh;
782 sb->st_size |= data.nFileSizeLow;
783 sb->st_blksize = 4096;
784 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
785 sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
786 sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
787 sb->st_ctime = cvt_ftime_to_utime(data.ftCreationTime);
791 int fcntl(int fd, int cmd, long arg)
814 lstat(const char *file, struct stat *sb)
816 return stat(file, sb);
832 execvp(const char *, char *[]) {
853 waitpid(int, int*, int)
860 readlink(const char *, char *, int)
869 strcasecmp(const char *s1, const char *s2)
871 register int ch1, ch2;
874 return 0; /* strings are equal if same object. */
884 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
891 strncasecmp(const char *s1, const char *s2, int len)
893 register int ch1 = 0, ch2 = 0;
896 return 0; /* strings are equal if same object. */
907 if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break;
914 gettimeofday(struct timeval *tv, struct timezone *)
924 if (!SystemTimeToFileTime(&now, &tmp)) {
925 errno = b_errno_win32;
929 int64_t _100nsec = tmp.dwHighDateTime;
931 _100nsec |= tmp.dwLowDateTime;
932 _100nsec -= WIN32_FILETIME_ADJUST;
934 tv->tv_sec =(long) (_100nsec / 10000000);
935 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
940 /* For apcupsd this is in src/lib/wincompat.c */
941 extern "C" void syslog(int type, const char *fmt, ...)
943 /*#ifndef HAVE_CONSOLE
944 MessageBox(NULL, msg, "Bacula", MB_OK);
965 // implement opendir/readdir/closedir on top of window's API
969 WIN32_FIND_DATAA data_a; // window's file info (ansii version)
970 WIN32_FIND_DATAW data_w; // window's file info (wchar version)
971 const char *spec; // the directory we're traversing
972 HANDLE dirh; // the search handle
973 BOOL valid_a; // the info in data_a field is valid
974 BOOL valid_w; // the info in data_w field is valid
975 UINT32 offset; // pseudo offset for d_off
979 opendir(const char *path)
981 /* enough space for VSS !*/
982 int max_len = strlen(path) + MAX_PATH;
989 Dmsg1(100, "Opendir path=%s\n", path);
990 rval = (_dir *)malloc(sizeof(_dir));
991 memset (rval, 0, sizeof (_dir));
992 if (rval == NULL) return NULL;
993 char *tspec = (char *)malloc(max_len);
994 if (tspec == NULL) return NULL;
996 conv_unix_to_win32_path(path, tspec, max_len);
997 Dmsg1(100, "win32 path=%s\n", tspec);
999 // add backslash only if there is none yet (think of c:\)
1000 if (tspec[strlen(tspec)-1] != '\\')
1001 bstrncat(tspec, "\\*", max_len);
1003 bstrncat(tspec, "*", max_len);
1007 // convert to wchar_t
1008 if (p_FindFirstFileW) {
1009 POOLMEM* pwcBuf = get_pool_memory(PM_FNAME);;
1010 make_win32_path_UTF8_2_wchar(&pwcBuf, rval->spec);
1012 rval->dirh = p_FindFirstFileW((LPCWSTR)pwcBuf, &rval->data_w);
1014 free_pool_memory(pwcBuf);
1016 if (rval->dirh != INVALID_HANDLE_VALUE)
1018 } else if (p_FindFirstFileA) {
1019 rval->dirh = p_FindFirstFileA(rval->spec, &rval->data_a);
1021 if (rval->dirh != INVALID_HANDLE_VALUE)
1026 Dmsg3(99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
1027 path, rval->spec, rval->dirh);
1030 if (rval->dirh == INVALID_HANDLE_VALUE)
1033 if (rval->valid_w) {
1034 Dmsg1(99, "\tFirstFile=%s\n", rval->data_w.cFileName);
1037 if (rval->valid_a) {
1038 Dmsg1(99, "\tFirstFile=%s\n", rval->data_a.cFileName);
1044 free((void *)rval->spec);
1046 errno = b_errno_win32;
1053 _dir *dp = (_dir *)dirp;
1054 FindClose(dp->dirh);
1055 free((void *)dp->spec);
1061 typedef struct _WIN32_FIND_DATA {
1062 DWORD dwFileAttributes;
1063 FILETIME ftCreationTime;
1064 FILETIME ftLastAccessTime;
1065 FILETIME ftLastWriteTime;
1066 DWORD nFileSizeHigh;
1070 TCHAR cFileName[MAX_PATH];
1071 TCHAR cAlternateFileName[14];
1072 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
1076 copyin(struct dirent &dp, const char *fname)
1080 char *cp = dp.d_name;
1090 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
1092 _dir *dp = (_dir *)dirp;
1093 if (dp->valid_w || dp->valid_a) {
1094 entry->d_off = dp->offset;
1098 char szBuf[MAX_PATH_UTF8+1];
1099 wchar_2_UTF8(szBuf,dp->data_w.cFileName);
1100 dp->offset += copyin(*entry, szBuf);
1101 } else if (dp->valid_a) { // copy ansi (only 1 will be valid)
1102 dp->offset += copyin(*entry, dp->data_a.cFileName);
1105 *result = entry; /* return entry address */
1106 Dmsg4(99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
1107 dirp, entry->d_name, entry->d_reclen, entry->d_off);
1109 // Dmsg0(99, "readdir_r !valid\n");
1110 errno = b_errno_win32;
1114 // get next file, try unicode first
1115 if (p_FindNextFileW)
1116 dp->valid_w = p_FindNextFileW(dp->dirh, &dp->data_w);
1117 else if (p_FindNextFileA)
1118 dp->valid_a = p_FindNextFileA(dp->dirh, &dp->data_a);
1120 dp->valid_a = FALSE;
1121 dp->valid_w = FALSE;
1128 * Dotted IP address to network address
1134 inet_aton(const char *a, struct in_addr *inp)
1137 uint32_t acc = 0, tmp = 0;
1140 if (!isdigit(*cp)) { /* first char must be digit */
1141 return 0; /* error */
1145 tmp = (tmp * 10) + (*cp -'0');
1146 } else if (*cp == '.' || *cp == 0) {
1148 return 0; /* error */
1150 acc = (acc << 8) + tmp;
1154 return 0; /* error */
1156 } while (*cp++ != 0);
1157 if (dotc != 4) { /* want 3 .'s plus EOS */
1158 return 0; /* error */
1160 inp->s_addr = htonl(acc); /* store addr in network format */
1165 nanosleep(const struct timespec *req, struct timespec *rem)
1168 rem->tv_sec = rem->tv_nsec = 0;
1169 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
1174 init_signals(void terminate(int sig))
1180 init_stack_dump(void)
1187 pathconf(const char *path, int name)
1191 if (strncmp(path, "\\\\?\\", 4) == 0)
1203 WORD wVersionRequested = MAKEWORD( 1, 1);
1206 int err = WSAStartup(wVersionRequested, &wsaData);
1210 printf("Can not start Windows Sockets\n");
1220 win32_chdir(const char *dir)
1222 if (p_SetCurrentDirectoryW) {
1223 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1224 make_win32_path_UTF8_2_wchar(&pwszBuf, dir);
1226 BOOL b=p_SetCurrentDirectoryW((LPCWSTR)pwszBuf);
1228 free_pool_memory(pwszBuf);
1231 errno = b_errno_win32;
1235 else if (p_SetCurrentDirectoryA) {
1236 if (0 == p_SetCurrentDirectoryA(dir)) {
1237 errno = b_errno_win32;
1247 win32_mkdir(const char *dir)
1250 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1251 make_win32_path_UTF8_2_wchar(&pwszBuf, dir);
1253 int n = p_wmkdir((LPCWSTR)pwszBuf);
1254 free_pool_memory(pwszBuf);
1263 win32_getcwd(char *buf, int maxlen)
1267 if (p_GetCurrentDirectoryW) {
1268 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1269 pwszBuf = check_pool_memory_size (pwszBuf, maxlen*sizeof(wchar_t));
1271 n = p_GetCurrentDirectoryW(maxlen, (LPWSTR) pwszBuf);
1273 n = wchar_2_UTF8 (buf, (wchar_t *)pwszBuf, maxlen)-1;
1274 free_pool_memory(pwszBuf);
1276 } else if (p_GetCurrentDirectoryA)
1277 n = p_GetCurrentDirectoryA(maxlen, buf);
1279 if (n == 0 || n > maxlen) return NULL;
1281 if (n+1 > maxlen) return NULL;
1290 win32_fputs(const char *string, FILE *stream)
1292 /* we use WriteConsoleA / WriteConsoleA
1293 so we can be sure that unicode support works on win32.
1294 with fallback if something fails
1297 HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
1298 if (hOut && (hOut != INVALID_HANDLE_VALUE) && p_WideCharToMultiByte &&
1299 p_MultiByteToWideChar && (stream == stdout)) {
1301 POOLMEM* pwszBuf = get_pool_memory(PM_MESSAGE);
1303 DWORD dwCharsWritten;
1306 dwChars = UTF8_2_wchar(&pwszBuf, string);
1308 /* try WriteConsoleW */
1309 if (WriteConsoleW (hOut, pwszBuf, dwChars-1, &dwCharsWritten, NULL)) {
1310 free_pool_memory(pwszBuf);
1311 return dwCharsWritten;
1314 /* convert to local codepage and try WriteConsoleA */
1315 POOLMEM* pszBuf = get_pool_memory(PM_MESSAGE);
1316 pszBuf = check_pool_memory_size(pszBuf, dwChars+1);
1318 dwChars = p_WideCharToMultiByte(GetConsoleOutputCP(),0,(LPCWSTR) pwszBuf,-1,pszBuf,dwChars,NULL,NULL);
1319 free_pool_memory(pwszBuf);
1321 if (WriteConsoleA (hOut, pszBuf, dwChars-1, &dwCharsWritten, NULL)) {
1322 free_pool_memory(pszBuf);
1323 return dwCharsWritten;
1327 return fputs(string, stream);
1331 win32_cgets (char* buffer, int len)
1333 /* we use console ReadConsoleA / ReadConsoleW to be able to read unicode
1334 from the win32 console and fallback if seomething fails */
1336 HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
1337 if (hIn && (hIn != INVALID_HANDLE_VALUE) && p_WideCharToMultiByte && p_MultiByteToWideChar) {
1339 wchar_t wszBuf[1024];
1342 /* nt and unicode conversion */
1343 if (ReadConsoleW (hIn, wszBuf, 1024, &dwRead, NULL)) {
1345 /* null terminate at end */
1346 if (wszBuf[dwRead-1] == L'\n') {
1347 wszBuf[dwRead-1] = L'\0';
1351 if (wszBuf[dwRead-1] == L'\r') {
1352 wszBuf[dwRead-1] = L'\0';
1356 wchar_2_UTF8(buffer, wszBuf, len);
1360 /* win 9x and unicode conversion */
1361 if (ReadConsoleA (hIn, szBuf, 1024, &dwRead, NULL)) {
1363 /* null terminate at end */
1364 if (szBuf[dwRead-1] == L'\n') {
1365 szBuf[dwRead-1] = L'\0';
1369 if (szBuf[dwRead-1] == L'\r') {
1370 szBuf[dwRead-1] = L'\0';
1374 /* convert from ansii to wchar_t */
1375 p_MultiByteToWideChar(GetConsoleCP(), 0, szBuf, -1, wszBuf,1024);
1376 /* convert from wchar_t to UTF-8 */
1377 if (wchar_2_UTF8(buffer, wszBuf, len))
1383 if (fgets(buffer, len, stdin))
1390 win32_unlink(const char *filename)
1394 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1395 make_win32_path_UTF8_2_wchar(&pwszBuf, filename);
1397 nRetCode = _wunlink((LPCWSTR) pwszBuf);
1399 /* special case if file is readonly,
1400 we retry but unset attribute before */
1401 if (nRetCode == -1 && errno == EACCES && p_SetFileAttributesW && p_GetFileAttributesW) {
1402 DWORD dwAttr = p_GetFileAttributesW((LPCWSTR)pwszBuf);
1403 if (dwAttr != INVALID_FILE_ATTRIBUTES) {
1404 if (p_SetFileAttributesW((LPCWSTR)pwszBuf, dwAttr & ~FILE_ATTRIBUTE_READONLY)) {
1405 nRetCode = _wunlink((LPCWSTR) pwszBuf);
1406 /* reset to original if it didn't help */
1408 p_SetFileAttributesW((LPCWSTR)pwszBuf, dwAttr);
1412 free_pool_memory(pwszBuf);
1414 nRetCode = _unlink(filename);
1416 /* special case if file is readonly,
1417 we retry but unset attribute before */
1418 if (nRetCode == -1 && errno == EACCES && p_SetFileAttributesA && p_GetFileAttributesA) {
1419 DWORD dwAttr = p_GetFileAttributesA(filename);
1420 if (dwAttr != INVALID_FILE_ATTRIBUTES) {
1421 if (p_SetFileAttributesA(filename, dwAttr & ~FILE_ATTRIBUTE_READONLY)) {
1422 nRetCode = _unlink(filename);
1423 /* reset to original if it didn't help */
1425 p_SetFileAttributesA(filename, dwAttr);
1434 #include "mswinver.h"
1436 char WIN_VERSION_LONG[64];
1437 char WIN_VERSION[32];
1438 char WIN_RAWVERSION[32];
1445 static winver INIT; // cause constructor to be called before main()
1448 winver::winver(void)
1450 const char *version = "";
1451 const char *platform = "";
1452 OSVERSIONINFO osvinfo;
1453 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
1455 // Get the current OS version
1456 if (!GetVersionEx(&osvinfo)) {
1457 version = "Unknown";
1458 platform = "Unknown";
1460 const int ver = _mkversion(osvinfo.dwPlatformId,
1461 osvinfo.dwMajorVersion,
1462 osvinfo.dwMinorVersion);
1463 snprintf(WIN_RAWVERSION, sizeof(WIN_RAWVERSION), "Windows %#08x", ver);
1466 case MS_WINDOWS_95: (version = "Windows 95"); break;
1467 case MS_WINDOWS_98: (version = "Windows 98"); break;
1468 case MS_WINDOWS_ME: (version = "Windows ME"); break;
1469 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
1470 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
1471 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
1472 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
1473 default: version = WIN_RAWVERSION; break;
1476 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
1477 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %lu.%lu.%lu",
1478 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
1481 HANDLE h = CreateFile("G:\\foobar", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
1485 BPIPE *b = open_bpipe("ls -l", 10, "r");
1487 while (!feof(b->rfd)) {
1488 fgets(buf, sizeof(buf), b->rfd);
1494 BOOL CreateChildProcess(VOID);
1495 VOID WriteToPipe(VOID);
1496 VOID ReadFromPipe(VOID);
1497 VOID ErrorExit(LPCSTR);
1498 VOID ErrMsg(LPTSTR, BOOL);
1501 * Check for a quoted path, if an absolute path name is given and it contains
1502 * spaces it will need to be quoted. i.e. "c:/Program Files/foo/bar.exe"
1503 * CreateProcess() says the best way to ensure proper results with executables
1504 * with spaces in path or filename is to quote the string.
1507 getArgv0(const char *cmdline)
1512 for (cp = cmdline; *cp; cp++)
1517 if (!inquote && isspace(*cp))
1522 int len = cp - cmdline;
1523 char *rval = (char *)malloc(len+1);
1537 * OK, so it would seem CreateProcess only handles true executables:
1538 * .com or .exe files.
1539 * So test to see whether we're getting a .bat file and if so grab
1540 * $COMSPEC value and pass batch file to it.
1543 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
1545 static const char *comspec = NULL;
1546 PROCESS_INFORMATION piProcInfo;
1547 STARTUPINFOA siStartInfo;
1548 BOOL bFuncRetn = FALSE;
1550 if (comspec == NULL) {
1551 comspec = getenv("COMSPEC");
1553 if (comspec == NULL) // should never happen
1554 return INVALID_HANDLE_VALUE;
1556 // Set up members of the PROCESS_INFORMATION structure.
1557 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
1559 // Set up members of the STARTUPINFO structure.
1561 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
1562 siStartInfo.cb = sizeof(STARTUPINFO);
1563 // setup new process to use supplied handles for stdin,stdout,stderr
1564 // if supplied handles are not used the send a copy of our STD_HANDLE
1566 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
1568 if (in != INVALID_HANDLE_VALUE)
1569 siStartInfo.hStdInput = in;
1571 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
1573 if (out != INVALID_HANDLE_VALUE)
1574 siStartInfo.hStdOutput = out;
1576 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1577 if (err != INVALID_HANDLE_VALUE)
1578 siStartInfo.hStdError = err;
1580 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
1581 // Create the child process.
1584 int cmdLen = strlen(cmdline) + strlen(comspec) + 16;
1586 char *cmdLine = (char *)alloca(cmdLen);
1588 bstrncpy(exeFile, comspec, sizeof(exeFile));
1589 bstrncpy(cmdLine, comspec, cmdLen);
1590 bstrncat(cmdLine, " /c ", cmdLen);
1591 bstrncat(cmdLine, cmdline, cmdLen);
1593 // try to execute program
1594 bFuncRetn = CreateProcessA(exeFile,
1595 cmdLine, // command line
1596 NULL, // process security attributes
1597 NULL, // primary thread security attributes
1598 TRUE, // handles are inherited
1599 0, // creation flags
1600 NULL, // use parent's environment
1601 NULL, // use parent's current directory
1602 &siStartInfo, // STARTUPINFO pointer
1603 &piProcInfo); // receives PROCESS_INFORMATION
1605 if (bFuncRetn == 0) {
1606 ErrorExit("CreateProcess failed\n");
1607 const char *err = errorString();
1608 Dmsg3(99, "CreateProcess(%s, %s, ...)=%s\n", exeFile, cmdLine, err);
1609 LocalFree((void *)err);
1610 return INVALID_HANDLE_VALUE;
1612 // we don't need a handle on the process primary thread so we close
1614 CloseHandle(piProcInfo.hThread);
1616 return piProcInfo.hProcess;
1621 ErrorExit (LPCSTR lpszMessage)
1623 Dmsg1(0, "%s", lpszMessage);
1628 typedef struct s_bpipe {
1630 time_t worker_stime;
1639 CloseIfValid(HANDLE handle)
1641 if (handle != INVALID_HANDLE_VALUE)
1642 CloseHandle(handle);
1646 open_bpipe(char *prog, int wait, const char *mode)
1648 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
1649 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
1652 SECURITY_ATTRIBUTES saAttr;
1656 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
1657 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
1658 hInputFile = INVALID_HANDLE_VALUE;
1660 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
1661 memset((void *)bpipe, 0, sizeof(BPIPE));
1663 int mode_read = (mode[0] == 'r');
1664 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
1667 // Set the bInheritHandle flag so pipe handles are inherited.
1669 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1670 saAttr.bInheritHandle = TRUE;
1671 saAttr.lpSecurityDescriptor = NULL;
1675 // Create a pipe for the child process's STDOUT.
1676 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
1677 ErrorExit("Stdout pipe creation failed\n");
1680 // Create noninheritable read handle and close the inheritable read
1683 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
1684 GetCurrentProcess(), &hChildStdoutRdDup , 0,
1686 DUPLICATE_SAME_ACCESS);
1688 ErrorExit("DuplicateHandle failed");
1692 CloseHandle(hChildStdoutRd);
1693 hChildStdoutRd = INVALID_HANDLE_VALUE;
1698 // Create a pipe for the child process's STDIN.
1700 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
1701 ErrorExit("Stdin pipe creation failed\n");
1705 // Duplicate the write handle to the pipe so it is not inherited.
1706 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
1707 GetCurrentProcess(), &hChildStdinWrDup,
1709 FALSE, // not inherited
1710 DUPLICATE_SAME_ACCESS);
1712 ErrorExit("DuplicateHandle failed");
1716 CloseHandle(hChildStdinWr);
1717 hChildStdinWr = INVALID_HANDLE_VALUE;
1719 // spawn program with redirected handles as appropriate
1720 bpipe->worker_pid = (pid_t)
1721 CreateChildProcess(prog, // commandline
1722 hChildStdinRd, // stdin HANDLE
1723 hChildStdoutWr, // stdout HANDLE
1724 hChildStdoutWr); // stderr HANDLE
1726 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1730 bpipe->worker_stime = time(NULL);
1733 CloseHandle(hChildStdoutWr); // close our write side so when
1734 // process terminates we can
1736 // ugly but convert WIN32 HANDLE to FILE*
1737 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1739 bpipe->rfd = _fdopen(rfd, "r");
1743 CloseHandle(hChildStdinRd); // close our read side so as not
1744 // to interfre with child's copy
1745 // ugly but convert WIN32 HANDLE to FILE*
1746 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1748 bpipe->wfd = _fdopen(wfd, "w");
1753 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1760 CloseIfValid(hChildStdoutRd);
1761 CloseIfValid(hChildStdoutRdDup);
1762 CloseIfValid(hChildStdinWr);
1763 CloseIfValid(hChildStdinWrDup);
1765 free((void *) bpipe);
1766 errno = b_errno_win32; /* do GetLastError() for error code */
1772 kill(int pid, int signal)
1775 if (!TerminateProcess((HANDLE)pid, (UINT) signal)) {
1777 errno = b_errno_win32;
1779 CloseHandle((HANDLE)pid);
1785 close_bpipe(BPIPE *bpipe)
1788 int32_t remaining_wait = bpipe->wait;
1790 if (remaining_wait == 0) { /* wait indefinitely */
1791 remaining_wait = INT32_MAX;
1795 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) {
1796 const char *err = errorString();
1797 rval = b_errno_win32;
1798 Dmsg1(0, "GetExitCode error %s\n", err);
1799 LocalFree((void *)err);
1802 if (exitCode == STILL_ACTIVE) {
1803 if (remaining_wait <= 0) {
1804 rval = ETIME; /* timed out */
1807 bmicrosleep(1, 0); /* wait one second */
1809 } else if (exitCode != 0) {
1810 /* Truncate exit code as it doesn't seem to be correct */
1811 rval = (exitCode & 0xFF) | b_errno_exit;
1814 break; /* Shouldn't get here */
1818 if (bpipe->timer_id) {
1819 stop_child_timer(bpipe->timer_id);
1821 if (bpipe->rfd) fclose(bpipe->rfd);
1822 if (bpipe->wfd) fclose(bpipe->wfd);
1823 free((void *)bpipe);
1828 close_wpipe(BPIPE *bpipe)
1834 if (fclose(bpipe->wfd) != 0) {
1842 #include "findlib/find.h"
1845 utime(const char *fname, struct utimbuf *times)
1850 conv_unix_to_win32_path(fname, tmpbuf, 5000);
1852 cvt_utime_to_ftime(times->actime, acc);
1853 cvt_utime_to_ftime(times->modtime, mod);
1855 HANDLE h = INVALID_HANDLE_VALUE;
1857 if (p_CreateFileW) {
1858 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1859 make_win32_path_UTF8_2_wchar(&pwszBuf, tmpbuf);
1861 h = p_CreateFileW((LPCWSTR)pwszBuf,
1862 FILE_WRITE_ATTRIBUTES,
1863 FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
1866 FILE_FLAG_BACKUP_SEMANTICS, // required for directories
1869 free_pool_memory(pwszBuf);
1870 } else if (p_CreateFileA) {
1871 h = p_CreateFileA(tmpbuf,
1872 FILE_WRITE_ATTRIBUTES,
1873 FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
1876 FILE_FLAG_BACKUP_SEMANTICS, // required for directories
1880 if (h == INVALID_HANDLE_VALUE) {
1881 const char *err = errorString();
1882 Dmsg2(99, "Cannot open file \"%s\" for utime(): ERR=%s", tmpbuf, err);
1883 LocalFree((void *)err);
1884 errno = b_errno_win32;
1888 int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
1891 errno = b_errno_win32;
1898 file_open(const char *file, int flags, int mode)
1901 DWORD shareMode = 0;
1904 HANDLE foo = INVALID_HANDLE_VALUE;
1905 const char *remap = file;
1907 if (flags & O_WRONLY) access = GENERIC_WRITE;
1908 else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
1909 else access = GENERIC_READ;
1911 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
1912 create = CREATE_NEW;
1913 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
1914 create = CREATE_ALWAYS;
1915 else if (flags & O_CREAT)
1916 create = OPEN_ALWAYS;
1917 else if (flags & O_TRUNC)
1918 create = TRUNCATE_EXISTING;
1920 create = OPEN_EXISTING;
1924 if (flags & O_APPEND) {
1925 printf("open...APPEND not implemented yet.");
1929 if (p_CreateFileW) {
1930 POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);
1931 make_win32_path_UTF8_2_wchar(&pwszBuf, file);
1933 foo = p_CreateFileW((LPCWSTR) pwszBuf, access, shareMode, NULL, create, msflags, NULL);
1934 free_pool_memory(pwszBuf);
1935 } else if (p_CreateFileA)
1936 foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
1938 if (INVALID_HANDLE_VALUE == foo) {
1939 errno = b_errno_win32;
1950 if (!CloseHandle((HANDLE)fd)) {
1951 errno = b_errno_win32;
1959 file_write(int fd, const void *data, ssize_t len)
1963 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
1964 if (status) return bwrite;
1965 errno = b_errno_win32;
1971 file_read(int fd, void *data, ssize_t len)
1976 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
1977 if (status) return bread;
1978 errno = b_errno_win32;
1983 file_seek(int fd, off_t offset, int whence)
1989 method = FILE_BEGIN;
1992 method = FILE_CURRENT;
2002 if ((val=SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method)) == INVALID_SET_FILE_POINTER) {
2003 errno = b_errno_win32;
2006 /* ***FIXME*** I doubt this works right */
2019 /* syslog function, added by Nicolas Boichat */
2020 void openlog(const char *ident, int option, int facility) {}