From 63a02cc06fe3530c6a0d02f8d80b6e86defd62ff Mon Sep 17 00:00:00 2001 From: Terry Hardie Date: Wed, 6 Dec 2023 15:04:01 -0800 Subject: [PATCH 1/2] Adding support for WriteAt --- fs.go | 1 + memfs/memory.go | 8 ++++++-- test/mock.go | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/fs.go b/fs.go index a9efccd..cf9e57b 100644 --- a/fs.go +++ b/fs.go @@ -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 diff --git a/memfs/memory.go b/memfs/memory.go index 2e17a3c..978fed1 100644 --- a/memfs/memory.go +++ b/memfs/memory.go @@ -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 } @@ -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 } diff --git a/test/mock.go b/test/mock.go index 40cbbf5..f782b26 100644 --- a/test/mock.go +++ b/test/mock.go @@ -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 } From 9fe7b83bdb1ebeac8d95490f7d85ddf629e8eccb Mon Sep 17 00:00:00 2001 From: Terry Hardie <65614981+sfc-gh-thardie@users.noreply.github.com> Date: Thu, 7 Dec 2023 13:17:27 -0800 Subject: [PATCH 2/2] Update fs.go Co-authored-by: Paulo Gomes --- fs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs.go b/fs.go index cf9e57b..d86f9d8 100644 --- a/fs.go +++ b/fs.go @@ -164,7 +164,8 @@ type File interface { // Name returns the name of the file as presented to Open. Name() string io.Writer - io.WriterAt + // TODO: Add io.WriterAt for v6 + // io.WriterAt io.Reader io.ReaderAt io.Seeker