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.
29 const FilePathSeparator = string(filepath.Separator)
32 // atomic requires 64-bit alignment for struct field access
40 func NewFileHandle(data *FileData) *File {
41 return &File{fileData: data}
44 func NewReadOnlyFileHandle(data *FileData) *File {
45 return &File{fileData: data, readOnly: true}
48 func (f File) Data() *FileData {
52 type FileData struct {
62 func (d *FileData) Name() string {
68 func CreateFile(name string) *FileData {
69 return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
72 func CreateDir(name string) *FileData {
73 return &FileData{name: name, memDir: &DirMap{}, dir: true}
76 func ChangeFileName(f *FileData, newname string) {
82 func SetMode(f *FileData, mode os.FileMode) {
88 func SetModTime(f *FileData, mtime time.Time) {
94 func setModTime(f *FileData, mtime time.Time) {
98 func GetFileInfo(f *FileData) *FileInfo {
102 func (f *File) Open() error {
103 atomic.StoreInt64(&f.at, 0)
104 atomic.StoreInt64(&f.readDirCount, 0)
111 func (f *File) Close() error {
115 setModTime(f.fileData, time.Now())
121 func (f *File) Name() string {
122 return f.fileData.Name()
125 func (f *File) Stat() (os.FileInfo, error) {
126 return &FileInfo{f.fileData}, nil
129 func (f *File) Sync() error {
133 func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
137 files := f.fileData.memDir.Files()[f.readDirCount:]
139 if len(files) < count {
140 outLength = int64(len(files))
142 outLength = int64(count)
148 outLength = int64(len(files))
150 f.readDirCount += outLength
153 res = make([]os.FileInfo, outLength)
155 res[i] = &FileInfo{files[i]}
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())
170 func (f *File) Read(b []byte) (n int, err error) {
172 defer f.fileData.Unlock()
173 if f.closed == true {
174 return 0, ErrFileClosed
176 if len(b) > 0 && int(f.at) == len(f.fileData.data) {
179 if len(f.fileData.data)-int(f.at) >= len(b) {
182 n = len(f.fileData.data) - int(f.at)
184 copy(b, f.fileData.data[f.at:f.at+int64(n)])
185 atomic.AddInt64(&f.at, int64(n))
189 func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
190 atomic.StoreInt64(&f.at, off)
194 func (f *File) Truncate(size int64) error {
195 if f.closed == true {
199 return &os.PathError{Op: "truncate", Path: f.fileData.name, Err: errors.New("file handle is read only")}
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))...)
208 f.fileData.data = f.fileData.data[0:size]
210 setModTime(f.fileData, time.Now())
214 func (f *File) Seek(offset int64, whence int) (int64, error) {
215 if f.closed == true {
216 return 0, ErrFileClosed
220 atomic.StoreInt64(&f.at, offset)
222 atomic.AddInt64(&f.at, int64(offset))
224 atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
229 func (f *File) Write(b []byte) (n int, err error) {
231 return 0, &os.PathError{Op: "write", Path: f.fileData.name, Err: errors.New("file handle is read only")}
234 cur := atomic.LoadInt64(&f.at)
236 defer f.fileData.Unlock()
237 diff := cur - int64(len(f.fileData.data))
239 if n+int(cur) < len(f.fileData.data) {
240 tail = f.fileData.data[n+int(cur):]
243 f.fileData.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
244 f.fileData.data = append(f.fileData.data, tail...)
246 f.fileData.data = append(f.fileData.data[:cur], b...)
247 f.fileData.data = append(f.fileData.data, tail...)
249 setModTime(f.fileData, time.Now())
251 atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
255 func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
256 atomic.StoreInt64(&f.at, off)
260 func (f *File) WriteString(s string) (ret int, err error) {
261 return f.Write([]byte(s))
264 func (f *File) Info() *FileInfo {
265 return &FileInfo{f.fileData}
268 type FileInfo struct {
272 // Implements os.FileInfo
273 func (s *FileInfo) Name() string {
275 _, name := filepath.Split(s.name)
279 func (s *FileInfo) Mode() os.FileMode {
284 func (s *FileInfo) ModTime() time.Time {
289 func (s *FileInfo) IsDir() bool {
294 func (s *FileInfo) Sys() interface{} { return nil }
295 func (s *FileInfo) Size() int64 {
301 return int64(len(s.data))
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