mirror of https://github.com/spf13/afero.git
fix mem.DirMap and mem.Dir to only use *FileData and not *File handles
This commit is contained in:
parent
7096d68458
commit
0321b04b21
22
mem/dir.go
22
mem/dir.go
|
@ -16,22 +16,22 @@ package mem
|
|||
type Dir interface {
|
||||
Len() int
|
||||
Names() []string
|
||||
Files() []*File
|
||||
Add(*File)
|
||||
Remove(*File)
|
||||
Files() []*FileData
|
||||
Add(*FileData)
|
||||
Remove(*FileData)
|
||||
}
|
||||
|
||||
func RemoveFromMemDir(dir *File, f *File) {
|
||||
dir.fileData.memDir.Remove(f)
|
||||
func RemoveFromMemDir(dir *FileData, f *FileData) {
|
||||
dir.memDir.Remove(f)
|
||||
}
|
||||
|
||||
func AddToMemDir(dir *File, f *File) {
|
||||
dir.fileData.memDir.Add(f)
|
||||
func AddToMemDir(dir *FileData, f *FileData) {
|
||||
dir.memDir.Add(f)
|
||||
}
|
||||
|
||||
func InitializeDir(d *File) {
|
||||
if d.fileData.memDir == nil {
|
||||
d.fileData.dir = true
|
||||
d.fileData.memDir = &DirMap{}
|
||||
func InitializeDir(d *FileData) {
|
||||
if d.memDir == nil {
|
||||
d.dir = true
|
||||
d.memDir = &DirMap{}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,23 +17,23 @@ import "sort"
|
|||
|
||||
type DirMap map[string]*FileData
|
||||
|
||||
func (m DirMap) Len() int { return len(m) }
|
||||
func (m DirMap) Add(f *File) { m[f.fileData.name] = f.fileData }
|
||||
func (m DirMap) Remove(f *File) { delete(m, f.fileData.name) }
|
||||
func (m DirMap) Files() (files []*File) {
|
||||
func (m DirMap) Len() int { return len(m) }
|
||||
func (m DirMap) Add(f *FileData) { m[f.name] = f }
|
||||
func (m DirMap) Remove(f *FileData) { delete(m, f.name) }
|
||||
func (m DirMap) Files() (files []*FileData) {
|
||||
for _, f := range m {
|
||||
files = append(files, NewFileHandle(f))
|
||||
files = append(files, f)
|
||||
}
|
||||
sort.Sort(filesSorter(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) 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) {
|
||||
for x := range m {
|
||||
|
|
|
@ -54,6 +54,10 @@ type FileData struct {
|
|||
modtime time.Time
|
||||
}
|
||||
|
||||
func (d FileData) Name() string {
|
||||
return d.name
|
||||
}
|
||||
|
||||
func CreateFile(name string) *FileData {
|
||||
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)
|
||||
for i := range res {
|
||||
res[i] = &FileInfo{files[i].fileData}
|
||||
res[i] = &FileInfo{files[i]}
|
||||
}
|
||||
|
||||
return res, err
|
||||
|
|
25
memmap.go
25
memmap.go
|
@ -50,11 +50,9 @@ func (m *MemMapFs) Create(name string) (File, error) {
|
|||
m.mu.Lock()
|
||||
file := mem.CreateFile(name)
|
||||
m.getData()[name] = file
|
||||
|
||||
handle := mem.NewFileHandle(file)
|
||||
m.registerWithParent(handle)
|
||||
m.registerWithParent(file)
|
||||
m.mu.Unlock()
|
||||
return handle, nil
|
||||
return mem.NewFileHandle(file), nil
|
||||
}
|
||||
|
||||
func (m *MemMapFs) unRegisterWithParent(fileName string) error {
|
||||
|
@ -66,7 +64,7 @@ func (m *MemMapFs) unRegisterWithParent(fileName string) error {
|
|||
if parent == nil {
|
||||
log.Fatal("parent of ", f.Name(), " is nil")
|
||||
}
|
||||
mem.RemoveFromMemDir(parent, f)
|
||||
mem.RemoveFromMemDir(parent.Data(), f.Data())
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -80,11 +78,11 @@ func (m *MemMapFs) findParent(f *mem.File) *mem.File {
|
|||
return pfile
|
||||
}
|
||||
|
||||
func (m *MemMapFs) registerWithParent(f *mem.File) {
|
||||
func (m *MemMapFs) registerWithParent(f *mem.FileData) {
|
||||
if f == nil {
|
||||
return
|
||||
}
|
||||
parent := m.findParent(f)
|
||||
parent := m.findParent(mem.NewFileHandle(f))
|
||||
if parent == nil {
|
||||
pdir := filepath.Dir(filepath.Clean(f.Name()))
|
||||
err := m.lockfreeMkdir(pdir, 0777)
|
||||
|
@ -99,8 +97,8 @@ func (m *MemMapFs) registerWithParent(f *mem.File) {
|
|||
}
|
||||
}
|
||||
|
||||
mem.InitializeDir(parent)
|
||||
mem.AddToMemDir(parent, f)
|
||||
mem.InitializeDir(parent.Data())
|
||||
mem.AddToMemDir(parent.Data(), f)
|
||||
}
|
||||
|
||||
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 {
|
||||
item := mem.CreateDir(name)
|
||||
m.getData()[name] = item
|
||||
m.registerWithParent(mem.NewFileHandle(item))
|
||||
m.registerWithParent(item)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -135,7 +133,7 @@ func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error {
|
|||
m.mu.Lock()
|
||||
item := mem.CreateDir(name)
|
||||
m.getData()[name] = item
|
||||
m.registerWithParent(mem.NewFileHandle(item))
|
||||
m.registerWithParent(item)
|
||||
m.mu.Unlock()
|
||||
}
|
||||
return nil
|
||||
|
@ -274,11 +272,10 @@ func (m *MemMapFs) Rename(oldname, newname string) error {
|
|||
m.mu.Lock()
|
||||
m.unRegisterWithParent(oldname)
|
||||
fileData := m.getData()[oldname]
|
||||
file := mem.NewFileHandle(fileData)
|
||||
delete(m.getData(), oldname)
|
||||
mem.ChangeFileName(file, newname)
|
||||
mem.ChangeFileName(mem.NewFileHandle(fileData), newname)
|
||||
m.getData()[newname] = fileData
|
||||
m.registerWithParent(file)
|
||||
m.registerWithParent(fileData)
|
||||
m.mu.Unlock()
|
||||
m.mu.RLock()
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue