Merge branch 'master' of github.com:spf13/afero

This commit is contained in:
spf13 2015-05-25 21:11:05 -04:00
commit 0efc34d380
1 changed files with 10 additions and 0 deletions

View File

@ -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) {