2014-10-28 17:29:28 +03:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// 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 afero
|
|
|
|
|
|
|
|
import (
|
2014-12-12 21:32:57 +03:00
|
|
|
"fmt"
|
2015-10-17 13:58:11 +03:00
|
|
|
"log"
|
2014-10-28 17:29:28 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-11-01 06:36:45 +03:00
|
|
|
"time"
|
2015-12-10 20:25:02 +03:00
|
|
|
|
|
|
|
"github.com/spf13/afero/mem"
|
2014-10-28 17:29:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemMapFs struct {
|
2015-12-18 23:26:04 +03:00
|
|
|
mu sync.RWMutex
|
2016-01-04 01:59:42 +03:00
|
|
|
data map[string]*mem.FileData
|
2015-12-18 21:20:45 +03:00
|
|
|
init sync.Once
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-12 05:41:03 +03:00
|
|
|
func NewMemMapFs() Fs {
|
|
|
|
return &MemMapFs{}
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
func (m *MemMapFs) getData() map[string]*mem.FileData {
|
2015-12-18 21:20:45 +03:00
|
|
|
m.init.Do(func() {
|
2016-01-04 01:59:42 +03:00
|
|
|
m.data = make(map[string]*mem.FileData)
|
2015-12-03 00:27:14 +03:00
|
|
|
// Root should always exist, right?
|
|
|
|
// TODO: what about windows?
|
2015-12-10 21:00:25 +03:00
|
|
|
m.data[FilePathSeparator] = mem.CreateDir(FilePathSeparator)
|
2015-12-18 21:20:45 +03:00
|
|
|
})
|
2014-10-28 17:29:28 +03:00
|
|
|
return m.data
|
|
|
|
}
|
|
|
|
|
2017-01-10 01:53:20 +03:00
|
|
|
func (*MemMapFs) Name() string { return "MemMapFS" }
|
2014-10-28 17:29:28 +03:00
|
|
|
|
|
|
|
func (m *MemMapFs) Create(name string) (File, error) {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.Lock()
|
2015-12-10 21:00:25 +03:00
|
|
|
file := mem.CreateFile(name)
|
2015-06-11 21:49:27 +03:00
|
|
|
m.getData()[name] = file
|
2016-01-04 02:18:26 +03:00
|
|
|
m.registerWithParent(file)
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.Unlock()
|
2016-01-04 02:18:26 +03:00
|
|
|
return mem.NewFileHandle(file), nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-21 00:09:09 +03:00
|
|
|
func (m *MemMapFs) unRegisterWithParent(fileName string) error {
|
2015-10-17 13:58:11 +03:00
|
|
|
f, err := m.lockfreeOpen(fileName)
|
|
|
|
if err != nil {
|
2015-12-21 00:09:09 +03:00
|
|
|
return err
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
parent := m.findParent(f)
|
2015-11-04 22:33:40 +03:00
|
|
|
if parent == nil {
|
2016-06-27 03:21:49 +03:00
|
|
|
log.Panic("parent of ", f.Name(), " is nil")
|
2015-11-04 22:33:40 +03:00
|
|
|
}
|
2017-07-23 01:23:01 +03:00
|
|
|
|
|
|
|
parent.Lock()
|
2016-01-04 02:36:01 +03:00
|
|
|
mem.RemoveFromMemDir(parent, f)
|
2017-07-23 01:23:01 +03:00
|
|
|
parent.Unlock()
|
2015-12-21 00:09:09 +03:00
|
|
|
return nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:36:01 +03:00
|
|
|
func (m *MemMapFs) findParent(f *mem.FileData) *mem.FileData {
|
2015-12-09 05:48:19 +03:00
|
|
|
pdir, _ := filepath.Split(f.Name())
|
|
|
|
pdir = filepath.Clean(pdir)
|
2015-10-17 13:58:11 +03:00
|
|
|
pfile, err := m.lockfreeOpen(pdir)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2015-10-17 13:58:11 +03:00
|
|
|
return pfile
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:18:26 +03:00
|
|
|
func (m *MemMapFs) registerWithParent(f *mem.FileData) {
|
2014-10-28 17:29:28 +03:00
|
|
|
if f == nil {
|
2015-10-17 13:58:11 +03:00
|
|
|
return
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2016-01-04 02:36:01 +03:00
|
|
|
parent := m.findParent(f)
|
2015-10-17 13:58:11 +03:00
|
|
|
if parent == nil {
|
2015-12-09 05:48:19 +03:00
|
|
|
pdir := filepath.Dir(filepath.Clean(f.Name()))
|
2015-11-04 22:33:40 +03:00
|
|
|
err := m.lockfreeMkdir(pdir, 0777)
|
2015-10-17 13:58:11 +03:00
|
|
|
if err != nil {
|
2015-11-07 03:00:03 +03:00
|
|
|
//log.Println("Mkdir error:", err)
|
2015-10-17 13:58:11 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
parent, err = m.lockfreeOpen(pdir)
|
|
|
|
if err != nil {
|
2015-11-07 03:00:03 +03:00
|
|
|
//log.Println("Open after Mkdir error:", err)
|
2015-10-17 13:58:11 +03:00
|
|
|
return
|
|
|
|
}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2016-01-04 01:59:42 +03:00
|
|
|
|
2017-07-23 01:23:01 +03:00
|
|
|
parent.Lock()
|
2016-01-04 02:36:01 +03:00
|
|
|
mem.InitializeDir(parent)
|
|
|
|
mem.AddToMemDir(parent, f)
|
2017-07-23 01:23:01 +03:00
|
|
|
parent.Unlock()
|
2015-10-17 13:58:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) lockfreeMkdir(name string, perm os.FileMode) error {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
2015-11-17 05:26:16 +03:00
|
|
|
x, ok := m.getData()[name]
|
2015-10-17 13:58:11 +03:00
|
|
|
if ok {
|
2015-11-17 05:26:16 +03:00
|
|
|
// Only return ErrFileExists if it's a file, not a directory.
|
2017-01-10 01:53:20 +03:00
|
|
|
i := mem.FileInfo{FileData: x}
|
2015-11-17 05:26:16 +03:00
|
|
|
if !i.IsDir() {
|
|
|
|
return ErrFileExists
|
|
|
|
}
|
2015-10-17 13:58:11 +03:00
|
|
|
} else {
|
2015-12-10 21:00:25 +03:00
|
|
|
item := mem.CreateDir(name)
|
2015-10-17 13:58:11 +03:00
|
|
|
m.getData()[name] = item
|
2016-01-04 02:18:26 +03:00
|
|
|
m.registerWithParent(item)
|
2015-10-17 13:58:11 +03:00
|
|
|
}
|
|
|
|
return nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
|
|
|
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.RLock()
|
2015-12-21 00:09:09 +03:00
|
|
|
_, ok := m.getData()[name]
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.RUnlock()
|
2014-10-28 17:29:28 +03:00
|
|
|
if ok {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "mkdir", Path: name, Err: ErrFileExists}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2016-12-26 08:43:06 +03:00
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
item := mem.CreateDir(name)
|
|
|
|
m.getData()[name] = item
|
|
|
|
m.registerWithParent(item)
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
2017-10-04 00:46:18 +03:00
|
|
|
m.Chmod(name, perm|os.ModeDir)
|
2016-12-26 08:43:06 +03:00
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) MkdirAll(path string, perm os.FileMode) error {
|
2015-12-21 00:09:09 +03:00
|
|
|
err := m.Mkdir(path, perm)
|
|
|
|
if err != nil {
|
|
|
|
if err.(*os.PathError).Err == ErrFileExists {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-04 00:46:18 +03:00
|
|
|
return err
|
2015-12-21 00:09:09 +03:00
|
|
|
}
|
|
|
|
return nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-03 00:28:58 +03:00
|
|
|
// Handle some relative paths
|
|
|
|
func normalizePath(path string) string {
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
|
|
|
|
switch path {
|
|
|
|
case ".":
|
2015-12-09 23:37:04 +03:00
|
|
|
return FilePathSeparator
|
2015-12-03 00:28:58 +03:00
|
|
|
case "..":
|
2015-12-09 23:37:04 +03:00
|
|
|
return FilePathSeparator
|
2015-12-03 00:28:58 +03:00
|
|
|
default:
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
func (m *MemMapFs) Open(name string) (File, error) {
|
2016-01-04 03:54:48 +03:00
|
|
|
f, err := m.open(name)
|
|
|
|
if f != nil {
|
|
|
|
return mem.NewReadOnlyFileHandle(f), err
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) openWrite(name string) (File, error) {
|
|
|
|
f, err := m.open(name)
|
|
|
|
if f != nil {
|
|
|
|
return mem.NewFileHandle(f), err
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) open(name string) (*mem.FileData, error) {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
|
|
|
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.RLock()
|
2014-10-28 17:29:28 +03:00
|
|
|
f, ok := m.getData()[name]
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.RUnlock()
|
2016-01-04 01:59:42 +03:00
|
|
|
if !ok {
|
2017-01-10 01:53:20 +03:00
|
|
|
return nil, &os.PathError{Op: "open", Path: name, Err: ErrFileNotFound}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2016-01-04 03:54:48 +03:00
|
|
|
return f, nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:36:01 +03:00
|
|
|
func (m *MemMapFs) lockfreeOpen(name string) (*mem.FileData, error) {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
2015-10-17 13:58:11 +03:00
|
|
|
f, ok := m.getData()[name]
|
|
|
|
if ok {
|
2016-01-04 02:36:01 +03:00
|
|
|
return f, nil
|
2015-10-17 13:58:11 +03:00
|
|
|
} else {
|
|
|
|
return nil, ErrFileNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
|
2016-11-22 19:24:52 +03:00
|
|
|
chmod := false
|
2016-01-04 03:54:48 +03:00
|
|
|
file, err := m.openWrite(name)
|
2020-05-29 23:21:33 +03:00
|
|
|
if err == nil && (flag&os.O_EXCL > 0) {
|
|
|
|
return nil, &os.PathError{Op: "open", Path: name, Err: ErrFileExists}
|
|
|
|
}
|
2015-08-08 20:50:19 +03:00
|
|
|
if os.IsNotExist(err) && (flag&os.O_CREATE > 0) {
|
|
|
|
file, err = m.Create(name)
|
2016-11-22 19:24:52 +03:00
|
|
|
chmod = true
|
2015-08-08 20:50:19 +03:00
|
|
|
}
|
2015-08-08 20:55:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-04 03:54:48 +03:00
|
|
|
if flag == os.O_RDONLY {
|
|
|
|
file = mem.NewReadOnlyFileHandle(file.(*mem.File).Data())
|
|
|
|
}
|
2015-08-08 20:55:20 +03:00
|
|
|
if flag&os.O_APPEND > 0 {
|
|
|
|
_, err = file.Seek(0, os.SEEK_END)
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-08-09 20:18:31 +03:00
|
|
|
if flag&os.O_TRUNC > 0 && flag&(os.O_RDWR|os.O_WRONLY) > 0 {
|
|
|
|
err = file.Truncate(0)
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-11-22 19:24:52 +03:00
|
|
|
if chmod {
|
|
|
|
m.Chmod(name, perm)
|
|
|
|
}
|
2015-08-08 20:55:20 +03:00
|
|
|
return file, nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) Remove(name string) error {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
|
|
|
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2014-10-28 17:29:28 +03:00
|
|
|
|
2015-08-09 00:47:18 +03:00
|
|
|
if _, ok := m.getData()[name]; ok {
|
2015-12-21 00:09:09 +03:00
|
|
|
err := m.unRegisterWithParent(name)
|
|
|
|
if err != nil {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "remove", Path: name, Err: err}
|
2015-12-21 00:09:09 +03:00
|
|
|
}
|
2014-10-28 17:29:28 +03:00
|
|
|
delete(m.getData(), name)
|
2015-08-09 00:55:14 +03:00
|
|
|
} else {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "remove", Path: name, Err: os.ErrNotExist}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) RemoveAll(path string) error {
|
2015-12-03 00:28:58 +03:00
|
|
|
path = normalizePath(path)
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.Lock()
|
2015-11-04 22:33:40 +03:00
|
|
|
m.unRegisterWithParent(path)
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.Unlock()
|
2015-11-04 22:33:40 +03:00
|
|
|
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2015-10-17 13:58:11 +03:00
|
|
|
|
2020-04-11 00:50:34 +03:00
|
|
|
for p := range m.getData() {
|
2014-10-28 17:29:28 +03:00
|
|
|
if strings.HasPrefix(p, path) {
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.RUnlock()
|
|
|
|
m.mu.Lock()
|
2014-10-28 17:29:28 +03:00
|
|
|
delete(m.getData(), p)
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.Unlock()
|
|
|
|
m.mu.RLock()
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) Rename(oldname, newname string) error {
|
2015-12-03 00:28:58 +03:00
|
|
|
oldname = normalizePath(oldname)
|
|
|
|
newname = normalizePath(newname)
|
|
|
|
|
|
|
|
if oldname == newname {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-18 23:26:04 +03:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2014-10-28 17:29:28 +03:00
|
|
|
if _, ok := m.getData()[oldname]; ok {
|
2016-01-07 13:47:44 +03:00
|
|
|
m.mu.RUnlock()
|
|
|
|
m.mu.Lock()
|
|
|
|
m.unRegisterWithParent(oldname)
|
|
|
|
fileData := m.getData()[oldname]
|
|
|
|
delete(m.getData(), oldname)
|
|
|
|
mem.ChangeFileName(fileData, newname)
|
|
|
|
m.getData()[newname] = fileData
|
|
|
|
m.registerWithParent(fileData)
|
|
|
|
m.mu.Unlock()
|
|
|
|
m.mu.RLock()
|
2014-10-28 17:29:28 +03:00
|
|
|
} else {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "rename", Path: oldname, Err: ErrFileNotFound}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) Stat(name string) (os.FileInfo, error) {
|
|
|
|
f, err := m.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-04 02:36:01 +03:00
|
|
|
fi := mem.GetFileInfo(f.(*mem.File).Data())
|
2015-12-10 21:00:25 +03:00
|
|
|
return fi, nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2014-11-01 06:36:45 +03:00
|
|
|
|
|
|
|
func (m *MemMapFs) Chmod(name string, mode os.FileMode) error {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
2016-12-26 08:43:06 +03:00
|
|
|
|
|
|
|
m.mu.RLock()
|
2014-11-01 06:36:45 +03:00
|
|
|
f, ok := m.getData()[name]
|
2016-12-26 08:43:06 +03:00
|
|
|
m.mu.RUnlock()
|
2014-11-01 06:36:45 +03:00
|
|
|
if !ok {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "chmod", Path: name, Err: ErrFileNotFound}
|
2014-11-01 06:36:45 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
m.mu.Lock()
|
2016-01-04 02:36:01 +03:00
|
|
|
mem.SetMode(f, mode)
|
2016-01-04 01:59:42 +03:00
|
|
|
m.mu.Unlock()
|
|
|
|
|
2014-11-01 06:36:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
2016-12-26 08:43:06 +03:00
|
|
|
|
|
|
|
m.mu.RLock()
|
2014-11-01 06:36:45 +03:00
|
|
|
f, ok := m.getData()[name]
|
2016-12-26 08:43:06 +03:00
|
|
|
m.mu.RUnlock()
|
2014-11-01 06:36:45 +03:00
|
|
|
if !ok {
|
2017-01-10 01:53:20 +03:00
|
|
|
return &os.PathError{Op: "chtimes", Path: name, Err: ErrFileNotFound}
|
2014-11-01 06:36:45 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
m.mu.Lock()
|
2016-01-04 02:36:01 +03:00
|
|
|
mem.SetModTime(f, mtime)
|
2016-01-04 01:59:42 +03:00
|
|
|
m.mu.Unlock()
|
|
|
|
|
2014-11-01 06:36:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-12 21:32:57 +03:00
|
|
|
|
|
|
|
func (m *MemMapFs) List() {
|
|
|
|
for _, x := range m.data {
|
2017-01-10 01:53:20 +03:00
|
|
|
y := mem.FileInfo{FileData: x}
|
2016-01-04 02:36:01 +03:00
|
|
|
fmt.Println(x.Name(), y.Size())
|
2014-12-12 21:32:57 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-10 21:01:25 +03:00
|
|
|
|
2016-10-04 06:03:51 +03:00
|
|
|
// func debugMemMapList(fs Fs) {
|
|
|
|
// if x, ok := fs.(*MemMapFs); ok {
|
|
|
|
// x.List()
|
|
|
|
// }
|
|
|
|
// }
|