forked from Azure/azure-storage-azcopy
/
unixStatAdapter.go
554 lines (458 loc) · 14.1 KB
/
unixStatAdapter.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package common
import (
"strconv"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/johnmic/azure-storage-azcopy/v10/azbfs"
)
const ( // POSIX property metadata
POSIXNlinkMeta = "posix_nlink"
POSIXINodeMeta = "posix_ino"
POSIXCTimeMeta = "posix_ctime"
LINUXBTimeMeta = "linux_btime"
POSIXBlockDeviceMeta = "is_block_dev" // todo: read & use these
POSIXCharDeviceMeta = "is_char_dev"
POSIXSocketMeta = "is_socket"
POSIXFIFOMeta = "is_fifo"
POSIXDevMeta = "posix_dev"
POSIXRDevMeta = "posix_rdev"
POSIXATimeMeta = "posix_atime"
POSIXFolderMeta = "hdi_isfolder" // todo: read & use these
POSIXSymlinkMeta = "is_symlink"
POSIXOwnerMeta = "posix_owner"
POSIXGroupMeta = "posix_group"
POSIXModeMeta = "permissions"
POSIXModTimeMeta = "modtime"
LINUXAttributeMeta = "linux_attribute"
LINUXAttributeMaskMeta = "linux_attribute_mask"
LINUXStatxMaskMeta = "linux_statx_mask"
)
var AllLinuxProperties = []string{
POSIXNlinkMeta,
POSIXINodeMeta,
LINUXBTimeMeta,
POSIXBlockDeviceMeta,
POSIXCharDeviceMeta,
POSIXSocketMeta,
POSIXFIFOMeta,
POSIXDevMeta,
POSIXRDevMeta,
POSIXATimeMeta,
POSIXFolderMeta,
POSIXSymlinkMeta,
POSIXOwnerMeta,
POSIXGroupMeta,
POSIXModeMeta,
}
//goland:noinspection GoCommentStart
type UnixStatAdapter interface {
Extended() bool // Did this call come from StatX?
// Statx properties
StatxMask() uint32 // Mask determines the availability of all stat/stax properties (except the device ID!). It is only used in statx though.
Attribute() uint64 // Attribute is masked by AttributeMask.
AttributeMask() uint64
BTime() time.Time // BTime may not always be available on every filesystem. It's important to check Mask first!
// ==========
// Base Stat properties
NLink() uint64
Owner() uint32
Group() uint32
FileMode() uint32 // Mode may not always be available to check in a Statx call (though it should be, since we requested it.) Best safe than sorry; check Mask!
INode() uint64
Device() uint64
RDevice() uint64 // RDevice is ONLY useful when Mode has S_IFCHR or S_IFBLK; as those determine if the file is a representitive of a block or character device.
ATime() time.Time
MTime() time.Time
CTime() time.Time
}
type UnixStatContainer struct { // Created for downloads
statx bool // Does the call contain extended properties (attributes, birthTime)?
mask uint32
attributes uint64
numLinks uint64
ownerUID uint32
groupGID uint32
mode uint32
iNode uint64
size uint64
attributesMask uint64
accessTime time.Time // atime
birthTime time.Time // btime, statx only
changeTime time.Time // ctime
modTime time.Time // mtime
repDevID uint64
devID uint64
}
func (u UnixStatContainer) Extended() bool {
return u.statx
}
func (u UnixStatContainer) StatxMask() uint32 {
return u.mask
}
func (u UnixStatContainer) Attribute() uint64 {
return u.attributes
}
func (u UnixStatContainer) AttributeMask() uint64 {
return u.attributesMask
}
func (u UnixStatContainer) BTime() time.Time {
return u.birthTime
}
func (u UnixStatContainer) NLink() uint64 {
return u.numLinks
}
func (u UnixStatContainer) Owner() uint32 {
return u.ownerUID
}
func (u UnixStatContainer) Group() uint32 {
return u.groupGID
}
func (u UnixStatContainer) FileMode() uint32 {
return u.mode
}
func (u UnixStatContainer) INode() uint64 {
return u.iNode
}
func (u UnixStatContainer) Device() uint64 {
return u.devID
}
func (u UnixStatContainer) RDevice() uint64 {
return u.repDevID
}
func (u UnixStatContainer) ATime() time.Time {
return u.accessTime
}
func (u UnixStatContainer) MTime() time.Time {
return u.modTime
}
func (u UnixStatContainer) CTime() time.Time {
return u.changeTime
}
// ReadStatFromMetadata is not fault-tolerant. If any given article does not parse,
// it will throw an error instead of continuing on, as it may be considered incorrect to attempt to persist the rest of the data.
// despite this function being used only in Downloads at the current moment, it still attempts to re-create as complete of a UnixStatAdapter as possible.
func ReadStatFromMetadata(metadata azblob.Metadata, contentLength int64) (UnixStatAdapter, error) {
s := UnixStatContainer{size: uint64(contentLength)}
if mask, ok := metadata[LINUXStatxMaskMeta]; ok {
m, err := strconv.ParseUint(mask, 10, 32)
if err != nil {
return s, err
}
s.statx = true
s.mask = uint32(m)
}
// cover additional statx properties here
if attr, ok := metadata[LINUXAttributeMeta]; ok {
a, err := strconv.ParseUint(attr, 10, 64)
if err != nil {
return s, err
}
s.attributes = a
}
if attr, ok := metadata[LINUXAttributeMaskMeta]; ok {
a, err := strconv.ParseUint(attr, 10, 64)
if err != nil {
return s, err
}
s.attributesMask = a
}
if btime, ok := metadata[LINUXBTimeMeta]; ok {
b, err := strconv.ParseInt(btime, 10, 64)
if err != nil {
return s, err
}
s.birthTime = time.Unix(0, b)
}
// base stat properties
if nlink, ok := metadata[POSIXNlinkMeta]; ok {
n, err := strconv.ParseUint(nlink, 10, 64)
if err != nil {
return s, err
}
s.numLinks = n
}
if owner, ok := metadata[POSIXOwnerMeta]; ok {
o, err := strconv.ParseUint(owner, 10, 32)
if err != nil {
return s, err
}
s.ownerUID = uint32(o)
}
if group, ok := metadata[POSIXGroupMeta]; ok {
g, err := strconv.ParseUint(group, 10, 32)
if err != nil {
return s, err
}
s.groupGID = uint32(g)
}
if mode, ok := metadata[POSIXModeMeta]; ok {
m, err := strconv.ParseUint(mode, 10, 32)
if err != nil {
return s, err
}
s.mode = uint32(m)
}
if inode, ok := metadata[POSIXINodeMeta]; ok {
ino, err := strconv.ParseUint(inode, 10, 64)
if err != nil {
return s, err
}
s.iNode = ino
}
if dev, ok := metadata[POSIXDevMeta]; ok {
d, err := strconv.ParseUint(dev, 10, 64)
if err != nil {
return s, err
}
s.devID = d
}
if rdev, ok := metadata[POSIXRDevMeta]; ok {
rd, err := strconv.ParseUint(rdev, 10, 64)
if err != nil {
return s, err
}
s.repDevID = rd
}
if atime, ok := metadata[POSIXATimeMeta]; ok {
at, err := strconv.ParseInt(atime, 10, 64)
if err != nil {
return s, err
}
s.accessTime = time.Unix(0, at)
}
if mtime, ok := metadata[POSIXModTimeMeta]; ok {
mt, err := strconv.ParseInt(mtime, 10, 64)
if err != nil {
return s, err
}
s.modTime = time.Unix(0, mt)
}
if ctime, ok := metadata[POSIXCTimeMeta]; ok {
ct, err := strconv.ParseInt(ctime, 10, 64)
if err != nil {
return s, err
}
s.changeTime = time.Unix(0, ct)
}
return s, nil
}
// ReadStatFromMetadata is not fault-tolerant. If any given article does not parse,
// it will throw an error instead of continuing on, as it may be considered incorrect to attempt to persist the rest of the data.
// despite this function being used only in Downloads at the current moment, it still attempts to re-create as complete of a UnixStatAdapter as possible.
func ReadStatFromBlobFSMetadata(metadata azbfs.Metadata, contentLength int64) (UnixStatAdapter, error) {
s := UnixStatContainer{size: uint64(contentLength)}
if mask, ok := metadata[LINUXStatxMaskMeta]; ok {
m, err := strconv.ParseUint(mask, 10, 32)
if err != nil {
return s, err
}
s.statx = true
s.mask = uint32(m)
}
// cover additional statx properties here
if attr, ok := metadata[LINUXAttributeMeta]; ok {
a, err := strconv.ParseUint(attr, 10, 64)
if err != nil {
return s, err
}
s.attributes = a
}
if attr, ok := metadata[LINUXAttributeMaskMeta]; ok {
a, err := strconv.ParseUint(attr, 10, 64)
if err != nil {
return s, err
}
s.attributesMask = a
}
if btime, ok := metadata[LINUXBTimeMeta]; ok {
b, err := strconv.ParseInt(btime, 10, 64)
if err != nil {
return s, err
}
s.birthTime = time.Unix(0, b)
}
// base stat properties
if nlink, ok := metadata[POSIXNlinkMeta]; ok {
n, err := strconv.ParseUint(nlink, 10, 64)
if err != nil {
return s, err
}
s.numLinks = n
}
if owner, ok := metadata[POSIXOwnerMeta]; ok {
o, err := strconv.ParseUint(owner, 10, 32)
if err != nil {
return s, err
}
s.ownerUID = uint32(o)
}
if group, ok := metadata[POSIXGroupMeta]; ok {
g, err := strconv.ParseUint(group, 10, 32)
if err != nil {
return s, err
}
s.groupGID = uint32(g)
}
if mode, ok := metadata[POSIXModeMeta]; ok {
m, err := strconv.ParseUint(mode, 10, 32)
if err != nil {
return s, err
}
s.mode = uint32(m)
}
if inode, ok := metadata[POSIXINodeMeta]; ok {
ino, err := strconv.ParseUint(inode, 10, 64)
if err != nil {
return s, err
}
s.iNode = ino
}
if dev, ok := metadata[POSIXDevMeta]; ok {
d, err := strconv.ParseUint(dev, 10, 64)
if err != nil {
return s, err
}
s.devID = d
}
if rdev, ok := metadata[POSIXRDevMeta]; ok {
rd, err := strconv.ParseUint(rdev, 10, 64)
if err != nil {
return s, err
}
s.repDevID = rd
}
if atime, ok := metadata[POSIXATimeMeta]; ok {
at, err := strconv.ParseInt(atime, 10, 64)
if err != nil {
return s, err
}
s.accessTime = time.Unix(0, at)
}
if mtime, ok := metadata[POSIXModTimeMeta]; ok {
mt, err := strconv.ParseInt(mtime, 10, 64)
if err != nil {
return s, err
}
s.modTime = time.Unix(0, mt)
}
if ctime, ok := metadata[POSIXCTimeMeta]; ok {
ct, err := strconv.ParseInt(ctime, 10, 64)
if err != nil {
return s, err
}
s.changeTime = time.Unix(0, ct)
}
return s, nil
}
const ( // Values cloned from x/sys/unix to avoid dependency
STATX_ALL = 0xfff
STATX_ATIME = 0x20
STATX_ATTR_APPEND = 0x20
STATX_ATTR_AUTOMOUNT = 0x1000
STATX_ATTR_COMPRESSED = 0x4
STATX_ATTR_DAX = 0x200000
STATX_ATTR_ENCRYPTED = 0x800
STATX_ATTR_IMMUTABLE = 0x10
STATX_ATTR_MOUNT_ROOT = 0x2000
STATX_ATTR_NODUMP = 0x40
STATX_ATTR_VERITY = 0x100000
STATX_BASIC_STATS = 0x7ff
STATX_BLOCKS = 0x400
STATX_BTIME = 0x800
STATX_CTIME = 0x80
STATX_GID = 0x10
STATX_INO = 0x100
STATX_MNT_ID = 0x1000
STATX_MODE = 0x2
STATX_MTIME = 0x40
STATX_NLINK = 0x4
STATX_SIZE = 0x200
STATX_TYPE = 0x1
STATX_UID = 0x8
S_IFSOCK = 0xc000
S_IFBLK = 0x6000
S_IFCHR = 0x2000
S_IFDIR = 0x4000
S_IFIFO = 0x1000
S_IFLNK = 0xa000
S_IRUSR = 0x400
S_IWUSR = 0x200
S_IXUSR = 0x100
S_IRGRP = 0x040
S_IWGRP = 0x020
S_IXGRP = 0x010
S_IROTH = 0x004
S_IWOTH = 0x002
S_IXOTH = 0x001
S_ALLPERM = 0x777
)
func ClearStatFromBlobMetadata(metadata azblob.Metadata) {
for _, v := range AllLinuxProperties {
delete(metadata, v)
}
}
func AddStatToBlobMetadata(s UnixStatAdapter, metadata azblob.Metadata) {
// TODO: File mode properties (hdi_isfolder, etc.)
if s.Extended() { // try to poll the other properties
mask := s.StatxMask()
tryAddMetadata(metadata, LINUXStatxMaskMeta, strconv.FormatUint(uint64(mask), 10))
tryAddMetadata(metadata, LINUXAttributeMeta, strconv.FormatUint(s.Attribute()&s.AttributeMask(), 10)) // AttributesMask indicates what attributes are supported by the filesystem
tryAddMetadata(metadata, LINUXAttributeMaskMeta, strconv.FormatUint(s.AttributeMask(), 10))
if StatXReturned(mask, STATX_BTIME) {
tryAddMetadata(metadata, LINUXBTimeMeta, strconv.FormatInt(s.BTime().UnixNano(), 10))
}
if StatXReturned(mask, STATX_MODE) {
tryAddMetadata(metadata, POSIXNlinkMeta, strconv.FormatUint(s.NLink(), 10))
}
if StatXReturned(mask, STATX_UID) {
tryAddMetadata(metadata, POSIXOwnerMeta, strconv.FormatUint(uint64(s.Owner()), 10))
}
if StatXReturned(mask, STATX_GID) {
tryAddMetadata(metadata, POSIXGroupMeta, strconv.FormatUint(uint64(s.Group()), 10))
}
if StatXReturned(mask, STATX_MODE) {
tryAddMetadata(metadata, POSIXModeMeta, strconv.FormatUint(uint64(s.FileMode()), 10))
}
if StatXReturned(mask, STATX_INO) {
tryAddMetadata(metadata, POSIXINodeMeta, strconv.FormatUint(s.INode(), 10))
}
// This is not optional.
tryAddMetadata(metadata, POSIXDevMeta, strconv.FormatUint(s.Device(), 10))
if StatXReturned(mask, STATX_MODE) && ((s.FileMode()&S_IFCHR) == S_IFCHR || (s.FileMode()&S_IFBLK) == S_IFBLK) {
tryAddMetadata(metadata, POSIXRDevMeta, strconv.FormatUint(s.RDevice(), 10))
}
// Sometimes, the filesystem will return ATime, but the vfs layer will overwrite it in the mask. It's still accurate, so we can use it.
// e.g. ext4+noatime will still return & properly store atimes, but won't be included in the statx mask.
if StatXReturned(mask, STATX_ATIME) || s.ATime().UnixNano() > 0 {
tryAddMetadata(metadata, POSIXATimeMeta, strconv.FormatInt(s.ATime().UnixNano(), 10))
}
if StatXReturned(mask, STATX_MTIME) {
tryAddMetadata(metadata, POSIXModTimeMeta, strconv.FormatInt(s.MTime().UnixNano(), 10))
}
if StatXReturned(mask, STATX_CTIME) {
tryAddMetadata(metadata, POSIXCTimeMeta, strconv.FormatInt(s.CTime().UnixNano(), 10))
}
} else {
tryAddMetadata(metadata, POSIXNlinkMeta, strconv.FormatUint(s.NLink(), 10))
tryAddMetadata(metadata, POSIXOwnerMeta, strconv.FormatUint(uint64(s.Owner()), 10))
tryAddMetadata(metadata, POSIXGroupMeta, strconv.FormatUint(uint64(s.Group()), 10))
tryAddMetadata(metadata, POSIXModeMeta, strconv.FormatUint(uint64(s.FileMode()), 10))
tryAddMetadata(metadata, POSIXINodeMeta, strconv.FormatUint(s.INode(), 10))
tryAddMetadata(metadata, POSIXDevMeta, strconv.FormatUint(s.Device(), 10))
if (s.FileMode()&S_IFCHR) == S_IFCHR || (s.FileMode()&S_IFBLK) == S_IFBLK { // this is not relevant unless the file is a block or character device.
tryAddMetadata(metadata, POSIXRDevMeta, strconv.FormatUint(s.RDevice(), 10))
}
tryAddMetadata(metadata, POSIXATimeMeta, strconv.FormatInt(s.ATime().UnixNano(), 10))
tryAddMetadata(metadata, POSIXModTimeMeta, strconv.FormatInt(s.MTime().UnixNano(), 10))
tryAddMetadata(metadata, POSIXCTimeMeta, strconv.FormatInt(s.CTime().UnixNano(), 10))
}
}
func StatXReturned(mask uint32, want uint32) bool {
return (mask & want) == want
}
func tryAddMetadata(metadata azblob.Metadata, key, value string) {
if _, ok := metadata[key]; ok {
return // Don't overwrite the user's metadata
}
metadata[key] = value
}