-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
231 lines (191 loc) · 4.68 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package dfs
import (
"bytes"
"errors"
"io"
"os"
"time"
"path/filepath"
"sync/atomic"
"github.com/spf13/afero"
)
type (
// File represents a file handle within
// a datastore filesystem session
File struct {
// atomic requires 64-bit alignment for struct field access
at int64
readDirCount int64
closed bool
readOnly bool
fileData *FileData
fs *FileSystem
}
)
// implements afero.File
var _ afero.File = (*File)(nil)
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
)
// NewFileHandle initializes a File object
func NewFileHandle(fs *FileSystem, fileData *FileData) *File {
return &File{
fs: fs,
fileData: fileData,
}
}
// NewReadOnlyFileHandle initializes a File object
func NewReadOnlyFileHandle(fs *FileSystem, fileData *FileData) *File {
return &File{
readOnly: true,
fs: fs,
fileData: fileData,
}
}
func (f *File) Open() error {
atomic.StoreInt64(&f.at, 0)
atomic.StoreInt64(&f.readDirCount, 0)
f.fileData.Lock()
f.closed = false
f.fileData.Unlock()
return nil
}
func (f *File) Close() error {
f.fileData.Lock()
defer f.fileData.Unlock()
f.closed = true
if f.fileData.dirty {
f.fileData.ModTime = time.Now()
f.fileData.Size = int64(len(f.fileData.Data))
f.fileData.dirty = false
return f.fs.saveFileData(f.fileData)
}
return nil
}
// Name returns the filename
func (f *File) Name() string {
return f.fileData.name
}
func (f *File) Stat() (os.FileInfo, error) {
return &FileInfo{f.fileData}, nil
}
func (f *File) Sync() error {
return nil
}
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
logger.Println("Readdir", count, f.readDirCount)
files, err := f.fs.readDir(f.fileData.name, int(f.readDirCount), count)
if err != nil {
return nil, err
}
if len(files) == 0 && count > 0 {
return nil, io.EOF
}
f.readDirCount += int64(len(files))
return files, nil
}
func (f *File) Readdirnames(n int) ([]string, error) {
logger.Println("Readdirnames", n, f.readDirCount)
fi, err := f.Readdir(n)
if err != nil {
return nil, err
}
names := make([]string, len(fi))
for i, f := range fi {
_, names[i] = filepath.Split(f.Name())
}
return names, err
}
func (f *File) Read(data []byte) (int, error) {
logger.Println("Read", len(data))
f.fileData.Lock()
defer f.fileData.Unlock()
if f.closed {
return 0, ErrFileClosed
}
if len(data) > 0 && int(f.at) == len(f.fileData.Data) {
return 0, io.EOF
}
var n int
if len(f.fileData.Data)-int(f.at) >= len(data) {
n = len(data)
} else {
n = len(f.fileData.Data) - int(f.at)
}
copy(data, f.fileData.Data[f.at:f.at+int64(n)])
atomic.AddInt64(&f.at, int64(n))
return n, nil
}
func (f *File) ReadAt(data []byte, off int64) (int, error) {
atomic.StoreInt64(&f.at, off)
return f.Read(data)
}
func (f *File) Truncate(size int64) error {
f.fileData.Lock()
defer f.fileData.Unlock()
if f.closed == true {
return ErrFileClosed
}
if size < 0 {
return ErrOutOfRange
}
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))...)
} else {
f.fileData.Data = f.fileData.Data[0:size]
}
f.fileData.ModTime = time.Now()
f.fileData.dirty = true
return nil
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
logger.Println("Seek", offset, whence)
if f.closed {
return 0, ErrFileClosed
}
switch whence {
case 0:
atomic.StoreInt64(&f.at, offset)
case 1:
atomic.AddInt64(&f.at, int64(offset))
case 2:
atomic.StoreInt64(&f.at, int64(len(f.fileData.Data))+offset)
}
return f.at, nil
}
func (f *File) Write(data []byte) (int, error) {
logger.Println("Write", len(data))
n := len(data)
cur := atomic.LoadInt64(&f.at)
f.fileData.Lock()
defer f.fileData.Unlock()
diff := cur - int64(len(f.fileData.Data))
var tail []byte
if n+int(cur) < len(f.fileData.Data) {
tail = f.fileData.Data[n+int(cur):]
}
if diff > 0 {
f.fileData.Data = append(bytes.Repeat([]byte{00}, int(diff)), data...)
f.fileData.Data = append(f.fileData.Data, tail...)
} else {
f.fileData.Data = append(f.fileData.Data[:cur], data...)
f.fileData.Data = append(f.fileData.Data, tail...)
}
atomic.StoreInt64(&f.at, int64(len(f.fileData.Data)))
f.fileData.ModTime = time.Now()
f.fileData.dirty = true
return n, nil
}
func (f *File) WriteAt(data []byte, off int64) (int, error) {
atomic.StoreInt64(&f.at, off)
return f.Write(data)
}
func (f *File) WriteString(s string) (ret int, err error) {
return f.Write([]byte(s))
}