2 // compat.cpp -- compatibilty layer to make bacula-fd run
3 // natively under windows
5 // Copyright (C)2004 Raider Solutions, Inc
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of
10 // the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public
18 // License along with this program; if not, write to the Free
19 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 // MA 02111-1307, USA.
22 // Author : Christopher S. Hull
23 // Created On : Sat Jan 31 15:55:00 2004
24 // Last Modified By: Christopher S. Hull
25 // Last Modified On: Mon Feb 23 10:48:23 2004
34 #define USE_WIN32_COMPAT_IO 1
36 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
37 extern DWORD g_platform_id;
39 // from CYGWIN (should be diff between Jan 1 1601 and Jan 1 1970
40 #define WIN32_FILETIME_ADJUST 0x19DB1DED53E8000I64
42 #define WIN32_FILETIME_SCALE 10000000 // 100ns/second
45 cygwin_conv_to_win32_path(const char *name, char *win32_name)
47 const char *fname = name;
49 /* Check for Unix separator and convert to Win32 */
51 *win32_name++ = '\\'; /* convert char */
52 /* If Win32 separated that is "quoted", remove quote */
53 } else if (*name == '\\' && name[1] == '\\') {
55 name++; /* skip first \ */
57 *win32_name++ = *name; /* copy character */
61 /* Strip any trailing slash, if we stored something */
62 if (*fname != 0 && win32_name[-1] == '\\') {
71 wchar_win32_path(const char *name, WCHAR *win32_name)
73 const char *fname = name;
75 /* Check for Unix separator and convert to Win32 */
77 *win32_name++ = '\\'; /* convert char */
78 /* If Win32 separated that is "quoted", remove quote */
79 } else if (*name == '\\' && name[1] == '\\') {
81 name++; /* skip first \ */
83 *win32_name++ = *name; /* copy character */
87 /* Strip any trailing slash, if we stored something */
88 if (*fname != 0 && win32_name[-1] == '\\') {
102 chmod(const char *, mode_t)
108 chown(const char *k, uid_t, gid_t)
114 lchown(const char *k, uid_t, gid_t)
128 srandom(unsigned int seed)
132 // /////////////////////////////////////////////////////////////////
133 // convert from Windows concept of time to Unix concept of time
134 // /////////////////////////////////////////////////////////////////
136 cvt_utime_to_ftime(const time_t &time, FILETIME &wintime)
138 uint64_t mstime = time;
139 mstime *= WIN32_FILETIME_SCALE;
140 mstime += WIN32_FILETIME_ADJUST;
142 wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64);
143 wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL);
147 cvt_ftime_to_utime(const FILETIME &time)
149 uint64_t mstime = time.dwHighDateTime;
151 mstime |= time.dwLowDateTime;
153 mstime -= WIN32_FILETIME_ADJUST;
154 mstime /= WIN32_FILETIME_SCALE; // convert to seconds.
156 return (time_t) (mstime & 0xffffffff);
164 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
165 FORMAT_MESSAGE_FROM_SYSTEM |
166 FORMAT_MESSAGE_IGNORE_INSERTS,
169 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang
174 return (const char *) lpMsgBuf;
178 statDir(const char *file, struct stat *sb)
180 WIN32_FIND_DATA info; // window's file info
182 if (file[1] == ':' && file[2] == 0) {
183 d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
184 sb->st_mode = S_IFDIR;
185 sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
191 HANDLE h = FindFirstFile(file, &info);
193 if (h == INVALID_HANDLE_VALUE) {
194 const char *err = errorString();
195 d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err);
196 LocalFree((void *)err);
197 errno = GetLastError();
201 sb->st_mode = 0777; /* start with everything */
202 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
203 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
204 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
205 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
206 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
207 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
208 sb->st_mode |= S_IFDIR;
210 sb->st_size = info.nFileSizeHigh;
212 sb->st_size |= info.nFileSizeLow;
213 sb->st_blksize = 4096;
214 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
216 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
217 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
218 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
225 stat2(const char *file, struct stat *sb)
227 BY_HANDLE_FILE_INFORMATION info;
231 cygwin_conv_to_win32_path(file, tmpbuf);
233 DWORD attr = GetFileAttributes(tmpbuf);
236 const char *err = errorString();
237 d_msg(__FILE__, __LINE__, 99,
238 "GetFileAttrubtes(%s): %s\n", tmpbuf, err);
239 LocalFree((void *)err);
240 errno = GetLastError();
244 if (attr & FILE_ATTRIBUTE_DIRECTORY)
245 return statDir(tmpbuf, sb);
247 h = CreateFile(tmpbuf, GENERIC_READ,
248 FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
250 if (h == INVALID_HANDLE_VALUE) {
251 const char *err = errorString();
252 d_msg(__FILE__, __LINE__, 99,
253 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
254 LocalFree((void *)err);
256 errno = GetLastError();
260 if (!GetFileInformationByHandle(h, &info)) {
261 const char *err = errorString();
262 d_msg(__FILE__, __LINE__, 99,
263 "GetfileInformationByHandle(%s): %s\n", tmpbuf, err);
264 LocalFree((void *)err);
266 errno = GetLastError();
270 sb->st_dev = info.dwVolumeSerialNumber;
271 sb->st_ino = info.nFileIndexHigh;
273 sb->st_ino |= info.nFileIndexLow;
274 sb->st_nlink = (short)info.nNumberOfLinks;
276 sb->st_mode = 0777; /* start with everything */
277 if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
278 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
279 if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
280 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
281 if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
282 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
283 sb->st_mode |= S_IFREG;
285 sb->st_size = info.nFileSizeHigh;
287 sb->st_size |= info.nFileSizeLow;
288 sb->st_blksize = 4096;
289 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
290 sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
291 sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
292 sb->st_ctime = cvt_ftime_to_utime(info.ftLastWriteTime);
300 stat(const char *file, struct stat *sb)
302 WIN32_FILE_ATTRIBUTE_DATA data;
306 memset(sb, 0, sizeof(*sb));
308 if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS)
309 return stat2(file, sb);
311 // otherwise we're on NT
319 wchar_win32_path(file, buf+4);
321 if (!GetFileAttributesExW((WCHAR *)buf, GetFileExInfoStandard, &data))
322 return stat2(file, sb);
324 if (!GetFileAttributesEx(file, GetFileExInfoStandard, &data))
325 return stat2(file, sb);
328 sb->st_mode = 0777; /* start with everything */
329 if (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
330 sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
331 if (data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
332 sb->st_mode &= ~S_IRWXO; /* remove everything for other */
333 if (data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
334 sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
336 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
337 sb->st_mode |= S_IFDIR;
339 sb->st_mode |= S_IFREG;
342 sb->st_size = data.nFileSizeHigh;
344 sb->st_size |= data.nFileSizeLow;
345 sb->st_blksize = 4096;
346 sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
347 sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime);
348 sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime);
349 sb->st_ctime = cvt_ftime_to_utime(data.ftLastWriteTime);
354 lstat(const char *file, struct stat *sb)
356 return stat(file, sb);
372 execvp(const char *, char *[]) {
390 waitpid(int, int*, int)
396 readlink(const char *, char *, int)
403 strcasecmp(const char *s1, const char *s2)
405 register int ch1, ch2;
408 return 0; /* strings are equal if same object. */
419 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
425 strncasecmp(const char *s1, const char *s2, int len)
427 register int ch1, ch2;
430 return 0; /* strings are equal if same object. */
441 } while (len-- && ch1 != 0 && tolower(ch1) == tolower(ch2));
447 gettimeofday(struct timeval *tv, struct timezone *)
453 if (tv == NULL) return -1;
454 if (!SystemTimeToFileTime(&now, &tmp)) return -1;
456 int64_t _100nsec = tmp.dwHighDateTime;
458 _100nsec |= tmp.dwLowDateTime;
459 _100nsec -= WIN32_FILETIME_ADJUST;
461 tv->tv_sec =(long) (_100nsec / 10000000);
462 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
468 syslog(int, const char *, const char *)
486 // implement opendir/readdir/closedir on top of window's API
489 WIN32_FIND_DATA data; // window's file info
490 const char *spec; // the directory we're traversing
491 HANDLE dirh; // the search handle
492 BOOL valid; // the info in data field is valid
493 UINT32 offset; // pseudo offset for d_off
497 opendir(const char *path)
499 int max_len = strlen(path) + 16;
501 if (path == NULL) return NULL;
503 rval = (_dir *)malloc(sizeof(_dir));
504 if (rval == NULL) return NULL;
505 char *tspec = (char *)malloc(max_len);
506 if (tspec == NULL) goto err1;
508 if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
509 // allow path to be 32767 bytes
515 cygwin_conv_to_win32_path(path, tspec+4);
517 cygwin_conv_to_win32_path(path, tspec);
519 strncat(tspec, "\\*", max_len);
522 rval->dirh = FindFirstFile(rval->spec, &rval->data);
523 d_msg(__FILE__, __LINE__,
524 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
525 path, rval->spec, rval->dirh);
528 if (rval->dirh == INVALID_HANDLE_VALUE)
531 d_msg(__FILE__, __LINE__,
532 99, "\tFirstFile=%s\n", rval->data.cFileName);
535 free((void *)rval->spec);
544 _dir *dp = (_dir *)dirp;
546 free((void *)dp->spec);
552 typedef struct _WIN32_FIND_DATA {
553 DWORD dwFileAttributes;
554 FILETIME ftCreationTime;
555 FILETIME ftLastAccessTime;
556 FILETIME ftLastWriteTime;
561 TCHAR cFileName[MAX_PATH];
562 TCHAR cAlternateFileName[14];
563 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
567 copyin(struct dirent &dp, const char *fname)
571 char *cp = dp.d_name;
580 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
583 _dir *dp = (_dir *)dirp;
585 entry->d_off = dp->offset;
586 dp->offset += copyin(*entry, dp->data.cFileName);
587 *result = entry; /* return entry address */
588 d_msg(__FILE__, __LINE__,
589 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
590 dirp, entry->d_name, entry->d_reclen, entry->d_off);
592 // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
595 dp->valid = FindNextFile(dp->dirh, &dp->data);
601 inet_aton(const char *a, struct in_addr *inp)
605 uint32_t acc = 0, tmp = 0;
607 if (!isdigit(*a)) return 0;
610 tmp = (tmp * 10) + (*cp -'0');
611 else if (*cp == '.') {
612 if (tmp > 255) return 0;
613 acc = (acc << 8) + tmp;
619 if (dotc != 3) return 0;
625 nanosleep(const struct timespec *req, struct timespec *rem)
628 rem->tv_sec = rem->tv_nsec = 0;
629 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
634 init_signals(void terminate(int sig))
640 init_stack_dump(void)
647 pathconf(const char *path, int name)
651 if (strncmp(path, "\\\\?\\", 4) == 0)
663 WORD wVersionRequested = MAKEWORD( 1, 1);
666 int err = WSAStartup(wVersionRequested, &wsaData);
671 printf("Can not start Windows Sockets\n");
680 win32_chdir(const char *dir)
682 if (0 == SetCurrentDirectory(dir)) return -1;
687 win32_getcwd(char *buf, int maxlen)
689 int n = GetCurrentDirectory(maxlen, buf);
691 if (n == 0 || n > maxlen) return NULL;
693 if (n+1 > maxlen) return NULL;
702 #include "mswinver.h"
704 char WIN_VERSION_LONG[64];
705 char WIN_VERSION[32];
712 static winver INIT; // cause constructor to be called before main()
719 const char *version = "";
720 const char *platform = "";
721 OSVERSIONINFO osvinfo;
722 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
724 // Get the current OS version
725 if (!GetVersionEx(&osvinfo)) {
727 platform = "Unknown";
730 switch (_mkversion(osvinfo.dwPlatformId, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion))
732 case MS_WINDOWS_95: (version = "Windows 95"); break;
733 case MS_WINDOWS_98: (version = "Windows 98"); break;
734 case MS_WINDOWS_ME: (version = "Windows ME"); break;
735 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
736 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
737 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
738 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
739 default: version = "Windows ??"; break;
742 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
743 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
744 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
747 HANDLE h = CreateFile("G:\\foobar", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
751 BPIPE *b = open_bpipe("ls -l", 10, "r");
753 while (!feof(b->rfd)) {
754 fgets(buf, sizeof(buf), b->rfd);
760 BOOL CreateChildProcess(VOID);
761 VOID WriteToPipe(VOID);
762 VOID ReadFromPipe(VOID);
763 VOID ErrorExit(LPTSTR);
764 VOID ErrMsg(LPTSTR, BOOL);
768 getArgv0(const char *cmdline)
770 const char *cp = cmdline;
772 while (*cp && !isspace(*cp)) cp++;
774 int len = cp - cmdline;
775 char *rval = (char *)malloc(len+1);
787 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
789 PROCESS_INFORMATION piProcInfo;
790 STARTUPINFO siStartInfo;
791 BOOL bFuncRetn = FALSE;
793 // Set up members of the PROCESS_INFORMATION structure.
795 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
797 // Set up members of the STARTUPINFO structure.
799 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
800 siStartInfo.cb = sizeof(STARTUPINFO);
801 // setup new process to use supplied handles for stdin,stdout,stderr
802 // if supplied handles are not used the send a copy of our STD_HANDLE
804 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
806 if (in != INVALID_HANDLE_VALUE)
807 siStartInfo.hStdInput = in;
809 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
811 if (out != INVALID_HANDLE_VALUE)
812 siStartInfo.hStdOutput = out;
814 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
815 if (err != INVALID_HANDLE_VALUE)
816 siStartInfo.hStdError = err;
818 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
819 // Create the child process.
823 // retrive the first compont of the command line which should be the
825 const char *exeName = getArgv0(cmdline);
826 // check to see if absolute path was passed to us already?
827 if (exeName[1] != ':'
828 || (strchr(cmdline, '/') == NULL
829 && strchr(cmdline, '\\') == NULL))
831 // only command name so perform search of PATH to find
833 DWORD rval = SearchPath(NULL,
840 return INVALID_HANDLE_VALUE;
841 if (rval > sizeof(exeFile))
842 return INVALID_HANDLE_VALUE;
846 strcpy(exeFile, exeName);
848 // exeFile now has absolute path to program to execute.
849 free((void *)exeName);
850 // copy original command line to pass to create process
851 strcpy(cmdLine, cmdline);
852 // try to execute program
853 bFuncRetn = CreateProcess(exeFile,
854 cmdLine, // command line
855 NULL, // process security attributes
856 NULL, // primary thread security attributes
857 TRUE, // handles are inherited
859 NULL, // use parent's environment
860 NULL, // use parent's current directory
861 &siStartInfo, // STARTUPINFO pointer
862 &piProcInfo); // receives PROCESS_INFORMATION
864 if (bFuncRetn == 0) {
865 ErrorExit("CreateProcess failed\n");
866 return INVALID_HANDLE_VALUE;
868 // we don't need a handle on the process primary thread so we close
870 CloseHandle(piProcInfo.hThread);
872 return piProcInfo.hProcess;
877 ErrorExit (LPTSTR lpszMessage)
879 d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
884 typedef struct s_bpipe {
895 CloseIfValid(HANDLE handle)
897 if (handle != INVALID_HANDLE_VALUE)
902 open_bpipe(char *prog, int wait, const char *mode)
904 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
905 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
908 SECURITY_ATTRIBUTES saAttr;
912 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
913 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
914 hInputFile = INVALID_HANDLE_VALUE;
916 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
917 memset((void *)bpipe, 0, sizeof(BPIPE));
919 int mode_read = (mode[0] == 'r');
920 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
923 // Set the bInheritHandle flag so pipe handles are inherited.
925 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
926 saAttr.bInheritHandle = TRUE;
927 saAttr.lpSecurityDescriptor = NULL;
931 // Create a pipe for the child process's STDOUT.
932 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
933 ErrorExit("Stdout pipe creation failed\n");
936 // Create noninheritable read handle and close the inheritable read
939 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
940 GetCurrentProcess(), &hChildStdoutRdDup , 0,
942 DUPLICATE_SAME_ACCESS);
944 ErrorExit("DuplicateHandle failed");
948 CloseHandle(hChildStdoutRd);
953 // Create a pipe for the child process's STDIN.
955 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
956 ErrorExit("Stdin pipe creation failed\n");
960 // Duplicate the write handle to the pipe so it is not inherited.
961 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
962 GetCurrentProcess(), &hChildStdinWrDup,
964 FALSE, // not inherited
965 DUPLICATE_SAME_ACCESS);
967 ErrorExit("DuplicateHandle failed");
971 CloseHandle(hChildStdinWr);
973 // spawn program with redirected handles as appropriate
974 bpipe->worker_pid = (pid_t)
975 CreateChildProcess(prog, // commandline
976 hChildStdinRd, // stdin HANDLE
977 hChildStdoutWr, // stdout HANDLE
978 hChildStdoutWr);// stderr HANDLE
980 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
984 bpipe->worker_stime = time(NULL);
987 CloseHandle(hChildStdoutWr); // close our write side so when
988 // process terminates we can
990 // ugly but convert WIN32 HANDLE to FILE*
991 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
992 bpipe->rfd = _fdopen(rfd, "r");
995 CloseHandle(hChildStdinRd); // close our read side so as not
996 // to interfre with child's copy
997 // ugly but convert WIN32 HANDLE to FILE*
998 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
999 bpipe->wfd = _fdopen(wfd, "w");
1003 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1010 CloseIfValid(hChildStdoutRd);
1011 CloseIfValid(hChildStdoutRdDup);
1012 CloseIfValid(hChildStdinWr);
1013 CloseIfValid(hChildStdinWrDup);
1015 free((void *) bpipe);
1021 kill(int pid, int signal)
1024 if (!TerminateProcess((HANDLE)pid, (UINT) signal))
1026 CloseHandle((HANDLE)pid);
1031 close_bpipe(BPIPE *bpipe)
1034 if (bpipe->rfd) fclose(bpipe->rfd);
1035 if (bpipe->wfd) fclose(bpipe->wfd);
1038 int remaining_wait = bpipe->wait;
1042 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode))
1044 const char *err = errorString();
1045 rval = GetLastError();
1046 d_msg(__FILE__, __LINE__, 0,
1047 "GetExitCode error %s\n", err);
1048 LocalFree((void *)err);
1052 if (exitCode == STILL_ACTIVE) {
1053 bmicrosleep(1, 0); /* wait one second */
1057 } while(remaining_wait);
1060 if (bpipe->timer_id) {
1061 stop_child_timer(bpipe->timer_id);
1063 free((void *)bpipe);
1068 close_wpipe(BPIPE *bpipe)
1074 if (fclose(bpipe->wfd) != 0) {
1082 #include "findlib/find.h"
1085 utime(const char *fname, struct utimbuf *times)
1090 cygwin_conv_to_win32_path(fname, tmpbuf);
1092 cvt_utime_to_ftime(times->actime, acc);
1093 cvt_utime_to_ftime(times->modtime, mod);
1095 HANDLE h = CreateFile(tmpbuf,
1096 FILE_WRITE_ATTRIBUTES,
1103 if (h == INVALID_HANDLE_VALUE) {
1104 const char *err = errorString();
1105 d_msg(__FILE__, __LINE__, 99,
1106 "Cannot open file for stat (%s):%s\n", tmpbuf, err);
1107 LocalFree((void *)err);
1111 int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
1118 #if USE_WIN32_COMPAT_IO
1121 open(const char *file, int flags, int mode)
1123 return _open(file, flags, mode);
1134 read(int fd, void *buf, size_t len)
1136 return _read(fd, buf, len);
1140 write(int fd, const void *buf, size_t len)
1142 return _write(fd, buf, len);
1146 lseek(int fd, off_t offset, int whence)
1148 return _lseek(fd, offset, whence);
1152 dup2(int fd1, int fd2)
1154 return _dup2(fd1, fd2);
1158 open(const char *file, int flags, int mode)
1161 DWORD shareMode = 0;
1165 const char *remap = file;
1167 if (flags & O_WRONLY) access = GENERIC_WRITE;
1168 else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
1169 else access = GENERIC_READ;
1171 if (flags & O_CREAT) create = CREATE_NEW;
1172 else create = OPEN_EXISTING;
1174 if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
1176 if (!(flags & O_EXCL))
1177 shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
1179 if (flags & O_APPEND)
1181 printf("open...APPEND not implemented yet.");
1185 foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
1186 if (INVALID_HANDLE_VALUE == foo)
1197 if (!CloseHandle((HANDLE)fd))
1204 write(int fd, const void *data, size_t len)
1208 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
1209 if (status) return bwrite;
1215 read(int fd, void *data, size_t len)
1220 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
1221 if (status) return bread;
1226 lseek(int fd, off_t offset, int whence)
1231 method = FILE_BEGIN;
1234 method = FILE_CURRENT;
1243 return SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method);