vfs: write 0 bytes when flushing unwritten handles to avoid race conditions in FUSE - fixes #1181

This commit is contained in:
Stefan Breunig 2018-01-20 11:10:55 +01:00 committed by Nick Craig-Wood
parent b33e3f779c
commit 846bbef1e9
2 changed files with 13 additions and 5 deletions

View File

@ -231,9 +231,9 @@ func (fh *WriteFileHandle) Flush() error {
// If Write hasn't been called then ignore the Flush - Release
// will pick it up
if !fh.writeCalled {
fs.Debugf(fh.remote, "WriteFileHandle.Flush ignoring flush on unwritten handle")
return nil
fs.Debugf(fh.remote, "WriteFileHandle.Flush unwritten handle, writing 0 bytes to avoid race conditions")
_, err := fh.writeAt([]byte{}, fh.offset)
return err
}
err := fh.close()
if err != nil {

View File

@ -168,12 +168,15 @@ func TestWriteFileHandleWriteAt(t *testing.T) {
func TestWriteFileHandleFlush(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
_, fh := writeHandleCreate(t, r)
vfs, fh := writeHandleCreate(t, r)
// Check Flush does nothing if write not called
// Check Flush already creates file for unwritten handles, without closing it
err := fh.Flush()
assert.NoError(t, err)
assert.False(t, fh.closed)
root, err := vfs.Root()
assert.NoError(t, err)
checkListing(t, root, []string{"file1,0,false"})
// Write some data
n, err := fh.Write([]byte("hello"))
@ -189,6 +192,11 @@ func TestWriteFileHandleFlush(t *testing.T) {
err = fh.Flush()
assert.NoError(t, err)
assert.True(t, fh.closed)
// Check file was written properly
root, err = vfs.Root()
assert.NoError(t, err)
checkListing(t, root, []string{"file1,5,false"})
}
func TestWriteFileHandleRelease(t *testing.T) {