-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.go
More file actions
206 lines (174 loc) · 4.25 KB
/
Copy pathentry.go
File metadata and controls
206 lines (174 loc) · 4.25 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package fs
import (
"errors"
"fmt"
"strings"
"time"
"github.com/transientvariable/anchor"
json "github.com/json-iterator/go"
gofs "io/fs"
gopath "path"
)
var (
_ gofs.DirEntry = (*Entry)(nil)
_ gofs.FileInfo = (*Entry)(nil)
)
type PathValidator func(string) bool
// Entry is a container for file and directory metadata.
type Entry struct {
attrs *Attribute
path string
pathValidator PathValidator
}
// NewEntry creates a new Entry.
func NewEntry(path string, options ...func(*Entry)) (*Entry, error) {
entry := &Entry{path: path}
for _, opt := range options {
opt(entry)
}
if entry.attrs == nil {
attrs, err := NewAttributes()
if err != nil {
return entry, err
}
entry.attrs = attrs
}
if err := validPath(path, entry.pathValidator); err != nil {
return entry, err
}
return entry, nil
}
// Attributes returns the attributes for the Entry.
func (e *Entry) Attributes() *Attribute {
return e.attrs
}
// Dir returns the path for the Entry with the last element truncated.
func (e *Entry) Dir() string {
return gopath.Dir(e.path)
}
// Info ...
func (e *Entry) Info() (gofs.FileInfo, error) {
return e, nil
}
// IsDir returns whether the Entry represents a directory.
func (e *Entry) IsDir() bool {
return e.attrs.mode&gofs.ModeDir != 0
}
// Mode returns mode bits for the Entry.
func (e *Entry) Mode() gofs.FileMode {
return e.attrs.mode
}
// ModTime returns the modification time for the Entry.
func (e *Entry) ModTime() time.Time {
return e.attrs.Mtime()
}
// Name returns the Entry name.
func (e *Entry) Name() string {
return gopath.Base(e.path)
}
// Path returns the full path for the Entry.
func (e *Entry) Path() string {
return e.path
}
// Size returns the length in bytes if an Entry represents a regular file.
func (e *Entry) Size() int64 {
return e.attrs.size
}
// SetModTime sets the modification time for the Entry.
func (e *Entry) SetModTime(t time.Time) error {
t = t.UTC()
if t.IsZero() || t.Equal(e.attrs.mtime) {
return nil
}
if t.Before(e.attrs.mtime) {
return fmt.Errorf("entry: %w", ErrMtimeMismatch)
}
e.attrs.mtime = t
return nil
}
// SetPath sets the path for the Entry.
func (e *Entry) SetPath(p string) error {
if err := validPath(p, e.pathValidator); err != nil {
return err
}
e.path = p
return nil
}
// SetSize sets the size for the Entry if it represents a regular file.
func (e *Entry) SetSize(s uint64) {
if !e.IsDir() {
e.attrs.size = int64(s)
}
}
// Sys returns the underlying data source for the Entry (can return nil).
func (e *Entry) Sys() any {
return nil
}
// Type returns the type bits for the Entry.
//
// The type bits are a subset of the usual FileMode bits, those returned by the FileMode.Type method.
func (e *Entry) Type() gofs.FileMode {
return e.Mode().Type()
}
// Copy returns a copy of the Entry.
func (e *Entry) Copy() *Entry {
var attrs *Attribute
if e.attrs != nil {
attrs = e.attrs.Copy()
}
return &Entry{
attrs: attrs,
path: e.path,
pathValidator: e.pathValidator,
}
}
// ToMap returns a map representation of the Entry properties.
func (e *Entry) ToMap() (map[string]any, error) {
var m map[string]any
if err := json.NewDecoder(strings.NewReader(e.String())).Decode(&m); err != nil {
return m, err
}
return m, nil
}
// String returns a string representation of the Entry.
func (e *Entry) String() string {
s := make(map[string]any)
s["dir"] = e.Dir()
s["is_dir"] = e.IsDir()
s["name"] = e.Name()
s["mode"] = e.Mode().String()
s["mod_time"] = e.ModTime()
s["path"] = e.Path()
s["size"] = e.Size()
s["type"] = e.Type()
if e.attrs != nil {
attrs, err := e.attrs.ToMap()
if err != nil {
s["attributes"] = err.Error()
} else {
s["attributes"] = attrs
}
}
return string(anchor.ToJSONFormatted(s))
}
func validPath(p string, v func(string) bool) error {
if v == nil {
v = gofs.ValidPath
}
if !v(p) {
return errors.New(fmt.Sprintf("entry: path is invalid: %s", p))
}
return nil
}
// WithAttributes sets the Attribute for an Entry.
func WithAttributes(attrs *Attribute) func(*Entry) {
return func(e *Entry) {
e.attrs = attrs
}
}
// WithPathValidator sets the function used for validating paths for the Entry.
func WithPathValidator(v func(string) bool) func(*Entry) {
return func(e *Entry) {
e.pathValidator = v
}
}