-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.go
More file actions
204 lines (174 loc) · 3.6 KB
/
attribute.go
File metadata and controls
204 lines (174 loc) · 3.6 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
package fs
import (
"fmt"
"strings"
"time"
"github.com/transientvariable/anchor"
json "github.com/json-iterator/go"
gofs "io/fs"
)
// Attribute ...
type Attribute struct {
ctime time.Time
gid int32
group string
inode int64
mimeType string
mode gofs.FileMode
mtime time.Time
owner string
size int64
uid int32
}
// NewAttributes ..
func NewAttributes(attributes ...func(*Attribute)) (*Attribute, error) {
attrs := &Attribute{}
for _, attr := range attributes {
attr(attrs)
}
if attrs.ctime.IsZero() {
attrs.ctime = time.Now().UTC()
}
if mtime := attrs.mtime; !mtime.IsZero() && mtime.Before(attrs.ctime) {
return nil, fmt.Errorf("attribute: %w", ErrCtimeMismatch)
}
return attrs, nil
}
// Ctime ...
func (a *Attribute) Ctime() time.Time {
return a.ctime
}
// GID ...
func (a *Attribute) GID() int32 {
return a.gid
}
// Group ...
func (a *Attribute) Group() string {
return a.group
}
// Inode ...
func (a *Attribute) Inode() int64 {
return a.inode
}
// MimeType ...
func (a *Attribute) MimeType() string {
return a.mimeType
}
// Mode ...
func (a *Attribute) Mode() gofs.FileMode {
return a.mode
}
// Mtime ...
func (a *Attribute) Mtime() time.Time {
return a.mtime
}
// Owner ...
func (a *Attribute) Owner() string {
return a.owner
}
// Size ...
func (a *Attribute) Size() int64 {
return a.size
}
// UID ...
func (a *Attribute) UID() int32 {
return a.uid
}
// Copy returns a copy of the Attribute.
func (a *Attribute) Copy() *Attribute {
return &Attribute{
ctime: a.Ctime(),
gid: a.GID(),
group: a.Group(),
inode: a.Inode(),
mimeType: a.MimeType(),
mode: a.Mode(),
mtime: a.Mtime(),
owner: a.Owner(),
size: a.Size(),
uid: a.UID(),
}
}
// ToMap returns a map representation of the Attribute properties.
func (a *Attribute) ToMap() (map[string]any, error) {
var m map[string]any
if err := json.NewDecoder(strings.NewReader(a.String())).Decode(&m); err != nil {
return m, err
}
return m, nil
}
// String returns a string representation of the Attribute properties.
func (a *Attribute) String() string {
s := make(map[string]any)
s["ctime"] = a.Ctime()
s["gid"] = a.GID()
s["group"] = a.Group()
s["inode"] = a.Inode()
s["mime_type"] = a.MimeType()
s["mode"] = a.Mode()
s["mtime"] = a.Mtime()
s["owner"] = a.Owner()
s["size"] = a.Size()
s["uid"] = a.UID()
return string(anchor.ToJSONFormatted(s))
}
// WithCtime ...
func WithCtime(ctime time.Time) func(*Attribute) {
return func(a *Attribute) {
a.ctime = ctime.UTC()
}
}
// WithGID ...
func WithGID(gid uint32) func(*Attribute) {
return func(attrs *Attribute) {
attrs.gid = int32(gid)
}
}
// WithGroup ...
func WithGroup(group string) func(*Attribute) {
return func(a *Attribute) {
a.group = group
}
}
// WithInode ...
func WithInode(inode uint64) func(*Attribute) {
return func(a *Attribute) {
a.inode = int64(inode)
}
}
// WithMimeType ...
func WithMimeType(mimeType string) func(*Attribute) {
return func(a *Attribute) {
a.mimeType = mimeType
}
}
// WithMode ...
func WithMode(mode uint32) func(*Attribute) {
return func(a *Attribute) {
a.mode = gofs.FileMode(mode)
}
}
// WithMtime ...
func WithMtime(mtime time.Time) func(*Attribute) {
return func(a *Attribute) {
a.mtime = mtime.UTC()
}
}
// WithOwner ...
func WithOwner(owner string) func(*Attribute) {
return func(a *Attribute) {
a.owner = owner
}
}
// WithSize ...
func WithSize(size uint64) func(*Attribute) {
return func(a *Attribute) {
a.size = int64(size)
}
}
// WithUID ...
func WithUID(uid uint32) func(*Attribute) {
return func(a *Attribute) {
a.uid = int32(uid)
}
}