Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for WriteAt #39

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
sfc-gh-thardie marked this conversation as resolved.
Show resolved Hide resolved
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