Skip to content

Commit f0a9264

Browse files
committed
feat: Add EOF and short write support to memory backend
1 parent d85fb66 commit f0a9264

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pkg/backend/memory.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package backend
22

33
import (
4+
"io"
45
"sync"
56
)
67

@@ -16,6 +17,10 @@ func NewMemoryBackend(memory []byte) *MemoryBackend {
1617
func (b *MemoryBackend) ReadAt(p []byte, off int64) (n int, err error) {
1718
b.lock.Lock()
1819

20+
if off >= int64(len(b.memory)) {
21+
return 0, io.EOF
22+
}
23+
1924
n = copy(p, b.memory[off:off+int64(len(p))])
2025

2126
b.lock.Unlock()
@@ -26,8 +31,16 @@ func (b *MemoryBackend) ReadAt(p []byte, off int64) (n int, err error) {
2631
func (b *MemoryBackend) WriteAt(p []byte, off int64) (n int, err error) {
2732
b.lock.Lock()
2833

34+
if off >= int64(len(b.memory)) {
35+
return 0, io.EOF
36+
}
37+
2938
n = copy(b.memory[off:off+int64(len(p))], p)
3039

40+
if n < len(p) {
41+
return n, io.ErrShortWrite
42+
}
43+
3144
b.lock.Unlock()
3245

3346
return

0 commit comments

Comments
 (0)