From 163b30373110044032707f415f83f2ad51d849cb Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Thu, 5 Mar 2015 19:33:10 +1100 Subject: [PATCH] Implemented memFS.WriteFile. --- samples/memfs/fs.go | 18 ++++++++++++++++++ samples/memfs/inode.go | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/samples/memfs/fs.go b/samples/memfs/fs.go index 07e3885..36b3630 100644 --- a/samples/memfs/fs.go +++ b/samples/memfs/fs.go @@ -437,3 +437,21 @@ func (fs *memFS) ReadDir( return } + +func (fs *memFS) WriteFile( + ctx context.Context, + req *fuse.WriteFileRequest) (resp *fuse.WriteFileResponse, err error) { + resp = &fuse.WriteFileResponse{} + + fs.mu.RLock() + defer fs.mu.RUnlock() + + // Find the inode in question. + inode := fs.getInodeForModifyingOrDie(req.Inode) + defer inode.mu.Unlock() + + // Serve the request. + _, err = inode.WriteAt(req.Data, req.Offset) + + return +} diff --git a/samples/memfs/inode.go b/samples/memfs/inode.go index 163a22f..bfb2244 100644 --- a/samples/memfs/inode.go +++ b/samples/memfs/inode.go @@ -279,3 +279,30 @@ func (inode *inode) ReadDir(offset int, size int) (data []byte, err error) { return } + +// Write to the file's contents. See documentation for ioutil.WriterAt. +// +// REQUIRES: !inode.dir +// EXCLUSIVE_LOCKS_REQUIRED(inode.mu) +func (inode *inode) WriteAt(p []byte, off int64) (n int, err error) { + if inode.dir { + panic("WriteAt called on directory.") + } + + // Ensure that the contents slice is long enough. + newLen := int(off) + len(p) + if len(inode.contents) < newLen { + padding := make([]byte, newLen-len(inode.contents)) + inode.contents = append(inode.contents, padding...) + } + + // Copy in the data. + n = copy(inode.contents[off:], p) + + // Sanity check. + if n != len(p) { + panic(fmt.Sprintf("Unexpected short copy: %v", n)) + } + + return +}