From 9e9dd69b096b029e604465c94c30ea62df5e034b Mon Sep 17 00:00:00 2001 From: Yerden Zhumabekov Date: Wed, 2 Sep 2020 00:30:41 +0600 Subject: [PATCH 1/3] mem: add test for Write/Seek Signed-off-by: Yerden Zhumabekov --- mem/file_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/mem/file_test.go b/mem/file_test.go index bb54db6..22af970 100644 --- a/mem/file_test.go +++ b/mem/file_test.go @@ -1,6 +1,7 @@ package mem import ( + "bytes" "io" "testing" "time" @@ -205,3 +206,42 @@ func TestFileReadAtSeekOffset(t *testing.T) { t.Fatal(err) } } + +func TestFileWriteAndSeek(t *testing.T) { + fd := CreateFile("foo") + f := NewFileHandle(fd) + + assert := func(expected bool, v ...interface{}) { + if !expected { + t.Helper() + t.Fatal(v...) + } + } + + data4 := []byte{0, 1, 2, 3} + data20 := bytes.Repeat(data4, 5) + var off int64 + + for i := 0; i < 100; i++ { + // write 20 bytes + n, err := f.Write(data20) + assert(err == nil, err) + off += int64(n) + assert(n == len(data20), n) + assert(off == int64((i+1)*len(data20)), off) + + // rewind to start and write 4 bytes there + cur, err := f.Seek(-off, io.SeekCurrent) + assert(err == nil, err) + assert(cur == 0, cur) + + n, err = f.Write(data4) + assert(err == nil, err) + assert(n == len(data4), n) + + // back at the end + cur, err = f.Seek(off-int64(n), io.SeekCurrent) + assert(err == nil, err) + assert(cur == off, cur, off) + } +} From d2521ec52d87373b36184c09ec18d8ef76a0ed1e Mon Sep 17 00:00:00 2001 From: Yerden Zhumabekov Date: Wed, 2 Sep 2020 00:30:04 +0600 Subject: [PATCH 2/3] mem: fix code style in Seek Signed-off-by: Yerden Zhumabekov --- mem/file.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mem/file.go b/mem/file.go index 699f1fb..ae667e8 100644 --- a/mem/file.go +++ b/mem/file.go @@ -225,11 +225,11 @@ func (f *File) Seek(offset int64, whence int) (int64, error) { return 0, ErrFileClosed } switch whence { - case 0: + case io.SeekStart: atomic.StoreInt64(&f.at, offset) - case 1: - atomic.AddInt64(&f.at, int64(offset)) - case 2: + case io.SeekCurrent: + atomic.AddInt64(&f.at, offset) + case io.SeekEnd: atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset) } return f.at, nil From 84f9c83b7ebc472585fda69371c8c6050c74ec8c Mon Sep 17 00:00:00 2001 From: Yerden Zhumabekov Date: Wed, 2 Sep 2020 10:25:49 +0600 Subject: [PATCH 3/3] mem: fix Write setting wrong offset Signed-off-by: Yerden Zhumabekov --- mem/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mem/file.go b/mem/file.go index ae667e8..07b2e12 100644 --- a/mem/file.go +++ b/mem/file.go @@ -260,7 +260,7 @@ func (f *File) Write(b []byte) (n int, err error) { } setModTime(f.fileData, time.Now()) - atomic.StoreInt64(&f.at, int64(len(f.fileData.data))) + atomic.AddInt64(&f.at, int64(n)) return }