[project @ 2003-08-23 00:08:02 by sof]
authorsof <unknown>
Sat, 23 Aug 2003 00:08:02 +0000 (00:08 +0000)
committersof <unknown>
Sat, 23 Aug 2003 00:08:02 +0000 (00:08 +0000)
[win32]renameFile:
  Try bridging between GetLastError() error values and the
  errno-based error handling code in System.Directory. As was,
  errors ended up being reported as 'no errors'.

cbits/dirUtils.c

index ad3b406..b3fb6c0 100644 (file)
 
 #if defined(mingw32_TARGET_OS)
 #include <windows.h>
+
+static
+int
+toErrno(DWORD rc)
+{
+    switch (rc) {
+    case ERROR_FILE_NOT_FOUND:    return ENOENT;
+    case ERROR_PATH_NOT_FOUND:    return ENOENT;
+    case ERROR_TOO_MANY_OPEN_FILES: return EMFILE;
+    case ERROR_ACCESS_DENIED:     return EACCES;
+    case ERROR_INVALID_HANDLE:    return EBADF; /* kinda sorta */
+    case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
+    case ERROR_INVALID_ACCESS:    return EINVAL;
+    case ERROR_INVALID_DATA:      return EINVAL;
+    case ERROR_OUTOFMEMORY:       return ENOMEM;
+    case ERROR_SHARING_VIOLATION: return EACCES;
+    case ERROR_LOCK_VIOLATION:    return EACCES;
+    case ERROR_ALREADY_EXISTS:    return EEXIST;
+    case ERROR_BUSY:              return EBUSY;
+    case ERROR_BROKEN_PIPE:       return EPIPE;
+    case ERROR_PIPE_CONNECTED:    return EBUSY;
+    case ERROR_PIPE_LISTENING:    return EBUSY;
+    case ERROR_NOT_CONNECTED:     return EINVAL;
+
+    case ERROR_NOT_OWNER:         return EPERM;
+    case ERROR_DIRECTORY:         return ENOTDIR;
+    case ERROR_FILE_INVALID:      return EACCES;
+    case ERROR_FILE_EXISTS:       return EEXIST;
+
+    default:
+       return rc;
+    }
+}
 #endif
 
+
 /*
  * read an entry from the directory stream; opt for the
  * re-entrant friendly way of doing this, if available.
@@ -79,21 +113,20 @@ HsInt
 __hscore_renameFile( HsAddr src,
                     HsAddr dest)
 {
-#if defined(_MSC_VER) || defined(_WIN32)
+#if (defined(_MSC_VER) || defined(_WIN32))
     static int forNT = -1;
-    DWORD rc;
     
     /* ToDo: propagate error codes back */
     if (MoveFileA(src, dest)) {
        return 0;
     } else {
-       rc = GetLastError();
+       ;
     }
     
     /* Failed...it could be because the target already existed. */
     if ( !GetFileAttributes(dest) ) {
        /* No, it's not there - just fail. */
-       errno = 0;
+       errno = toErrno(GetLastError());
        return (-1);
     }
 
@@ -101,7 +134,7 @@ __hscore_renameFile( HsAddr src,
        OSVERSIONINFO ovi;
        ovi.dwOSVersionInfoSize = sizeof(ovi);
        if ( !GetVersionEx(&ovi) ) {
-           errno = 0; 
+           errno = toErrno(GetLastError()); 
            return (-1);
        }
        forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
@@ -112,7 +145,7 @@ __hscore_renameFile( HsAddr src,
        if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
            return 0;
        } else {
-           errno = 0; 
+           errno = toErrno(GetLastError()); 
            return (-1);
        }
     }
@@ -123,11 +156,11 @@ __hscore_renameFile( HsAddr src,
        if (MoveFileA(src,dest)) {
            return 0;
        } else {
-           errno = 0;
+           errno = toErrno(GetLastError());
            return (-1);
        }
     } else {
-       errno = 0;
+       errno = toErrno(GetLastError());
        return (-1);
     }
 #else