From eead2d89b23532ce34ca51916ac5a2505d45006b Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sun, 16 Oct 2022 15:37:48 +0800 Subject: [PATCH] fix --- file/FileWR.go | 12 +++++++++--- file/FileWR_test.go | 16 +++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/file/FileWR.go b/file/FileWR.go index add3770..139922b 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -24,6 +24,7 @@ type File struct { file *os.File wr io.Writer rr io.Reader + cu int64 sync.RWMutex } @@ -168,7 +169,7 @@ func (t *File) Seed(index int64) (e error) { if index < 0 { whenc = 2 } - t.Config.CurIndex, e = t.file.Seek(index, whenc) + t.cu, e = t.file.Seek(index, whenc) return nil } @@ -200,6 +201,8 @@ func (t *File) Close() error { if t.file != nil { if e := t.file.Close(); e != nil { return e + } else { + t.file = nil } } return nil @@ -234,7 +237,8 @@ func (t *File) getRWCloser() { panic(e) } else { if t.Config.CurIndex > 0 { - t.Config.CurIndex, e = f.Seek(t.Config.CurIndex, 0) + t.cu = t.Config.CurIndex + t.cu, e = f.Seek(t.cu, 0) if e != nil { panic(e) } @@ -246,11 +250,13 @@ func (t *File) getRWCloser() { panic(e) } else { if t.Config.CurIndex != 0 { + t.cu = t.Config.CurIndex whenc := 0 if t.Config.CurIndex < 0 { + t.cu = t.cu + 1 whenc = 2 } - t.Config.CurIndex, e = f.Seek(t.Config.CurIndex, whenc) + t.cu, e = f.Seek(t.cu, whenc) if e != nil { panic(e) } diff --git a/file/FileWR_test.go b/file/FileWR_test.go index 91e1851..430efd9 100644 --- a/file/FileWR_test.go +++ b/file/FileWR_test.go @@ -11,27 +11,25 @@ import ( ) func TestWriteReadDelSync(t *testing.T) { - f := New("test/rwd.txt", -1, true) - if i, e := f.Write([]byte("sss"), true); i == 0 || e != nil { + f := New("test/rwd.txt", -6, true) + if i, e := f.Write([]byte("sssa\n"), true); i == 0 || e != nil { t.Fatal(e) } - var buf = make([]byte, 3) + var buf = make([]byte, 5) if i, e := f.Read(buf); i == 0 || e != nil { t.Fatal(i, e) } else { - if !bytes.Equal(buf[:i], []byte("sss")) { - t.Fatal(string(buf[:i]), e) + if !bytes.Equal(buf[:i], []byte("sssa\n")) { + t.Fatal(i, string(buf), e) } } if i, e := f.Read(buf); i == 0 || e != nil { t.Fatal(i, e) } else { - for _, v := range buf { - if v != 's' { - t.Fatal(v) - } + if !bytes.Equal(buf[:i], []byte("sssa\n")) { + t.Fatal(i, string(buf), e) } } -- 2.39.2