mirror of https://github.com/spf13/afero.git
replace mem.File handles with mem.FileData when mem.FileData is all we need
This commit is contained in:
parent
0321b04b21
commit
a5f01f1106
16
mem/file.go
16
mem/file.go
|
@ -66,20 +66,20 @@ func CreateDir(name string) *FileData {
|
||||||
return &FileData{name: name, memDir: &DirMap{}, dir: true}
|
return &FileData{name: name, memDir: &DirMap{}, dir: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ChangeFileName(f *File, newname string) {
|
func ChangeFileName(f *FileData, newname string) {
|
||||||
f.fileData.name = newname
|
f.name = newname
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetMode(f *File, mode os.FileMode) {
|
func SetMode(f *FileData, mode os.FileMode) {
|
||||||
f.fileData.mode = mode
|
f.mode = mode
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetModTime(f *File, mtime time.Time) {
|
func SetModTime(f *FileData, mtime time.Time) {
|
||||||
f.fileData.modtime = mtime
|
f.modtime = mtime
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFileInfo(f *File) *FileInfo {
|
func GetFileInfo(f *FileData) *FileInfo {
|
||||||
return &FileInfo{f.fileData}
|
return &FileInfo{f}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) Open() error {
|
func (f *File) Open() error {
|
||||||
|
|
41
memmap.go
41
memmap.go
|
@ -64,11 +64,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")
|
||||||
}
|
}
|
||||||
mem.RemoveFromMemDir(parent.Data(), f.Data())
|
mem.RemoveFromMemDir(parent, f)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MemMapFs) findParent(f *mem.File) *mem.File {
|
func (m *MemMapFs) findParent(f *mem.FileData) *mem.FileData {
|
||||||
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)
|
||||||
|
@ -82,7 +82,7 @@ func (m *MemMapFs) registerWithParent(f *mem.FileData) {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
parent := m.findParent(mem.NewFileHandle(f))
|
parent := m.findParent(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)
|
||||||
|
@ -97,8 +97,8 @@ func (m *MemMapFs) registerWithParent(f *mem.FileData) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mem.InitializeDir(parent.Data())
|
mem.InitializeDir(parent)
|
||||||
mem.AddToMemDir(parent.Data(), f)
|
mem.AddToMemDir(parent, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
|
func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
|
||||||
|
@ -106,13 +106,10 @@ 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 := mem.NewFileHandle(x).Stat()
|
i := mem.FileInfo{x}
|
||||||
if !i.IsDir() {
|
if !i.IsDir() {
|
||||||
return ErrFileExists
|
return ErrFileExists
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
item := mem.CreateDir(name)
|
item := mem.CreateDir(name)
|
||||||
m.getData()[name] = item
|
m.getData()[name] = item
|
||||||
|
@ -174,19 +171,14 @@ func (m *MemMapFs) Open(name string) (File, error) {
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, &os.PathError{"open", name, ErrFileNotFound}
|
return nil, &os.PathError{"open", name, ErrFileNotFound}
|
||||||
}
|
}
|
||||||
ff := mem.NewFileHandle(f)
|
return mem.NewFileHandle(f), nil
|
||||||
ff.Open()
|
|
||||||
return ff, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MemMapFs) lockfreeOpen(name string) (*mem.File, error) {
|
func (m *MemMapFs) lockfreeOpen(name string) (*mem.FileData, error) {
|
||||||
name = normalizePath(name)
|
name = normalizePath(name)
|
||||||
f, ok := m.getData()[name]
|
f, ok := m.getData()[name]
|
||||||
if ok {
|
if ok {
|
||||||
// TODO (mbertschler)
|
return f, nil
|
||||||
// not used anymore?
|
|
||||||
//ff.Open()
|
|
||||||
return mem.NewFileHandle(f), nil
|
|
||||||
} else {
|
} else {
|
||||||
return nil, ErrFileNotFound
|
return nil, ErrFileNotFound
|
||||||
}
|
}
|
||||||
|
@ -273,7 +265,7 @@ func (m *MemMapFs) Rename(oldname, newname string) error {
|
||||||
m.unRegisterWithParent(oldname)
|
m.unRegisterWithParent(oldname)
|
||||||
fileData := m.getData()[oldname]
|
fileData := m.getData()[oldname]
|
||||||
delete(m.getData(), oldname)
|
delete(m.getData(), oldname)
|
||||||
mem.ChangeFileName(mem.NewFileHandle(fileData), newname)
|
mem.ChangeFileName(fileData, newname)
|
||||||
m.getData()[newname] = fileData
|
m.getData()[newname] = fileData
|
||||||
m.registerWithParent(fileData)
|
m.registerWithParent(fileData)
|
||||||
m.mu.Unlock()
|
m.mu.Unlock()
|
||||||
|
@ -292,7 +284,7 @@ func (m *MemMapFs) Stat(name string) (os.FileInfo, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fi := mem.GetFileInfo(f.(*mem.File))
|
fi := mem.GetFileInfo(f.(*mem.File).Data())
|
||||||
return fi, nil
|
return fi, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,9 +295,8 @@ func (m *MemMapFs) Chmod(name string, mode os.FileMode) error {
|
||||||
return &os.PathError{"chmod", name, ErrFileNotFound}
|
return &os.PathError{"chmod", name, ErrFileNotFound}
|
||||||
}
|
}
|
||||||
|
|
||||||
ff := mem.NewFileHandle(f)
|
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
mem.SetMode(ff, mode)
|
mem.SetMode(f, mode)
|
||||||
m.mu.Unlock()
|
m.mu.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -318,9 +309,8 @@ 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 := mem.NewFileHandle(f)
|
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
mem.SetModTime(ff, mtime)
|
mem.SetModTime(f, mtime)
|
||||||
m.mu.Unlock()
|
m.mu.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -328,9 +318,8 @@ func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error
|
||||||
|
|
||||||
func (m *MemMapFs) List() {
|
func (m *MemMapFs) List() {
|
||||||
for _, x := range m.data {
|
for _, x := range m.data {
|
||||||
f := mem.NewFileHandle(x)
|
y := mem.FileInfo{x}
|
||||||
y, _ := f.Stat()
|
fmt.Println(x.Name(), y.Size())
|
||||||
fmt.Println(f.Name(), y.Size())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue