]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/win32/compat/compat.cpp
Correct a minor build problem with wx-console.
[bacula/bacula] / bacula / src / win32 / compat / compat.cpp
index 7d114289c1303c9b3e3acf277a44cb870ff3e0fe..96c21319601df516948bf35a5e43098ab0e8d7ed 100644 (file)
@@ -5,7 +5,7 @@
 // Copyright transferred from Raider Solutions, Inc to
 //   Kern Sibbald and John Walker by express permission.
 //
-// Copyright (C) 2004 Kern Sibbald and John Walker
+// Copyright (C) 2004-2005 Kern Sibbald
 //
 //   This program is free software; you can redistribute it and/or
 //   modify it under the terms of the GNU General Public License as
 #include <stdio.h>
 
 #include "compat.h"
+#ifdef WIN32_VSS
+#include "vss.h"
+#endif 
 #include "pthread.h"
+#include "../../lib/winapi.h"
+
+/* to allow the usage of the original version in this file here */
+#undef fputs
 
 #define b_errno_win32 (1<<29)
 
@@ -48,9 +55,10 @@ extern DWORD   g_platform_id;
 #define WIN32_FILETIME_SCALE  10000000             // 100ns/second
 
 extern "C" void
-cygwin_conv_to_win32_path(const char *name, char *win32_name)
+cygwin_conv_to_win32_path(const char *name, char *win32_name, DWORD dwSize)
 {
     const char *fname = name;
+    char *tname = win32_name;
     while (*name) {
         /* Check for Unix separator and convert to Win32 */
         if (*name == '/') {
@@ -70,6 +78,37 @@ cygwin_conv_to_win32_path(const char *name, char *win32_name)
     } else {
         *win32_name = 0;
     }
+
+#ifdef WIN32_VSS
+    /* here we convert to VSS specific file name */    
+    char szFile[MAX_PATH_UNICODE];
+    strncpy (szFile, tname, MAX_PATH_UNICODE-1);
+    g_VSSClient.GetShadowPath(szFile,tname,dwSize);
+#endif
+}
+
+int 
+wchar_2_UTF8(char *pszUTF, const WCHAR *pszUCS, int cchChar)
+{
+   /* the return value is the number of bytes written to the buffer. 
+      The number includes the byte for the null terminator. */
+
+   if (p_WideCharToMultiByte)
+      return p_WideCharToMultiByte(CP_UTF8,0,pszUCS,-1,pszUTF,cchChar,NULL,NULL);
+   else
+      return NULL;
+}
+
+int 
+UTF8_2_wchar(WCHAR *pszUCS, const char *pszUTF, int cchWideChar)
+{
+   /*  the return value is the number of wide characters written to the buffer. */
+   /* convert null terminated string from utf-8 to ucs2*/
+
+   if (p_MultiByteToWideChar)
+      return p_MultiByteToWideChar(CP_UTF8, 0, pszUTF, -1, pszUCS,cchWideChar);
+   else
+      return NULL;
 }
 
 
@@ -98,30 +137,32 @@ wchar_win32_path(const char *name, WCHAR *win32_name)
     }
 }
 
-int
-umask(int)
+int umask(int)
 {
-    return 0;
+   return 0;
 }
 
-int
-chmod(const char *, mode_t)
+int chmod(const char *, mode_t)
 {
-    return 0;
+   return 0;
 }
 
-int
-chown(const char *k, uid_t, gid_t)
+int chown(const char *k, uid_t, gid_t)
 {
-    return 0;
+   return 0;
 }
 
-int
-lchown(const char *k, uid_t, gid_t)
+int lchown(const char *k, uid_t, gid_t)
 {
-    return 0;
+   return 0;
 }
 
+#ifdef needed
+bool fstype(const char *fname, char *fs, int fslen)
+{
+   return true;                       /* accept anything */
+}
+#endif
 
 
 long int
@@ -133,7 +174,7 @@ random(void)
 void
 srandom(unsigned int seed)
 {
-    srand(seed);
+   srand(seed);
 }
 // /////////////////////////////////////////////////////////////////
 // convert from Windows concept of time to Unix concept of time
@@ -189,9 +230,18 @@ errorString(void)
 static int
 statDir(const char *file, struct stat *sb)
 {
-    WIN32_FIND_DATA info;       // window's file info
-
-    if (file[1] == ':' && file[2] == 0) {
+   WIN32_FIND_DATAW info_w;       // window's file info
+   WIN32_FIND_DATAA info_a;       // window's file info
+
+   // cache some common vars to make code more transparent
+   DWORD* pdwFileAttributes;
+   DWORD* pnFileSizeHigh;
+   DWORD* pnFileSizeLow;
+   FILETIME* pftLastAccessTime;
+   FILETIME* pftLastWriteTime;
+   FILETIME* pftCreationTime;
+
+   if (file[1] == ':' && file[2] == 0) {
         d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file);
         sb->st_mode = S_IFDIR;
         sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE;
@@ -200,7 +250,33 @@ statDir(const char *file, struct stat *sb)
         time(&sb->st_atime);
         return 0;
     }
-    HANDLE h = FindFirstFile(file, &info);
+
+   HANDLE h = INVALID_HANDLE_VALUE;
+
+   // use unicode or ascii
+   if (p_FindFirstFileW) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
+      
+      h = p_FindFirstFileW(szBuf, &info_w);
+
+      pdwFileAttributes = &info_w.dwFileAttributes;
+      pnFileSizeHigh    = &info_w.nFileSizeHigh;
+      pnFileSizeLow     = &info_w.nFileSizeLow;
+      pftLastAccessTime = &info_w.ftLastAccessTime;
+      pftLastWriteTime  = &info_w.ftLastWriteTime;
+      pftCreationTime   = &info_w.ftCreationTime;
+   }
+   else if (p_FindFirstFileA) {
+      h = p_FindFirstFileA(file, &info_a);
+
+      pdwFileAttributes = &info_a.dwFileAttributes;
+      pnFileSizeHigh    = &info_a.nFileSizeHigh;
+      pnFileSizeLow     = &info_a.nFileSizeLow;
+      pftLastAccessTime = &info_a.ftLastAccessTime;
+      pftLastWriteTime  = &info_a.ftLastWriteTime;
+      pftCreationTime   = &info_a.ftCreationTime;
+   }
 
     if (h == INVALID_HANDLE_VALUE) {
         const char *err = errorString();
@@ -211,23 +287,23 @@ statDir(const char *file, struct stat *sb)
     }
 
     sb->st_mode = 0777;               /* start with everything */
-    if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
+    if (*pdwFileAttributes & FILE_ATTRIBUTE_READONLY)
         sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH);
-    if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
+    if (*pdwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
         sb->st_mode &= ~S_IRWXO; /* remove everything for other */
-    if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
+    if (*pdwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
         sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */
     sb->st_mode |= S_IFDIR;
 
-    sb->st_size = info.nFileSizeHigh;
+    sb->st_size = *pnFileSizeHigh;
     sb->st_size <<= 32;
-    sb->st_size |= info.nFileSizeLow;
+    sb->st_size |= *pnFileSizeLow;
     sb->st_blksize = 4096;
     sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096;
 
-    sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime);
-    sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime);
-    sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime);
+    sb->st_atime = cvt_ftime_to_utime(*pftLastAccessTime);
+    sb->st_mtime = cvt_ftime_to_utime(*pftLastWriteTime);
+    sb->st_ctime = cvt_ftime_to_utime(*pftCreationTime);
     FindClose(h);
 
     return 0;
@@ -240,9 +316,19 @@ stat2(const char *file, struct stat *sb)
     HANDLE h;
     int rval = 0;
     char tmpbuf[1024];
-    cygwin_conv_to_win32_path(file, tmpbuf);
+    cygwin_conv_to_win32_path(file, tmpbuf, 1024);
 
-    DWORD attr = GetFileAttributes(tmpbuf);
+    DWORD attr = -1;
+
+    if (p_GetFileAttributesW) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, tmpbuf, MAX_PATH_UNICODE);
+      
+      attr = p_GetFileAttributesW(szBuf);
+    }
+    else if (p_GetFileAttributesA) {
+       attr = p_GetFileAttributesA(tmpbuf);
+    }
 
     if (attr == -1) {
         const char *err = errorString();
@@ -256,7 +342,7 @@ stat2(const char *file, struct stat *sb)
     if (attr & FILE_ATTRIBUTE_DIRECTORY)
         return statDir(tmpbuf, sb);
 
-    h = CreateFile(tmpbuf, GENERIC_READ,
+    h = CreateFileA(tmpbuf, GENERIC_READ,
                    FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
 
     if (h == INVALID_HANDLE_VALUE) {
@@ -284,6 +370,9 @@ stat2(const char *file, struct stat *sb)
     sb->st_ino <<= 32;
     sb->st_ino |= info.nFileIndexLow;
     sb->st_nlink = (short)info.nNumberOfLinks;
+    if (sb->st_nlink > 1) {
+       d_msg(__FILE__, __LINE__, 99,  "st_nlink=%d\n", sb->st_nlink);
+    }
 
     sb->st_mode = 0777;               /* start with everything */
     if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
@@ -333,8 +422,19 @@ stat(const char *file, struct stat *sb)
     if (!GetFileAttributesExW((WCHAR *)buf, GetFileExInfoStandard, &data))
         return stat2(file, sb);
 #else
-    if (!GetFileAttributesEx(file, GetFileExInfoStandard, &data))
-        return stat2(file, sb);
+
+    if (p_GetFileAttributesExW) {
+      WCHAR buf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(buf, file, MAX_PATH_UNICODE);
+      if (!p_GetFileAttributesExW(buf, GetFileExInfoStandard, &data))
+         return stat2(file, sb);
+    } else if (p_GetFileAttributesExA) {
+         if (!p_GetFileAttributesExA(file, GetFileExInfoStandard, &data))
+            return stat2(file, sb);
+    }
+    else
+       return stat2(file, sb);
+
 #endif
 
     sb->st_mode = 0777;               /* start with everything */
@@ -514,19 +614,23 @@ getgrgid(uid_t)
 }
 
 // implement opendir/readdir/closedir on top of window's API
+
 typedef struct _dir
 {
-    WIN32_FIND_DATA data;       // window's file info
+    WIN32_FIND_DATAA data_a;    // window's file info (ansii version)
+    WIN32_FIND_DATAW data_w;    // window's file info (wchar version)
     const char *spec;           // the directory we're traversing
     HANDLE      dirh;           // the search handle
-    BOOL        valid;          // the info in data field is valid
+    BOOL        valid_a;        // the info in data_a field is valid
+    BOOL        valid_w;        // the info in data_w field is valid
     UINT32      offset;         // pseudo offset for d_off
 } _dir;
 
 DIR *
 opendir(const char *path)
 {
-    int max_len = strlen(path) + 16;
+    /* enough space for VSS !*/
+    int max_len = strlen(path) + MAX_PATH;
     _dir *rval = NULL;
     if (path == NULL) {
        errno = ENOENT;
@@ -534,6 +638,7 @@ opendir(const char *path)
     }
 
     rval = (_dir *)malloc(sizeof(_dir));
+    memset (rval, 0, sizeof (_dir));
     if (rval == NULL) return NULL;
     char *tspec = (char *)malloc(max_len);
     if (tspec == NULL) return NULL;
@@ -545,15 +650,30 @@ opendir(const char *path)
         tspec[2] = '?';
         tspec[3] = '\\';
         tspec[4] = 0;
-        cygwin_conv_to_win32_path(path, tspec+4);
+        cygwin_conv_to_win32_path(path, tspec+4, max_len-4);
     } else {
-        cygwin_conv_to_win32_path(path, tspec);
+        cygwin_conv_to_win32_path(path, tspec, max_len);
     }
 
     strncat(tspec, "\\*", max_len);
     rval->spec = tspec;
 
-    rval->dirh = FindFirstFile(rval->spec, &rval->data);
+    // convert to WCHAR
+    if (p_FindFirstFileW) {
+      WCHAR wcBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(wcBuf,rval->spec, MAX_PATH_UNICODE);
+      rval->dirh = p_FindFirstFileW(wcBuf, &rval->data_w);   
+
+      if (rval->dirh != INVALID_HANDLE_VALUE)
+        rval->valid_w = 1;
+    } else if (p_FindFirstFileA) {
+      rval->dirh = p_FindFirstFileA(rval->spec, &rval->data_a);
+      
+      if (rval->dirh != INVALID_HANDLE_VALUE)
+        rval->valid_a = 1;
+    } else goto err;
+
+
     d_msg(__FILE__, __LINE__,
           99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n",
           path, rval->spec, rval->dirh);
@@ -561,9 +681,15 @@ opendir(const char *path)
     rval->offset = 0;
     if (rval->dirh == INVALID_HANDLE_VALUE)
         goto err;
-    rval->valid = 1;
-    d_msg(__FILE__, __LINE__,
-          99, "\tFirstFile=%s\n", rval->data.cFileName);
+
+    if (rval->valid_w)
+      d_msg(__FILE__, __LINE__,
+            99, "\tFirstFile=%s\n", rval->data_w.cFileName);
+
+    if (rval->valid_a)
+      d_msg(__FILE__, __LINE__,
+            99, "\tFirstFile=%s\n", rval->data_a.cFileName);
+
     return (DIR *)rval;
 
 err:
@@ -616,19 +742,38 @@ int
 readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
 {
     _dir *dp = (_dir *)dirp;
-    if (dp->valid) {
-        entry->d_off = dp->offset;
-        dp->offset += copyin(*entry, dp->data.cFileName);
-        *result = entry;              /* return entry address */
-        d_msg(__FILE__, __LINE__,
-              99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
-              dirp, entry->d_name, entry->d_reclen, entry->d_off);
+    if (dp->valid_w || dp->valid_a) {
+      entry->d_off = dp->offset;
+                
+      // copy unicode
+      if (dp->valid_w) {
+         char szBuf[MAX_PATH_UTF8];
+         wchar_2_UTF8(szBuf,dp->data_w.cFileName);
+         dp->offset += copyin(*entry, szBuf);
+      } else if (dp->valid_a) { // copy ansi (only 1 will be valid)
+         dp->offset += copyin(*entry, dp->data_a.cFileName);
+      }
+                
+      *result = entry;              /* return entry address */
+      d_msg(__FILE__, __LINE__,
+            99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n",
+            dirp, entry->d_name, entry->d_reclen, entry->d_off);
     } else {
 //      d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n");
         errno = b_errno_win32;
         return -1;
     }
-    dp->valid = FindNextFile(dp->dirh, &dp->data);
+
+    // get next file, try unicode first
+    if (p_FindNextFileW) 
+       dp->valid_w = p_FindNextFileW(dp->dirh, &dp->data_w);
+    else if (p_FindNextFileA)
+       dp->valid_a = p_FindNextFileA(dp->dirh, &dp->data_a);
+    else {
+       dp->valid_a = FALSE;
+       dp->valid_w = FALSE;
+    }
+
     return 0;
 }
 
@@ -657,6 +802,7 @@ inet_aton(const char *a, struct in_addr *inp)
          }
          acc = (acc << 8) + tmp;
          dotc++;
+         tmp = 0;
       } else {
          return 0;              /* error */
       }
@@ -726,17 +872,50 @@ WSA_Init(void)
 int
 win32_chdir(const char *dir)
 {
-    if (0 == SetCurrentDirectory(dir)) {
-       errno = b_errno_win32;
-       return -1;
-    }
-    return 0;
+   if (p_SetCurrentDirectoryW) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, dir, MAX_PATH_UNICODE);
+
+      if (0 == p_SetCurrentDirectoryW(szBuf)) {
+         errno = b_errno_win32;
+         return -1;
+      }
+   }
+   else if (p_SetCurrentDirectoryA) {
+      if (0 == p_SetCurrentDirectoryA(dir)) {
+         errno = b_errno_win32;
+         return -1;
+      }
+   }
+   else return -1;
+   
+   return 0;
 }
 
+int
+win32_mkdir(const char *dir)
+{
+   if (p_wmkdir){
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, dir, MAX_PATH_UNICODE);
+      return p_wmkdir(szBuf);
+   }
+
+   return _mkdir(dir);        
+}
+
+
 char *
 win32_getcwd(char *buf, int maxlen)
 {
-   int n =  GetCurrentDirectory(maxlen, buf);
+   int n=0;
+
+   if (p_GetCurrentDirectoryW) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      n = p_GetCurrentDirectoryW(maxlen, szBuf);
+      n = wchar_2_UTF8 (buf, szBuf, maxlen)-1;
+   } else if (p_GetCurrentDirectoryA)
+      n = p_GetCurrentDirectoryA(maxlen, buf);
 
    if (n == 0 || n > maxlen) return NULL;
 
@@ -748,6 +927,72 @@ win32_getcwd(char *buf, int maxlen)
    return buf;
 }
 
+int 
+win32_fputs(const char *string, FILE *stream)
+{
+   /* we use _cwprintf (console printf)
+      so we can be sure that unicode support works on win32.
+      this works under nt and 98 (95 and me not tested)
+   */
+
+   if (p_MultiByteToWideChar && (stream == stdout) && p_cwprintf) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, string, MAX_PATH_UNICODE);
+      return p_cwprintf (szBuf);
+   }
+
+   return fputs(string, stream);
+}
+
+char*
+win32_cgets (char* buffer, int len)
+{
+   /* we use console gets / getws to be able to read unicode
+      from the win32 console */
+
+   /* nt and unicode conversion */
+   if ((g_platform_id == VER_PLATFORM_WIN32_NT) && p_WideCharToMultiByte && p_cgetws) {      
+      WCHAR szBuf[260];
+      szBuf[0] = min (255, len); /* max len, must be smaller than buffer */
+      if (p_cgetws(szBuf) && wchar_2_UTF8(buffer, &szBuf[2], len))
+         return buffer;
+   }
+
+   /* win 9x and unicode conversion */
+   if ((g_platform_id == VER_PLATFORM_WIN32_WINDOWS) && p_WideCharToMultiByte && p_MultiByteToWideChar) {
+      char szBuf[260];
+      szBuf[0] = min(255, len); /* max len, must be smaller than buffer */
+      if (_cgets(szBuf)) {  
+         WCHAR wszBuf[260];
+         p_MultiByteToWideChar(CP_OEMCP, 0, &szBuf[2], -1, wszBuf,260);
+
+         if (wchar_2_UTF8(buffer, wszBuf, len))
+            return buffer;
+      }
+   }
+
+   /* fallback */
+   if (fgets(buffer, len, stdin)) 
+      return buffer;   
+   else
+      return NULL;
+}
+
+int
+win32_unlink(const char *filename)
+{
+   int nRetCode;
+   if (p_wunlink) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, filename, MAX_PATH_UNICODE);
+      nRetCode = _wunlink(szBuf);
+   } else {
+      nRetCode = _unlink(filename);
+   }
+   return nRetCode;
+}
+
+
 #include "mswinver.h"
 
 char WIN_VERSION_LONG[64];
@@ -809,7 +1054,7 @@ winver::winver(void)
 BOOL CreateChildProcess(VOID);
 VOID WriteToPipe(VOID);
 VOID ReadFromPipe(VOID);
-VOID ErrorExit(LPTSTR);
+VOID ErrorExit(LPCSTR);
 VOID ErrMsg(LPTSTR, BOOL);
 
 /**
@@ -857,7 +1102,7 @@ HANDLE
 CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
 {
     PROCESS_INFORMATION piProcInfo;
-    STARTUPINFO siStartInfo;
+    STARTUPINFOA siStartInfo;
     BOOL bFuncRetn = FALSE;
 
     // Set up members of the PROCESS_INFORMATION structure.
@@ -903,7 +1148,7 @@ CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
     strcat(cmdLine, cmdline);
 
     // try to execute program
-    bFuncRetn = CreateProcess(exeFile,
+    bFuncRetn = CreateProcessA(exeFile,
                               cmdLine, // command line
                               NULL, // process security attributes
                               NULL, // primary thread security attributes
@@ -931,7 +1176,7 @@ CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err)
 
 
 void
-ErrorExit (LPTSTR lpszMessage)
+ErrorExit (LPCSTR lpszMessage)
 {
     d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage);
 }
@@ -1086,13 +1331,13 @@ cleanup:
 int
 kill(int pid, int signal)
 {
-    int rval = 0;
-    if (!TerminateProcess((HANDLE)pid, (UINT) signal)) {
-        rval = -1;
-        errno = b_errno_win32;
-    }
-    CloseHandle((HANDLE)pid);
-    return rval;
+   int rval = 0;
+   if (!TerminateProcess((HANDLE)pid, (UINT) signal)) {
+      rval = -1;
+      errno = b_errno_win32;
+   }
+   CloseHandle((HANDLE)pid);
+   return rval;
 }
 
 #ifndef HAVE_MINGW
@@ -1100,37 +1345,45 @@ kill(int pid, int signal)
 int
 close_bpipe(BPIPE *bpipe)
 {
-    int rval = 0;
-    if (bpipe->rfd) fclose(bpipe->rfd);
-    if (bpipe->wfd) fclose(bpipe->wfd);
-
-    if (bpipe->wait) {
-        int remaining_wait = bpipe->wait;
-        do {
-            DWORD exitCode;
-            if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) {
-                const char *err = errorString();
-                rval = b_errno_win32;
-                d_msg(__FILE__, __LINE__, 0,
-                      "GetExitCode error %s\n", err);
-                LocalFree((void *)err);
-                break;
-            }
-
-            if (exitCode == STILL_ACTIVE) {
-                bmicrosleep(1, 0);             /* wait one second */
-                remaining_wait--;
-            }
-            else break;
-        } while(remaining_wait);
-        rval = ETIME;                 /* timed out */
-    }
+   int rval = 0;
+   int32_t remaining_wait = bpipe->wait;
 
-    if (bpipe->timer_id) {
-        stop_child_timer(bpipe->timer_id);
-    }
-    free((void *)bpipe);
-    return rval;
+   if (remaining_wait == 0) {         /* wait indefinitely */
+      remaining_wait = INT32_MAX;
+   }
+   for ( ;; ) {
+      DWORD exitCode;
+      if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) {
+         const char *err = errorString();
+         rval = b_errno_win32;
+         d_msg(__FILE__, __LINE__, 0,
+               "GetExitCode error %s\n", err);
+         LocalFree((void *)err);
+         break;
+      }
+      if (exitCode == STILL_ACTIVE) {
+         if (remaining_wait <= 0) {
+            rval = ETIME;             /* timed out */
+            break;
+         }
+         bmicrosleep(1, 0);           /* wait one second */
+         remaining_wait--;
+      } else if (exitCode != 0) {
+         /* Truncate exit code as it doesn't seem to be correct */
+         rval = (exitCode & 0xFF) | b_errno_exit;
+         break;
+      } else {
+         break;                       /* Shouldn't get here */
+      }
+   }
+
+   if (bpipe->timer_id) {
+       stop_child_timer(bpipe->timer_id);
+   }
+   if (bpipe->rfd) fclose(bpipe->rfd);
+   if (bpipe->wfd) fclose(bpipe->wfd);
+   free((void *)bpipe);
+   return rval;
 }
 
 int
@@ -1156,23 +1409,38 @@ utime(const char *fname, struct utimbuf *times)
     FILETIME acc, mod;
     char tmpbuf[1024];
 
-    cygwin_conv_to_win32_path(fname, tmpbuf);
+    cygwin_conv_to_win32_path(fname, tmpbuf, 1024);
 
     cvt_utime_to_ftime(times->actime, acc);
     cvt_utime_to_ftime(times->modtime, mod);
 
-    HANDLE h = CreateFile(tmpbuf,
-                          FILE_WRITE_ATTRIBUTES,
-                          FILE_SHARE_WRITE,
-                          NULL,
-                          OPEN_EXISTING,
-                          0,
-                          NULL);
+    HANDLE h = INVALID_HANDLE_VALUE;
+
+    if (p_CreateFileW) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, tmpbuf, MAX_PATH_UNICODE);
+
+      h = p_CreateFileW(szBuf,
+                        FILE_WRITE_ATTRIBUTES,
+                        FILE_SHARE_WRITE,
+                        NULL,
+                        OPEN_EXISTING,
+                        0,
+                        NULL);
+    } else if (p_CreateFileA) {
+      h = p_CreateFileA(tmpbuf,
+                        FILE_WRITE_ATTRIBUTES,
+                        FILE_SHARE_WRITE,
+                        NULL,
+                        OPEN_EXISTING,
+                        0,
+                        NULL);
+    }
 
     if (h == INVALID_HANDLE_VALUE) {
         const char *err = errorString();
         d_msg(__FILE__, __LINE__, 99,
-              "Cannot open file for utime(%s,...):%s\n", tmpbuf, err);
+              "Cannot open file \"%s\" for utime(): ERR=%s", tmpbuf, err);
         LocalFree((void *)err);
         errno = b_errno_win32;
         return -1;
@@ -1180,6 +1448,9 @@ utime(const char *fname, struct utimbuf *times)
 
     int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1;
     CloseHandle(h);
+    if (rval == -1) {
+       errno = b_errno_win32;
+    }
     return rval;
 }
 
@@ -1188,8 +1459,14 @@ utime(const char *fname, struct utimbuf *times)
 int
 open(const char *file, int flags, int mode)
 {
-    return _open(file, flags|_O_BINARY, mode);
+   if (p_wopen) {
+      WCHAR szBuf[MAX_PATH_UNICODE];
+      UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
+
+      return p_wopen(szBuf, flags|_O_BINARY, mode);
+   }
 
+   return _open(file, flags|_O_BINARY, mode);
 }
 
 /*
@@ -1218,6 +1495,7 @@ write(int fd, const void *buf, ssize_t len)
 }
 #endif
 
+
 off_t
 lseek(int fd, off_t offset, int whence)
 {
@@ -1237,7 +1515,7 @@ open(const char *file, int flags, int mode)
     DWORD shareMode = 0;
     DWORD create = 0;
     DWORD msflags = 0;
-    HANDLE foo;
+    HANDLE foo = INVALID_HANDLE_VALUE;
     const char *remap = file;
 
     if (flags & O_WRONLY) access = GENERIC_WRITE;
@@ -1257,7 +1535,15 @@ open(const char *file, int flags, int mode)
         exit(-1);
     }
 
-    foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
+    if (p_CreateFileW) {
+        WCHAR szBuf[MAX_PATH_UNICODE];
+        UTF8_2_wchar(szBuf, file, MAX_PATH_UNICODE);
+
+        foo = p_CreateFileW(szBuf, access, shareMode, NULL, create, msflags, NULL);
+    }
+    else if (p_CreateFileA)
+       foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL);
+
     if (INVALID_HANDLE_VALUE == foo) {
         errno = b_errno_win32;
         return(int) -1;