This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fsnodeattrs.go
329 lines (299 loc) · 7.32 KB
/
fsnodeattrs.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package fs
import (
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/mgoltzsche/ctnr/pkg/idutils"
"github.com/pkg/errors"
)
const (
AttrsHash AttrSet = 1
AttrsMtime AttrSet = 2
AttrsAtime AttrSet = 4
AttrsAll = AttrsHash | AttrsMtime | AttrsAtime
AttrsCompare = AttrsHash | AttrsMtime
TimeFormat = time.RFC3339
)
type AttrSet uint
var (
TypeFile NodeType = "file"
TypeDir NodeType = "dir"
TypeOverlay NodeType = "overlay"
TypeSymlink NodeType = "symlink"
TypeFifo NodeType = "fifo"
TypeDevice NodeType = "dev"
TypeWhiteout NodeType = "whiteout"
nameRegex = regexp.MustCompile("^[a-zA-Z0-9\\-_\\.,]+$")
nilWriter = hashingNilWriter("noop writer")
_ Source = &NodeAttrs{}
)
type NodeType string
type FileAttrs struct {
Mode os.FileMode
idutils.UserIds
Xattrs map[string]string
FileTimes
Size int64
Symlink string
}
func (a *FileAttrs) AttrString(attrs AttrSet) string {
s := ""
if !a.UserIds.IsZero() {
s = "usr=" + a.UserIds.String()
}
perm := a.Mode.Perm()
if perm != 0 && a.Mode&os.ModeSymlink == 0 {
s += " mode=" + strconv.FormatUint(uint64(perm), 8)
}
if a.Size > 0 {
s += " size=" + strconv.Itoa(int(a.Size))
}
if a.Symlink != "" {
s += " link=" + encodePath(a.Symlink)
}
if len(a.Xattrs) > 0 {
xa := make([]string, 0, len(a.Xattrs))
for k, v := range a.Xattrs {
xa = append(xa, fmt.Sprintf("xattr.%s=%s", url.QueryEscape(k), url.QueryEscape(v)))
}
sort.Strings(xa)
s += " " + strings.Join(xa, " ")
}
if attrs&AttrsMtime != 0 {
if !a.Mtime.IsZero() {
s += " mtime=" + fmt.Sprintf("%d", a.Mtime.Unix())
}
}
if attrs&AttrsAtime != 0 {
if !a.Atime.IsZero() {
s += " atime=" + fmt.Sprintf("%d", a.Atime.Unix())
}
}
return strings.TrimLeft(s, " ")
}
func (a *FileAttrs) Equal(o *FileAttrs) bool {
return a.Mode.Perm() == o.Mode.Perm() && a.Uid == o.Uid && a.Gid == o.Gid &&
a.Mtime.Unix() == o.Mtime.Unix() && a.Size == o.Size && a.Symlink == o.Symlink &&
xattrsEqual(a.Xattrs, o.Xattrs)
}
func xattrsEqual(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
if a != nil {
for k, v := range a {
if bv, ok := b[k]; !ok || v != bv {
return false
}
}
}
return true
}
type FileTimes struct {
Atime time.Time
Mtime time.Time
}
type NodeAttrs struct {
NodeInfo
DerivedAttrs
}
type DerivedAttrs struct {
Hash string
URL string
HTTPInfo string
}
func (s *DerivedAttrs) Equal(o *DerivedAttrs) bool {
return s.Hash == o.Hash && s.URL == o.URL && s.HTTPInfo == o.HTTPInfo
}
type NodeInfo struct {
NodeType NodeType
FileAttrs
}
func (s NodeInfo) Equal(o NodeInfo) bool {
return s.NodeType == o.NodeType && s.FileAttrs.Equal(&o.FileAttrs)
}
func (a *NodeInfo) AttrString(attrs AttrSet) string {
return "type=" + string(a.NodeType) + " " + a.FileAttrs.AttrString(attrs)
}
type DeviceAttrs struct {
FileAttrs
Devmajor int64
Devminor int64
}
func (s *NodeAttrs) Attrs() NodeInfo {
return s.NodeInfo
}
func (s *NodeAttrs) DeriveAttrs() (DerivedAttrs, error) {
return s.DerivedAttrs, nil
}
func (s *NodeAttrs) Write(path, name string, w Writer, written map[Source]string) (err error) {
if linkDest, ok := written[s]; ok {
err = w.LowerLink(path, linkDest, s)
} else {
written[s] = path
err = w.LowerNode(path, name, s)
}
return
}
func ParseNodeAttrs(s string) (r NodeAttrs, err error) {
for _, e := range strings.Split(s, " ") {
l := strings.SplitN(e, "=", 2)
if len(l) != 2 {
return r, errors.Errorf("parse file attrs: invalid attr %q in %q (missing '=')", e, s)
}
k := l[0]
v := l[1]
switch k {
case "type":
r.NodeType = NodeType(v)
case "mode":
var m uint64
m, err = strconv.ParseUint(v, 8, 32)
if err != nil {
return r, errors.Errorf("parse file attrs: invalid mode %q", v)
}
r.Mode = os.FileMode(m)
case "usr":
r.UserIds, err = idutils.ParseUser(v).ToIds()
if err != nil {
return r, errors.Wrap(err, "parse file attrs")
}
case "size":
r.Size, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return r, errors.Errorf("parse file attrs: invalid size %q", v)
}
case "link":
r.Symlink, err = decodePath(v)
if err != nil {
return r, errors.Errorf("parse file attrs: invalid symlink %q", v)
}
case "hash":
r.Hash = v
case "url":
r.URL = v
case "http":
if r.HTTPInfo, err = url.QueryUnescape(v); err != nil {
return r, errors.Wrap(err, "parse file attrs: invalid http attr")
}
case "mtime":
tme, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return r, errors.Wrap(err, "parse file attrs: invalid mtime")
}
r.Mtime = time.Unix(tme, 0)
case "atime":
tme, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return r, errors.Wrap(err, "parse file attrs: invalid atime")
}
r.Atime = time.Unix(tme, 0)
default:
if strings.HasPrefix(k, "xattr.") {
k, err = url.QueryUnescape(k[6:])
if err != nil {
return r, errors.Errorf("parse file attrs: invalid xattr key %q", k)
}
v, err = url.QueryUnescape(v)
if err != nil {
return r, errors.Errorf("parse file attrs: invalid xattr value %q", v)
}
if r.Xattrs == nil {
r.Xattrs = map[string]string{}
}
r.Xattrs[k] = v
}
}
}
if r.Mtime.IsZero() {
r.Mtime = time.Now()
}
if r.Atime.IsZero() {
r.Atime = r.Mtime
}
return
}
func parseUnixTime(s string) (t time.Time, err error) {
dotPos := strings.Index(s, ".")
if dotPos == -1 && dotPos+1 < len(s) {
return t, errors.Errorf("parse unix time %q", s)
}
sec, err := strconv.ParseInt(s[:dotPos], 10, 64)
if err != nil {
return t, errors.Errorf("parse unix second from %q", s)
}
nsec, err := strconv.ParseInt(s[dotPos+1:], 10, 64)
if err != nil {
return t, errors.Errorf("parse nanosecond from %q", s)
}
return time.Unix(sec, nsec), nil
}
func (a *NodeAttrs) AttrString(attrs AttrSet) string {
s := a.NodeInfo.AttrString(attrs)
if attrs&AttrsHash != 0 {
if a.Hash != "" {
s += " hash=" + a.Hash
}
if a.URL != "" {
if strings.Contains(a.URL, " ") {
panic("attrs string: provided URL contains space: " + a.URL)
}
s += " url=" + a.URL
if a.HTTPInfo != "" {
s += " http=" + url.QueryEscape(a.HTTPInfo)
}
}
}
return s
}
func (a *NodeAttrs) String() string {
return "NodeAttrs{" + a.AttrString(AttrsAll) + "}"
}
func encodePath(p string) string {
l := strings.Split(p, string(filepath.Separator))
for i, s := range l {
l[i] = url.PathEscape(s)
}
return strings.Join(l, string(filepath.Separator))
}
func decodePath(p string) (r string, err error) {
l := strings.Split(p, string(filepath.Separator))
for i, s := range l {
if l[i], err = url.PathUnescape(s); err != nil {
return "", errors.Wrap(err, "decode path")
}
}
return strings.Join(l, string(filepath.Separator)), nil
}
type FileInfo struct {
name string
*FileAttrs
}
func NewFileInfo(name string, attrs *FileAttrs) *FileInfo {
return &FileInfo{name, attrs}
}
var _ os.FileInfo = &FileInfo{}
func (fi *FileInfo) IsDir() bool {
return fi.Mode()&os.ModeDir != 0
}
func (fi *FileInfo) ModTime() time.Time {
return fi.Mtime
}
func (fi *FileInfo) Mode() os.FileMode {
return fi.FileAttrs.Mode
}
func (fi *FileInfo) Name() string {
return fi.name
}
func (fi *FileInfo) Size() int64 {
return fi.FileAttrs.Size
}
func (fi *FileInfo) Sys() interface{} {
return fi
}