forked from TA-Tran/caddy-gitea
/
fs.go
93 lines (72 loc) · 1.36 KB
/
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
package gitea
import (
"io"
"io/fs"
"time"
)
type fileInfo struct {
size int64
isdir bool
name string
}
type openFile struct {
content []byte
offset int64
name string
isdir bool
}
func (g fileInfo) Name() string {
return g.name
}
func (g fileInfo) Size() int64 {
return g.size
}
func (g fileInfo) Mode() fs.FileMode {
return 0o444
}
func (g fileInfo) ModTime() time.Time {
return time.Time{}
}
func (g fileInfo) Sys() any {
return nil
}
func (g fileInfo) IsDir() bool {
return g.isdir
}
var _ io.Seeker = (*openFile)(nil)
func (o *openFile) Close() error {
return nil
}
func (o *openFile) Stat() (fs.FileInfo, error) {
return fileInfo{
size: int64(len(o.content)),
isdir: o.isdir,
name: o.name,
}, nil
}
func (o *openFile) Read(b []byte) (int, error) {
if o.offset >= int64(len(o.content)) {
return 0, io.EOF
}
if o.offset < 0 {
return 0, &fs.PathError{Op: "read", Path: o.name, Err: fs.ErrInvalid}
}
n := copy(b, o.content[o.offset:])
o.offset += int64(n)
return n, nil
}
func (o *openFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
offset += 0
case 1:
offset += o.offset
case 2:
offset += int64(len(o.content))
}
if offset < 0 || offset > int64(len(o.content)) {
return 0, &fs.PathError{Op: "seek", Path: o.name, Err: fs.ErrInvalid}
}
o.offset = offset
return offset, nil
}