From c529e727d7a5ad1ba85c212d290a43c240173277 Mon Sep 17 00:00:00 2001 From: Thomas Faber Date: Sun, 25 Mar 2018 16:15:31 +0200 Subject: [PATCH] [CRT] Update file descriptor handling to match Wine (3/7). CORE-14504 Import Wine commits by Piotr Caban: * 6c2d4f1092d msvcrt: Use fd critical section in _fstat64. * 9278190d468 msvcrt: Use fd critical section in _futime64. --- sdk/lib/crt/include/internal/wine/msvcrt.h | 9 +++++++++ sdk/lib/crt/stdio/file.c | 14 +++----------- sdk/lib/crt/stdio/stat64.c | 20 ++++++++++++++------ sdk/lib/crt/time/futime.c | 15 +++++++++------ 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/sdk/lib/crt/include/internal/wine/msvcrt.h b/sdk/lib/crt/include/internal/wine/msvcrt.h index 5dc3a37395d..ab37a4aeda4 100644 --- a/sdk/lib/crt/include/internal/wine/msvcrt.h +++ b/sdk/lib/crt/include/internal/wine/msvcrt.h @@ -153,4 +153,13 @@ typedef struct _sig_element char* _setlocale(int,const char*); NTSYSAPI VOID NTAPI RtlAssert(PVOID FailedAssertion,PVOID FileName,ULONG LineNumber,PCHAR Message); +/* ioinfo structure size is different in msvcrXX.dll's */ +typedef struct { + HANDLE handle; + unsigned char wxflag; + char lookahead[3]; + int exflag; + CRITICAL_SECTION crit; +} ioinfo; + #endif /* __WINE_MSVCRT_H */ diff --git a/sdk/lib/crt/stdio/file.c b/sdk/lib/crt/stdio/file.c index 878f3b13d1f..dde5a8ebbab 100644 --- a/sdk/lib/crt/stdio/file.c +++ b/sdk/lib/crt/stdio/file.c @@ -45,6 +45,7 @@ #include #include "wine/unicode.h" +#include "internal/wine/msvcrt.h" #include #include @@ -113,15 +114,6 @@ static char utf16_bom[2] = { 0xff, 0xfe }; #define MSVCRT_INTERNAL_BUFSIZ 4096 -/* ioinfo structure size is different in msvcrXX.dll's */ -typedef struct { - HANDLE handle; - unsigned char wxflag; - char lookahead[3]; - int exflag; - CRITICAL_SECTION crit; -} ioinfo; - /********************************************************************* * __pioinfo (MSVCRT.@) * array of pointers to ioinfo arrays [32] @@ -180,7 +172,7 @@ static inline ioinfo* get_ioinfo_nolock(int fd) return ret + (fd%MSVCRT_FD_BLOCK_SIZE); } -static inline ioinfo* get_ioinfo(int fd) +/*static*/ inline ioinfo* get_ioinfo(int fd) { ioinfo *ret = get_ioinfo_nolock(fd); if(ret->exflag & EF_CRIT_INIT) @@ -188,7 +180,7 @@ static inline ioinfo* get_ioinfo(int fd) return ret; } -static inline void release_ioinfo(ioinfo *info) +/*static*/ inline void release_ioinfo(ioinfo *info) { if(info->exflag & EF_CRIT_INIT) LeaveCriticalSection(&info->crit); diff --git a/sdk/lib/crt/stdio/stat64.c b/sdk/lib/crt/stdio/stat64.c index 41340bbd9f5..98e63c57fbe 100644 --- a/sdk/lib/crt/stdio/stat64.c +++ b/sdk/lib/crt/stdio/stat64.c @@ -1,8 +1,10 @@ #include #include #include +#include -HANDLE fdtoh(int fd); +inline ioinfo* get_ioinfo(int fd); +inline void release_ioinfo(ioinfo *info); #define ALL_S_IREAD (_S_IREAD | (_S_IREAD >> 3) | (_S_IREAD >> 6)) #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6)) @@ -97,25 +99,29 @@ int CDECL _tstat64(const _TCHAR *path, struct __stat64 *buf) int CDECL _fstat64(int fd, struct __stat64* buf) { + ioinfo *info = get_ioinfo(fd); DWORD dw; DWORD type; BY_HANDLE_FILE_INFORMATION hfi; - HANDLE hand = fdtoh(fd); - TRACE(":fd (%d) stat (%p)\n",fd,buf); - if (hand == INVALID_HANDLE_VALUE) + TRACE(":fd (%d) stat (%p)\n", fd, buf); + if (info->handle == INVALID_HANDLE_VALUE) + { + release_ioinfo(info); return -1; + } if (!buf) { WARN(":failed-NULL buf\n"); _dosmaperr(ERROR_INVALID_PARAMETER); + release_ioinfo(info); return -1; } memset(&hfi, 0, sizeof(hfi)); memset(buf, 0, sizeof(struct __stat64)); - type = GetFileType(hand); + type = GetFileType(info->handle); if (type == FILE_TYPE_PIPE) { buf->st_dev = buf->st_rdev = fd; @@ -130,10 +136,11 @@ int CDECL _fstat64(int fd, struct __stat64* buf) } else /* FILE_TYPE_DISK etc. */ { - if (!GetFileInformationByHandle(hand, &hfi)) + if (!GetFileInformationByHandle(info->handle, &hfi)) { WARN(":failed-last error (%d)\n",GetLastError()); _dosmaperr(ERROR_INVALID_PARAMETER); + release_ioinfo(info); return -1; } buf->st_mode = _S_IFREG | ALL_S_IREAD; @@ -148,6 +155,7 @@ int CDECL _fstat64(int fd, struct __stat64* buf) } TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes, buf->st_mode); + release_ioinfo(info); return 0; } diff --git a/sdk/lib/crt/time/futime.c b/sdk/lib/crt/time/futime.c index 834b527aaa2..535012c05ee 100644 --- a/sdk/lib/crt/time/futime.c +++ b/sdk/lib/crt/time/futime.c @@ -39,8 +39,10 @@ #include #include #include "bitsfixup.h" +#include -HANDLE fdtoh(int fd); +inline ioinfo* get_ioinfo(int fd); +inline void release_ioinfo(ioinfo *info); /****************************************************************************** * \name _futime @@ -52,12 +54,12 @@ HANDLE fdtoh(int fd); int _futime(int fd, struct _utimbuf *filetime) { - HANDLE handle; + ioinfo *info = get_ioinfo(fd); FILETIME at, wt; - handle = fdtoh(fd); - if (handle == INVALID_HANDLE_VALUE) + if (info->handle == INVALID_HANDLE_VALUE) { + release_ioinfo(info); return -1; } @@ -84,11 +86,12 @@ _futime(int fd, struct _utimbuf *filetime) } } - if (!SetFileTime(handle, NULL, &at, &wt)) + if (!SetFileTime(info->handle, NULL, &at, &wt)) { + release_ioinfo(info); _dosmaperr(GetLastError()); return -1 ; } - + release_ioinfo(info); return 0; } -- 2.17.1