afero/memfile.go

229 lines
4.9 KiB
Go
Raw Normal View History

2014-10-28 17:29:28 +03:00
// Copyright © 2014 Steve Francia <spf@spf13.com>.
// Copyright 2013 tsuru authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package afero
import (
"bytes"
"io"
"os"
2015-12-09 23:37:04 +03:00
"path/filepath"
2015-03-22 03:24:08 +03:00
"sync"
2014-10-28 17:29:28 +03:00
"sync/atomic"
)
import "time"
type MemDir interface {
Len() int
Names() []string
Files() []File
Add(File)
Remove(File)
}
type InMemoryFile struct {
// atomic requires 64-bit alignment for struct field access
at int64
readDirCount int64
sync.Mutex
name string
data []byte
memDir MemDir
dir bool
closed bool
mode os.FileMode
modtime time.Time
2014-10-28 17:29:28 +03:00
}
func MemFileCreate(name string) *InMemoryFile {
2014-11-01 06:36:45 +03:00
return &InMemoryFile{name: name, mode: os.ModeTemporary, modtime: time.Now()}
}
2014-11-01 06:38:54 +03:00
func (f *InMemoryFile) Open() error {
atomic.StoreInt64(&f.at, 0)
atomic.StoreInt64(&f.readDirCount, 0)
2015-03-22 03:24:08 +03:00
f.Lock()
2014-11-01 06:38:54 +03:00
f.closed = false
2015-03-22 03:24:08 +03:00
f.Unlock()
2014-11-01 06:38:54 +03:00
return nil
2014-10-28 17:29:28 +03:00
}
func (f *InMemoryFile) Close() error {
2015-03-22 03:24:08 +03:00
f.Lock()
2014-10-28 17:29:28 +03:00
f.closed = true
2015-03-22 03:24:08 +03:00
f.Unlock()
2014-10-28 17:29:28 +03:00
return nil
}
func (f *InMemoryFile) Name() string {
return f.name
}
func (f *InMemoryFile) Stat() (os.FileInfo, error) {
return &InMemoryFileInfo{f}, nil
}
func (f *InMemoryFile) Sync() error {
return nil
}
2014-10-28 17:29:28 +03:00
func (f *InMemoryFile) Readdir(count int) (res []os.FileInfo, err error) {
var outLength int64
2014-10-28 17:29:28 +03:00
f.Lock()
files := f.memDir.Files()[f.readDirCount:]
2014-10-28 17:29:28 +03:00
if count > 0 {
if len(files) < count {
outLength = int64(len(files))
} else {
outLength = int64(count)
}
if len(files) == 0 {
err = io.EOF
}
} else {
outLength = int64(len(files))
2014-10-28 17:29:28 +03:00
}
f.readDirCount += outLength
f.Unlock()
2014-10-28 17:29:28 +03:00
res = make([]os.FileInfo, outLength)
for i := range res {
res[i], _ = files[i].Stat()
2014-10-28 17:29:28 +03:00
}
return res, err
2014-10-28 17:29:28 +03:00
}
func (f *InMemoryFile) Readdirnames(n int) (names []string, err error) {
fi, err := f.Readdir(n)
names = make([]string, len(fi))
for i, f := range fi {
2015-12-09 23:37:04 +03:00
_, names[i] = filepath.Split(f.Name())
2014-10-28 17:29:28 +03:00
}
return names, err
}
func (f *InMemoryFile) Read(b []byte) (n int, err error) {
2015-03-22 03:24:08 +03:00
f.Lock()
defer f.Unlock()
2014-10-28 17:29:28 +03:00
if f.closed == true {
return 0, ErrFileClosed
}
2015-03-22 03:24:08 +03:00
if len(b) > 0 && int(f.at) == len(f.data) {
return 0, io.EOF
}
2014-10-28 17:29:28 +03:00
if len(f.data)-int(f.at) >= len(b) {
n = len(b)
} else {
n = len(f.data) - int(f.at)
}
copy(b, f.data[f.at:f.at+int64(n)])
atomic.AddInt64(&f.at, int64(n))
return
}
func (f *InMemoryFile) ReadAt(b []byte, off int64) (n int, err error) {
atomic.StoreInt64(&f.at, off)
return f.Read(b)
}
func (f *InMemoryFile) Truncate(size int64) error {
if f.closed == true {
return ErrFileClosed
}
if size < 0 {
return ErrOutOfRange
}
if size > int64(len(f.data)) {
diff := size - int64(len(f.data))
f.data = append(f.data, bytes.Repeat([]byte{00}, int(diff))...)
} else {
f.data = f.data[0:size]
}
return nil
}
func (f *InMemoryFile) Seek(offset int64, whence int) (int64, error) {
if f.closed == true {
return 0, ErrFileClosed
}
switch whence {
case 0:
atomic.StoreInt64(&f.at, offset)
case 1:
atomic.AddInt64(&f.at, int64(offset))
case 2:
atomic.StoreInt64(&f.at, int64(len(f.data))+offset)
}
return f.at, nil
}
func (f *InMemoryFile) Write(b []byte) (n int, err error) {
n = len(b)
cur := atomic.LoadInt64(&f.at)
2015-03-22 03:24:08 +03:00
f.Lock()
defer f.Unlock()
2014-10-28 17:29:28 +03:00
diff := cur - int64(len(f.data))
var tail []byte
if n+int(cur) < len(f.data) {
tail = f.data[n+int(cur):]
}
if diff > 0 {
f.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
f.data = append(f.data, tail...)
} else {
f.data = append(f.data[:cur], b...)
f.data = append(f.data, tail...)
}
atomic.StoreInt64(&f.at, int64(len(f.data)))
return
}
func (f *InMemoryFile) WriteAt(b []byte, off int64) (n int, err error) {
atomic.StoreInt64(&f.at, off)
return f.Write(b)
}
func (f *InMemoryFile) WriteString(s string) (ret int, err error) {
return f.Write([]byte(s))
}
func (f *InMemoryFile) Info() *InMemoryFileInfo {
return &InMemoryFileInfo{file: f}
}
type InMemoryFileInfo struct {
file *InMemoryFile
}
// Implements os.FileInfo
func (s *InMemoryFileInfo) Name() string {
2015-12-09 23:37:04 +03:00
_, name := filepath.Split(s.file.Name())
return name
}
2014-11-01 06:36:45 +03:00
func (s *InMemoryFileInfo) Mode() os.FileMode { return s.file.mode }
func (s *InMemoryFileInfo) ModTime() time.Time { return s.file.modtime }
2014-10-28 17:29:28 +03:00
func (s *InMemoryFileInfo) IsDir() bool { return s.file.dir }
func (s *InMemoryFileInfo) Sys() interface{} { return nil }
2014-11-01 06:39:14 +03:00
func (s *InMemoryFileInfo) Size() int64 {
if s.IsDir() {
return int64(42)
}
return int64(len(s.file.data))
}