Debian Codegraph @

memfs/file.go

raw symbols SCIP
1 package memfs
2
3 import (
4 "errors"
5 "io"
6 "io/fs"
7 "os"
8 "sync"
9 "time"
10
11 "github.com/go-git/go-billy/v6"
12 )
13
14 type file struct {
15 name string
16 content *content
17 position int64
18 flag int
19 mode os.FileMode
20
21 isClosed bool
22 }
23
24 func (f *file) Name() string {
25 return f.name
26 }
27
28 func (f *file) Read(b []byte) (int, error) {
29 n, err := f.ReadAt(b, f.position)
30 f.position += int64(n)
31
32 if errors.Is(err, io.EOF) && n != 0 {
33 err = nil
34 }
35
36 return n, err
37 }
38
39 func (f *file) ReadAt(b []byte, off int64) (int, error) {
40 if f.isClosed {
41 return 0, os.ErrClosed
42 }
43
44 if !isReadAndWrite(f.flag) && !isReadOnly(f.flag) {
45 return 0, errors.New("read not supported")
46 }
47
48 n, err := f.content.ReadAt(b, off)
49
50 return n, err
51 }
52
53 func (f *file) Seek(offset int64, whence int) (int64, error) {
54 if f.isClosed {
55 return 0, os.ErrClosed
56 }
57
58 switch whence {
59 case io.SeekCurrent:
60 f.position += offset
61 case io.SeekStart:
62 f.position = offset
63 case io.SeekEnd:
64 f.position = int64(f.content.Len()) + offset
65 }
66
67 return f.position, nil
68 }
69
70 func (f *file) Write(p []byte) (int, error) {
71 return f.WriteAt(p, f.position)
72 }
73
74 func (f *file) WriteAt(p []byte, off int64) (int, error) {
75 if f.isClosed {
76 return 0, os.ErrClosed
77 }
78
79 if !isReadAndWrite(f.flag) && !isWriteOnly(f.flag) {
80 return 0, errors.New("write not supported")
81 }
82
83 n, err := f.content.WriteAt(p, off)
84 f.position = off + int64(n)
85
86 return n, err
87 }
88
89 func (f *file) Close() error {
90 if f.isClosed {
91 return os.ErrClosed
92 }
93
94 f.isClosed = true
95 return nil
96 }
97
98 func (f *file) Truncate(size int64) error {
99 if size < int64(len(f.content.bytes)) {
100 f.content.bytes = f.content.bytes[:size]
101 } else if more := int(size) - len(f.content.bytes); more > 0 {
102 f.content.bytes = append(f.content.bytes, make([]byte, more)...)
103 }
104
105 return nil
106 }
107
108 func (f *file) Duplicate(filename string, mode fs.FileMode, flag int) billy.File {
109 n := &file{
110 name: filename,
111 content: f.content,
112 mode: mode,
113 flag: flag,
114 }
115
116 if isTruncate(flag) {
117 n.content.Truncate()
118 }
119
120 if isAppend(flag) {
121 n.position = int64(n.content.Len())
122 }
123
124 return n
125 }
126
127 func (f *file) Stat() (os.FileInfo, error) {
128 return &fileInfo{
129 name: f.Name(),
130 mode: f.mode,
131 size: f.content.Len(),
132 modTime: f.content.modTime,
133 }, nil
134 }
135
136 type fileInfo struct {
137 name string
138 size int
139 mode os.FileMode
140 modTime time.Time
141 }
142
143 func (fi *fileInfo) Name() string {
144 return fi.name
145 }
146
147 func (fi *fileInfo) Size() int64 {
148 return int64(fi.size)
149 }
150
151 func (fi *fileInfo) Mode() fs.FileMode {
152 return fi.mode
153 }
154
155 func (fi *fileInfo) ModTime() time.Time {
156 return fi.modTime
157 }
158
159 func (fi *fileInfo) IsDir() bool {
160 return fi.mode.IsDir()
161 }
162
163 func (*fileInfo) Sys() any {
164 return nil
165 }
166
167 type content struct {
168 name string
169 bytes []byte
170 modTime time.Time
171
172 m sync.RWMutex
173 }
174
175 func (c *content) WriteAt(p []byte, off int64) (int, error) {
176 if off < 0 {
177 return 0, &os.PathError{
178 Op: "writeat",
179 Path: c.name,
180 Err: errors.New("negative offset"),
181 }
182 }
183
184 c.m.Lock()
185 prev := len(c.bytes)
186
187 diff := int(off) - prev
188 if diff > 0 {
189 c.bytes = append(c.bytes, make([]byte, diff)...)
190 }
191
192 c.bytes = append(c.bytes[:off], p...)
193 if len(c.bytes) < prev {
194 c.bytes = c.bytes[:prev]
195 }
196 c.modTime = time.Now()
197 c.m.Unlock()
198
199 return len(p), nil
200 }
201
202 func (c *content) ReadAt(b []byte, off int64) (n int, err error) {
203 if off < 0 {
204 return 0, &os.PathError{
205 Op: "readat",
206 Path: c.name,
207 Err: errors.New("negative offset"),
208 }
209 }
210
211 c.m.RLock()
212 size := int64(len(c.bytes))
213 if off >= size {
214 c.m.RUnlock()
215 return 0, io.EOF
216 }
217
218 l := int64(len(b))
219 if off+l > size {
220 l = size - off
221 }
222
223 btr := c.bytes[off : off+l]
224 n = copy(b, btr)
225
226 if len(btr) < len(b) {
227 err = io.EOF
228 }
229 c.m.RUnlock()
230
231 return
232 }