1
1
package system
2
2
3
- import (
4
- "unsafe"
5
-
6
- "golang.org/x/sys/unix"
7
- )
8
-
9
- var _zero uintptr
10
-
11
- // Returns the size of xattrs and nil error
12
- // Requires path, takes allocated []byte or nil as last argument
13
- func Llistxattr (path string , dest []byte ) (size int , err error ) {
14
- pathBytes , err := unix .BytePtrFromString (path )
15
- if err != nil {
16
- return - 1 , err
17
- }
18
- var newpathBytes unsafe.Pointer
19
- if len (dest ) > 0 {
20
- newpathBytes = unsafe .Pointer (& dest [0 ])
21
- } else {
22
- newpathBytes = unsafe .Pointer (& _zero )
23
- }
24
-
25
- _size , _ , errno := unix .Syscall6 (unix .SYS_LLISTXATTR , uintptr (unsafe .Pointer (pathBytes )), uintptr (newpathBytes ), uintptr (len (dest )), 0 , 0 , 0 )
26
- size = int (_size )
27
- if errno != 0 {
28
- return - 1 , errno
29
- }
30
-
31
- return size , nil
32
- }
3
+ import "golang.org/x/sys/unix"
33
4
34
5
// Returns a []byte slice if the xattr is set and nil otherwise
35
6
// Requires path and its attribute as arguments
36
7
func Lgetxattr (path string , attr string ) ([]byte , error ) {
37
8
var sz int
38
- pathBytes , err := unix .BytePtrFromString (path )
39
- if err != nil {
40
- return nil , err
41
- }
42
- attrBytes , err := unix .BytePtrFromString (attr )
43
- if err != nil {
44
- return nil , err
45
- }
46
-
47
9
// Start with a 128 length byte array
48
- sz = 128
49
- dest := make ([]byte , sz )
50
- destBytes := unsafe .Pointer (& dest [0 ])
51
- _sz , _ , errno := unix .Syscall6 (unix .SYS_LGETXATTR , uintptr (unsafe .Pointer (pathBytes )), uintptr (unsafe .Pointer (attrBytes )), uintptr (destBytes ), uintptr (len (dest )), 0 , 0 )
10
+ dest := make ([]byte , 128 )
11
+ sz , errno := unix .Lgetxattr (path , attr , dest )
52
12
53
13
switch {
54
14
case errno == unix .ENODATA :
@@ -57,44 +17,19 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
57
17
return nil , errno
58
18
case errno == unix .ERANGE :
59
19
// 128 byte array might just not be good enough,
60
- // A dummy buffer is used ``uintptr(0)`` to get real size
20
+ // A dummy buffer is used to get the real size
61
21
// of the xattrs on disk
62
- _sz , _ , errno = unix .Syscall6 (unix .SYS_LGETXATTR , uintptr (unsafe .Pointer (pathBytes )), uintptr (unsafe .Pointer (attrBytes )), uintptr (unsafe .Pointer (nil )), uintptr (0 ), 0 , 0 )
63
- sz = int (_sz )
64
- if sz < 0 {
22
+ sz , errno = unix .Lgetxattr (path , attr , []byte {})
23
+ if errno != nil {
65
24
return nil , errno
66
25
}
67
26
dest = make ([]byte , sz )
68
- destBytes := unsafe .Pointer (& dest [0 ])
69
- _sz , _ , errno = unix .Syscall6 (unix .SYS_LGETXATTR , uintptr (unsafe .Pointer (pathBytes )), uintptr (unsafe .Pointer (attrBytes )), uintptr (destBytes ), uintptr (len (dest )), 0 , 0 )
70
- if errno != 0 {
27
+ sz , errno = unix .Lgetxattr (path , attr , dest )
28
+ if errno != nil {
71
29
return nil , errno
72
30
}
73
- case errno != 0 :
31
+ case errno != nil :
74
32
return nil , errno
75
33
}
76
- sz = int (_sz )
77
34
return dest [:sz ], nil
78
35
}
79
-
80
- func Lsetxattr (path string , attr string , data []byte , flags int ) error {
81
- pathBytes , err := unix .BytePtrFromString (path )
82
- if err != nil {
83
- return err
84
- }
85
- attrBytes , err := unix .BytePtrFromString (attr )
86
- if err != nil {
87
- return err
88
- }
89
- var dataBytes unsafe.Pointer
90
- if len (data ) > 0 {
91
- dataBytes = unsafe .Pointer (& data [0 ])
92
- } else {
93
- dataBytes = unsafe .Pointer (& _zero )
94
- }
95
- _ , _ , errno := unix .Syscall6 (unix .SYS_LSETXATTR , uintptr (unsafe .Pointer (pathBytes )), uintptr (unsafe .Pointer (attrBytes )), uintptr (dataBytes ), uintptr (len (data )), uintptr (flags ), 0 )
96
- if errno != 0 {
97
- return errno
98
- }
99
- return nil
100
- }
0 commit comments