-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
192 lines (171 loc) · 3.28 KB
/
metadata.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
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
package xsum
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
func NewAttrString(s string) (Attr, error) {
var attr Attr
L:
for _, c := range []byte(s) {
for _, p := range attrRep {
if p.rep == c {
attr |= p.attr
continue L
}
}
return 0, fmt.Errorf("invalid attribute `%s'", string(c))
}
return attr, nil
}
func NewAttrHex(s string) (Attr, error) {
if len(s)%2 != 0 {
return 0, errors.New("invalid hex attribute length")
}
b, err := hex.DecodeString(s)
if err != nil {
return 0, fmt.Errorf("invalid hex attribute `%s'", s)
}
// ignore attrs beyond 2 bytes
if len(b) > 2 {
b = b[:2]
} else if len(b) == 1 {
b = []byte{0, b[0]}
} else if len(b) == 0 {
b = []byte{0, 0}
}
return Attr(binary.BigEndian.Uint16(b)), nil
}
type Attr uint16
const (
AttrUID Attr = 1 << iota
AttrGID
AttrAtime
AttrMtime
AttrCtime
AttrBtime
AttrSpecial
AttrX
AttrInclusive
AttrNoName
AttrNoData
AttrFollow
AttrEmpty Attr = 0
)
var attrRep = []struct {
attr Attr
rep byte
}{
{AttrUID, 'u'},
{AttrGID, 'g'},
{AttrSpecial, 's'},
{AttrMtime, 't'},
{AttrCtime, 'c'},
{AttrX, 'x'},
{AttrInclusive, 'i'},
{AttrNoName, 'n'},
{AttrNoData, 'e'},
{AttrFollow, 'l'},
}
func (a Attr) String() string {
var out strings.Builder
for _, p := range attrRep {
if a&p.attr != 0 {
out.Write([]byte{p.rep})
}
}
return out.String()
}
func (a Attr) Hex() string {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(a))
return hex.EncodeToString(b)
}
func NewModeString(s string) (Mode, error) {
if s == "" {
return 0, nil
}
mode64, err := strconv.ParseUint(s, 8, 12)
if err != nil {
return 0, fmt.Errorf("invalid mode `%s'", s)
}
return Mode(mode64), nil
}
func NewModeHex(s string) (Mode, error) {
if len(s) != 3 {
return 0, errors.New("invalid hex mode length")
}
b, err := hex.DecodeString("0" + s)
if err != nil || len(b) != 2 {
return 0, fmt.Errorf("invalid hex mode `%s'", s)
}
return Mode(binary.BigEndian.Uint16(b)), nil
}
type Mode uint16
func (m Mode) String() string {
return fmt.Sprintf("%04o", m)[:4]
}
func (m Mode) Hex() string {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(m))
return hex.EncodeToString(b)[1:]
}
func NewMaskString(s string) (Mask, error) {
parts := strings.SplitN(s, "+", 2)
mode := strings.TrimSpace(parts[0])
var attrs string
if len(parts) > 1 {
attrs = parts[1]
}
m, err := NewModeString(mode)
if err != nil {
return Mask{}, err
}
attr, err := NewAttrString(attrs)
return Mask{
Mode: m,
Attr: attr,
}, err
}
func NewMaskHex(s string) (Mask, error) {
if len(s) < 3 {
return Mask{}, errors.New("mask too short")
}
if s[0] != 'a' && s[0] != 'A' {
return Mask{}, errors.New("invalid mask code")
}
mode, err := NewModeHex(s[1:4])
if err != nil {
return Mask{}, err
}
attr, err := NewAttrHex(s[4:])
return Mask{
Mode: mode,
Attr: attr,
}, err
}
func NewMask(mode os.FileMode, attr Attr) Mask {
return Mask{
Mode: Mode(mode),
Attr: attr,
}
}
type Mask struct {
Mode Mode
Attr Attr
}
func (m Mask) String() string {
mode := m.Mode.String()
attrs := m.Attr.String()
if attrs == "" {
return mode
}
return fmt.Sprintf("%s+%s", mode, attrs)
}
func (m Mask) Hex() string {
return fmt.Sprintf("%x%s%s", 0xa, m.Mode.Hex(), m.Attr.Hex())
}