Skip to content

Commit

Permalink
Fix go-git data races whilst running tests
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <pjbgf@linux.com>
  • Loading branch information
pjbgf committed Dec 5, 2022
1 parent 863defb commit 05c28a8
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions memfs/storage.go
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"sync"
)

type storage struct {
Expand Down Expand Up @@ -174,6 +175,8 @@ func clean(path string) string {
type content struct {
name string
bytes []byte

m sync.RWMutex
}

func (c *content) WriteAt(p []byte, off int64) (int, error) {
Expand All @@ -185,6 +188,7 @@ func (c *content) WriteAt(p []byte, off int64) (int, error) {
}
}

c.m.Lock()
prev := len(c.bytes)

diff := int(off) - prev
Expand All @@ -196,6 +200,7 @@ func (c *content) WriteAt(p []byte, off int64) (int, error) {
if len(c.bytes) < prev {
c.bytes = c.bytes[:prev]
}
c.m.Unlock()

return len(p), nil
}
Expand All @@ -209,8 +214,10 @@ func (c *content) ReadAt(b []byte, off int64) (n int, err error) {
}
}

c.m.RLock()
size := int64(len(c.bytes))
if off >= size {
c.m.RUnlock()
return 0, io.EOF
}

Expand All @@ -220,6 +227,7 @@ func (c *content) ReadAt(b []byte, off int64) (n int, err error) {
}

btr := c.bytes[off : off+l]
c.m.RUnlock()
if len(btr) < len(b) {
err = io.EOF
}
Expand Down

0 comments on commit 05c28a8

Please sign in to comment.