File: xattr.go

package info (click to toggle)
golang-github-vbatts-go-mtree 0.5.4%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 796 kB
  • sloc: sh: 198; makefile: 80
file content (43 lines) | stat: -rw-r--r-- 1,001 bytes parent folder | download | duplicates (2)
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
//go:build linux
// +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
	}

	// If the returned list is empty, return nil instead of []string{""}
	str := string(dest[:i])
	if str == "" {
		return nil, nil
	}

	return strings.Split(strings.TrimRight(str, nilByte), nilByte), nil
}

const nilByte = "\x00"