[vlc-devel] [PATCH] core: fix block_test on Win32
Petri Hintukainen
phintuka at gmail.com
Sat Jun 4 16:09:36 CEST 2016
On la, 2016-06-04 at 12:54 +0200, Steve Lhomme wrote:
> fopen("wb+e") doesn't share the file we write in for reading
> ReadFile() doesn't work on shared fileno
> ---
> src/misc/block.c | 17 +++--------------
> src/test/block_test.c | 11 ++++++++++-
> 2 files changed, 13 insertions(+), 15 deletions(-)
>
> diff --git a/src/misc/block.c b/src/misc/block.c
> index f7ee8d8..0d51bca 100644
> --- a/src/misc/block.c
> +++ b/src/misc/block.c
> @@ -378,23 +378,12 @@ block_t *block_shm_Alloc (void *addr, size_t
> length)
>
>
> #ifdef _WIN32
> -# include <io.h>
> -
> -static
> ssize_t pread (int fd, void *buf, size_t count, off_t offset)
> {
> - HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
> - if (handle == INVALID_HANDLE_VALUE)
> + if ( lseek (fd, offset, SEEK_SET) != offset ) {
> return -1;
> -
> - OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset
> >> 32);
> - DWORD written;
> - /* This braindead API will override the file pointer even if we
> specify
> - * an explicit read offset... So do not expect this to mix well
> with
> - * regular read() calls. */
> - if (ReadFile (handle, buf, count, &written, &olap))
> - return written;
> - return -1;
> + }
> + return read( fd, buf, count );
> }
> #endif
I think this change makes pread() more or less useless. seek() + read()
should be atomic. If the function is meant to be something else than
pread() replacement, it should use different name.
> diff --git a/src/test/block_test.c b/src/test/block_test.c
> index f11ea2f..9642d9c 100644
> --- a/src/test/block_test.c
> +++ b/src/test/block_test.c
> @@ -23,6 +23,9 @@
> #endif
>
> #include <stdio.h>
> +#ifdef _WIN32
> +#include <share.h>
> +#endif
> #include <string.h>
> #undef NDEBUG
> #include <assert.h>
> @@ -39,7 +42,11 @@ static void test_block_File (void)
> FILE *stream;
> int res;
>
> +#ifdef _WIN32
> + stream = _fsopen ("testfile.txt", "wb+", _SH_DENYNO);
> +#else
> stream = fopen ("testfile.txt", "wb+e");
> +#endif
> assert (stream != NULL);
>
> res = fputs (text, stream);
> @@ -47,7 +54,9 @@ static void test_block_File (void)
> res = fflush (stream);
> assert (res != EOF);
>
> - block_t *block = block_File (fileno (stream));
> + int fd = fileno (stream);
> + assert(fd != -1);
> + block_t *block = block_File (fd);
> fclose (stream);
>
> assert (block != NULL);
More information about the vlc-devel
mailing list