diff --git a/samples/memfs/fs.go b/samples/memfs/fs.go index b0fe38c..312963e 100644 --- a/samples/memfs/fs.go +++ b/samples/memfs/fs.go @@ -459,6 +459,28 @@ func (fs *memFS) OpenFile( return } +func (fs *memFS) ReadFile( + ctx context.Context, + req *fuse.ReadFileRequest) (resp *fuse.ReadFileResponse, err error) { + resp = &fuse.ReadFileResponse{} + + fs.mu.RLock() + defer fs.mu.RUnlock() + + // Find the inode in question. + inode := fs.getInodeForReadingOrDie(req.Inode) + defer inode.mu.RUnlock() + + // Serve the request. + // + // TODO(jacobsa): Add tests involving EOF matching posix_test.go. + resp.Data = make([]byte, req.Size) + n, err := inode.ReadAt(resp.Data, req.Offset) + resp.Data = resp.Data[:n] + + return +} + func (fs *memFS) WriteFile( ctx context.Context, req *fuse.WriteFileRequest) (resp *fuse.WriteFileResponse, err error) { @@ -472,6 +494,8 @@ func (fs *memFS) WriteFile( defer inode.mu.Unlock() // Serve the request. + // + // TODO(jacobsa): Add tests involving EOF matching posix_test.go. _, err = inode.WriteAt(req.Data, req.Offset) return diff --git a/samples/memfs/inode.go b/samples/memfs/inode.go index 647614c..3ef46b6 100644 --- a/samples/memfs/inode.go +++ b/samples/memfs/inode.go @@ -16,6 +16,7 @@ package memfs import ( "fmt" + "io" "os" "github.com/jacobsa/fuse" @@ -290,6 +291,30 @@ func (inode *inode) ReadDir(offset int, size int) (data []byte, err error) { return } +// Read from the file's contents. See documentation for ioutil.ReaderAt. +// +// REQUIRES: !inode.dir +// SHARED_LOCKS_REQUIRED(inode.mu) +func (inode *inode) ReadAt(p []byte, off int64) (n int, err error) { + if inode.dir { + panic("ReadAt called on directory.") + } + + // Ensure the offset is in range. + if off > int64(len(inode.contents)) { + err = io.EOF + return + } + + // Read what we can. + n = copy(p, inode.contents[off:]) + if n < len(p) { + err = io.EOF + } + + return +} + // Write to the file's contents. See documentation for ioutil.WriterAt. // // REQUIRES: !inode.dir