lib9p

Go 9P library.
Log | Files | Refs | LICENSE

commit 46d6acafd1f08e02443ae59769f66b790ed2c030
parent c949290b6a88b4867ea9be8881a20efa951f58b6
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat, 13 Jan 2024 15:39:01 +0900

fix offset

Diffstat:
Mfs_test.go | 22++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/fs_test.go b/fs_test.go @@ -3,6 +3,7 @@ package lib9p import ( "bytes" "fmt" + "io" "io/fs" "path" "strings" @@ -42,6 +43,7 @@ type testFile struct { // when this testFile is read. // It contains content. r *bytes.Reader + offset int64 // stat is the Stat of this testFile. stat Stat } @@ -52,6 +54,7 @@ func (f *testFile) Stat() (*FileInfo, error) { func (f *testFile) Close() error { f.r = nil + f.offset = 0 return nil } @@ -60,7 +63,12 @@ func (f *testFile) Read(b []byte) (int, error) { f.fsys.waitc <- struct{}{} <-f.fsys.waitc } - return f.r.Read(b) + if f.r == nil { + f.r = bytes.NewReader(f.content) + } + n, err := f.r.ReadAt(b, f.offset) + f.offset += int64(n) + return n, err } func (f *testFile) ReadAt(b []byte, off int64) (n int, err error) { @@ -68,7 +76,9 @@ func (f *testFile) ReadAt(b []byte, off int64) (n int, err error) { f.fsys.waitc <- struct{}{} <-f.fsys.waitc } - return f.r.ReadAt(b, off) + n, err = f.r.ReadAt(b, off) + f.offset = off + int64(n) + return n, err } func (f *testFile) ReadDir(n int) ([]fs.DirEntry, error) { @@ -84,9 +94,6 @@ func (f *testFile) WriteAt(p []byte, off int64) (int, error) { if f.fsys.slow { f.fsys.waitc <- struct{}{} } - if f.r == nil { - return 0, fmt.Errorf("not open") - } if off < 0 || off > int64(len(f.content)) { return 0, fmt.Errorf("bad offset") } @@ -96,7 +103,10 @@ func (f *testFile) WriteAt(p []byte, off int64) (int, error) { f.content = newcon } copy(f.content[off:], p) - f.r.Reset(f.content) + if f.r != nil { + f.r.Reset(f.content) + f.r.Seek(f.offset, io.SeekStart) + } if f.fsys.slow { <-f.fsys.waitc }