Skip to content

Commit

Permalink
Adding support for WriteAt
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-thardie committed Dec 6, 2023
1 parent d23351b commit 63a02cc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type File interface {
// Name returns the name of the file as presented to Open.
Name() string
io.Writer
io.WriterAt
io.Reader
io.ReaderAt
io.Seeker
Expand Down
8 changes: 6 additions & 2 deletions memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ func (f *file) Seek(offset int64, whence int) (int64, error) {
}

func (f *file) Write(p []byte) (int, error) {
return f.WriteAt(p, f.position)
}

func (f *file) WriteAt(p []byte, off int64) (int, error) {
if f.isClosed {
return 0, os.ErrClosed
}
Expand All @@ -280,8 +284,8 @@ func (f *file) Write(p []byte) (int, error) {
return 0, errors.New("write not supported")
}

n, err := f.content.WriteAt(p, f.position)
f.position += int64(n)
n, err := f.content.WriteAt(p, off)
f.position = off + int64(n)

return n, err
}
Expand Down
4 changes: 4 additions & 0 deletions test/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (*FileMock) ReadAt(b []byte, off int64) (int, error) {
return 0, nil
}

func (*FileMock) WriteAt(b []byte, off int64) (int, error) {
return 0, nil
}

func (*FileMock) Seek(offset int64, whence int) (int64, error) {
return 0, nil
}
Expand Down

0 comments on commit 63a02cc

Please sign in to comment.