2014-02-21 04:12:25 -05:00
|
|
|
package system
|
2013-11-27 21:22:47 -05:00
|
|
|
|
2013-12-13 09:43:50 -05:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2013-11-27 21:22:47 -05:00
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
// Lgetxattr retrieves the value of the extended attribute identified by attr
|
|
|
|
// and associated with the given path in the file system.
|
|
|
|
// It will returns a nil slice and nil error if the xattr is not set.
|
2014-02-14 08:24:37 -05:00
|
|
|
func Lgetxattr(path string, attr string) ([]byte, error) {
|
|
|
|
pathBytes, err := syscall.BytePtrFromString(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
attrBytes, err := syscall.BytePtrFromString(attr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := make([]byte, 128)
|
|
|
|
destBytes := unsafe.Pointer(&dest[0])
|
|
|
|
sz, _, errno := syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
|
|
|
|
if errno == syscall.ENODATA {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if errno == syscall.ERANGE {
|
|
|
|
dest = make([]byte, sz)
|
|
|
|
destBytes := unsafe.Pointer(&dest[0])
|
|
|
|
sz, _, errno = syscall.Syscall6(syscall.SYS_LGETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(destBytes), uintptr(len(dest)), 0, 0)
|
|
|
|
}
|
|
|
|
if errno != 0 {
|
|
|
|
return nil, errno
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest[:sz], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _zero uintptr
|
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
// Lsetxattr sets the value of the extended attribute identified by attr
|
|
|
|
// and associated with the given path in the file system.
|
2014-02-14 08:24:37 -05:00
|
|
|
func Lsetxattr(path string, attr string, data []byte, flags int) error {
|
|
|
|
pathBytes, err := syscall.BytePtrFromString(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attrBytes, err := syscall.BytePtrFromString(attr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var dataBytes unsafe.Pointer
|
|
|
|
if len(data) > 0 {
|
|
|
|
dataBytes = unsafe.Pointer(&data[0])
|
|
|
|
} else {
|
|
|
|
dataBytes = unsafe.Pointer(&_zero)
|
|
|
|
}
|
|
|
|
_, _, errno := syscall.Syscall6(syscall.SYS_LSETXATTR, uintptr(unsafe.Pointer(pathBytes)), uintptr(unsafe.Pointer(attrBytes)), uintptr(dataBytes), uintptr(len(data)), uintptr(flags), 0)
|
|
|
|
if errno != 0 {
|
|
|
|
return errno
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|