11package system
22
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"
334
345// Returns a []byte slice if the xattr is set and nil otherwise
356// Requires path and its attribute as arguments
367func Lgetxattr (path string , attr string ) ([]byte , error ) {
378 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-
479 // 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 )
5212
5313 switch {
5414 case errno == unix .ENODATA :
@@ -57,44 +17,19 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
5717 return nil , errno
5818 case errno == unix .ERANGE :
5919 // 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
6121 // 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 {
6524 return nil , errno
6625 }
6726 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 {
7129 return nil , errno
7230 }
73- case errno != 0 :
31+ case errno != nil :
7432 return nil , errno
7533 }
76- sz = int (_sz )
7734 return dest [:sz ], nil
7835}
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