Implemented memFS.WriteFile.

geesefs-0-30-9
Aaron Jacobs 2015-03-05 19:33:10 +11:00
parent fcf3be2896
commit 163b303731
2 changed files with 45 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}