-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxattr_bsd.go
93 lines (76 loc) · 1.95 KB
/
xattr_bsd.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
// xattr_bsd.go - xattr support for BSDs
//
// (c) 2023- Sudhi Herle <sudhi@herle.net>
//
// Licensing Terms: GPLv2
//
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
//go:build freebsd || netbsd
package walk
import (
"errors"
"fmt"
"golang.org/x/sys/unix"
"strings"
)
type ns struct {
id int
nm string
}
var (
_NS = [...]ns{
{unix.EXTATTR_NAMESPACE_USER, "user."},
{unix.EXTATTR_NAMESPACE_SYSTEM, "system."},
}
)
// explicitly query each namespace
func listxattr(p string) ([]string, error) {
b := make([]byte, 1024)
attrs := make([]string, 0, 4)
for i := range _NS {
n := &_NS[i]
// pass the namespace id to a diff syscall
sz, err := unix.ExtattrListLink(p, n.id, unsafe.Pointer(&b[0]), len(b))
if err != nil {
if err == unix.EPERM && ns.id != unix.EXTATTR_NAMESPACE_USER {
continue
}
return nil, fmt.Errorf("%p: listxattr (%s): %w", p, n.nm, err)
}
if errors.Is(err, unix.ERANGE) || sz == len(b) {
var zero uintptr
sz, err := unix.ExtattrListLink(p, n.id, unsafe.Pointer(zero), 0)
if err != nil {
return nil, fmt.Errorf("%p: listxattr (%s): %w", p, n.nm, err)
}
b = make([]byte, sz)
sz, err := unix.ExtattrListLink(p, n.id, unsafe.Pointer(&b[0]), len(b))
}
if err != nil {
return nil, fmt.Errorf("%p: listxattr (%s): %w", p, n.nm, err)
}
buf = b[:sz]
j := 0
// Per BSD manpage: every attr is encoded as a <length, name> pair.
// The first byte is the length, followed by non-null terminated attr name.
for j < len(buf) {
var m int
m, j = int(buf[j]), j+1
if (j + m) > sz {
return nil, fmt.Errorf("%p: listxattr (%s): attr length error (%d > %d)",
p, n.nm, m, sz)
}
if m > 0 {
z := string(buf[j : j+m])
j += m
attrs = append(attrs, n.nm+z)
}
}
}
return attrs, nil
}