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) {
135 return nil, &os.PathError{Op: "readdir", Path: f.fileData.name, Err: errors.New("not a dir")}
140 files := f.fileData.memDir.Files()[f.readDirCount:]
142 if len(files) < count {
143 outLength = int64(len(files))
145 outLength = int64(count)
151 outLength = int64(len(files))
153 f.readDirCount += outLength
156 res = make([]os.FileInfo, outLength)
158 res[i] = &FileInfo{files[i]}
164 func (f *File) Readdirnames(n int) (names []string, err error) {
165 fi, err := f.Readdir(n)
166 names = make([]string, len(fi))
167 for i, f := range fi {
168 _, names[i] = filepath.Split(f.Name())
173 func (f *File) Read(b []byte) (n int, err error) {
175 defer f.fileData.Unlock()
176 if f.closed == true {
177 return 0, ErrFileClosed
179 if len(b) > 0 && int(f.at) == len(f.fileData.data) {
182 if int(f.at) > len(f.fileData.data) {
183 return 0, io.ErrUnexpectedEOF
185 if len(f.fileData.data)-int(f.at) >= len(b) {
188 n = len(f.fileData.data) - int(f.at)
190 copy(b, f.fileData.data[f.at:f.at+int64(n)])
191 atomic.AddInt64(&f.at, int64(n))
195 func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
196 atomic.StoreInt64(&f.at, off)
200 func (f *File) Truncate(size int64) error {
201 if f.closed == true {
205 return &os.PathError{Op: "truncate", Path: f.fileData.name, Err: errors.New("file handle is read only")}
210 if size > int64(len(f.fileData.data)) {
211 diff := size - int64(len(f.fileData.data))
212 f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...)
214 f.fileData.data = f.fileData.data[0:size]
216 setModTime(f.fileData, time.Now())
220 func (f *File) Seek(offset int64, whence int) (int64, error) {
221 if f.closed == true {
222 return 0, ErrFileClosed
226 atomic.StoreInt64(&f.at, offset)
228 atomic.AddInt64(&f.at, int64(offset))
230 atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
235 func (f *File) Write(b []byte) (n int, err error) {
237 return 0, &os.PathError{Op: "write", Path: f.fileData.name, Err: errors.New("file handle is read only")}
240 cur := atomic.LoadInt64(&f.at)
242 defer f.fileData.Unlock()
243 diff := cur - int64(len(f.fileData.data))
245 if n+int(cur) < len(f.fileData.data) {
246 tail = f.fileData.data[n+int(cur):]
249 f.fileData.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
250 f.fileData.data = append(f.fileData.data, tail...)
252 f.fileData.data = append(f.fileData.data[:cur], b...)
253 f.fileData.data = append(f.fileData.data, tail...)
255 setModTime(f.fileData, time.Now())
257 atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
261 func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
262 atomic.StoreInt64(&f.at, off)
266 func (f *File) WriteString(s string) (ret int, err error) {
267 return f.Write([]byte(s))
270 func (f *File) Info() *FileInfo {
271 return &FileInfo{f.fileData}
274 type FileInfo struct {
278 // Implements os.FileInfo
279 func (s *FileInfo) Name() string {
281 _, name := filepath.Split(s.name)
285 func (s *FileInfo) Mode() os.FileMode {
290 func (s *FileInfo) ModTime() time.Time {
295 func (s *FileInfo) IsDir() bool {
300 func (s *FileInfo) Sys() interface{} { return nil }
301 func (s *FileInfo) Size() int64 {
307 return int64(len(s.data))
311 ErrFileClosed = errors.New("File is closed")
312 ErrOutOfRange = errors.New("Out of range")
313 ErrTooLarge = errors.New("Too large")
314 ErrFileNotFound = os.ErrNotExist
315 ErrFileExists = os.ErrExist
316 ErrDestinationExists = os.ErrExist