From 965d098e7c42716791fda6f3d2acd1854fa17474 Mon Sep 17 00:00:00 2001 From: Steve Francia Date: Thu, 10 Dec 2015 12:25:02 -0500 Subject: [PATCH] Moving memory structures and interfaces into sub package --- mem/dir.go | 38 +++++++++++++++ mem/dirmap.go | 43 ++++++++++++++++ memfile.go => mem/file.go | 100 +++++++++++++++++++++++--------------- memmap.go | 74 +++++++++------------------- 4 files changed, 166 insertions(+), 89 deletions(-) create mode 100644 mem/dir.go create mode 100644 mem/dirmap.go rename memfile.go => mem/file.go (61%) diff --git a/mem/dir.go b/mem/dir.go new file mode 100644 index 0000000..02f632e --- /dev/null +++ b/mem/dir.go @@ -0,0 +1,38 @@ +// Copyright © 2014 Steve Francia . +// +// 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 mem + +type Dir interface { + Len() int + Names() []string + Files() []File + Add(File) + Remove(File) +} + +func RemoveFromMemDir(dir *File, f *File) { + dir.memDir.Remove(*f) +} + +func AddToMemDir(dir *File, f *File) { + dir.memDir.Add(*f) +} + +func InitializeDir(d *File) { + if d.memDir == nil { + d.dir = true + d.memDir = &DirMap{} + } + +} diff --git a/mem/dirmap.go b/mem/dirmap.go new file mode 100644 index 0000000..59c5d8d --- /dev/null +++ b/mem/dirmap.go @@ -0,0 +1,43 @@ +// Copyright © 2015 Steve Francia . +// +// 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 mem + +import "sort" + +type DirMap map[string]File + +func (m DirMap) Len() int { return len(m) } +func (m DirMap) Add(f File) { m[f.Name()] = f } +func (m DirMap) Remove(f File) { delete(m, f.Name()) } +func (m DirMap) Files() (files []File) { + for _, f := range m { + files = append(files, f) + } + sort.Sort(filesSorter(files)) + return files +} + +type filesSorter []File + +// 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].Name() < s[j].Name() } + +func (m DirMap) Names() (names []string) { + for x := range m { + names = append(names, x) + } + return names +} diff --git a/memfile.go b/mem/file.go similarity index 61% rename from memfile.go rename to mem/file.go index d3d8223..4a652cb 100644 --- a/memfile.go +++ b/mem/file.go @@ -1,4 +1,4 @@ -// Copyright © 2014 Steve Francia . +// Copyright © 2015 Steve Francia . // Copyright 2013 tsuru authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,10 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -package afero +package mem import ( "bytes" + "errors" "io" "os" "path/filepath" @@ -25,15 +26,9 @@ import ( import "time" -type MemDir interface { - Len() int - Names() []string - Files() []File - Add(File) - Remove(File) -} +const FilePathSeparator = string(filepath.Separator) -type InMemoryFile struct { +type File struct { // atomic requires 64-bit alignment for struct field access at int64 readDirCount int64 @@ -41,18 +36,38 @@ type InMemoryFile struct { sync.Mutex name string data []byte - memDir MemDir + memDir Dir dir bool closed bool mode os.FileMode modtime time.Time } -func MemFileCreate(name string) *InMemoryFile { - return &InMemoryFile{name: name, mode: os.ModeTemporary, modtime: time.Now()} +func CreateFile(name string) *File { + return &File{name: name, mode: os.ModeTemporary, modtime: time.Now()} } -func (f *InMemoryFile) Open() error { +func CreateDir(name string) *File { + return &File{name: FilePathSeparator, memDir: &DirMap{}, dir: true} +} + +func ChangeFileName(f *File, newname string) { + f.name = newname +} + +func SetMode(f *File, mode os.FileMode) { + f.mode = mode +} + +func SetModTime(f *File, mtime time.Time) { + f.modtime = mtime +} + +func GetFileInfo(f *File) *FileInfo { + return &FileInfo{file: f} +} + +func (f *File) Open() error { atomic.StoreInt64(&f.at, 0) atomic.StoreInt64(&f.readDirCount, 0) f.Lock() @@ -61,26 +76,26 @@ func (f *InMemoryFile) Open() error { return nil } -func (f *InMemoryFile) Close() error { +func (f *File) Close() error { f.Lock() f.closed = true f.Unlock() return nil } -func (f *InMemoryFile) Name() string { +func (f *File) Name() string { return f.name } -func (f *InMemoryFile) Stat() (os.FileInfo, error) { - return &InMemoryFileInfo{f}, nil +func (f *File) Stat() (os.FileInfo, error) { + return &FileInfo{f}, nil } -func (f *InMemoryFile) Sync() error { +func (f *File) Sync() error { return nil } -func (f *InMemoryFile) Readdir(count int) (res []os.FileInfo, err error) { +func (f *File) Readdir(count int) (res []os.FileInfo, err error) { var outLength int64 f.Lock() @@ -108,7 +123,7 @@ func (f *InMemoryFile) Readdir(count int) (res []os.FileInfo, err error) { return res, err } -func (f *InMemoryFile) Readdirnames(n int) (names []string, err error) { +func (f *File) Readdirnames(n int) (names []string, err error) { fi, err := f.Readdir(n) names = make([]string, len(fi)) for i, f := range fi { @@ -117,7 +132,7 @@ func (f *InMemoryFile) Readdirnames(n int) (names []string, err error) { return names, err } -func (f *InMemoryFile) Read(b []byte) (n int, err error) { +func (f *File) Read(b []byte) (n int, err error) { f.Lock() defer f.Unlock() if f.closed == true { @@ -136,12 +151,12 @@ func (f *InMemoryFile) Read(b []byte) (n int, err error) { return } -func (f *InMemoryFile) ReadAt(b []byte, off int64) (n int, err error) { +func (f *File) 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 { +func (f *File) Truncate(size int64) error { if f.closed == true { return ErrFileClosed } @@ -157,7 +172,7 @@ func (f *InMemoryFile) Truncate(size int64) error { return nil } -func (f *InMemoryFile) Seek(offset int64, whence int) (int64, error) { +func (f *File) Seek(offset int64, whence int) (int64, error) { if f.closed == true { return 0, ErrFileClosed } @@ -172,7 +187,7 @@ func (f *InMemoryFile) Seek(offset int64, whence int) (int64, error) { return f.at, nil } -func (f *InMemoryFile) Write(b []byte) (n int, err error) { +func (f *File) Write(b []byte) (n int, err error) { n = len(b) cur := atomic.LoadInt64(&f.at) f.Lock() @@ -194,35 +209,44 @@ func (f *InMemoryFile) Write(b []byte) (n int, err error) { return } -func (f *InMemoryFile) WriteAt(b []byte, off int64) (n int, err error) { +func (f *File) 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) { +func (f *File) WriteString(s string) (ret int, err error) { return f.Write([]byte(s)) } -func (f *InMemoryFile) Info() *InMemoryFileInfo { - return &InMemoryFileInfo{file: f} +func (f *File) Info() *FileInfo { + return &FileInfo{file: f} } -type InMemoryFileInfo struct { - file *InMemoryFile +type FileInfo struct { + file *File } // Implements os.FileInfo -func (s *InMemoryFileInfo) Name() string { +func (s *FileInfo) Name() string { _, name := filepath.Split(s.file.Name()) return name } -func (s *InMemoryFileInfo) Mode() os.FileMode { return s.file.mode } -func (s *InMemoryFileInfo) ModTime() time.Time { return s.file.modtime } -func (s *InMemoryFileInfo) IsDir() bool { return s.file.dir } -func (s *InMemoryFileInfo) Sys() interface{} { return nil } -func (s *InMemoryFileInfo) Size() int64 { +func (s *FileInfo) Mode() os.FileMode { return s.file.mode } +func (s *FileInfo) ModTime() time.Time { return s.file.modtime } +func (s *FileInfo) IsDir() bool { return s.file.dir } +func (s *FileInfo) Sys() interface{} { return nil } +func (s *FileInfo) Size() int64 { if s.IsDir() { return int64(42) } return int64(len(s.file.data)) } + +var ( + ErrFileClosed = errors.New("File is closed") + ErrOutOfRange = errors.New("Out of range") + ErrTooLarge = errors.New("Too large") + ErrFileNotFound = os.ErrNotExist + ErrFileExists = os.ErrExist + ErrDestinationExists = os.ErrExist +) diff --git a/memmap.go b/memmap.go index 6ad32e6..0263953 100644 --- a/memmap.go +++ b/memmap.go @@ -19,10 +19,11 @@ import ( "log" "os" "path/filepath" - "sort" "strings" "sync" "time" + + "github.com/spf13/afero/mem" ) var mux = &sync.Mutex{} @@ -47,7 +48,7 @@ func (m *MemMapFs) getData() map[string]File { // Root should always exist, right? // TODO: what about windows? - m.data[FilePathSeparator] = &InMemoryFile{name: FilePathSeparator, memDir: &MemDirMap{}, dir: true} + m.data[FilePathSeparator] = mem.CreateDir(FilePathSeparator) } return m.data } @@ -60,40 +61,12 @@ func (m *MemMapFs) getMutex() *sync.RWMutex { mux.Unlock() return m.mutex } - -type MemDirMap map[string]File - -func (m MemDirMap) Len() int { return len(m) } -func (m MemDirMap) Add(f File) { m[f.Name()] = f } -func (m MemDirMap) Remove(f File) { delete(m, f.Name()) } -func (m MemDirMap) Files() (files []File) { - for _, f := range m { - files = append(files, f) - } - sort.Sort(filesSorter(files)) - return files -} - -type filesSorter []File - -// 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].Name() < s[j].Name() } - -func (m MemDirMap) Names() (names []string) { - for x := range m { - names = append(names, x) - } - return names -} - func (MemMapFs) Name() string { return "MemMapFS" } func (m *MemMapFs) Create(name string) (File, error) { name = normalizePath(name) m.lock() - file := MemFileCreate(name) + file := mem.CreateFile(name) m.getData()[name] = file m.registerWithParent(file) m.unlock() @@ -112,8 +85,9 @@ func (m *MemMapFs) unRegisterWithParent(fileName string) { if parent == nil { log.Fatal("parent of ", f.Name(), " is nil") } - pmem := parent.(*InMemoryFile) - pmem.memDir.Remove(f) + pmem := parent.(*mem.File) + fmem := f.(*mem.File) + mem.RemoveFromMemDir(pmem, fmem) } func (m *MemMapFs) findParent(f File) File { @@ -144,18 +118,15 @@ func (m *MemMapFs) registerWithParent(f File) { return } } - pmem := parent.(*InMemoryFile) + pmem := parent.(*mem.File) + fmem := f.(*mem.File) // TODO(mbertschler): memDir is only nil when it was not made with Mkdir // or lockfreeMkdir. In this case the parent is also not a real directory. // This currently only happens for the file ".". // This is a quick hack to make the library usable with relative paths. - if pmem.memDir == nil { - pmem.dir = true - pmem.memDir = &MemDirMap{} - } - - pmem.memDir.Add(f) + mem.InitializeDir(pmem) + mem.AddToMemDir(pmem, fmem) } func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error { @@ -171,7 +142,7 @@ func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error { return err } } else { - item := &InMemoryFile{name: name, memDir: &MemDirMap{}, dir: true} + item := mem.CreateDir(name) m.getData()[name] = item m.registerWithParent(item) } @@ -195,7 +166,7 @@ func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error { } } else { m.lock() - item := &InMemoryFile{name: name, memDir: &MemDirMap{}, dir: true} + item := mem.CreateDir(name) m.getData()[name] = item m.registerWithParent(item) m.unlock() @@ -226,7 +197,7 @@ func (m *MemMapFs) Open(name string) (File, error) { m.rlock() f, ok := m.getData()[name] - ff, ok := f.(*InMemoryFile) + ff, ok := f.(*mem.File) if ok { ff.Open() @@ -243,7 +214,7 @@ func (m *MemMapFs) Open(name string) (File, error) { func (m *MemMapFs) lockfreeOpen(name string) (File, error) { name = normalizePath(name) f, ok := m.getData()[name] - ff, ok := f.(*InMemoryFile) + ff, ok := f.(*mem.File) if ok { ff.Open() } @@ -330,9 +301,9 @@ func (m *MemMapFs) Rename(oldname, newname string) error { m.runlock() m.lock() m.unRegisterWithParent(oldname) - file := m.getData()[oldname].(*InMemoryFile) + file := m.getData()[oldname].(*mem.File) delete(m.getData(), oldname) - file.name = newname + mem.ChangeFileName(file, newname) m.getData()[newname] = file m.registerWithParent(file) m.unlock() @@ -351,7 +322,8 @@ func (m *MemMapFs) Stat(name string) (os.FileInfo, error) { if err != nil { return nil, err } - return &InMemoryFileInfo{file: f.(*InMemoryFile)}, nil + fi := mem.GetFileInfo(f.(*mem.File)) + return fi, nil } func (m *MemMapFs) Chmod(name string, mode os.FileMode) error { @@ -361,10 +333,10 @@ func (m *MemMapFs) Chmod(name string, mode os.FileMode) error { return &os.PathError{"chmod", name, ErrFileNotFound} } - ff, ok := f.(*InMemoryFile) + ff, ok := f.(*mem.File) if ok { m.lock() - ff.mode = mode + mem.SetMode(ff, mode) m.unlock() } else { return errors.New("Unable to Chmod Memory File") @@ -379,10 +351,10 @@ func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error return &os.PathError{"chtimes", name, ErrFileNotFound} } - ff, ok := f.(*InMemoryFile) + ff, ok := f.(*mem.File) if ok { m.lock() - ff.modtime = mtime + mem.SetModTime(ff, mtime) m.unlock() } else { return errors.New("Unable to Chtime Memory File")