-
Notifications
You must be signed in to change notification settings - Fork 191
/
fs.go
156 lines (127 loc) · 2.72 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
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
package fakeobj
import (
"io"
"io/fs"
"path/filepath"
"time"
"github.com/gookit/goutil/basefn"
)
// DirEntry implements the fs.DirEntry
type DirEntry struct {
Dir bool
Nam string // basename
Mod fs.FileMode
Fi fs.FileInfo
Err error
}
// NewDirEntry create a fs.DirEntry
func NewDirEntry(fpath string, isDir ...bool) *DirEntry {
isd := basefn.FirstOr(isDir, false)
return &DirEntry{Nam: filepath.Base(fpath), Dir: isd, Mod: fs.ModePerm}
}
// Name get
func (d *DirEntry) Name() string {
return d.Nam
}
// IsDir get
func (d *DirEntry) IsDir() bool {
return d.Dir
}
// Type get
func (d *DirEntry) Type() fs.FileMode {
return d.Mod
}
// Info get
func (d *DirEntry) Info() (fs.FileInfo, error) {
if d.Fi == nil {
d.Fi = &FileInfo{
Dir: d.Dir,
Nam: d.Nam,
Mod: d.Mod,
}
}
return d.Fi, d.Err
}
// FileInfo implements the fs.FileInfo, fs.File
type FileInfo struct {
Dir bool
Nam string // basename
Mod fs.FileMode
Mt time.Time
// Path full path
Path string
Contents string
CloseErr error
offset int
}
// NewFile instance
func NewFile(fpath string) *FileInfo {
return NewFileInfo(fpath)
}
// NewFileInfo instance
func NewFileInfo(fpath string, isDir ...bool) *FileInfo {
return &FileInfo{
Dir: basefn.FirstOr(isDir, false),
Nam: filepath.Base(fpath),
Mod: fs.ModePerm,
Path: fpath,
}
}
// WithBody set file body contents
func (f *FileInfo) WithBody(s string) *FileInfo {
f.Contents = s
return f
}
// WithMtime set file modify time
func (f *FileInfo) WithMtime(mt time.Time) *FileInfo {
f.Mt = mt
return f
}
// Reset prepares a FileInfo for reuse.
func (f *FileInfo) Reset() *FileInfo {
f.offset = 0
return f
}
// fs.File methods.
// Stat returns the FileInfo structure describing file.
func (f *FileInfo) Stat() (fs.FileInfo, error) {
return f, nil
}
// Read reads up to len(p) bytes into p.
func (f *FileInfo) Read(p []byte) (int, error) {
if f.offset >= len(f.Contents) {
return 0, io.EOF
}
n := copy(p, f.Contents[f.offset:])
f.offset += n
return n, nil
}
// Close closes the file
func (f *FileInfo) Close() error {
return f.CloseErr
}
// fs.FileInfo methods.
// Name returns the base name of the file.
func (f *FileInfo) Name() string {
return f.Nam
}
// Size returns the length in bytes for regular files; system-dependent for others.
func (f *FileInfo) Size() int64 {
return int64(len(f.Contents))
}
// Mode returns file mode bits.
func (f *FileInfo) Mode() fs.FileMode {
return f.Mod
}
// ModTime returns the modification time.
func (f *FileInfo) ModTime() time.Time {
return f.Mt
}
// IsDir returns true if the file is a directory.
func (f *FileInfo) IsDir() bool {
return f.Dir
}
// Sys returns underlying data source (can return nil).
func (f *FileInfo) Sys() any {
return nil
}