-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfile.go
166 lines (146 loc) · 2.96 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
package memoryfs
import (
"bytes"
"fmt"
"io"
"io/fs"
"sync"
"time"
)
type file struct {
sync.RWMutex
info fileinfo
opener LazyOpener
content []byte
}
type fileAccess struct {
file *file
reader io.Reader
}
// LazyOpener provides an io.Reader that can be used to access the content of a file, whatever the actual storage medium.
// If the LazyOpener returns an io.ReadCloser, it will be closed after each read.
type LazyOpener func() (io.Reader, error)
const bufferSize = 0x100
func (f *file) overwrite(data []byte, perm fs.FileMode) error {
f.RLock()
if f.opener == nil {
f.RUnlock()
return fmt.Errorf("missing opener")
}
f.RUnlock()
rw, err := f.open()
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
f.Lock()
f.info.size = int64(len(data))
f.info.modified = time.Now()
f.info.mode = perm
f.Unlock()
for len(data) > 0 {
n, err := rw.Write(data)
if err != nil {
return err
}
data = data[n:]
}
return nil
}
func (f *file) stat() fs.FileInfo {
f.RLock()
defer f.RUnlock()
return f.info
}
func (f *file) open() (*fileAccess, error) {
f.RLock()
defer f.RUnlock()
if f.opener == nil {
return nil, fmt.Errorf("missing opener")
}
return &fileAccess{
file: f,
}, nil
}
func (f *fileAccess) Stat() (fs.FileInfo, error) {
f.file.RLock()
defer f.file.RUnlock()
return f.file.info, nil
}
func (f *fileAccess) Read(data []byte) (int, error) {
r, err := func() (io.Reader, error) {
f.file.Lock()
defer f.file.Unlock()
if f.reader == nil {
r, err := f.file.opener()
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
f.reader = r
}
return f.reader, nil
}()
if err != nil {
return 0, err
}
return r.Read(data)
}
func (f *fileAccess) Close() error {
f.file.Lock()
defer f.file.Unlock()
if f.reader == nil {
return nil
}
if closer, ok := f.reader.(io.Closer); ok {
return closer.Close()
}
return nil
}
func (f *fileAccess) Write(p []byte) (n int, err error) {
w, err := func() (io.Writer, error) {
f.file.Lock()
defer f.file.Unlock()
if f.reader == nil {
r, err := f.file.opener()
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
f.reader = r
}
w, ok := f.reader.(io.Writer)
if !ok {
return nil, fmt.Errorf("cannot write - opener did not provide io.Writer")
}
return w, nil
}()
if err != nil {
return 0, err
}
return w.Write(p)
}
type lazyAccess struct {
file *file
reader io.Reader
writer *bytes.Buffer
}
func (l *lazyAccess) Read(data []byte) (int, error) {
l.file.RLock()
defer l.file.RUnlock()
if l.reader == nil {
l.reader = bytes.NewReader(l.file.content)
}
return l.reader.Read(data)
}
func (l *lazyAccess) Write(data []byte) (int, error) {
l.file.Lock()
defer l.file.Unlock()
if l.writer == nil {
l.writer = bytes.NewBuffer(l.file.content)
l.writer.Reset()
}
n, err := l.writer.Write(data)
if err != nil {
return 0, err
}
l.file.content = l.writer.Bytes()
return n, nil
}