mirror of https://github.com/spf13/afero.git
Merge branch 'master' of github.com:spf13/afero
This commit is contained in:
commit
0efc34d380
10
memfile.go
10
memfile.go
|
@ -18,6 +18,7 @@ import (
|
|||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
|
@ -32,6 +33,7 @@ type MemDir interface {
|
|||
}
|
||||
|
||||
type InMemoryFile struct {
|
||||
sync.Mutex
|
||||
at int64
|
||||
name string
|
||||
data []byte
|
||||
|
@ -48,13 +50,17 @@ func MemFileCreate(name string) *InMemoryFile {
|
|||
|
||||
func (f *InMemoryFile) Open() error {
|
||||
atomic.StoreInt64(&f.at, 0)
|
||||
f.Lock()
|
||||
f.closed = false
|
||||
f.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *InMemoryFile) Close() error {
|
||||
atomic.StoreInt64(&f.at, 0)
|
||||
f.Lock()
|
||||
f.closed = true
|
||||
f.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -102,6 +108,8 @@ func (f *InMemoryFile) Readdirnames(n int) (names []string, err error) {
|
|||
}
|
||||
|
||||
func (f *InMemoryFile) Read(b []byte) (n int, err error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
if f.closed == true {
|
||||
return 0, ErrFileClosed
|
||||
}
|
||||
|
@ -157,6 +165,8 @@ func (f *InMemoryFile) Seek(offset int64, whence int) (int64, error) {
|
|||
func (f *InMemoryFile) Write(b []byte) (n int, err error) {
|
||||
n = len(b)
|
||||
cur := atomic.LoadInt64(&f.at)
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
diff := cur - int64(len(f.data))
|
||||
var tail []byte
|
||||
if n+int(cur) < len(f.data) {
|
||||
|
|
Loading…
Reference in New Issue