-
Notifications
You must be signed in to change notification settings - Fork 6
/
straw_mem.go
265 lines (229 loc) · 5.01 KB
/
straw_mem.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package straw
import (
"bytes"
"errors"
"fmt"
"io"
"net/url"
"os"
"sort"
"strings"
"sync"
"time"
)
var _ StreamStore = &memStreamStore{}
func init() {
Register("mem", func(u *url.URL) (StreamStore, error) {
return newMemStreamStore(), nil
})
}
func newMemStreamStore() *memStreamStore {
return &memStreamStore{Root: &memFile{
IsDir_: true,
}}
}
type memStreamStore struct {
lk sync.Mutex
Root *memFile
}
type memFile struct {
Name_ string
Content []byte
IsDir_ bool
Entries map[string]*memFile
Modtime time.Time
}
func (mf *memFile) IsDir() bool {
return mf.IsDir_
}
func (mf *memFile) ModTime() time.Time {
return mf.Modtime
}
func (mf *memFile) Mode() os.FileMode {
if mf.IsDir_ {
return os.FileMode(0755) | os.ModeDir
}
return os.FileMode(0644)
}
func (mf *memFile) Name() string {
return mf.Name_
}
func (mf *memFile) Size() int64 {
if mf.IsDir_ {
return 4096
}
return int64(len(mf.Content))
}
func (mf *memFile) Sys() interface{} {
return nil
}
func (fs *memStreamStore) Close() error {
return nil
}
func (fs *memStreamStore) Lstat(name string) (os.FileInfo, error) {
return fs.Stat(name)
}
func (fs *memStreamStore) Stat(name string) (os.FileInfo, error) {
return fs.getExisting(name)
}
func (fs *memStreamStore) OpenReadCloser(name string) (StrawReader, error) {
fs.lk.Lock()
defer fs.lk.Unlock()
file, err := fs.getExistingFile(name)
if err != nil {
return nil, err
}
return newMemFileReader(file)
}
func newMemFileReader(mf *memFile) (*memFileReader, error) {
return &memFileReader{bytes.NewReader(mf.Content)}, nil
}
type memFileReader struct {
*bytes.Reader
}
func (mfr *memFileReader) SeekStart(offset int64) error {
_, err := mfr.Seek(offset, io.SeekStart)
return err
}
func (mfr *memFileReader) Close() error {
return nil
}
func (fs *memStreamStore) Mkdir(name string, mode os.FileMode) error {
fs.lk.Lock()
defer fs.lk.Unlock()
list := fs.Split(name)
dir := fs.Root
for _, elem := range list[0 : len(list)-1] {
dir = dir.Entries[elem]
if dir == nil {
return os.ErrNotExist
}
}
newdir := list[len(list)-1]
if dir.Entries == nil {
dir.Entries = make(map[string]*memFile)
} else if dir.Entries[newdir] != nil {
return errors.New("file exists")
}
dir.Entries[newdir] = &memFile{IsDir_: true, Name_: newdir}
return nil
}
func (fs *memStreamStore) Remove(name string) error {
fs.lk.Lock()
defer fs.lk.Unlock()
list := fs.Split(name)
parent := fs.Root
for _, elem := range list[0 : len(list)-1] {
parent = parent.Entries[elem]
if parent == nil {
return os.ErrNotExist
}
}
filename := list[len(list)-1]
if parent.Entries == nil {
return os.ErrNotExist
}
if parent.Entries[filename] == nil {
return os.ErrNotExist
}
file := parent.Entries[filename]
if file == nil {
return os.ErrNotExist
}
if file.IsDir_ && file.Entries != nil && len(file.Entries) != 0 {
return errors.New("directory not empty")
}
delete(parent.Entries, filename)
return nil
}
func (fs *memStreamStore) getExistingFile(name string) (*memFile, error) {
file, err := fs.getExisting(name)
if err != nil {
return nil, err
}
if file.IsDir_ {
return nil, fmt.Errorf("%s is a directory", name)
}
return file, nil
}
func (fs *memStreamStore) getExisting(name string) (*memFile, error) {
list := fs.Split(name)
f := fs.Root
for _, elem := range list {
f = f.Entries[elem]
if f == nil {
break
}
}
if f == nil {
return nil, os.ErrNotExist
}
return f, nil
}
func (fs *memStreamStore) CreateWriteCloser(name string) (StrawWriter, error) {
fs.lk.Lock()
defer fs.lk.Unlock()
list := fs.Split(name)
dir := fs.Root
for _, elem := range list[0 : len(list)-1] {
dir = dir.Entries[elem]
if dir == nil {
return nil, errors.New("not found")
}
}
if !dir.IsDir() {
return nil, errors.New("not a directory")
}
fileName := list[len(list)-1]
f := dir.Entries[fileName]
if f == nil {
f = &memFile{Name_: fileName}
if dir.Entries == nil {
dir.Entries = make(map[string]*memFile)
}
dir.Entries[fileName] = f
}
if f.IsDir() {
return nil, fmt.Errorf("%s is a directory", name)
}
f.Content = f.Content[0:0]
return &memfileWriteCloser{f}, nil
}
type memfileWriteCloser struct {
mf *memFile
}
func (mfwc *memfileWriteCloser) Write(buf []byte) (int, error) {
mfwc.mf.Content = append(mfwc.mf.Content, buf...)
return len(buf), nil
}
func (mfwc *memfileWriteCloser) Close() error {
return nil
}
func (fs *memStreamStore) Readdir(name string) ([]os.FileInfo, error) {
file, err := fs.getExisting(name)
if err != nil {
return nil, err
}
if !file.IsDir() {
return nil, fmt.Errorf("%v is not a dir", name)
}
var res []os.FileInfo
for _, entry := range file.Entries {
res = append(res, entry)
}
sort.Slice(res, func(i, j int) bool { return res[i].Name() < res[j].Name() })
return res, nil
}
func (fs *memStreamStore) Split(name string) []string {
if name == "" {
return []string{}
}
spl := strings.Split(name, string(os.PathSeparator))
if spl[0] == "" {
spl = spl[1:]
}
if spl[len(spl)-1] == "" {
spl = spl[0 : len(spl)-1]
}
return spl
}