File: extattr_freebsd.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (93 lines) | stat: -rw-r--r-- 2,861 bytes parent folder | download | duplicates (3)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//go:build freebsd

package system

import (
	"os"
	"unsafe"

	"golang.org/x/sys/unix"
)

const (
	EXTATTR_NAMESPACE_EMPTY  = unix.EXTATTR_NAMESPACE_EMPTY
	EXTATTR_NAMESPACE_USER   = unix.EXTATTR_NAMESPACE_USER
	EXTATTR_NAMESPACE_SYSTEM = unix.EXTATTR_NAMESPACE_SYSTEM
)

// ExtattrGetLink retrieves the value of the extended attribute identified by attrname
// in the given namespace and associated with the given path in the file system.
// If the path is a symbolic link, the extended attribute is retrieved from the link itself.
// Returns a []byte slice if the extattr is set and nil otherwise.
func ExtattrGetLink(path string, attrnamespace int, attrname string) ([]byte, error) {
	size, errno := unix.ExtattrGetLink(path, attrnamespace, attrname,
		uintptr(unsafe.Pointer(nil)), 0)
	if errno != nil {
		if errno == unix.ENOATTR {
			return nil, nil
		}
		return nil, &os.PathError{Op: "extattr_get_link", Path: path, Err: errno}
	}
	if size == 0 {
		return []byte{}, nil
	}

	dest := make([]byte, size)
	size, errno = unix.ExtattrGetLink(path, attrnamespace, attrname,
		uintptr(unsafe.Pointer(&dest[0])), size)
	if errno != nil {
		return nil, &os.PathError{Op: "extattr_get_link", Path: path, Err: errno}
	}

	return dest[:size], nil
}

// ExtattrSetLink sets the value of extended attribute identified by attrname
// in the given namespace and associated with the given path in the file system.
// If the path is a symbolic link, the extended attribute is set on the link itself.
func ExtattrSetLink(path string, attrnamespace int, attrname string, data []byte) error {
	if len(data) == 0 {
		data = []byte{} // ensure non-nil for empty data
	}
	if _, errno := unix.ExtattrSetLink(path, attrnamespace, attrname,
		uintptr(unsafe.Pointer(&data[0])), len(data)); errno != nil {
		return &os.PathError{Op: "extattr_set_link", Path: path, Err: errno}
	}

	return nil
}

// ExtattrListLink lists extended attributes associated with the given path
// in the specified namespace. If the path is a symbolic link, the attributes
// are listed from the link itself.
func ExtattrListLink(path string, attrnamespace int) ([]string, error) {
	size, errno := unix.ExtattrListLink(path, attrnamespace,
		uintptr(unsafe.Pointer(nil)), 0)
	if errno != nil {
		return nil, &os.PathError{Op: "extattr_list_link", Path: path, Err: errno}
	}
	if size == 0 {
		return []string{}, nil
	}

	dest := make([]byte, size)
	size, errno = unix.ExtattrListLink(path, attrnamespace,
		uintptr(unsafe.Pointer(&dest[0])), size)
	if errno != nil {
		return nil, &os.PathError{Op: "extattr_list_link", Path: path, Err: errno}
	}

	var attrs []string
	for i := 0; i < size; {
		// Each attribute is preceded by a single byte length
		length := int(dest[i])
		i++
		if i+length > size {
			break
		}
		attrs = append(attrs, string(dest[i:i+length]))
		i += length
	}

	return attrs, nil
}