From b6545a7e398693cf2539c3ae6cb3ba63baf911a8 Mon Sep 17 00:00:00 2001 From: manojampalam Date: Sat, 5 Mar 2016 19:52:34 -0800 Subject: [PATCH] 3-5 c6 --- contrib/win32/win32compat/fileio.c | 12 +++++++- contrib/win32/win32compat/socketio.c | 5 +--- regress/unittests/win32compat/file_tests.c | 35 ++++++++++++++++++++++ regress/unittests/win32compat/tests.c | 5 ++-- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/contrib/win32/win32compat/fileio.c b/contrib/win32/win32compat/fileio.c index a2bda41..e545073 100644 --- a/contrib/win32/win32compat/fileio.c +++ b/contrib/win32/win32compat/fileio.c @@ -325,6 +325,15 @@ fileio_read(struct w32_io* pio, void *dst, unsigned int max) { } if (fileio_is_io_available(pio, TRUE) == FALSE) { + /* Workaround for - ReadFileEx is restting file pointer to beginning upon encoutering a EOF*/ + /* If there was a previous read and if the file pointer is at 0, then its been reset*/ + if ((pio->type == FILE_FD) && (pio->read_details.completed > 0)){ + DWORD fp = SetFilePointer(pio->handle, 0, NULL, FILE_CURRENT); + if (fp == 0) { + errno = 0; + return 0; + } + } if (-1 == fileio_ReadFileEx(pio)) { if ((pio->type == PIPE_FD) && (errno == ERROR_NEGATIVE_SEEK)) { /* write end of the pipe closed */ @@ -355,7 +364,8 @@ fileio_read(struct w32_io* pio, void *dst, unsigned int max) { if (pio->read_details.error) { errno = errno_from_Win32Error(pio->read_details.error); /*write end of the pipe is closed or pipe broken or eof reached*/ - if ((errno == ERROR_BROKEN_PIPE) || (errno == ERROR_HANDLE_EOF)) { + if ((pio->read_details.error == ERROR_BROKEN_PIPE) || + (pio->read_details.error == ERROR_HANDLE_EOF)) { debug2("no more data"); errno = 0; pio->read_details.error = 0; diff --git a/contrib/win32/win32compat/socketio.c b/contrib/win32/win32compat/socketio.c index 1d4cc87..1591f4e 100644 --- a/contrib/win32/win32compat/socketio.c +++ b/contrib/win32/win32compat/socketio.c @@ -603,10 +603,7 @@ socketio_close(struct w32_io* pio) { struct acceptEx_context *ctx = (struct acceptEx_context*)pio->internal.context; if (ctx->accept_socket != INVALID_SOCKET) closesocket(ctx->accept_socket); - if (ctx->lpOutputBuf) - free(ctx->lpOutputBuf); - /* TODO: check why this is crashing */ - //free(pio->internal.context); + free(pio->internal.context); } } diff --git a/regress/unittests/win32compat/file_tests.c b/regress/unittests/win32compat/file_tests.c index 786a194..aa69c61 100644 --- a/regress/unittests/win32compat/file_tests.c +++ b/regress/unittests/win32compat/file_tests.c @@ -64,6 +64,40 @@ file_blocking_io_tests() TEST_DONE(); } +void file_simple_fileio() +{ + char* small_write_buf = "sample payload"; + char small_read_buf[SMALL_RECV_BUF_SIZE]; + + int f; + TEST_START("file io"); + f = open("e:\\tmp.txt", O_WRONLY | O_CREAT | O_TRUNC); + ASSERT_INT_NE(f, -1); + close(f); + f = open("e:\\tmp.txt", O_RDONLY); + ASSERT_INT_NE(f, -1); + ret = read(f, small_read_buf, SMALL_RECV_BUF_SIZE); + ASSERT_INT_EQ(ret, 0); + close(f); + f = open("e:\\tmp.txt", O_WRONLY | O_CREAT | O_TRUNC); + ASSERT_INT_NE(f, -1); + ret = write(f, small_write_buf, strlen(small_write_buf)); + ASSERT_INT_EQ(ret, strlen(small_write_buf)); + close(f); + f = open("e:\\tmp.txt", O_RDONLY); + ASSERT_INT_NE(f, -1); + ret = read(f, small_read_buf, SMALL_RECV_BUF_SIZE); + ASSERT_INT_EQ(ret, strlen(small_write_buf)); + small_read_buf[ret] = '\0'; + ASSERT_STRING_EQ(small_write_buf, small_read_buf); + ret = read(f, small_read_buf, SMALL_RECV_BUF_SIZE); + ASSERT_INT_EQ(ret, 0); + close(f); + TEST_DONE(); + + +} + void file_nonblocking_io_tests() { @@ -188,6 +222,7 @@ void file_tests() { w32posix_initialize(); + //file_simple_fileio(); file_blocking_io_tests(); file_nonblocking_io_tests(); file_select_tests(); diff --git a/regress/unittests/win32compat/tests.c b/regress/unittests/win32compat/tests.c index fcb5adf..fcc3794 100644 --- a/regress/unittests/win32compat/tests.c +++ b/regress/unittests/win32compat/tests.c @@ -4,6 +4,7 @@ #include #include +#include #include #include "test_helper.h" @@ -17,9 +18,9 @@ void tests(void) { _set_abort_behavior(0, 1); log_init(NULL, 7, 2, 0); - logfd = _open("unittests.log", O_WRONLY | O_CREAT ); + logfd = _open("unittests.log", _O_WRONLY | _O_CREAT | _O_TRUNC, S_IREAD | S_IWRITE | _O_NOINHERIT); socket_tests(); file_tests(); - _close(logfd); + if (logfd > 0) _close(logfd); return; } \ No newline at end of file