Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/afero/mem/file.go
2880 views
1
// Copyright © 2015 Steve Francia <[email protected]>.
2
// Copyright 2013 tsuru authors. All rights reserved.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
package mem
16
17
import (
18
"bytes"
19
"errors"
20
"io"
21
"io/fs"
22
"os"
23
"path/filepath"
24
"sync"
25
"sync/atomic"
26
"time"
27
28
"github.com/spf13/afero/internal/common"
29
)
30
31
const FilePathSeparator = string(filepath.Separator)
32
33
var _ fs.ReadDirFile = &File{}
34
35
type File struct {
36
// atomic requires 64-bit alignment for struct field access
37
at int64
38
readDirCount int64
39
closed bool
40
readOnly bool
41
fileData *FileData
42
}
43
44
func NewFileHandle(data *FileData) *File {
45
return &File{fileData: data}
46
}
47
48
func NewReadOnlyFileHandle(data *FileData) *File {
49
return &File{fileData: data, readOnly: true}
50
}
51
52
func (f File) Data() *FileData {
53
return f.fileData
54
}
55
56
type FileData struct {
57
sync.Mutex
58
name string
59
data []byte
60
memDir Dir
61
dir bool
62
mode os.FileMode
63
modtime time.Time
64
uid int
65
gid int
66
}
67
68
func (d *FileData) Name() string {
69
d.Lock()
70
defer d.Unlock()
71
return d.name
72
}
73
74
func CreateFile(name string) *FileData {
75
return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}
76
}
77
78
func CreateDir(name string) *FileData {
79
return &FileData{name: name, memDir: &DirMap{}, dir: true, modtime: time.Now()}
80
}
81
82
func ChangeFileName(f *FileData, newname string) {
83
f.Lock()
84
f.name = newname
85
f.Unlock()
86
}
87
88
func SetMode(f *FileData, mode os.FileMode) {
89
f.Lock()
90
f.mode = mode
91
f.Unlock()
92
}
93
94
func SetModTime(f *FileData, mtime time.Time) {
95
f.Lock()
96
setModTime(f, mtime)
97
f.Unlock()
98
}
99
100
func setModTime(f *FileData, mtime time.Time) {
101
f.modtime = mtime
102
}
103
104
func SetUID(f *FileData, uid int) {
105
f.Lock()
106
f.uid = uid
107
f.Unlock()
108
}
109
110
func SetGID(f *FileData, gid int) {
111
f.Lock()
112
f.gid = gid
113
f.Unlock()
114
}
115
116
func GetFileInfo(f *FileData) *FileInfo {
117
return &FileInfo{f}
118
}
119
120
func (f *File) Open() error {
121
atomic.StoreInt64(&f.at, 0)
122
atomic.StoreInt64(&f.readDirCount, 0)
123
f.fileData.Lock()
124
f.closed = false
125
f.fileData.Unlock()
126
return nil
127
}
128
129
func (f *File) Close() error {
130
f.fileData.Lock()
131
f.closed = true
132
if !f.readOnly {
133
setModTime(f.fileData, time.Now())
134
}
135
f.fileData.Unlock()
136
return nil
137
}
138
139
func (f *File) Name() string {
140
return f.fileData.Name()
141
}
142
143
func (f *File) Stat() (os.FileInfo, error) {
144
return &FileInfo{f.fileData}, nil
145
}
146
147
func (f *File) Sync() error {
148
return nil
149
}
150
151
func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
152
if !f.fileData.dir {
153
return nil, &os.PathError{
154
Op: "readdir",
155
Path: f.fileData.name,
156
Err: errors.New("not a dir"),
157
}
158
}
159
var outLength int64
160
161
f.fileData.Lock()
162
files := f.fileData.memDir.Files()[f.readDirCount:]
163
if count > 0 {
164
if len(files) < count {
165
outLength = int64(len(files))
166
} else {
167
outLength = int64(count)
168
}
169
if len(files) == 0 {
170
err = io.EOF
171
}
172
} else {
173
outLength = int64(len(files))
174
}
175
f.readDirCount += outLength
176
f.fileData.Unlock()
177
178
res = make([]os.FileInfo, outLength)
179
for i := range res {
180
res[i] = &FileInfo{files[i]}
181
}
182
183
return res, err
184
}
185
186
func (f *File) Readdirnames(n int) (names []string, err error) {
187
fi, err := f.Readdir(n)
188
names = make([]string, len(fi))
189
for i, f := range fi {
190
_, names[i] = filepath.Split(f.Name())
191
}
192
return names, err
193
}
194
195
// Implements fs.ReadDirFile
196
func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
197
fi, err := f.Readdir(n)
198
if err != nil {
199
return nil, err
200
}
201
di := make([]fs.DirEntry, len(fi))
202
for i, f := range fi {
203
di[i] = common.FileInfoDirEntry{FileInfo: f}
204
}
205
return di, nil
206
}
207
208
func (f *File) Read(b []byte) (n int, err error) {
209
f.fileData.Lock()
210
defer f.fileData.Unlock()
211
if f.closed {
212
return 0, ErrFileClosed
213
}
214
if len(b) > 0 && int(f.at) == len(f.fileData.data) {
215
return 0, io.EOF
216
}
217
if int(f.at) > len(f.fileData.data) {
218
return 0, io.ErrUnexpectedEOF
219
}
220
if len(f.fileData.data)-int(f.at) >= len(b) {
221
n = len(b)
222
} else {
223
n = len(f.fileData.data) - int(f.at)
224
}
225
copy(b, f.fileData.data[f.at:f.at+int64(n)])
226
atomic.AddInt64(&f.at, int64(n))
227
return
228
}
229
230
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
231
prev := atomic.LoadInt64(&f.at)
232
atomic.StoreInt64(&f.at, off)
233
n, err = f.Read(b)
234
atomic.StoreInt64(&f.at, prev)
235
return
236
}
237
238
func (f *File) Truncate(size int64) error {
239
if f.closed {
240
return ErrFileClosed
241
}
242
if f.readOnly {
243
return &os.PathError{
244
Op: "truncate",
245
Path: f.fileData.name,
246
Err: errors.New("file handle is read only"),
247
}
248
}
249
if size < 0 {
250
return ErrOutOfRange
251
}
252
f.fileData.Lock()
253
defer f.fileData.Unlock()
254
if size > int64(len(f.fileData.data)) {
255
diff := size - int64(len(f.fileData.data))
256
f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{0o0}, int(diff))...)
257
} else {
258
f.fileData.data = f.fileData.data[0:size]
259
}
260
setModTime(f.fileData, time.Now())
261
return nil
262
}
263
264
func (f *File) Seek(offset int64, whence int) (int64, error) {
265
if f.closed {
266
return 0, ErrFileClosed
267
}
268
switch whence {
269
case io.SeekStart:
270
atomic.StoreInt64(&f.at, offset)
271
case io.SeekCurrent:
272
atomic.AddInt64(&f.at, offset)
273
case io.SeekEnd:
274
atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
275
}
276
return f.at, nil
277
}
278
279
func (f *File) Write(b []byte) (n int, err error) {
280
if f.closed {
281
return 0, ErrFileClosed
282
}
283
if f.readOnly {
284
return 0, &os.PathError{
285
Op: "write",
286
Path: f.fileData.name,
287
Err: errors.New("file handle is read only"),
288
}
289
}
290
n = len(b)
291
cur := atomic.LoadInt64(&f.at)
292
f.fileData.Lock()
293
defer f.fileData.Unlock()
294
diff := cur - int64(len(f.fileData.data))
295
var tail []byte
296
if n+int(cur) < len(f.fileData.data) {
297
tail = f.fileData.data[n+int(cur):]
298
}
299
if diff > 0 {
300
f.fileData.data = append(
301
f.fileData.data,
302
append(bytes.Repeat([]byte{0o0}, int(diff)), b...)...)
303
f.fileData.data = append(f.fileData.data, tail...)
304
} else {
305
f.fileData.data = append(f.fileData.data[:cur], b...)
306
f.fileData.data = append(f.fileData.data, tail...)
307
}
308
setModTime(f.fileData, time.Now())
309
310
atomic.AddInt64(&f.at, int64(n))
311
return
312
}
313
314
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
315
atomic.StoreInt64(&f.at, off)
316
return f.Write(b)
317
}
318
319
func (f *File) WriteString(s string) (ret int, err error) {
320
return f.Write([]byte(s))
321
}
322
323
func (f *File) Info() *FileInfo {
324
return &FileInfo{f.fileData}
325
}
326
327
type FileInfo struct {
328
*FileData
329
}
330
331
// Implements os.FileInfo
332
func (s *FileInfo) Name() string {
333
s.Lock()
334
_, name := filepath.Split(s.name)
335
s.Unlock()
336
return name
337
}
338
339
func (s *FileInfo) Mode() os.FileMode {
340
s.Lock()
341
defer s.Unlock()
342
return s.mode
343
}
344
345
func (s *FileInfo) ModTime() time.Time {
346
s.Lock()
347
defer s.Unlock()
348
return s.modtime
349
}
350
351
func (s *FileInfo) IsDir() bool {
352
s.Lock()
353
defer s.Unlock()
354
return s.dir
355
}
356
func (s *FileInfo) Sys() interface{} { return nil }
357
func (s *FileInfo) Size() int64 {
358
if s.IsDir() {
359
return int64(42)
360
}
361
s.Lock()
362
defer s.Unlock()
363
return int64(len(s.data))
364
}
365
366
var (
367
ErrFileClosed = errors.New("File is closed")
368
ErrOutOfRange = errors.New("out of range")
369
ErrTooLarge = errors.New("too large")
370
ErrFileNotFound = os.ErrNotExist
371
ErrFileExists = os.ErrExist
372
ErrDestinationExists = os.ErrExist
373
)
374
375