OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / spf13 / afero / mem / file.go
1 // Copyright © 2015 Steve Francia <spf@spf13.com>.
2 // Copyright 2013 tsuru authors. All rights reserved.
3 //
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
8 //
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.
14
15 package mem
16
17 import (
18         "bytes"
19         "errors"
20         "io"
21         "os"
22         "path/filepath"
23         "sync"
24         "sync/atomic"
25 )
26
27 import "time"
28
29 const FilePathSeparator = string(filepath.Separator)
30
31 type File struct {
32         // atomic requires 64-bit alignment for struct field access
33         at           int64
34         readDirCount int64
35         closed       bool
36         readOnly     bool
37         fileData     *FileData
38 }
39
40 func NewFileHandle(data *FileData) *File {
41         return &File{fileData: data}
42 }
43
44 func NewReadOnlyFileHandle(data *FileData) *File {
45         return &File{fileData: data, readOnly: true}
46 }
47
48 func (f File) Data() *FileData {
49         return f.fileData
50 }
51
52 type FileData struct {
53         sync.Mutex
54         name    string
55         data    []byte
56         memDir  Dir
57         dir     bool
58         mode    os.FileMode
59         modtime time.Time
60 }
61
62 func (d *FileData) Name() string {
63         d.Lock()
64         defer d.Unlock()
65         return d.name
66 }
67
68 func CreateFile(name string) *FileData {
69         return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
70 }
71
72 func CreateDir(name string) *FileData {
73         return &FileData{name: name, memDir: &DirMap{}, dir: true}
74 }
75
76 func ChangeFileName(f *FileData, newname string) {
77         f.Lock()
78         f.name = newname
79         f.Unlock()
80 }
81
82 func SetMode(f *FileData, mode os.FileMode) {
83         f.Lock()
84         f.mode = mode
85         f.Unlock()
86 }
87
88 func SetModTime(f *FileData, mtime time.Time) {
89         f.Lock()
90         setModTime(f, mtime)
91         f.Unlock()
92 }
93
94 func setModTime(f *FileData, mtime time.Time) {
95         f.modtime = mtime
96 }
97
98 func GetFileInfo(f *FileData) *FileInfo {
99         return &FileInfo{f}
100 }
101
102 func (f *File) Open() error {
103         atomic.StoreInt64(&f.at, 0)
104         atomic.StoreInt64(&f.readDirCount, 0)
105         f.fileData.Lock()
106         f.closed = false
107         f.fileData.Unlock()
108         return nil
109 }
110
111 func (f *File) Close() error {
112         f.fileData.Lock()
113         f.closed = true
114         if !f.readOnly {
115                 setModTime(f.fileData, time.Now())
116         }
117         f.fileData.Unlock()
118         return nil
119 }
120
121 func (f *File) Name() string {
122         return f.fileData.Name()
123 }
124
125 func (f *File) Stat() (os.FileInfo, error) {
126         return &FileInfo{f.fileData}, nil
127 }
128
129 func (f *File) Sync() error {
130         return nil
131 }
132
133 func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
134         var outLength int64
135
136         f.fileData.Lock()
137         files := f.fileData.memDir.Files()[f.readDirCount:]
138         if count > 0 {
139                 if len(files) < count {
140                         outLength = int64(len(files))
141                 } else {
142                         outLength = int64(count)
143                 }
144                 if len(files) == 0 {
145                         err = io.EOF
146                 }
147         } else {
148                 outLength = int64(len(files))
149         }
150         f.readDirCount += outLength
151         f.fileData.Unlock()
152
153         res = make([]os.FileInfo, outLength)
154         for i := range res {
155                 res[i] = &FileInfo{files[i]}
156         }
157
158         return res, err
159 }
160
161 func (f *File) Readdirnames(n int) (names []string, err error) {
162         fi, err := f.Readdir(n)
163         names = make([]string, len(fi))
164         for i, f := range fi {
165                 _, names[i] = filepath.Split(f.Name())
166         }
167         return names, err
168 }
169
170 func (f *File) Read(b []byte) (n int, err error) {
171         f.fileData.Lock()
172         defer f.fileData.Unlock()
173         if f.closed == true {
174                 return 0, ErrFileClosed
175         }
176         if len(b) > 0 && int(f.at) == len(f.fileData.data) {
177                 return 0, io.EOF
178         }
179         if len(f.fileData.data)-int(f.at) >= len(b) {
180                 n = len(b)
181         } else {
182                 n = len(f.fileData.data) - int(f.at)
183         }
184         copy(b, f.fileData.data[f.at:f.at+int64(n)])
185         atomic.AddInt64(&f.at, int64(n))
186         return
187 }
188
189 func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
190         atomic.StoreInt64(&f.at, off)
191         return f.Read(b)
192 }
193
194 func (f *File) Truncate(size int64) error {
195         if f.closed == true {
196                 return ErrFileClosed
197         }
198         if f.readOnly {
199                 return &os.PathError{Op: "truncate", Path: f.fileData.name, Err: errors.New("file handle is read only")}
200         }
201         if size < 0 {
202                 return ErrOutOfRange
203         }
204         if size > int64(len(f.fileData.data)) {
205                 diff := size - int64(len(f.fileData.data))
206                 f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...)
207         } else {
208                 f.fileData.data = f.fileData.data[0:size]
209         }
210         setModTime(f.fileData, time.Now())
211         return nil
212 }
213
214 func (f *File) Seek(offset int64, whence int) (int64, error) {
215         if f.closed == true {
216                 return 0, ErrFileClosed
217         }
218         switch whence {
219         case 0:
220                 atomic.StoreInt64(&f.at, offset)
221         case 1:
222                 atomic.AddInt64(&f.at, int64(offset))
223         case 2:
224                 atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
225         }
226         return f.at, nil
227 }
228
229 func (f *File) Write(b []byte) (n int, err error) {
230         if f.readOnly {
231                 return 0, &os.PathError{Op: "write", Path: f.fileData.name, Err: errors.New("file handle is read only")}
232         }
233         n = len(b)
234         cur := atomic.LoadInt64(&f.at)
235         f.fileData.Lock()
236         defer f.fileData.Unlock()
237         diff := cur - int64(len(f.fileData.data))
238         var tail []byte
239         if n+int(cur) < len(f.fileData.data) {
240                 tail = f.fileData.data[n+int(cur):]
241         }
242         if diff > 0 {
243                 f.fileData.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
244                 f.fileData.data = append(f.fileData.data, tail...)
245         } else {
246                 f.fileData.data = append(f.fileData.data[:cur], b...)
247                 f.fileData.data = append(f.fileData.data, tail...)
248         }
249         setModTime(f.fileData, time.Now())
250
251         atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
252         return
253 }
254
255 func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
256         atomic.StoreInt64(&f.at, off)
257         return f.Write(b)
258 }
259
260 func (f *File) WriteString(s string) (ret int, err error) {
261         return f.Write([]byte(s))
262 }
263
264 func (f *File) Info() *FileInfo {
265         return &FileInfo{f.fileData}
266 }
267
268 type FileInfo struct {
269         *FileData
270 }
271
272 // Implements os.FileInfo
273 func (s *FileInfo) Name() string {
274         s.Lock()
275         _, name := filepath.Split(s.name)
276         s.Unlock()
277         return name
278 }
279 func (s *FileInfo) Mode() os.FileMode {
280         s.Lock()
281         defer s.Unlock()
282         return s.mode
283 }
284 func (s *FileInfo) ModTime() time.Time {
285         s.Lock()
286         defer s.Unlock()
287         return s.modtime
288 }
289 func (s *FileInfo) IsDir() bool {
290         s.Lock()
291         defer s.Unlock()
292         return s.dir
293 }
294 func (s *FileInfo) Sys() interface{} { return nil }
295 func (s *FileInfo) Size() int64 {
296         if s.IsDir() {
297                 return int64(42)
298         }
299         s.Lock()
300         defer s.Unlock()
301         return int64(len(s.data))
302 }
303
304 var (
305         ErrFileClosed        = errors.New("File is closed")
306         ErrOutOfRange        = errors.New("Out of range")
307         ErrTooLarge          = errors.New("Too large")
308         ErrFileNotFound      = os.ErrNotExist
309         ErrFileExists        = os.ErrExist
310         ErrDestinationExists = os.ErrExist
311 )