From 513bebcb9573280433c5939027945d93d5181143 Mon Sep 17 00:00:00 2001 From: v1ne Date: Sat, 20 Jul 2019 01:34:27 +0200 Subject: [PATCH] FileStream: Approximate the file offset better when appending Instead of starting at zero, start at the current file size. This offset is stored in FileStream. It is only an approximation because concurrent writes could advance the file without FileStream's knowledge. Add a test that shows that the offset is approximated as expected and that appending to an existing file works, too. This is also a regression test which shows that appending to an existing file works as expected. See vibe-d/eventcore#115. --- source/vibe/core/file.d | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/source/vibe/core/file.d b/source/vibe/core/file.d index fcec414..09f8754 100644 --- a/source/vibe/core/file.d +++ b/source/vibe/core/file.d @@ -438,6 +438,9 @@ struct FileStream { m_ctx.mode = mode; m_ctx.size = eventDriver.files.getSize(fd); m_ctx.driver = () @trusted { return cast(shared)eventDriver; } (); + + if (mode == FileMode.append) + m_ctx.ptr = m_ctx.size; } this(this) @@ -787,3 +790,23 @@ version (Windows) {} else unittest { testCreate(".test_foo/", ".test_foo", true); test("/", "", false); } + +unittest { + auto name = "toAppend.txt"; + scope(exit) removeFile(name); + + { + auto handle = openFile(name, FileMode.createTrunc); + handle.write("create,"); + assert(handle.tell() == "create,".length); + handle.close(); + } + { + auto handle = openFile(name, FileMode.append); + handle.write(" then append"); + assert(handle.tell() == "create, then append".length); + handle.close(); + } + + assert(readFile(name) == "create, then append"); +}