mirror of
https://github.com/vbatts/go-mtree.git
synced 2025-09-14 06:03:21 +00:00
xattr: simple wrapper for syscalls
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
ec1977017d
commit
2d3aea8623
3 changed files with 86 additions and 0 deletions
35
xattr/xattr.go
Normal file
35
xattr/xattr.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
// +build linux
|
||||
|
||||
package xattr
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Get returns the extended attributes (xattr) on file `path`, for the given `name`.
|
||||
func Get(path, name string) ([]byte, error) {
|
||||
dest := make([]byte, 1024)
|
||||
i, err := syscall.Getxattr(path, name, dest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dest[:i], nil
|
||||
}
|
||||
|
||||
// Set sets the extended attributes (xattr) on file `path`, for the given `name` and `value`
|
||||
func Set(path, name string, value []byte) error {
|
||||
return syscall.Setxattr(path, name, value, 0)
|
||||
}
|
||||
|
||||
// List returns a list of all the extended attributes (xattr) for file `path`
|
||||
func List(path string) ([]string, error) {
|
||||
dest := make([]byte, 1024)
|
||||
i, err := syscall.Listxattr(path, dest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return strings.Split(strings.TrimRight(string(dest[:i]), nilByte), nilByte), nil
|
||||
}
|
||||
|
||||
const nilByte = "\x00"
|
Loading…
Add table
Add a link
Reference in a new issue