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 USE_WIN32_COMPAT_IO 1
36 extern void d_msg(const char *file, int line, int level, const char *fmt,...);
38 extern DWORD g_platform_id;
41 // from CYGWIN (should be 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 = GetLastError();
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.ftLastWriteTime);
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 = GetLastError();
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 = GetLastError();
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 = GetLastError();
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.ftLastWriteTime);
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.ftLastWriteTime);
368 lstat(const char *file, struct stat *sb)
370 return stat(file, sb);
386 execvp(const char *, char *[]) {
404 waitpid(int, int*, int)
410 readlink(const char *, char *, int)
418 strcasecmp(const char *s1, const char *s2)
420 register int ch1, ch2;
423 return 0; /* strings are equal if same object. */
434 } while (ch1 != 0 && tolower(ch1) == tolower(ch2));
441 strncasecmp(const char *s1, const char *s2, int len)
443 register int ch1, ch2;
446 return 0; /* strings are equal if same object. */
457 } while (len-- && ch1 != 0 && tolower(ch1) == tolower(ch2));
463 gettimeofday(struct timeval *tv, struct timezone *)
469 if (tv == NULL) return -1;
470 if (!SystemTimeToFileTime(&now, &tmp)) return -1;
472 int64_t _100nsec = tmp.dwHighDateTime;
474 _100nsec |= tmp.dwLowDateTime;
475 _100nsec -= WIN32_FILETIME_ADJUST;
477 tv->tv_sec =(long) (_100nsec / 10000000);
478 tv->tv_usec = (long) ((_100nsec % 10000000)/10);
484 syslog(int, const char *, const char *)
501 // implement opendir/readdir/closedir on top of window's API
504 WIN32_FIND_DATA data; // window's file info
505 const char *spec; // the directory we're traversing
506 HANDLE dirh; // the search handle
507 BOOL valid; // the info in data field is valid
508 UINT32 offset; // pseudo offset for d_off
512 opendir(const char *path)
514 int max_len = strlen(path) + 16;
516 if (path == NULL) return NULL;
518 rval = (_dir *)malloc(sizeof(_dir));
519 if (rval == NULL) return NULL;
520 char *tspec = (char *)malloc(max_len);
521 if (tspec == NULL) goto err1;
524 if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
525 // allow path to be 32767 bytes
531 cygwin_conv_to_win32_path(path, tspec+4);
533 cygwin_conv_to_win32_path(path, tspec);
536 cygwin_conv_to_win32_path(path, tspec);
538 strncat(tspec, "\\*", max_len);
541 rval->dirh = FindFirstFile(rval->spec, &rval->data);
542 d_msg(__FILE__, __LINE__,
543 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
544 path, rval->spec, rval->dirh);
547 if (rval->dirh == INVALID_HANDLE_VALUE)
550 d_msg(__FILE__, __LINE__,
551 99, "\tFirstFile=%s\n", rval->data.cFileName);
554 free((void *)rval->spec);
563 _dir *dp = (_dir *)dirp;
565 free((void *)dp->spec);
571 typedef struct _WIN32_FIND_DATA {
572 DWORD dwFileAttributes;
573 FILETIME ftCreationTime;
574 FILETIME ftLastAccessTime;
575 FILETIME ftLastWriteTime;
580 TCHAR cFileName[MAX_PATH];
581 TCHAR cAlternateFileName[14];
582 } WIN32_FIND_DATA, *PWIN32_FIND_DATA;
586 copyin(struct dirent &dp, const char *fname)
590 char *cp = dp.d_name;
600 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
603 _dir *dp = (_dir *)dirp;
605 entry->d_off = dp->offset;
606 dp->offset += copyin(*entry, dp->data.cFileName);
607 *result = entry; /* return entry address */
608 d_msg(__FILE__, __LINE__,
609 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
610 dirp, entry->d_name, entry->d_reclen, entry->d_off);
612 // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
615 dp->valid = FindNextFile(dp->dirh, &dp->data);
620 inet_aton(const char *a, struct in_addr *inp)
624 uint32_t acc = 0, tmp = 0;
626 if (!isdigit(*a)) return 0;
629 tmp = (tmp * 10) + (*cp -'0');
630 else if (*cp == '.') {
631 if (tmp > 255) return 0;
632 acc = (acc << 8) + tmp;
638 if (dotc != 3) return 0;
644 nanosleep(const struct timespec *req, struct timespec *rem)
647 rem->tv_sec = rem->tv_nsec = 0;
648 Sleep((req->tv_sec * 1000) + (req->tv_nsec/100000));
653 init_signals(void terminate(int sig))
659 init_stack_dump(void)
666 pathconf(const char *path, int name)
670 if (strncmp(path, "\\\\?\\", 4) == 0)
682 WORD wVersionRequested = MAKEWORD( 1, 1);
685 int err = WSAStartup(wVersionRequested, &wsaData);
690 printf("Can not start Windows Sockets\n");
699 win32_chdir(const char *dir)
701 if (0 == SetCurrentDirectory(dir)) return -1;
706 win32_getcwd(char *buf, int maxlen)
708 int n = GetCurrentDirectory(maxlen, buf);
710 if (n == 0 || n > maxlen) return NULL;
712 if (n+1 > maxlen) return NULL;
721 #include "mswinver.h"
723 char WIN_VERSION_LONG[64];
724 char WIN_VERSION[32];
731 static winver INIT; // cause constructor to be called before main()
738 const char *version = "";
739 const char *platform = "";
740 OSVERSIONINFO osvinfo;
741 osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
743 // Get the current OS version
744 if (!GetVersionEx(&osvinfo)) {
746 platform = "Unknown";
749 switch (_mkversion(osvinfo.dwPlatformId, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion))
751 case MS_WINDOWS_95: (version = "Windows 95"); break;
752 case MS_WINDOWS_98: (version = "Windows 98"); break;
753 case MS_WINDOWS_ME: (version = "Windows ME"); break;
754 case MS_WINDOWS_NT4:(version = "Windows NT 4.0"); platform = "NT"; break;
755 case MS_WINDOWS_2K: (version = "Windows 2000");platform = "NT"; break;
756 case MS_WINDOWS_XP: (version = "Windows XP");platform = "NT"; break;
757 case MS_WINDOWS_S2003: (version = "Windows Server 2003");platform = "NT"; break;
758 default: version = "Windows ??"; break;
761 bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG));
762 snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d",
763 platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber);
766 HANDLE h = CreateFile("G:\\foobar", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
770 BPIPE *b = open_bpipe("ls -l", 10, "r");
772 while (!feof(b->rfd)) {
773 fgets(buf, sizeof(buf), b->rfd);
779 BOOL CreateChildProcess(VOID);
780 VOID WriteToPipe(VOID);
781 VOID ReadFromPipe(VOID);
782 VOID ErrorExit(LPTSTR);
783 VOID ErrMsg(LPTSTR, BOOL);
787 getArgv0(const char *cmdline)
789 const char *cp = cmdline;
791 while (*cp && !isspace(*cp)) cp++;
793 int len = cp - cmdline;
794 char *rval = (char *)malloc(len+1);
806 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
808 PROCESS_INFORMATION piProcInfo;
809 STARTUPINFO siStartInfo;
810 BOOL bFuncRetn = FALSE;
812 // Set up members of the PROCESS_INFORMATION structure.
814 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
816 // Set up members of the STARTUPINFO structure.
818 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
819 siStartInfo.cb = sizeof(STARTUPINFO);
820 // setup new process to use supplied handles for stdin,stdout,stderr
821 // if supplied handles are not used the send a copy of our STD_HANDLE
823 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
825 if (in != INVALID_HANDLE_VALUE)
826 siStartInfo.hStdInput = in;
828 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
830 if (out != INVALID_HANDLE_VALUE)
831 siStartInfo.hStdOutput = out;
833 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
834 if (err != INVALID_HANDLE_VALUE)
835 siStartInfo.hStdError = err;
837 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
838 // Create the child process.
842 // retrive the first compont of the command line which should be the
844 const char *exeName = getArgv0(cmdline);
845 // check to see if absolute path was passed to us already?
846 if (exeName[1] != ':'
847 || (strchr(cmdline, '/') == NULL
848 && strchr(cmdline, '\\') == NULL))
850 // only command name so perform search of PATH to find
852 DWORD rval = SearchPath(NULL,
859 return INVALID_HANDLE_VALUE;
860 if (rval > sizeof(exeFile))
861 return INVALID_HANDLE_VALUE;
865 strcpy(exeFile, exeName);
867 // exeFile now has absolute path to program to execute.
868 free((void *)exeName);
869 // copy original command line to pass to create process
870 strcpy(cmdLine, cmdline);
871 // try to execute program
872 bFuncRetn = CreateProcess(exeFile,
873 cmdLine, // command line
874 NULL, // process security attributes
875 NULL, // primary thread security attributes
876 TRUE, // handles are inherited
878 NULL, // use parent's environment
879 NULL, // use parent's current directory
880 &siStartInfo, // STARTUPINFO pointer
881 &piProcInfo); // receives PROCESS_INFORMATION
883 if (bFuncRetn == 0) {
884 ErrorExit("CreateProcess failed\n");
885 return INVALID_HANDLE_VALUE;
887 // we don't need a handle on the process primary thread so we close
889 CloseHandle(piProcInfo.hThread);
891 return piProcInfo.hProcess;
896 ErrorExit (LPTSTR lpszMessage)
898 d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
903 typedef struct s_bpipe {
914 CloseIfValid(HANDLE handle)
916 if (handle != INVALID_HANDLE_VALUE)
922 open_bpipe(char *prog, int wait, const char *mode)
924 HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
925 hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
928 SECURITY_ATTRIBUTES saAttr;
932 hChildStdinRd = hChildStdinWr = hChildStdinWrDup =
933 hChildStdoutRd = hChildStdoutWr = hChildStdoutRdDup =
934 hInputFile = INVALID_HANDLE_VALUE;
936 BPIPE *bpipe = (BPIPE *)malloc(sizeof(BPIPE));
937 memset((void *)bpipe, 0, sizeof(BPIPE));
939 int mode_read = (mode[0] == 'r');
940 int mode_write = (mode[0] == 'w' || mode[1] == 'w');
943 // Set the bInheritHandle flag so pipe handles are inherited.
945 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
946 saAttr.bInheritHandle = TRUE;
947 saAttr.lpSecurityDescriptor = NULL;
951 // Create a pipe for the child process's STDOUT.
952 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
953 ErrorExit("Stdout pipe creation failed\n");
956 // Create noninheritable read handle and close the inheritable read
959 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
960 GetCurrentProcess(), &hChildStdoutRdDup , 0,
962 DUPLICATE_SAME_ACCESS);
964 ErrorExit("DuplicateHandle failed");
968 CloseHandle(hChildStdoutRd);
973 // Create a pipe for the child process's STDIN.
975 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
976 ErrorExit("Stdin pipe creation failed\n");
980 // Duplicate the write handle to the pipe so it is not inherited.
981 fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
982 GetCurrentProcess(), &hChildStdinWrDup,
984 FALSE, // not inherited
985 DUPLICATE_SAME_ACCESS);
987 ErrorExit("DuplicateHandle failed");
991 CloseHandle(hChildStdinWr);
993 // spawn program with redirected handles as appropriate
994 bpipe->worker_pid = (pid_t)
995 CreateChildProcess(prog, // commandline
996 hChildStdinRd, // stdin HANDLE
997 hChildStdoutWr, // stdout HANDLE
998 hChildStdoutWr);// stderr HANDLE
1000 if ((HANDLE) bpipe->worker_pid == INVALID_HANDLE_VALUE)
1004 bpipe->worker_stime = time(NULL);
1007 CloseHandle(hChildStdoutWr); // close our write side so when
1008 // process terminates we can
1010 // ugly but convert WIN32 HANDLE to FILE*
1011 int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY);
1012 bpipe->rfd = _fdopen(rfd, "r");
1015 CloseHandle(hChildStdinRd); // close our read side so as not
1016 // to interfre with child's copy
1017 // ugly but convert WIN32 HANDLE to FILE*
1018 int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY);
1019 bpipe->wfd = _fdopen(wfd, "w");
1023 bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait);
1030 CloseIfValid(hChildStdoutRd);
1031 CloseIfValid(hChildStdoutRdDup);
1032 CloseIfValid(hChildStdinWr);
1033 CloseIfValid(hChildStdinWrDup);
1035 free((void *) bpipe);
1043 kill(int pid, int signal)
1046 if (!TerminateProcess((HANDLE)pid, (UINT) signal))
1048 CloseHandle((HANDLE)pid);
1055 close_bpipe(BPIPE *bpipe)
1058 if (bpipe->rfd) fclose(bpipe->rfd);
1059 if (bpipe->wfd) fclose(bpipe->wfd);
1062 int remaining_wait = bpipe->wait;
1066 if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode))
1068 const char *err = errorString();
1069 rval = GetLastError();
1070 d_msg(__FILE__, __LINE__, 0,
1071 "GetExitCode error %s\n", err);
1072 LocalFree((void *)err);
1076 if (exitCode == STILL_ACTIVE) {
1077 bmicrosleep(1, 0); /* wait one second */
1081 } while(remaining_wait);
1084 if (bpipe->timer_id) {
1085 stop_child_timer(bpipe->timer_id);
1087 free((void *)bpipe);
1092 close_wpipe(BPIPE *bpipe)
1098 if (fclose(bpipe->wfd) != 0) {
1106 #include "findlib/find.h"
1109 utime(const char *fname, struct utimbuf *times)
1114 cygwin_conv_to_win32_path(fname, tmpbuf);
1116 cvt_utime_to_ftime(times->actime, acc);
1117 cvt_utime_to_ftime(times->modtime, mod);
1119 HANDLE h = CreateFile(tmpbuf,
1120 FILE_WRITE_ATTRIBUTES,
1127 if (h == INVALID_HANDLE_VALUE) {
1128 const char *err = errorString();
1129 d_msg(__FILE__, __LINE__, 99,
1130 "Cannot open file for utime(%s,...):%s\n", tmpbuf, err);
1131 LocalFree((void *)err);
1135 int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
1142 #if USE_WIN32_COMPAT_IO
1145 open(const char *file, int flags, int mode)
1147 return _open(file, flags|_O_BINARY, mode);
1158 read(int fd, void *buf, size_t len)
1160 return _read(fd, buf, len);
1164 write(int fd, const void *buf, size_t len)
1166 return _write(fd, buf, len);
1170 lseek(int fd, off_t offset, int whence)
1172 return _lseeki64(fd, offset, whence);
1176 dup2(int fd1, int fd2)
1178 return _dup2(fd1, fd2);
1182 open(const char *file, int flags, int mode)
1185 DWORD shareMode = 0;
1189 const char *remap = file;
1191 if (flags & O_WRONLY) access = GENERIC_WRITE;
1192 else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE;
1193 else access = GENERIC_READ;
1195 if (flags & O_CREAT) create = CREATE_NEW;
1196 else create = OPEN_EXISTING;
1198 if (flags & O_TRUNC) create = TRUNCATE_EXISTING;
1200 if (!(flags & O_EXCL))
1201 shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE;
1203 if (flags & O_APPEND)
1205 printf("open...APPEND not implemented yet.");
1209 foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
1210 if (INVALID_HANDLE_VALUE == foo)
1221 if (!CloseHandle((HANDLE)fd))
1228 write(int fd, const void *data, size_t len)
1232 status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL);
1233 if (status) return bwrite;
1239 read(int fd, void *data, size_t len)
1244 status = ReadFile((HANDLE)fd, data, len, &bread, NULL);
1245 if (status) return bread;
1250 lseek(int fd, off_t offset, int whence)
1255 method = FILE_BEGIN;
1258 method = FILE_CURRENT;
1267 return SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method);
1282 /* syslog function, added by Nicolas Boichat */