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-11-01 06:36:45 +03:00
|
|
|
"errors"
|
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"
|
|
|
|
"path/filepath"
|
2015-11-04 22:33:40 +03:00
|
|
|
"sort"
|
2014-10-28 17:29:28 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-11-01 06:36:45 +03:00
|
|
|
"time"
|
2014-10-28 17:29:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var mux = &sync.Mutex{}
|
|
|
|
|
|
|
|
type MemMapFs struct {
|
|
|
|
data map[string]File
|
|
|
|
mutex *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) lock() {
|
|
|
|
mx := m.getMutex()
|
|
|
|
mx.Lock()
|
|
|
|
}
|
2015-12-04 06:28:21 +03:00
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
func (m *MemMapFs) unlock() { m.getMutex().Unlock() }
|
|
|
|
func (m *MemMapFs) rlock() { m.getMutex().RLock() }
|
|
|
|
func (m *MemMapFs) runlock() { m.getMutex().RUnlock() }
|
|
|
|
|
|
|
|
func (m *MemMapFs) getData() map[string]File {
|
|
|
|
if m.data == nil {
|
|
|
|
m.data = make(map[string]File)
|
2015-12-03 00:27:14 +03:00
|
|
|
|
|
|
|
// Root should always exist, right?
|
|
|
|
// TODO: what about windows?
|
2015-12-04 06:29:31 +03:00
|
|
|
m.data["/"] = &InMemoryFile{name: "/", memDir: &MemDirMap{}, dir: true}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
return m.data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) getMutex() *sync.RWMutex {
|
|
|
|
mux.Lock()
|
|
|
|
if m.mutex == nil {
|
|
|
|
m.mutex = &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)
|
|
|
|
}
|
2015-11-04 22:33:40 +03:00
|
|
|
sort.Sort(filesSorter(files))
|
2014-10-28 17:29:28 +03:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2015-11-04 22:33:40 +03:00
|
|
|
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() }
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
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) {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
2014-10-28 17:29:28 +03:00
|
|
|
m.lock()
|
2015-06-11 21:49:27 +03:00
|
|
|
file := MemFileCreate(name)
|
|
|
|
m.getData()[name] = file
|
2015-10-17 13:58:11 +03:00
|
|
|
m.registerWithParent(file)
|
2014-10-28 17:29:28 +03:00
|
|
|
m.unlock()
|
2015-06-11 21:49:27 +03:00
|
|
|
return file, nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-10-17 13:58:11 +03:00
|
|
|
func (m *MemMapFs) unRegisterWithParent(fileName string) {
|
|
|
|
f, err := m.lockfreeOpen(fileName)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Println("Open err:", err)
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2015-10-17 13:58:11 +03:00
|
|
|
return
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
parent := m.findParent(f)
|
2015-11-04 22:33:40 +03:00
|
|
|
if parent == nil {
|
|
|
|
log.Fatal("parent of ", f.Name(), " is nil")
|
|
|
|
}
|
2014-10-28 17:29:28 +03:00
|
|
|
pmem := parent.(*InMemoryFile)
|
|
|
|
pmem.memDir.Remove(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) findParent(f File) File {
|
2015-10-17 13:58:11 +03:00
|
|
|
pdir, _ := path.Split(f.Name())
|
|
|
|
pdir = path.Clean(pdir)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-10-17 13:58:11 +03:00
|
|
|
func (m *MemMapFs) registerWithParent(f File) {
|
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
|
|
|
}
|
|
|
|
parent := m.findParent(f)
|
2015-10-17 13:58:11 +03:00
|
|
|
if parent == nil {
|
2014-10-28 17:29:28 +03:00
|
|
|
pdir := filepath.Dir(path.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
|
|
|
}
|
2015-10-17 13:58:11 +03:00
|
|
|
pmem := parent.(*InMemoryFile)
|
2015-11-07 03:00:03 +03:00
|
|
|
|
|
|
|
// 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{}
|
|
|
|
}
|
|
|
|
|
2015-10-17 13:58:11 +03:00
|
|
|
pmem.memDir.Add(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
i, err := x.Stat()
|
|
|
|
if !i.IsDir() {
|
|
|
|
return ErrFileExists
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-17 13:58:11 +03:00
|
|
|
} else {
|
|
|
|
item := &InMemoryFile{name: name, memDir: &MemDirMap{}, dir: true}
|
|
|
|
m.getData()[name] = item
|
|
|
|
m.registerWithParent(item)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
m.rlock()
|
2015-11-17 05:26:16 +03:00
|
|
|
x, ok := m.getData()[name]
|
2014-10-28 17:29:28 +03:00
|
|
|
m.runlock()
|
|
|
|
if ok {
|
2015-11-17 05:26:16 +03:00
|
|
|
// Only return ErrFileExists if it's a file, not a directory.
|
|
|
|
i, err := x.Stat()
|
|
|
|
if !i.IsDir() {
|
|
|
|
return ErrFileExists
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-28 17:29:28 +03:00
|
|
|
} else {
|
|
|
|
m.lock()
|
2015-06-11 21:49:27 +03:00
|
|
|
item := &InMemoryFile{name: name, memDir: &MemDirMap{}, dir: true}
|
|
|
|
m.getData()[name] = item
|
2015-10-17 13:58:11 +03:00
|
|
|
m.registerWithParent(item)
|
2014-10-28 17:29:28 +03:00
|
|
|
m.unlock()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) MkdirAll(path string, perm os.FileMode) error {
|
2015-12-04 06:27:28 +03:00
|
|
|
return m.Mkdir(path, perm)
|
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 ".":
|
|
|
|
return "/"
|
|
|
|
case "..":
|
|
|
|
return "/"
|
|
|
|
default:
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
func (m *MemMapFs) Open(name string) (File, error) {
|
2015-12-03 00:28:58 +03:00
|
|
|
name = normalizePath(name)
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
m.rlock()
|
|
|
|
f, ok := m.getData()[name]
|
2014-11-01 06:38:54 +03:00
|
|
|
ff, ok := f.(*InMemoryFile)
|
2015-12-04 06:28:21 +03:00
|
|
|
|
2014-11-01 06:38:54 +03:00
|
|
|
if ok {
|
|
|
|
ff.Open()
|
|
|
|
}
|
2014-10-28 17:29:28 +03:00
|
|
|
m.runlock()
|
2014-11-01 06:38:54 +03:00
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
if ok {
|
|
|
|
return f, nil
|
|
|
|
} else {
|
|
|
|
return nil, ErrFileNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-17 13:58:11 +03:00
|
|
|
func (m *MemMapFs) lockfreeOpen(name string) (File, 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]
|
|
|
|
ff, ok := f.(*InMemoryFile)
|
|
|
|
if ok {
|
|
|
|
ff.Open()
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return f, nil
|
|
|
|
} 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) {
|
2015-08-08 20:50:19 +03:00
|
|
|
file, err := m.Open(name)
|
|
|
|
if os.IsNotExist(err) && (flag&os.O_CREATE > 0) {
|
|
|
|
file, err = m.Create(name)
|
|
|
|
}
|
2015-08-08 20:55:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
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-08-09 00:47:18 +03:00
|
|
|
m.lock()
|
|
|
|
defer m.unlock()
|
2014-10-28 17:29:28 +03:00
|
|
|
|
2015-08-09 00:47:18 +03:00
|
|
|
if _, ok := m.getData()[name]; ok {
|
2015-10-17 13:58:11 +03:00
|
|
|
m.unRegisterWithParent(name)
|
2014-10-28 17:29:28 +03:00
|
|
|
delete(m.getData(), name)
|
2015-08-09 00:55:14 +03:00
|
|
|
} else {
|
|
|
|
return &os.PathError{"remove", name, 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-11-04 22:33:40 +03:00
|
|
|
m.lock()
|
|
|
|
m.unRegisterWithParent(path)
|
|
|
|
m.unlock()
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
m.rlock()
|
|
|
|
defer m.runlock()
|
2015-10-17 13:58:11 +03:00
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
for p, _ := range m.getData() {
|
|
|
|
if strings.HasPrefix(p, path) {
|
|
|
|
m.runlock()
|
|
|
|
m.lock()
|
|
|
|
delete(m.getData(), p)
|
|
|
|
m.unlock()
|
|
|
|
m.rlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-10-28 17:29:28 +03:00
|
|
|
m.rlock()
|
|
|
|
defer m.runlock()
|
|
|
|
if _, ok := m.getData()[oldname]; ok {
|
|
|
|
if _, ok := m.getData()[newname]; !ok {
|
|
|
|
m.runlock()
|
|
|
|
m.lock()
|
2015-11-22 16:34:57 +03:00
|
|
|
m.unRegisterWithParent(oldname)
|
|
|
|
file := m.getData()[oldname].(*InMemoryFile)
|
2014-10-28 17:29:28 +03:00
|
|
|
delete(m.getData(), oldname)
|
2015-11-22 16:34:57 +03:00
|
|
|
file.name = newname
|
|
|
|
m.getData()[newname] = file
|
|
|
|
m.registerWithParent(file)
|
2014-10-28 17:29:28 +03:00
|
|
|
m.unlock()
|
|
|
|
m.rlock()
|
|
|
|
} else {
|
|
|
|
return ErrDestinationExists
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ErrFileNotFound
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemMapFs) Stat(name string) (os.FileInfo, error) {
|
|
|
|
f, err := m.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &InMemoryFileInfo{file: f.(*InMemoryFile)}, nil
|
|
|
|
}
|
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)
|
2014-11-01 06:36:45 +03:00
|
|
|
f, ok := m.getData()[name]
|
|
|
|
if !ok {
|
|
|
|
return &os.PathError{"chmod", name, ErrFileNotFound}
|
|
|
|
}
|
|
|
|
|
|
|
|
ff, ok := f.(*InMemoryFile)
|
|
|
|
if ok {
|
|
|
|
m.lock()
|
|
|
|
ff.mode = mode
|
|
|
|
m.unlock()
|
|
|
|
} else {
|
|
|
|
return errors.New("Unable to Chmod Memory File")
|
|
|
|
}
|
|
|
|
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)
|
2014-11-01 06:36:45 +03:00
|
|
|
f, ok := m.getData()[name]
|
|
|
|
if !ok {
|
|
|
|
return &os.PathError{"chtimes", name, ErrFileNotFound}
|
|
|
|
}
|
|
|
|
|
|
|
|
ff, ok := f.(*InMemoryFile)
|
|
|
|
if ok {
|
|
|
|
m.lock()
|
|
|
|
ff.modtime = mtime
|
|
|
|
m.unlock()
|
|
|
|
} else {
|
|
|
|
return errors.New("Unable to Chtime Memory File")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-12 21:32:57 +03:00
|
|
|
|
|
|
|
func (m *MemMapFs) List() {
|
|
|
|
for _, x := range m.data {
|
|
|
|
y, _ := x.Stat()
|
|
|
|
fmt.Println(x.Name(), y.Size())
|
|
|
|
}
|
|
|
|
}
|