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 Kern Sibbald and John Walker
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of
13 // the License, or (at your option) any later version.
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 GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public
21 // License along with this program; if not, write to the Free
22 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 // MA 02111-1307, USA.
25 // Author : Christopher S. Hull
26 // Created On : Sat Jan 31 15:55:00 2004
34 #define b_errno_win32 (1<<29)
36 #define USE_WIN32_COMPAT_IO 1
38 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
39 extern DWORD g_platform_id;
41 // from MicroSoft SDK (KES) is the diff between Jan 1 1601 and Jan 1 1970
43 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000UL //Not sure it works
45 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000I64
48 #define WIN32_FILETIME_SCALE 10000000 // 100ns/second
51 cygwin_conv_to_win32_path(const char *name, char *win32_name)
53 const char *fname = name;
55 /* Check for Unix separator and convert to Win32 */
57 *win32_name++ = '\\'; /* convert char */
58 /* If Win32 separated that is "quoted", remove quote */
59 } else if (*name == '\\' && name[1] == '\\') {
61 name++; /* skip first \ */
63 *win32_name++ = *name; /* copy character */
67 /* Strip any trailing slash, if we stored something */
68 if (*fname != 0 && win32_name[-1] == '\\') {
77 wchar_win32_path(const char *name, WCHAR *win32_name)
79 const char *fname = name;
81 /* Check for Unix separator and convert to Win32 */
83 *win32_name++ = '\\'; /* convert char */
84 /* If Win32 separated that is "quoted", remove quote */
85 } else if (*name == '\\' && name[1] == '\\') {
87 name++; /* skip first \ */
89 *win32_name++ = *name; /* copy character */
93 /* Strip any trailing slash, if we stored something */
94 if (*fname != 0 && win32_name[-1] == '\\') {
108 chmod(const char *, mode_t)
114 chown(const char *k, uid_t, gid_t)
120 lchown(const char *k, uid_t, gid_t)
134 srandom(unsigned int seed)
138 // /////////////////////////////////////////////////////////////////
139 // convert from Windows concept of time to Unix concept of time
140 // /////////////////////////////////////////////////////////////////
142 cvt_utime_to_ftime(const time_t &time, FILETIME &wintime)
144 uint64_t mstime = time;
145 mstime *= WIN32_FILETIME_SCALE;
146 mstime += WIN32_FILETIME_ADJUST;
149 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffUL);
151 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
153 wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
157 cvt_ftime_to_utime(const FILETIME &time)
159 uint64_t mstime = time.dwHighDateTime;
161 mstime |= time.dwLowDateTime;
163 mstime -= WIN32_FILETIME_ADJUST;
164 mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
166 return (time_t) (mstime & 0xffffffff);
174 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
175 FORMAT_MESSAGE_FROM_SYSTEM |
176 FORMAT_MESSAGE_IGNORE_INSERTS,
179 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
184 return (const char *) lpMsgBuf;
190 statDir(const char *file, struct stat *sb)
192 WIN32_FIND_DATA info; // window's file info
194 if (file[1] == ':' && file[2] == 0) {
195 d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
196 sb->st_mode = S_IFDIR;
197 sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
203 HANDLE h = FindFirstFile(file, &info);
205 if (h == INVALID_HANDLE_VALUE) {
206 const char *err = errorString();
207 d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err);
208 LocalFree((void *)err);
209 errno = b_errno_win32;
213 sb->st_mode = 0777; /* start with everything */
214 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
215 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
216 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
217 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
218 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
219 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
220 sb->st_mode |= S_IFDIR;
222 sb->st_size = info.nFileSizeHigh;
224 sb->st_size |= info.nFileSizeLow;
225 sb->st_blksize = 4096;
226 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
228 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
229 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
230 sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime);
237 stat2(const char *file, struct stat *sb)
239 BY_HANDLE_FILE_INFORMATION info;
243 cygwin_conv_to_win32_path(file, tmpbuf);
245 DWORD attr = GetFileAttributes(tmpbuf);
248 const char *err = errorString();
249 d_msg(__FILE__, __LINE__, 99,
250 "GetFileAttrubtes(%s): %s\n", tmpbuf, err);
251 LocalFree((void *)err);
252 errno = b_errno_win32;
256 if (attr & FILE_ATTRIBUTE_DIRECTORY)
257 return statDir(tmpbuf, sb);
259 h = CreateFile(tmpbuf, GENERIC_READ,
260 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
262 if (h == INVALID_HANDLE_VALUE) {
263 const char *err = errorString();
264 d_msg(__FILE__, __LINE__, 99,
265 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
266 LocalFree((void *)err);
268 errno = b_errno_win32;
272 if (!GetFileInformationByHandle(h, &info)) {
273 const char *err = errorString();
274 d_msg(__FILE__, __LINE__, 99,
275 "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
276 LocalFree((void *)err);
278 errno = b_errno_win32;
282 sb->st_dev = info.dwVolumeSerialNumber;
283 sb->st_ino = info.nFileIndexHigh;
285 sb->st_ino |= info.nFileIndexLow;
286 sb->st_nlink = (short)info.nNumberOfLinks;
288 sb->st_mode = 0777; /* start with everything */
289 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
290 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
291 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
292 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
293 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
294 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
295 sb->st_mode |= S_IFREG;
297 sb->st_size = info.nFileSizeHigh;
299 sb->st_size |= info.nFileSizeLow;
300 sb->st_blksize = 4096;
301 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
302 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
303 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
304 sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime);
312 stat(const char *file, struct stat *sb)
314 WIN32_FILE_ATTRIBUTE_DATA data;
318 memset(sb, 0, sizeof(*sb));
320 if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS)
321 return stat2(file, sb);
323 // otherwise we're on NT
331 wchar_win32_path(file, buf+4);
333 if (!GetFileAttributesExW((WCHAR *)buf, GetFileExInfoStandard, &data))
334 return stat2(file, sb);
336 if (!GetFileAttributesEx(file, GetFileExInfoStandard, &data))
337 return stat2(file, sb);
340 sb->st_mode = 0777; /* start with everything */
341 if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
342 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
343 if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
344 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
345 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
346 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
348 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
349 sb->st_mode |= S_IFDIR;
351 sb->st_mode |= S_IFREG;
354 sb->st_size = data.nFileSizeHigh;
356 sb->st_size |= data.nFileSizeLow;
357 sb->st_blksize = 4096;
358 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
359 sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
360 sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
361 sb->st_ctime = cvt_ftime_to_utime(data.ftCreationTime);
368 lstat(const char *file, struct stat *sb)
370 return stat(file, sb);
386 execvp(const char *, char *[]) {
407 waitpid(int, int*, int)
414 readlink(const char *, char *, int)
423 strcasecmp(const char *s1, const char *s2)
425 register int ch1, ch2;
428 return 0; /* strings are equal if same object. */
439 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
446 strncasecmp(const char *s1, const char *s2, int len)
448 register int ch1, ch2;
451 return 0; /* strings are equal if same object. */
462 if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break;
469 gettimeofday(struct timeval *tv, struct timezone *)
479 if (!SystemTimeToFileTime(&now, &tmp)) {
480 errno = b_errno_win32;
484 int64_t _100nsec = tmp.dwHighDateTime;
486 _100nsec |= tmp.dwLowDateTime;
487 _100nsec -= WIN32_FILETIME_ADJUST;
489 tv->tv_sec =(long) (_100nsec / 10000000);
490 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
496 syslog(int type, const char *fmt, const char *msg)
498 /*#ifndef HAVE_CONSOLE
499 MessageBox(NULL, msg, "Bacula", MB_OK);
516 // implement opendir/readdir/closedir on top of window's API
519 WIN32_FIND_DATA data; // window's file info
520 const char *spec; // the directory we're traversing
521 HANDLE dirh; // the search handle
522 BOOL valid; // the info in data field is valid
523 UINT32 offset; // pseudo offset for d_off
527 opendir(const char *path)
529 int max_len = strlen(path) + 16;
536 rval = (_dir *)malloc(sizeof(_dir));
537 if (rval == NULL) return NULL;
538 char *tspec = (char *)malloc(max_len);
539 if (tspec == NULL) return NULL;
541 if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
542 // allow path to be 32767 bytes
548 cygwin_conv_to_win32_path(path, tspec+4);
550 cygwin_conv_to_win32_path(path, tspec);
553 strncat(tspec, "\\*", max_len);
556 rval->dirh = FindFirstFile(rval->spec, &rval->data);
557 d_msg(__FILE__, __LINE__,
558 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
559 path, rval->spec, rval->dirh);
562 if (rval->dirh == INVALID_HANDLE_VALUE)
565 d_msg(__FILE__, __LINE__,
566 99, "\tFirstFile=%s\n", rval->data.cFileName);
570 free((void *)rval->spec);
572 errno = b_errno_win32;
579 _dir *dp = (_dir *)dirp;
581 free((void *)dp->spec);
587 typedef struct _WIN32_FIND_DATA {
588 DWORD dwFileAttributes;
589 FILETIME ftCreationTime;
590 FILETIME ftLastAccessTime;
591 FILETIME ftLastWriteTime;
596 TCHAR cFileName[MAX_PATH];
597 TCHAR cAlternateFileName[14];
598 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
602 copyin(struct dirent &dp, const char *fname)
606 char *cp = dp.d_name;
616 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
618 _dir *dp = (_dir *)dirp;
620 entry->d_off = dp->offset;
621 dp->offset += copyin(*entry, dp->data.cFileName);
622 *result = entry; /* return entry address */
623 d_msg(__FILE__, __LINE__,
624 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
625 dirp, entry->d_name, entry->d_reclen, entry->d_off);
627 // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
628 errno = b_errno_win32;
631 dp->valid = FindNextFile(dp->dirh, &dp->data);
636 * Dotted IP address to network address
642 inet_aton(const char *a, struct in_addr *inp)
645 uint32_t acc = 0, tmp = 0;
648 if (!isdigit(*cp)) { /* first char must be digit */
649 return 0; /* error */
653 tmp = (tmp * 10) + (*cp -'0');
654 } else if (*cp == '.' || *cp == 0) {
656 return 0; /* error */
658 acc = (acc << 8) + tmp;
662 return 0; /* error */
664 } while (*cp++ != 0);
665 if (dotc != 4) { /* want 3 .'s plus EOS */
666 return 0; /* error */
668 inp->s_addr = htonl(acc); /* store addr in network format */
673 nanosleep(const struct timespec *req, struct timespec *rem)
676 rem->tv_sec = rem->tv_nsec = 0;
677 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
682 init_signals(void terminate(int sig))
688 init_stack_dump(void)
695 pathconf(const char *path, int name)
699 if (strncmp(path, "\\\\?\\", 4) == 0)
711 WORD wVersionRequested = MAKEWORD( 1, 1);
714 int err = WSAStartup(wVersionRequested, &wsaData);
718 printf("Can not start Windows Sockets\n");
728 win32_chdir(const char *dir)
730 if (0 == SetCurrentDirectory(dir)) {
731 errno = b_errno_win32;
738 win32_getcwd(char *buf, int maxlen)
740 int n = GetCurrentDirectory(maxlen, buf);
742 if (n == 0 || n > maxlen) return NULL;
744 if (n+1 > maxlen) return NULL;
752 #include "mswinver.h"
754 char WIN_VERSION_LONG[64];
755 char WIN_VERSION[32];
762 static winver INIT; // cause constructor to be called before main()
769 const char *version = "";
770 const char *platform = "";
771 OSVERSIONINFO osvinfo;
772 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
774 // Get the current OS version
775 if (!GetVersionEx(&osvinfo)) {
777 platform = "Unknown";
780 switch (_mkversion(osvinfo.dwPlatformId, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion))
782 case MS_WINDOWS_95: (version = "Windows 95"); break;
783 case MS_WINDOWS_98: (version = "Windows 98"); break;
784 case MS_WINDOWS_ME: (version = "Windows ME"); break;
785 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
786 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
787 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
788 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
789 default: version = "Windows ??"; break;
792 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
793 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
794 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
797 HANDLE h = CreateFile("G:\\foobar", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
801 BPIPE *b = open_bpipe("ls -l", 10, "r");
803 while (!feof(b->rfd)) {
804 fgets(buf, sizeof(buf), b->rfd);
810 BOOL CreateChildProcess(VOID);
811 VOID WriteToPipe(VOID);
812 VOID ReadFromPipe(VOID);
813 VOID ErrorExit(LPTSTR);
814 VOID ErrMsg(LPTSTR, BOOL);
817 * Check for a quoted path, if an absolute path name is given and it contains
818 * spaces it will need to be quoted. i.e. "c:/Program Files/foo/bar.exe"
819 * CreateProcess() says the best way to ensure proper results with executables
820 * with spaces in path or filename is to quote the string.
823 getArgv0(const char *cmdline)
827 for (const char *cp = cmdline; *cp; cp++)
832 if (!inquote && isspace(*cp))
837 int len = cp - cmdline;
838 char *rval = (char *)malloc(len+1);
852 * OK, so it would seem CreateProcess only handles true executables:
853 * .com or .exe files.
854 * So test to see whether we're getting a .bat file and if so grab
855 * $COMSPEC value and pass batch file to it.
858 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
860 PROCESS_INFORMATION piProcInfo;
861 STARTUPINFO siStartInfo;
862 BOOL bFuncRetn = FALSE;
864 // Set up members of the PROCESS_INFORMATION structure.
866 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
868 // Set up members of the STARTUPINFO structure.
870 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
871 siStartInfo.cb = sizeof(STARTUPINFO);
872 // setup new process to use supplied handles for stdin,stdout,stderr
873 // if supplied handles are not used the send a copy of our STD_HANDLE
875 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
877 if (in != INVALID_HANDLE_VALUE)
878 siStartInfo.hStdInput = in;
880 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
882 if (out != INVALID_HANDLE_VALUE)
883 siStartInfo.hStdOutput = out;
885 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
886 if (err != INVALID_HANDLE_VALUE)
887 siStartInfo.hStdError = err;
889 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
890 // Create the child process.
894 const char *comspec = getenv("COMSPEC");
896 if (comspec == NULL) // should never happen
897 return INVALID_HANDLE_VALUE;
899 char *cmdLine = (char *)alloca(strlen(cmdline) + strlen(comspec) + 16);
901 strcpy(exeFile, comspec);
902 strcpy(cmdLine, comspec);
903 strcat(cmdLine, " /c ");
904 strcat(cmdLine, cmdline);
906 // try to execute program
907 bFuncRetn = CreateProcess(exeFile,
908 cmdLine, // command line
909 NULL, // process security attributes
910 NULL, // primary thread security attributes
911 TRUE, // handles are inherited
913 NULL, // use parent's environment
914 NULL, // use parent's current directory
915 &siStartInfo, // STARTUPINFO pointer
916 &piProcInfo); // receives PROCESS_INFORMATION
918 if (bFuncRetn == 0) {
919 ErrorExit("CreateProcess failed\n");
920 const char *err = errorString();
921 d_msg(__FILE__, __LINE__, 99,
922 "CreateProcess(%s, %s, ...)=%s\n", exeFile, cmdLine, err);
923 LocalFree((void *)err);
924 return INVALID_HANDLE_VALUE;
926 // we don't need a handle on the process primary thread so we close
928 CloseHandle(piProcInfo.hThread);
930 return piProcInfo.hProcess;
935 ErrorExit (LPTSTR lpszMessage)
937 d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
942 typedef struct s_bpipe {
953 CloseIfValid(HANDLE handle)
955 if (handle != INVALID_HANDLE_VALUE)
961 open_bpipe(char *prog, int wait, const char *mode)
963 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
964 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
967 SECURITY_ATTRIBUTES saAttr;
971 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
972 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
973 hInputFile = INVALID_HANDLE_VALUE;
975 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
976 memset((void *)bpipe, 0, sizeof(BPIPE));
978 int mode_read = (mode[0] == 'r');
979 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
982 // Set the bInheritHandle flag so pipe handles are inherited.
984 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
985 saAttr.bInheritHandle = TRUE;
986 saAttr.lpSecurityDescriptor = NULL;
990 // Create a pipe for the child process's STDOUT.
991 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
992 ErrorExit("Stdout pipe creation failed\n");
995 // Create noninheritable read handle and close the inheritable read
998 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
999 GetCurrentProcess(), &hChildStdoutRdDup , 0,
1001 DUPLICATE_SAME_ACCESS);
1003 ErrorExit("DuplicateHandle failed");
1007 CloseHandle(hChildStdoutRd);
1008 hChildStdoutRd = INVALID_HANDLE_VALUE;
1013 // Create a pipe for the child process's STDIN.
1015 if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
1016 ErrorExit("Stdin pipe creation failed\n");
1020 // Duplicate the write handle to the pipe so it is not inherited.
1021 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
1022 GetCurrentProcess(), &hChildStdinWrDup,
1024 FALSE, // not inherited
1025 DUPLICATE_SAME_ACCESS);
1027 ErrorExit("DuplicateHandle failed");
1031 CloseHandle(hChildStdinWr);
1032 hChildStdinWr = INVALID_HANDLE_VALUE;
1034 // spawn program with redirected handles as appropriate
1035 bpipe->worker_pid = (pid_t)
1036 CreateChildProcess(prog, // commandline
1037 hChildStdinRd, // stdin HANDLE
1038 hChildStdoutWr, // stdout HANDLE
1039 hChildStdoutWr); // stderr HANDLE
1041 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1045 bpipe->worker_stime = time(NULL);
1048 CloseHandle(hChildStdoutWr); // close our write side so when
1049 // process terminates we can
1051 // ugly but convert WIN32 HANDLE to FILE*
1052 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1054 bpipe->rfd = _fdopen(rfd, "r");
1058 CloseHandle(hChildStdinRd); // close our read side so as not
1059 // to interfre with child's copy
1060 // ugly but convert WIN32 HANDLE to FILE*
1061 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1063 bpipe->wfd = _fdopen(wfd, "w");
1068 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1075 CloseIfValid(hChildStdoutRd);
1076 CloseIfValid(hChildStdoutRdDup);
1077 CloseIfValid(hChildStdinWr);
1078 CloseIfValid(hChildStdinWrDup);
1080 free((void *) bpipe);
1081 errno = b_errno_win32; /* do GetLastError() for error code */
1088 kill(int pid, int signal)
1091 if (!TerminateProcess((HANDLE)pid, (UINT) signal)) {
1093 errno = b_errno_win32;
1095 CloseHandle((HANDLE)pid);
1102 close_bpipe(BPIPE *bpipe)
1105 if (bpipe->rfd) fclose(bpipe->rfd);
1106 if (bpipe->wfd) fclose(bpipe->wfd);
1109 int remaining_wait = bpipe->wait;
1112 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) {
1113 const char *err = errorString();
1114 rval = b_errno_win32;
1115 d_msg(__FILE__, __LINE__, 0,
1116 "GetExitCode error %s\n", err);
1117 LocalFree((void *)err);
1121 if (exitCode == STILL_ACTIVE) {
1122 bmicrosleep(1, 0); /* wait one second */
1126 } while(remaining_wait);
1127 rval = ETIME; /* timed out */
1130 if (bpipe->timer_id) {
1131 stop_child_timer(bpipe->timer_id);
1133 free((void *)bpipe);
1138 close_wpipe(BPIPE *bpipe)
1144 if (fclose(bpipe->wfd) != 0) {
1152 #include "findlib/find.h"
1155 utime(const char *fname, struct utimbuf *times)
1160 cygwin_conv_to_win32_path(fname, tmpbuf);
1162 cvt_utime_to_ftime(times->actime, acc);
1163 cvt_utime_to_ftime(times->modtime, mod);
1165 HANDLE h = CreateFile(tmpbuf,
1166 FILE_WRITE_ATTRIBUTES,
1173 if (h == INVALID_HANDLE_VALUE) {
1174 const char *err = errorString();
1175 d_msg(__FILE__, __LINE__, 99,
1176 "Cannot open file for utime(%s,...):%s\n", tmpbuf, err);
1177 LocalFree((void *)err);
1178 errno = b_errno_win32;
1182 int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
1187 #if USE_WIN32_COMPAT_IO
1190 open(const char *file, int flags, int mode)
1192 return _open(file, flags|_O_BINARY, mode);
1197 * Note, this works only for a file. If you want
1198 * to close a socket, use closesocket().
1199 * Bacula has been modified in src/lib/bnet.c
1200 * to use closesocket().
1208 #ifndef HAVE_WXCONSOLE
1210 read(int fd, void *buf, ssize_t len)
1212 return _read(fd, buf, (size_t)len);
1216 write(int fd, const void *buf, ssize_t len)
1218 return _write(fd, buf, (size_t)len);
1223 lseek(int fd, off_t offset, int whence)
1225 return _lseeki64(fd, offset, whence);
1229 dup2(int fd1, int fd2)
1231 return _dup2(fd1, fd2);
1235 open(const char *file, int flags, int mode)
1238 DWORD shareMode = 0;
1242 const char *remap = file;
1244 if (flags & O_WRONLY) access = GENERIC_WRITE;
1245 else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
1246 else access = GENERIC_READ;
1248 if (flags & O_CREAT) create = CREATE_NEW;
1249 else create = OPEN_EXISTING;
1251 if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
1253 if (!(flags & O_EXCL))
1254 shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
1256 if (flags & O_APPEND) {
1257 printf("open...APPEND not implemented yet.");
1261 foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
1262 if (INVALID_HANDLE_VALUE == foo) {
1263 errno = b_errno_win32;
1274 if (!CloseHandle((HANDLE)fd)) {
1275 errno = b_errno_win32;
1283 write(int fd, const void *data, ssize_t len)
1287 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
1288 if (status) return bwrite;
1289 errno = b_errno_win32;
1295 read(int fd, void *data, ssize_t len)
1300 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
1301 if (status) return bread;
1302 errno = b_errno_win32;
1307 lseek(int fd, off_t offset, int whence)
1313 method = FILE_BEGIN;
1316 method = FILE_CURRENT;
1326 if ((val=SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method)) == INVALID_SET_FILE_POINTER) {
1327 errno = b_errno_win32;
1330 /* ***FIXME*** I doubt this works right */
1347 /* syslog function, added by Nicolas Boichat */