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.
This commit is contained in:
parent
ce9faec1c1
commit
513bebcb95
|
@ -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");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue