Merge pull request #833 from stevvooe/next-generation

Address bug in inmemory filesystem WriteAt method
This commit is contained in:
Olivier Gambier 2014-12-09 00:56:32 -08:00
commit a8ecb17d7e

View file

@ -294,15 +294,16 @@ func (f *file) ReadAt(p []byte, offset int64) (n int, err error) {
} }
func (f *file) WriteAt(p []byte, offset int64) (n int, err error) { func (f *file) WriteAt(p []byte, offset int64) (n int, err error) {
if len(f.data) > 0 && offset >= int64(len(f.data)) { off := int(offset)
// Extend missing region with a zero pad, while also preallocating out to size of p. if cap(f.data) < off+len(p) {
pad := offset - int64(len(f.data)) data := make([]byte, len(f.data), off+len(p))
size := len(p) + int(pad) copy(data, f.data)
f.data = append(f.data, make([]byte, pad, size)...) f.data = data
} }
f.data = append(f.data, p...) f.data = f.data[:off+len(p)]
return len(p), nil
return copy(f.data[off:off+len(p)], p), nil
} }
func (f *file) String() string { func (f *file) String() string {