From 0698d680322eacaf148dcd01c7a350a49c63bf22 Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Fri, 20 Mar 2015 11:49:45 +1100 Subject: [PATCH] flushFS.WriteFile --- samples/flushfs/flush_fs.go | 32 ++++++++++++++++++++++++++++++-- samples/flushfs/flush_fs_test.go | 9 ++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/samples/flushfs/flush_fs.go b/samples/flushfs/flush_fs.go index de77a20..223012c 100644 --- a/samples/flushfs/flush_fs.go +++ b/samples/flushfs/flush_fs.go @@ -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 +} diff --git a/samples/flushfs/flush_fs_test.go b/samples/flushfs/flush_fs_test.go index 9913d0e..0984d21 100644 --- a/samples/flushfs/flush_fs_test.go +++ b/samples/flushfs/flush_fs_test.go @@ -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()) }