X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=cbits%2FdirUtils.c;h=08ea54e550f3712c13fa3c1c6f3136d1aa153ff8;hb=608b4a941ca509be625951252f76931b4d7d6d84;hp=316a689e7db3ac239b9288bbacd62187c2633a72;hpb=25955bc845f3680632b6230148f1a39d6a5c1e88;p=ghc-base.git diff --git a/cbits/dirUtils.c b/cbits/dirUtils.c index 316a689..08ea54e 100644 --- a/cbits/dirUtils.c +++ b/cbits/dirUtils.c @@ -4,7 +4,7 @@ * Directory Runtime Support */ -#include "config.h" +#include "ghcconfig.h" // The following is required on Solaris to force the POSIX versions of // the various _r functions instead of the Solaris versions. @@ -14,10 +14,44 @@ #include "HsBase.h" -#if defined(mingw32_TARGET_OS) +#if defined(mingw32_TARGET_OS) || defined(__MINGW32__) || defined(_MSC_VER) #include + +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. @@ -29,12 +63,12 @@ __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt ) #if HAVE_READDIR_R struct dirent* p; int res; - static unsigned int nm_max = -1; + static unsigned int nm_max = (unsigned int)-1; if (pDirE == NULL) { return -1; } - if (nm_max == -1) { + if (nm_max == (unsigned int)-1) { #ifdef NAME_MAX nm_max = NAME_MAX + 1; #else @@ -47,7 +81,11 @@ __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt ) if (p == NULL) return -1; res = readdir_r((DIR*)dirPtr, p, pDirE); if (res != 0) { - *pDirE = NULL; + *pDirE = NULL; + free(p); + } + else if (*pDirE == NULL) { + // end of stream free(p); } return res; @@ -79,21 +117,20 @@ HsInt __hscore_renameFile( HsAddr src, HsAddr dest) { -#if defined(_MSC_VER) || defined(_WIN32) +#if defined(mingw32_TARGET_OS) || defined(__MINGW32__) || defined(_MSC_VER) 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 +138,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 +149,7 @@ __hscore_renameFile( HsAddr src, if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) { return 0; } else { - errno = 0; + errno = toErrno(GetLastError()); return (-1); } } @@ -123,11 +160,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