-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxattr_linux.go
More file actions
76 lines (71 loc) · 1.59 KB
/
xattr_linux.go
File metadata and controls
76 lines (71 loc) · 1.59 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
//go:build linux
package squashfs
import (
"io/fs"
"syscall"
)
// readXattrs reads extended attributes from the source filesystem for the given path.
// If the FS implements XattrFS, that interface is used. Otherwise returns nil.
func readXattrs(srcFS fs.FS, path string) map[string][]byte {
if xfs, ok := srcFS.(XattrFS); ok {
names, err := xfs.ListXattr(path)
if err != nil || len(names) == 0 {
return nil
}
result := make(map[string][]byte)
for _, name := range names {
val, err := xfs.GetXattr(path, name)
if err != nil {
continue
}
result[name] = val
}
if len(result) == 0 {
return nil
}
return result
}
return nil
}
// ListXattrSyscall lists xattr names on a file path using Linux syscalls.
func ListXattrSyscall(path string) ([]string, error) {
size, err := syscall.Listxattr(path, nil)
if err != nil {
return nil, err
}
if size == 0 {
return nil, nil
}
buf := make([]byte, size)
size, err = syscall.Listxattr(path, buf)
if err != nil {
return nil, err
}
var names []string
start := 0
for i, b := range buf[:size] {
if b == 0 {
if i > start {
names = append(names, string(buf[start:i]))
}
start = i + 1
}
}
return names, nil
}
// GetXattrSyscall gets a single xattr value from a file path using Linux syscalls.
func GetXattrSyscall(path, name string) ([]byte, error) {
size, err := syscall.Getxattr(path, name, nil)
if err != nil {
return nil, err
}
if size == 0 {
return []byte{}, nil
}
buf := make([]byte, size)
size, err = syscall.Getxattr(path, name, buf)
if err != nil {
return nil, err
}
return buf[:size], nil
}