mem.File is now mem.FileData and mem.File is a file handle with a pointer to a mem.FileData

This commit is contained in:
Martin Bertschler 2016-01-03 23:59:42 +01:00
parent 7b991cb257
commit 7096d68458
5 changed files with 144 additions and 131 deletions

View File

@ -16,22 +16,22 @@ package mem
type Dir interface { type Dir interface {
Len() int Len() int
Names() []string Names() []string
Files() []File Files() []*File
Add(File) Add(*File)
Remove(File) Remove(*File)
} }
func RemoveFromMemDir(dir *File, f *File) { func RemoveFromMemDir(dir *File, f *File) {
dir.memDir.Remove(*f) dir.fileData.memDir.Remove(f)
} }
func AddToMemDir(dir *File, f *File) { func AddToMemDir(dir *File, f *File) {
dir.memDir.Add(*f) dir.fileData.memDir.Add(f)
} }
func InitializeDir(d *File) { func InitializeDir(d *File) {
if d.memDir == nil { if d.fileData.memDir == nil {
d.dir = true d.fileData.dir = true
d.memDir = &DirMap{} d.fileData.memDir = &DirMap{}
} }
} }

View File

@ -15,25 +15,25 @@ package mem
import "sort" import "sort"
type DirMap map[string]File type DirMap map[string]*FileData
func (m DirMap) Len() int { return len(m) } func (m DirMap) Len() int { return len(m) }
func (m DirMap) Add(f File) { m[f.Name()] = f } func (m DirMap) Add(f *File) { m[f.fileData.name] = f.fileData }
func (m DirMap) Remove(f File) { delete(m, f.Name()) } func (m DirMap) Remove(f *File) { delete(m, f.fileData.name) }
func (m DirMap) Files() (files []File) { func (m DirMap) Files() (files []*File) {
for _, f := range m { for _, f := range m {
files = append(files, f) files = append(files, NewFileHandle(f))
} }
sort.Sort(filesSorter(files)) sort.Sort(filesSorter(files))
return files return files
} }
type filesSorter []File type filesSorter []*File
// implement sort.Interface for []File // implement sort.Interface for []File
func (s filesSorter) Len() int { return len(s) } func (s filesSorter) Len() int { return len(s) }
func (s filesSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func (s filesSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s filesSorter) Less(i, j int) bool { return s[i].Name() < s[j].Name() } func (s filesSorter) Less(i, j int) bool { return s[i].fileData.name < s[j].fileData.name }
func (m DirMap) Names() (names []string) { func (m DirMap) Names() (names []string) {
for x := range m { for x := range m {

View File

@ -32,63 +32,74 @@ type File struct {
// atomic requires 64-bit alignment for struct field access // atomic requires 64-bit alignment for struct field access
at int64 at int64
readDirCount int64 readDirCount int64
closed bool
fileData *FileData
}
func NewFileHandle(data *FileData) *File {
return &File{fileData: data}
}
func (f File) Data() *FileData {
return f.fileData
}
type FileData struct {
sync.Mutex sync.Mutex
name string name string
data []byte data []byte
memDir Dir memDir Dir
dir bool dir bool
closed bool
mode os.FileMode mode os.FileMode
modtime time.Time modtime time.Time
} }
func CreateFile(name string) *File { func CreateFile(name string) *FileData {
return &File{name: name, mode: os.ModeTemporary, modtime: time.Now()} return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
} }
func CreateDir(name string) *File { func CreateDir(name string) *FileData {
return &File{name: name, memDir: &DirMap{}, dir: true} return &FileData{name: name, memDir: &DirMap{}, dir: true}
} }
func ChangeFileName(f *File, newname string) { func ChangeFileName(f *File, newname string) {
f.name = newname f.fileData.name = newname
} }
func SetMode(f *File, mode os.FileMode) { func SetMode(f *File, mode os.FileMode) {
f.mode = mode f.fileData.mode = mode
} }
func SetModTime(f *File, mtime time.Time) { func SetModTime(f *File, mtime time.Time) {
f.modtime = mtime f.fileData.modtime = mtime
} }
func GetFileInfo(f *File) *FileInfo { func GetFileInfo(f *File) *FileInfo {
return &FileInfo{file: f} return &FileInfo{f.fileData}
} }
func (f *File) Open() error { func (f *File) Open() error {
atomic.StoreInt64(&f.at, 0) atomic.StoreInt64(&f.at, 0)
atomic.StoreInt64(&f.readDirCount, 0) atomic.StoreInt64(&f.readDirCount, 0)
f.Lock() f.fileData.Lock()
f.closed = false f.closed = false
f.Unlock() f.fileData.Unlock()
return nil return nil
} }
func (f *File) Close() error { func (f *File) Close() error {
f.Lock() f.fileData.Lock()
f.closed = true f.closed = true
f.Unlock() f.fileData.Unlock()
return nil return nil
} }
func (f *File) Name() string { func (f *File) Name() string {
return f.name return f.fileData.name
} }
func (f *File) Stat() (os.FileInfo, error) { func (f *File) Stat() (os.FileInfo, error) {
return &FileInfo{f}, nil return &FileInfo{f.fileData}, nil
} }
func (f *File) Sync() error { func (f *File) Sync() error {
@ -98,8 +109,8 @@ func (f *File) Sync() error {
func (f *File) Readdir(count int) (res []os.FileInfo, err error) { func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
var outLength int64 var outLength int64
f.Lock() f.fileData.Lock()
files := f.memDir.Files()[f.readDirCount:] files := f.fileData.memDir.Files()[f.readDirCount:]
if count > 0 { if count > 0 {
if len(files) < count { if len(files) < count {
outLength = int64(len(files)) outLength = int64(len(files))
@ -113,11 +124,11 @@ func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
outLength = int64(len(files)) outLength = int64(len(files))
} }
f.readDirCount += outLength f.readDirCount += outLength
f.Unlock() f.fileData.Unlock()
res = make([]os.FileInfo, outLength) res = make([]os.FileInfo, outLength)
for i := range res { for i := range res {
res[i], _ = files[i].Stat() res[i] = &FileInfo{files[i].fileData}
} }
return res, err return res, err
@ -133,20 +144,20 @@ func (f *File) Readdirnames(n int) (names []string, err error) {
} }
func (f *File) Read(b []byte) (n int, err error) { func (f *File) Read(b []byte) (n int, err error) {
f.Lock() f.fileData.Lock()
defer f.Unlock() defer f.fileData.Unlock()
if f.closed == true { if f.closed == true {
return 0, ErrFileClosed return 0, ErrFileClosed
} }
if len(b) > 0 && int(f.at) == len(f.data) { if len(b) > 0 && int(f.at) == len(f.fileData.data) {
return 0, io.EOF return 0, io.EOF
} }
if len(f.data)-int(f.at) >= len(b) { if len(f.fileData.data)-int(f.at) >= len(b) {
n = len(b) n = len(b)
} else { } else {
n = len(f.data) - int(f.at) n = len(f.fileData.data) - int(f.at)
} }
copy(b, f.data[f.at:f.at+int64(n)]) copy(b, f.fileData.data[f.at:f.at+int64(n)])
atomic.AddInt64(&f.at, int64(n)) atomic.AddInt64(&f.at, int64(n))
return return
} }
@ -163,11 +174,11 @@ func (f *File) Truncate(size int64) error {
if size < 0 { if size < 0 {
return ErrOutOfRange return ErrOutOfRange
} }
if size > int64(len(f.data)) { if size > int64(len(f.fileData.data)) {
diff := size - int64(len(f.data)) diff := size - int64(len(f.fileData.data))
f.data = append(f.data, bytes.Repeat([]byte{00}, int(diff))...) f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...)
} else { } else {
f.data = f.data[0:size] f.fileData.data = f.fileData.data[0:size]
} }
return nil return nil
} }
@ -182,7 +193,7 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
case 1: case 1:
atomic.AddInt64(&f.at, int64(offset)) atomic.AddInt64(&f.at, int64(offset))
case 2: case 2:
atomic.StoreInt64(&f.at, int64(len(f.data))+offset) atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
} }
return f.at, nil return f.at, nil
} }
@ -190,22 +201,22 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
func (f *File) Write(b []byte) (n int, err error) { func (f *File) Write(b []byte) (n int, err error) {
n = len(b) n = len(b)
cur := atomic.LoadInt64(&f.at) cur := atomic.LoadInt64(&f.at)
f.Lock() f.fileData.Lock()
defer f.Unlock() defer f.fileData.Unlock()
diff := cur - int64(len(f.data)) diff := cur - int64(len(f.fileData.data))
var tail []byte var tail []byte
if n+int(cur) < len(f.data) { if n+int(cur) < len(f.fileData.data) {
tail = f.data[n+int(cur):] tail = f.fileData.data[n+int(cur):]
} }
if diff > 0 { if diff > 0 {
f.data = append(bytes.Repeat([]byte{00}, int(diff)), b...) f.fileData.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
f.data = append(f.data, tail...) f.fileData.data = append(f.fileData.data, tail...)
} else { } else {
f.data = append(f.data[:cur], b...) f.fileData.data = append(f.fileData.data[:cur], b...)
f.data = append(f.data, tail...) f.fileData.data = append(f.fileData.data, tail...)
} }
atomic.StoreInt64(&f.at, int64(len(f.data))) atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
return return
} }
@ -219,27 +230,27 @@ func (f *File) WriteString(s string) (ret int, err error) {
} }
func (f *File) Info() *FileInfo { func (f *File) Info() *FileInfo {
return &FileInfo{file: f} return &FileInfo{f.fileData}
} }
type FileInfo struct { type FileInfo struct {
file *File *FileData
} }
// Implements os.FileInfo // Implements os.FileInfo
func (s *FileInfo) Name() string { func (s *FileInfo) Name() string {
_, name := filepath.Split(s.file.Name()) _, name := filepath.Split(s.name)
return name return name
} }
func (s *FileInfo) Mode() os.FileMode { return s.file.mode } func (s *FileInfo) Mode() os.FileMode { return s.mode }
func (s *FileInfo) ModTime() time.Time { return s.file.modtime } func (s *FileInfo) ModTime() time.Time { return s.modtime }
func (s *FileInfo) IsDir() bool { return s.file.dir } func (s *FileInfo) IsDir() bool { return s.dir }
func (s *FileInfo) Sys() interface{} { return nil } func (s *FileInfo) Sys() interface{} { return nil }
func (s *FileInfo) Size() int64 { func (s *FileInfo) Size() int64 {
if s.IsDir() { if s.IsDir() {
return int64(42) return int64(42)
} }
return int64(len(s.file.data)) return int64(len(s.data))
} }
var ( var (

View File

@ -14,7 +14,6 @@
package afero package afero
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -28,15 +27,15 @@ import (
type MemMapFs struct { type MemMapFs struct {
mu sync.RWMutex mu sync.RWMutex
data map[string]File data map[string]*mem.FileData
init sync.Once init sync.Once
} }
var memfsInit sync.Once var memfsInit sync.Once
func (m *MemMapFs) getData() map[string]File { func (m *MemMapFs) getData() map[string]*mem.FileData {
m.init.Do(func() { m.init.Do(func() {
m.data = make(map[string]File) m.data = make(map[string]*mem.FileData)
// Root should always exist, right? // Root should always exist, right?
// TODO: what about windows? // TODO: what about windows?
m.data[FilePathSeparator] = mem.CreateDir(FilePathSeparator) m.data[FilePathSeparator] = mem.CreateDir(FilePathSeparator)
@ -51,9 +50,11 @@ func (m *MemMapFs) Create(name string) (File, error) {
m.mu.Lock() m.mu.Lock()
file := mem.CreateFile(name) file := mem.CreateFile(name)
m.getData()[name] = file m.getData()[name] = file
m.registerWithParent(file)
handle := mem.NewFileHandle(file)
m.registerWithParent(handle)
m.mu.Unlock() m.mu.Unlock()
return file, nil return handle, nil
} }
func (m *MemMapFs) unRegisterWithParent(fileName string) error { func (m *MemMapFs) unRegisterWithParent(fileName string) error {
@ -65,13 +66,11 @@ func (m *MemMapFs) unRegisterWithParent(fileName string) error {
if parent == nil { if parent == nil {
log.Fatal("parent of ", f.Name(), " is nil") log.Fatal("parent of ", f.Name(), " is nil")
} }
pmem := parent.(*mem.File) mem.RemoveFromMemDir(parent, f)
fmem := f.(*mem.File)
mem.RemoveFromMemDir(pmem, fmem)
return nil return nil
} }
func (m *MemMapFs) findParent(f File) File { func (m *MemMapFs) findParent(f *mem.File) *mem.File {
pdir, _ := filepath.Split(f.Name()) pdir, _ := filepath.Split(f.Name())
pdir = filepath.Clean(pdir) pdir = filepath.Clean(pdir)
pfile, err := m.lockfreeOpen(pdir) pfile, err := m.lockfreeOpen(pdir)
@ -81,7 +80,7 @@ func (m *MemMapFs) findParent(f File) File {
return pfile return pfile
} }
func (m *MemMapFs) registerWithParent(f File) { func (m *MemMapFs) registerWithParent(f *mem.File) {
if f == nil { if f == nil {
return return
} }
@ -99,15 +98,9 @@ func (m *MemMapFs) registerWithParent(f File) {
return return
} }
} }
pmem := parent.(*mem.File)
fmem := f.(*mem.File)
// TODO(mbertschler): memDir is only nil when it was not made with Mkdir mem.InitializeDir(parent)
// or lockfreeMkdir. In this case the parent is also not a real directory. mem.AddToMemDir(parent, f)
// This currently only happens for the file ".".
// This is a quick hack to make the library usable with relative paths.
mem.InitializeDir(pmem)
mem.AddToMemDir(pmem, fmem)
} }
func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error { func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
@ -115,7 +108,7 @@ func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
x, ok := m.getData()[name] x, ok := m.getData()[name]
if ok { if ok {
// Only return ErrFileExists if it's a file, not a directory. // Only return ErrFileExists if it's a file, not a directory.
i, err := x.Stat() i, err := mem.NewFileHandle(x).Stat()
if !i.IsDir() { if !i.IsDir() {
return ErrFileExists return ErrFileExists
} }
@ -125,7 +118,7 @@ func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
} else { } else {
item := mem.CreateDir(name) item := mem.CreateDir(name)
m.getData()[name] = item m.getData()[name] = item
m.registerWithParent(item) m.registerWithParent(mem.NewFileHandle(item))
} }
return nil return nil
} }
@ -142,7 +135,7 @@ func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error {
m.mu.Lock() m.mu.Lock()
item := mem.CreateDir(name) item := mem.CreateDir(name)
m.getData()[name] = item m.getData()[name] = item
m.registerWithParent(item) m.registerWithParent(mem.NewFileHandle(item))
m.mu.Unlock() m.mu.Unlock()
} }
return nil return nil
@ -179,27 +172,23 @@ func (m *MemMapFs) Open(name string) (File, error) {
m.mu.RLock() m.mu.RLock()
f, ok := m.getData()[name] f, ok := m.getData()[name]
ff, ok := f.(*mem.File)
if ok {
ff.Open()
}
m.mu.RUnlock() m.mu.RUnlock()
if !ok {
if ok {
return f, nil
} else {
return nil, &os.PathError{"open", name, ErrFileNotFound} return nil, &os.PathError{"open", name, ErrFileNotFound}
} }
ff := mem.NewFileHandle(f)
ff.Open()
return ff, nil
} }
func (m *MemMapFs) lockfreeOpen(name string) (File, error) { func (m *MemMapFs) lockfreeOpen(name string) (*mem.File, error) {
name = normalizePath(name) name = normalizePath(name)
f, ok := m.getData()[name] f, ok := m.getData()[name]
ff, ok := f.(*mem.File)
if ok { if ok {
ff.Open() // TODO (mbertschler)
return f, nil // not used anymore?
//ff.Open()
return mem.NewFileHandle(f), nil
} else { } else {
return nil, ErrFileNotFound return nil, ErrFileNotFound
} }
@ -284,10 +273,11 @@ func (m *MemMapFs) Rename(oldname, newname string) error {
m.mu.RUnlock() m.mu.RUnlock()
m.mu.Lock() m.mu.Lock()
m.unRegisterWithParent(oldname) m.unRegisterWithParent(oldname)
file := m.getData()[oldname].(*mem.File) fileData := m.getData()[oldname]
file := mem.NewFileHandle(fileData)
delete(m.getData(), oldname) delete(m.getData(), oldname)
mem.ChangeFileName(file, newname) mem.ChangeFileName(file, newname)
m.getData()[newname] = file m.getData()[newname] = fileData
m.registerWithParent(file) m.registerWithParent(file)
m.mu.Unlock() m.mu.Unlock()
m.mu.RLock() m.mu.RLock()
@ -316,14 +306,11 @@ func (m *MemMapFs) Chmod(name string, mode os.FileMode) error {
return &os.PathError{"chmod", name, ErrFileNotFound} return &os.PathError{"chmod", name, ErrFileNotFound}
} }
ff, ok := f.(*mem.File) ff := mem.NewFileHandle(f)
if ok { m.mu.Lock()
m.mu.Lock() mem.SetMode(ff, mode)
mem.SetMode(ff, mode) m.mu.Unlock()
m.mu.Unlock()
} else {
return errors.New("Unable to Chmod Memory File")
}
return nil return nil
} }
@ -334,21 +321,19 @@ func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error
return &os.PathError{"chtimes", name, ErrFileNotFound} return &os.PathError{"chtimes", name, ErrFileNotFound}
} }
ff, ok := f.(*mem.File) ff := mem.NewFileHandle(f)
if ok { m.mu.Lock()
m.mu.Lock() mem.SetModTime(ff, mtime)
mem.SetModTime(ff, mtime) m.mu.Unlock()
m.mu.Unlock()
} else {
return errors.New("Unable to Chtime Memory File")
}
return nil return nil
} }
func (m *MemMapFs) List() { func (m *MemMapFs) List() {
for _, x := range m.data { for _, x := range m.data {
y, _ := x.Stat() f := mem.NewFileHandle(x)
fmt.Println(x.Name(), y.Size()) y, _ := f.Stat()
fmt.Println(f.Name(), y.Size())
} }
} }

View File

@ -106,10 +106,9 @@ func TestMultipleOpenFiles(t *testing.T) {
defer removeAllTestFiles(t) defer removeAllTestFiles(t)
const fileName = "./afero-demo2.txt" const fileName = "./afero-demo2.txt"
var fss = []Fs{&OsFs{}, &MemMapFs{}} var data = make([][]byte, len(Fss))
var data = make([][]byte, len(fss))
for i, fs := range fss { for i, fs := range Fss {
dir := testDir(fs) dir := testDir(fs)
path := filepath.Join(dir, fileName) path := filepath.Join(dir, fileName)
fh1, err := fs.Create(path) fh1, err := fs.Create(path)
@ -120,18 +119,36 @@ func TestMultipleOpenFiles(t *testing.T) {
if err != nil { if err != nil {
t.Error("fh.Write failed: " + err.Error()) t.Error("fh.Write failed: " + err.Error())
} }
fh1.Seek(0, os.SEEK_SET) _, err = fh1.Seek(0, os.SEEK_SET)
if err != nil {
t.Error(err)
}
fh2, err := fs.OpenFile(path, os.O_RDWR, 0777) fh2, err := fs.OpenFile(path, os.O_RDWR, 0777)
if err != nil { if err != nil {
t.Error("fs.OpenFile failed: " + err.Error()) t.Error("fs.OpenFile failed: " + err.Error())
} }
fh2.Seek(0, os.SEEK_END) _, err = fh2.Seek(0, os.SEEK_END)
fh2.Write([]byte("data")) if err != nil {
fh2.Close() t.Error(err)
}
_, err = fh2.Write([]byte("data"))
if err != nil {
t.Error(err)
}
err = fh2.Close()
if err != nil {
t.Error(err)
}
fh1.Write([]byte("data")) _, err = fh1.Write([]byte("data"))
fh1.Close() if err != nil {
t.Error(err)
}
err = fh1.Close()
if err != nil {
t.Error(err)
}
// the file now should contain "datadata" // the file now should contain "datadata"
data[i], err = ReadFile(fs, path) data[i], err = ReadFile(fs, path)
if err != nil { if err != nil {
@ -139,14 +156,14 @@ func TestMultipleOpenFiles(t *testing.T) {
} }
} }
for i, fs := range fss { for i, fs := range Fss {
if i == 0 { if i == 0 {
continue continue
} }
if string(data[0]) != string(data[i]) { if string(data[0]) != string(data[i]) {
t.Errorf("OsFs and %s don't behave the same\n"+ t.Errorf("%s and %s don't behave the same\n"+
"OsFs: \"%s\"\n%s: \"%s\"\n", "%s: \"%s\"\n%s: \"%s\"\n",
fs.Name(), data[0], fs.Name(), data[i]) Fss[0].Name(), fs.Name(), Fss[0].Name(), data[0], fs.Name(), data[i])
} }
} }
} }