This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
memory_fs.go
141 lines (115 loc) · 2.47 KB
/
memory_fs.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
package storage
import (
"bytes"
"context"
"io"
"strings"
"sync"
"time"
)
// NewMemoryFS creates a a basic in-memory implementation of FS.
func NewMemoryFS() FS {
return &memoryFS{
data: make(map[string]*memFile),
}
}
type memFile struct {
data []byte
attrs Attributes
}
func (f *memFile) readCloser() io.ReadCloser {
return io.NopCloser(bytes.NewReader(f.data))
}
type memoryFS struct {
sync.RWMutex
data map[string]*memFile
}
// Open implements FS.
func (m *memoryFS) Open(_ context.Context, path string, _ *ReaderOptions) (*File, error) {
m.RLock()
f, ok := m.data[path]
m.RUnlock()
if ok {
return &File{
ReadCloser: f.readCloser(),
Attributes: f.attrs,
}, nil
}
return nil, ¬ExistError{
Path: path,
}
}
// Attributes implements FS.
func (m *memoryFS) Attributes(_ context.Context, path string, _ *ReaderOptions) (*Attributes, error) {
m.RLock()
f, ok := m.data[path]
m.RUnlock()
if ok {
attrs := f.attrs
return &attrs, nil
}
return nil, ¬ExistError{
Path: path,
}
}
type writingFile struct {
*bytes.Buffer
path string
m *memoryFS
options *WriterOptions
}
func (wf *writingFile) Close() error {
if wf.options.Attributes.Size == 0 {
wf.options.Attributes.Size = int64(wf.Buffer.Len())
}
wf.m.Lock()
// Record time with the lock so the time is accurate
if wf.options.Attributes.ModTime.IsZero() {
wf.options.Attributes.ModTime = time.Now()
}
wf.m.data[wf.path] = &memFile{
data: wf.Buffer.Bytes(),
attrs: wf.options.Attributes,
}
wf.m.Unlock()
return nil
}
// Create implements FS. NB: Callers must close the io.WriteCloser to create the file.
func (m *memoryFS) Create(_ context.Context, path string, options *WriterOptions) (io.WriteCloser, error) {
if options == nil {
options = &WriterOptions{}
}
return &writingFile{
Buffer: &bytes.Buffer{},
path: path,
m: m,
options: options,
}, nil
}
// Delete implements FS.
func (m *memoryFS) Delete(_ context.Context, path string) error {
m.Lock()
delete(m.data, path)
m.Unlock()
return nil
}
// Walk implements FS.
func (m *memoryFS) Walk(_ context.Context, path string, fn WalkFn) error {
var list []string
m.RLock()
for k := range m.data {
if strings.HasPrefix(k, path) {
list = append(list, k)
}
}
m.RUnlock()
for _, k := range list {
if err := fn(k); err != nil {
return err
}
}
return nil
}
func (m *memoryFS) URL(_ context.Context, _ string, _ *SignedURLOptions) (string, error) {
return "", ErrNotImplemented
}