fix mem.DirMap and mem.Dir to only use *FileData and not *File handles

This commit is contained in:
Martin Bertschler 2016-01-04 00:18:26 +01:00
parent 7096d68458
commit 0321b04b21
4 changed files with 35 additions and 34 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() []*FileData
Add(*File) Add(*FileData)
Remove(*File) Remove(*FileData)
} }
func RemoveFromMemDir(dir *File, f *File) { func RemoveFromMemDir(dir *FileData, f *FileData) {
dir.fileData.memDir.Remove(f) dir.memDir.Remove(f)
} }
func AddToMemDir(dir *File, f *File) { func AddToMemDir(dir *FileData, f *FileData) {
dir.fileData.memDir.Add(f) dir.memDir.Add(f)
} }
func InitializeDir(d *File) { func InitializeDir(d *FileData) {
if d.fileData.memDir == nil { if d.memDir == nil {
d.fileData.dir = true d.dir = true
d.fileData.memDir = &DirMap{} d.memDir = &DirMap{}
} }
} }

View File

@ -18,22 +18,22 @@ import "sort"
type DirMap map[string]*FileData 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.fileData.name] = f.fileData } func (m DirMap) Add(f *FileData) { m[f.name] = f }
func (m DirMap) Remove(f *File) { delete(m, f.fileData.name) } func (m DirMap) Remove(f *FileData) { delete(m, f.name) }
func (m DirMap) Files() (files []*File) { func (m DirMap) Files() (files []*FileData) {
for _, f := range m { for _, f := range m {
files = append(files, NewFileHandle(f)) files = append(files, f)
} }
sort.Sort(filesSorter(files)) sort.Sort(filesSorter(files))
return files return files
} }
type filesSorter []*File // implement sort.Interface for []*FileData
type filesSorter []*FileData
// 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].fileData.name < s[j].fileData.name } func (s filesSorter) Less(i, j int) bool { return s[i].name < s[j].name }
func (m DirMap) Names() (names []string) { func (m DirMap) Names() (names []string) {
for x := range m { for x := range m {

View File

@ -54,6 +54,10 @@ type FileData struct {
modtime time.Time modtime time.Time
} }
func (d FileData) Name() string {
return d.name
}
func CreateFile(name string) *FileData { func CreateFile(name string) *FileData {
return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()} return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
} }
@ -128,7 +132,7 @@ func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
res = make([]os.FileInfo, outLength) res = make([]os.FileInfo, outLength)
for i := range res { for i := range res {
res[i] = &FileInfo{files[i].fileData} res[i] = &FileInfo{files[i]}
} }
return res, err return res, err

View File

@ -50,11 +50,9 @@ 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 handle, nil return mem.NewFileHandle(file), nil
} }
func (m *MemMapFs) unRegisterWithParent(fileName string) error { func (m *MemMapFs) unRegisterWithParent(fileName string) error {
@ -66,7 +64,7 @@ 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")
} }
mem.RemoveFromMemDir(parent, f) mem.RemoveFromMemDir(parent.Data(), f.Data())
return nil return nil
} }
@ -80,11 +78,11 @@ func (m *MemMapFs) findParent(f *mem.File) *mem.File {
return pfile return pfile
} }
func (m *MemMapFs) registerWithParent(f *mem.File) { func (m *MemMapFs) registerWithParent(f *mem.FileData) {
if f == nil { if f == nil {
return return
} }
parent := m.findParent(f) parent := m.findParent(mem.NewFileHandle(f))
if parent == nil { if parent == nil {
pdir := filepath.Dir(filepath.Clean(f.Name())) pdir := filepath.Dir(filepath.Clean(f.Name()))
err := m.lockfreeMkdir(pdir, 0777) err := m.lockfreeMkdir(pdir, 0777)
@ -99,8 +97,8 @@ func (m *MemMapFs) registerWithParent(f *mem.File) {
} }
} }
mem.InitializeDir(parent) mem.InitializeDir(parent.Data())
mem.AddToMemDir(parent, f) mem.AddToMemDir(parent.Data(), f)
} }
func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error { func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
@ -118,7 +116,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(mem.NewFileHandle(item)) m.registerWithParent(item)
} }
return nil return nil
} }
@ -135,7 +133,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(mem.NewFileHandle(item)) m.registerWithParent(item)
m.mu.Unlock() m.mu.Unlock()
} }
return nil return nil
@ -274,11 +272,10 @@ func (m *MemMapFs) Rename(oldname, newname string) error {
m.mu.Lock() m.mu.Lock()
m.unRegisterWithParent(oldname) m.unRegisterWithParent(oldname)
fileData := m.getData()[oldname] fileData := m.getData()[oldname]
file := mem.NewFileHandle(fileData)
delete(m.getData(), oldname) delete(m.getData(), oldname)
mem.ChangeFileName(file, newname) mem.ChangeFileName(mem.NewFileHandle(fileData), newname)
m.getData()[newname] = fileData m.getData()[newname] = fileData
m.registerWithParent(file) m.registerWithParent(fileData)
m.mu.Unlock() m.mu.Unlock()
m.mu.RLock() m.mu.RLock()
} else { } else {