From 289244b2d40beda79db806556168c9c654ed4b7e Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Sun, 19 Aug 2018 12:00:20 +0200 Subject: [PATCH] fix #1 - Windows scattered sync read/write incorrectly implemented - must break on partial reads/writes so that newly arriving data/buffer-space in between read/write calls doesn't lead to gaps in scatter buffers --- src/std/io/driver/sync.d | 14 ++++++++++++-- src/std/io/file.d | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/std/io/driver/sync.d b/src/std/io/driver/sync.d index 3bd0e61..284a915 100644 --- a/src/std/io/driver/sync.d +++ b/src/std/io/driver/sync.d @@ -143,7 +143,12 @@ shared @safe @nogc: { size_t total; foreach (b; bufs) - total += read(f, b); + { + immutable len = read(f, b); + total += len; + if (len < b.length) + break; + } return total; } } @@ -179,7 +184,12 @@ shared @safe @nogc: { size_t total; foreach (b; bufs) - total += write(f, b); + { + immutable len = write(f, b); + total += len; + if (len < b.length) + break; + } return total; } } diff --git a/src/std/io/file.d b/src/std/io/file.d index 14aa65b..c602962 100644 --- a/src/std/io/file.d +++ b/src/std/io/file.d @@ -255,6 +255,17 @@ struct File assert(f.read(buf[$ / 2 .. $], buf[0 .. $ / 2]) == buf.length); } + @("partial reads") + unittest + { + auto f = File("LICENSE.txt"); + ubyte[256] buf = void; + auto len = f.read(buf[$ / 2 .. $], buf[0 .. $ / 2]); + while (len == buf.length) + len = f.read(buf[$ / 2 .. $], buf[0 .. $ / 2]); + assert(len < buf.length); + } + /** Write buffer content to file.