1
// Copyright © 2015 Steve Francia <spf@spf13.com>.
2
// Copyright 2013 tsuru authors. All rights reserved.
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
28
"github.com/spf13/afero/internal/common"
31
const FilePathSeparator = string(filepath.Separator)
33
var _ fs.ReadDirFile = &File{}
36
// atomic requires 64-bit alignment for struct field access
44
func NewFileHandle(data *FileData) *File {
45
return &File{fileData: data}
48
func NewReadOnlyFileHandle(data *FileData) *File {
49
return &File{fileData: data, readOnly: true}
52
func (f File) Data() *FileData {
68
func (d *FileData) Name() string {
74
func CreateFile(name string) *FileData {
75
return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
78
func CreateDir(name string) *FileData {
79
return &FileData{name: name, memDir: &DirMap{}, dir: true, modtime: time.Now()}
82
func ChangeFileName(f *FileData, newname string) {
88
func SetMode(f *FileData, mode os.FileMode) {
94
func SetModTime(f *FileData, mtime time.Time) {
100
func setModTime(f *FileData, mtime time.Time) {
104
func SetUID(f *FileData, uid int) {
110
func SetGID(f *FileData, gid int) {
116
func GetFileInfo(f *FileData) *FileInfo {
120
func (f *File) Open() error {
121
atomic.StoreInt64(&f.at, 0)
122
atomic.StoreInt64(&f.readDirCount, 0)
129
func (f *File) Close() error {
133
setModTime(f.fileData, time.Now())
139
func (f *File) Name() string {
140
return f.fileData.Name()
143
func (f *File) Stat() (os.FileInfo, error) {
144
return &FileInfo{f.fileData}, nil
147
func (f *File) Sync() error {
151
func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
153
return nil, &os.PathError{Op: "readdir", Path: f.fileData.name, Err: errors.New("not a dir")}
158
files := f.fileData.memDir.Files()[f.readDirCount:]
160
if len(files) < count {
161
outLength = int64(len(files))
163
outLength = int64(count)
169
outLength = int64(len(files))
171
f.readDirCount += outLength
174
res = make([]os.FileInfo, outLength)
176
res[i] = &FileInfo{files[i]}
182
func (f *File) Readdirnames(n int) (names []string, err error) {
183
fi, err := f.Readdir(n)
184
names = make([]string, len(fi))
185
for i, f := range fi {
186
_, names[i] = filepath.Split(f.Name())
191
// Implements fs.ReadDirFile
192
func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
193
fi, err := f.Readdir(n)
197
di := make([]fs.DirEntry, len(fi))
198
for i, f := range fi {
199
di[i] = common.FileInfoDirEntry{FileInfo: f}
204
func (f *File) Read(b []byte) (n int, err error) {
206
defer f.fileData.Unlock()
208
return 0, ErrFileClosed
210
if len(b) > 0 && int(f.at) == len(f.fileData.data) {
213
if int(f.at) > len(f.fileData.data) {
214
return 0, io.ErrUnexpectedEOF
216
if len(f.fileData.data)-int(f.at) >= len(b) {
219
n = len(f.fileData.data) - int(f.at)
221
copy(b, f.fileData.data[f.at:f.at+int64(n)])
222
atomic.AddInt64(&f.at, int64(n))
226
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
227
prev := atomic.LoadInt64(&f.at)
228
atomic.StoreInt64(&f.at, off)
230
atomic.StoreInt64(&f.at, prev)
234
func (f *File) Truncate(size int64) error {
239
return &os.PathError{Op: "truncate", Path: f.fileData.name, Err: errors.New("file handle is read only")}
245
defer f.fileData.Unlock()
246
if size > int64(len(f.fileData.data)) {
247
diff := size - int64(len(f.fileData.data))
248
f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{0o0}, int(diff))...)
250
f.fileData.data = f.fileData.data[0:size]
252
setModTime(f.fileData, time.Now())
256
func (f *File) Seek(offset int64, whence int) (int64, error) {
258
return 0, ErrFileClosed
262
atomic.StoreInt64(&f.at, offset)
264
atomic.AddInt64(&f.at, offset)
266
atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
271
func (f *File) Write(b []byte) (n int, err error) {
273
return 0, ErrFileClosed
276
return 0, &os.PathError{Op: "write", Path: f.fileData.name, Err: errors.New("file handle is read only")}
279
cur := atomic.LoadInt64(&f.at)
281
defer f.fileData.Unlock()
282
diff := cur - int64(len(f.fileData.data))
284
if n+int(cur) < len(f.fileData.data) {
285
tail = f.fileData.data[n+int(cur):]
288
f.fileData.data = append(f.fileData.data, append(bytes.Repeat([]byte{0o0}, int(diff)), b...)...)
289
f.fileData.data = append(f.fileData.data, tail...)
291
f.fileData.data = append(f.fileData.data[:cur], b...)
292
f.fileData.data = append(f.fileData.data, tail...)
294
setModTime(f.fileData, time.Now())
296
atomic.AddInt64(&f.at, int64(n))
300
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
301
atomic.StoreInt64(&f.at, off)
305
func (f *File) WriteString(s string) (ret int, err error) {
306
return f.Write([]byte(s))
309
func (f *File) Info() *FileInfo {
310
return &FileInfo{f.fileData}
313
type FileInfo struct {
317
// Implements os.FileInfo
318
func (s *FileInfo) Name() string {
320
_, name := filepath.Split(s.name)
325
func (s *FileInfo) Mode() os.FileMode {
331
func (s *FileInfo) ModTime() time.Time {
337
func (s *FileInfo) IsDir() bool {
342
func (s *FileInfo) Sys() interface{} { return nil }
343
func (s *FileInfo) Size() int64 {
349
return int64(len(s.data))
353
ErrFileClosed = errors.New("File is closed")
354
ErrOutOfRange = errors.New("out of range")
355
ErrTooLarge = errors.New("too large")
356
ErrFileNotFound = os.ErrNotExist
357
ErrFileExists = os.ErrExist
358
ErrDestinationExists = os.ErrExist