2015-12-10 20:25:02 +03:00
|
|
|
// Copyright © 2015 Steve Francia <spf@spf13.com>.
|
2014-10-28 17:29:28 +03:00
|
|
|
// Copyright 2013 tsuru authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
package mem
|
2014-10-28 17:29:28 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-12-10 20:25:02 +03:00
|
|
|
"errors"
|
2014-10-28 17:29:28 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
2015-12-09 23:37:04 +03:00
|
|
|
"path/filepath"
|
2015-03-22 03:24:08 +03:00
|
|
|
"sync"
|
2014-10-28 17:29:28 +03:00
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
2015-12-10 21:00:25 +03:00
|
|
|
const FilePathSeparator = string(filepath.Separator)
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
type File struct {
|
2015-11-26 07:04:28 +03:00
|
|
|
// atomic requires 64-bit alignment for struct field access
|
2015-11-04 22:33:40 +03:00
|
|
|
at int64
|
|
|
|
readDirCount int64
|
2016-01-04 01:59:42 +03:00
|
|
|
closed bool
|
|
|
|
fileData *FileData
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileHandle(data *FileData) *File {
|
|
|
|
return &File{fileData: data}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) Data() *FileData {
|
|
|
|
return f.fileData
|
|
|
|
}
|
2015-11-26 07:04:28 +03:00
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
type FileData struct {
|
2015-12-10 20:55:43 +03:00
|
|
|
sync.Mutex
|
2015-12-10 21:00:25 +03:00
|
|
|
name string
|
|
|
|
data []byte
|
|
|
|
memDir Dir
|
|
|
|
dir bool
|
|
|
|
mode os.FileMode
|
|
|
|
modtime time.Time
|
|
|
|
}
|
|
|
|
|
2016-01-04 02:18:26 +03:00
|
|
|
func (d FileData) Name() string {
|
|
|
|
return d.name
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
func CreateFile(name string) *FileData {
|
|
|
|
return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
|
2015-12-10 21:00:25 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
func CreateDir(name string) *FileData {
|
|
|
|
return &FileData{name: name, memDir: &DirMap{}, dir: true}
|
2015-12-10 21:00:25 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:36:01 +03:00
|
|
|
func ChangeFileName(f *FileData, newname string) {
|
|
|
|
f.name = newname
|
2015-12-10 21:00:25 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:36:01 +03:00
|
|
|
func SetMode(f *FileData, mode os.FileMode) {
|
|
|
|
f.mode = mode
|
2015-12-10 21:00:25 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:36:01 +03:00
|
|
|
func SetModTime(f *FileData, mtime time.Time) {
|
|
|
|
f.modtime = mtime
|
2015-12-10 20:25:02 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 02:36:01 +03:00
|
|
|
func GetFileInfo(f *FileData) *FileInfo {
|
|
|
|
return &FileInfo{f}
|
2015-12-10 20:25:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *File) Open() error {
|
2014-11-01 06:38:54 +03:00
|
|
|
atomic.StoreInt64(&f.at, 0)
|
2015-11-04 22:33:40 +03:00
|
|
|
atomic.StoreInt64(&f.readDirCount, 0)
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Lock()
|
2014-11-01 06:38:54 +03:00
|
|
|
f.closed = false
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Unlock()
|
2014-11-01 06:38:54 +03:00
|
|
|
return nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Close() error {
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Lock()
|
2014-10-28 17:29:28 +03:00
|
|
|
f.closed = true
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Unlock()
|
2014-10-28 17:29:28 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Name() string {
|
2016-01-04 01:59:42 +03:00
|
|
|
return f.fileData.name
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Stat() (os.FileInfo, error) {
|
2016-01-04 01:59:42 +03:00
|
|
|
return &FileInfo{f.fileData}, nil
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Sync() error {
|
2015-11-05 16:36:41 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
|
2015-11-04 22:33:40 +03:00
|
|
|
var outLength int64
|
2014-10-28 17:29:28 +03:00
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Lock()
|
|
|
|
files := f.fileData.memDir.Files()[f.readDirCount:]
|
2014-10-28 17:29:28 +03:00
|
|
|
if count > 0 {
|
2015-11-04 22:33:40 +03:00
|
|
|
if len(files) < count {
|
|
|
|
outLength = int64(len(files))
|
|
|
|
} else {
|
|
|
|
outLength = int64(count)
|
|
|
|
}
|
|
|
|
if len(files) == 0 {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
outLength = int64(len(files))
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2015-11-04 22:33:40 +03:00
|
|
|
f.readDirCount += outLength
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Unlock()
|
2014-10-28 17:29:28 +03:00
|
|
|
|
2015-11-04 22:33:40 +03:00
|
|
|
res = make([]os.FileInfo, outLength)
|
|
|
|
for i := range res {
|
2016-01-04 02:18:26 +03:00
|
|
|
res[i] = &FileInfo{files[i]}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-11-04 22:33:40 +03:00
|
|
|
return res, err
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Readdirnames(n int) (names []string, err error) {
|
2014-10-28 17:29:28 +03:00
|
|
|
fi, err := f.Readdir(n)
|
|
|
|
names = make([]string, len(fi))
|
|
|
|
for i, f := range fi {
|
2015-12-09 23:37:04 +03:00
|
|
|
_, names[i] = filepath.Split(f.Name())
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
return names, err
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Read(b []byte) (n int, err error) {
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Lock()
|
|
|
|
defer f.fileData.Unlock()
|
2014-10-28 17:29:28 +03:00
|
|
|
if f.closed == true {
|
|
|
|
return 0, ErrFileClosed
|
|
|
|
}
|
2016-01-04 01:59:42 +03:00
|
|
|
if len(b) > 0 && int(f.at) == len(f.fileData.data) {
|
2015-03-22 03:24:08 +03:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2016-01-04 01:59:42 +03:00
|
|
|
if len(f.fileData.data)-int(f.at) >= len(b) {
|
2014-10-28 17:29:28 +03:00
|
|
|
n = len(b)
|
|
|
|
} else {
|
2016-01-04 01:59:42 +03:00
|
|
|
n = len(f.fileData.data) - int(f.at)
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
2016-01-04 01:59:42 +03:00
|
|
|
copy(b, f.fileData.data[f.at:f.at+int64(n)])
|
2014-10-28 17:29:28 +03:00
|
|
|
atomic.AddInt64(&f.at, int64(n))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
|
2014-10-28 17:29:28 +03:00
|
|
|
atomic.StoreInt64(&f.at, off)
|
|
|
|
return f.Read(b)
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Truncate(size int64) error {
|
2014-10-28 17:29:28 +03:00
|
|
|
if f.closed == true {
|
|
|
|
return ErrFileClosed
|
|
|
|
}
|
|
|
|
if size < 0 {
|
|
|
|
return ErrOutOfRange
|
|
|
|
}
|
2016-01-04 01:59:42 +03:00
|
|
|
if size > int64(len(f.fileData.data)) {
|
|
|
|
diff := size - int64(len(f.fileData.data))
|
|
|
|
f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...)
|
2014-10-28 17:29:28 +03:00
|
|
|
} else {
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.data = f.fileData.data[0:size]
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Seek(offset int64, whence int) (int64, error) {
|
2014-10-28 17:29:28 +03:00
|
|
|
if f.closed == true {
|
|
|
|
return 0, ErrFileClosed
|
|
|
|
}
|
|
|
|
switch whence {
|
|
|
|
case 0:
|
|
|
|
atomic.StoreInt64(&f.at, offset)
|
|
|
|
case 1:
|
|
|
|
atomic.AddInt64(&f.at, int64(offset))
|
|
|
|
case 2:
|
2016-01-04 01:59:42 +03:00
|
|
|
atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
return f.at, nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Write(b []byte) (n int, err error) {
|
2014-10-28 17:29:28 +03:00
|
|
|
n = len(b)
|
|
|
|
cur := atomic.LoadInt64(&f.at)
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.Lock()
|
|
|
|
defer f.fileData.Unlock()
|
|
|
|
diff := cur - int64(len(f.fileData.data))
|
2014-10-28 17:29:28 +03:00
|
|
|
var tail []byte
|
2016-01-04 01:59:42 +03:00
|
|
|
if n+int(cur) < len(f.fileData.data) {
|
|
|
|
tail = f.fileData.data[n+int(cur):]
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
if diff > 0 {
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
|
|
|
|
f.fileData.data = append(f.fileData.data, tail...)
|
2014-10-28 17:29:28 +03:00
|
|
|
} else {
|
2016-01-04 01:59:42 +03:00
|
|
|
f.fileData.data = append(f.fileData.data[:cur], b...)
|
|
|
|
f.fileData.data = append(f.fileData.data, tail...)
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 01:59:42 +03:00
|
|
|
atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
|
2014-10-28 17:29:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
|
2014-10-28 17:29:28 +03:00
|
|
|
atomic.StoreInt64(&f.at, off)
|
|
|
|
return f.Write(b)
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) WriteString(s string) (ret int, err error) {
|
2014-10-28 17:29:28 +03:00
|
|
|
return f.Write([]byte(s))
|
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
func (f *File) Info() *FileInfo {
|
2016-01-04 01:59:42 +03:00
|
|
|
return &FileInfo{f.fileData}
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
2015-12-10 20:25:02 +03:00
|
|
|
type FileInfo struct {
|
2016-01-04 01:59:42 +03:00
|
|
|
*FileData
|
2014-10-28 17:29:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements os.FileInfo
|
2015-12-10 20:25:02 +03:00
|
|
|
func (s *FileInfo) Name() string {
|
2016-01-04 01:59:42 +03:00
|
|
|
_, name := filepath.Split(s.name)
|
2015-10-17 14:02:31 +03:00
|
|
|
return name
|
|
|
|
}
|
2016-01-04 01:59:42 +03:00
|
|
|
func (s *FileInfo) Mode() os.FileMode { return s.mode }
|
|
|
|
func (s *FileInfo) ModTime() time.Time { return s.modtime }
|
|
|
|
func (s *FileInfo) IsDir() bool { return s.dir }
|
2015-12-10 20:25:02 +03:00
|
|
|
func (s *FileInfo) Sys() interface{} { return nil }
|
|
|
|
func (s *FileInfo) Size() int64 {
|
2014-11-01 06:39:14 +03:00
|
|
|
if s.IsDir() {
|
|
|
|
return int64(42)
|
|
|
|
}
|
2016-01-04 01:59:42 +03:00
|
|
|
return int64(len(s.data))
|
2014-11-01 06:39:14 +03:00
|
|
|
}
|
2015-12-10 20:25:02 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|