flushFS.WriteFile

geesefs-0-30-9
Aaron Jacobs 2015-03-20 11:49:45 +11:00
parent b87740f7f2
commit 0698d68032
2 changed files with 36 additions and 5 deletions

View File

@ -15,6 +15,7 @@
package flushfs
import (
"fmt"
"os"
"sync"
@ -40,8 +41,8 @@ const fooID = fuse.RootInodeID + 1
type flushFS struct {
fuseutil.NotImplementedFileSystem
mu sync.Mutex
foo *os.File // GUARDED_BY(mu)
mu sync.Mutex
fooContents []byte // GUARDED_BY(mu)
}
////////////////////////////////////////////////////////////////////////
@ -140,3 +141,30 @@ func (fs *flushFS) OpenFile(
return
}
func (fs *flushFS) WriteFile(
ctx context.Context,
req *fuse.WriteFileRequest) (
resp *fuse.WriteFileResponse, err error) {
resp = &fuse.WriteFileResponse{}
fs.mu.Lock()
defer fs.mu.Unlock()
// Ensure that the contents slice is long enough.
newLen := int(req.Offset) + len(req.Data)
if len(fs.fooContents) < newLen {
padding := make([]byte, newLen-len(fs.fooContents))
fs.fooContents = append(fs.fooContents, padding...)
}
// Copy in the data.
n := copy(fs.fooContents[req.Offset:], req.Data)
// Sanity check.
if n != len(req.Data) {
panic(fmt.Sprintf("Unexpected short copy: %v", n))
}
return
}

View File

@ -75,6 +75,9 @@ func (t *FlushFSTest) SetUp(ti *TestInfo) {
// Helpers
////////////////////////////////////////////////////////////////////////
// Match byte slices equal to the supplied string.
func byteSliceEq(expected string) Matcher
// Return a copy of the current contents of t.flushes.
//
// LOCKS_EXCLUDED(t.mu)
@ -127,11 +130,11 @@ func (t *FlushFSTest) CloseReports_ReadWrite() {
// Seek and read them back.
off, err = f.Seek(0, 0)
AssertEq(nil, err)
AssertEq(4, off)
AssertEq(0, off)
n, err = f.Read(buf)
AssertThat(err, AnyOf(nil, io.EOF))
AssertEq("taco", buf[:n])
AssertEq("taco", string(buf[:n]))
// At this point, no flushes or fsyncs should have happened.
AssertThat(t.getFlushes(), ElementsAre())
@ -143,7 +146,7 @@ func (t *FlushFSTest) CloseReports_ReadWrite() {
AssertEq(nil, err)
// Now we should have received the flush operation (but still no fsync).
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFlushes(), ElementsAre(byteSliceEq("taco")))
ExpectThat(t.getFsyncs(), ElementsAre())
}