Update pkg/xattr to v0.2.0

This commit is contained in:
Alexander Neumann 2017-04-18 21:32:55 +02:00
parent 3bbcf89105
commit 88e8eaa575
7 changed files with 56 additions and 56 deletions

4
vendor/manifest vendored
View file

@ -52,8 +52,8 @@
{
"importpath": "github.com/pkg/xattr",
"repository": "https://github.com/pkg/xattr",
"revision": "b867675798fa7708a444945602b452ca493f2272",
"branch": "master"
"revision": "858d49c224b241ba9393e20f521f6a76f52dd482",
"branch": "HEAD"
},
{
"importpath": "github.com/restic/chunker",

View file

@ -14,12 +14,12 @@ Extended attribute support for Go (linux + darwin + freebsd).
const path = "/tmp/myfile"
const prefix = "user."
if err := xattr.Setxattr(path, prefix+"test", []byte("test-attr-value")); err != nil {
if err := xattr.Set(path, prefix+"test", []byte("test-attr-value")); err != nil {
log.Fatal(err)
}
var data []byte
data, err = xattr.Getxattr(path, prefix+"test"); err != nil {
data, err = xattr.Get(path, prefix+"test"); err != nil {
log.Fatal(err)
}
```

View file

@ -1,21 +1,21 @@
/*
Package xattr provides support for extended attributes on linux, darwin and freebsd.
Extended attributes are name:value pairs associated permanently with files and directories,
similar to the environment strings associated with a process.
Extended attributes are name:value pairs associated permanently with files and directories,
similar to the environment strings associated with a process.
An attribute may be defined or undefined. If it is defined, its value may be empty or non-empty.
More details you can find here: https://en.wikipedia.org/wiki/Extended_file_attributes
*/
package xattr
// XAttrError records an error and the operation, file path and attribute that caused it.
type XAttrError struct {
// Error records an error and the operation, file path and attribute that caused it.
type Error struct {
Op string
Path string
Name string
Err error
}
func (e *XAttrError) Error() string {
func (e *Error) Error() string {
return e.Op + " " + e.Path + " " + e.Name + ": " + e.Err.Error()
}

View file

@ -2,32 +2,32 @@
package xattr
// Getxattr retrieves extended attribute data associated with path.
func Getxattr(path, name string) ([]byte, error) {
// Get retrieves extended attribute data associated with path.
func Get(path, name string) ([]byte, error) {
// find size.
size, err := getxattr(path, name, nil, 0, 0, 0)
if err != nil {
return nil, &XAttrError{"getxattr", path, name, err}
return nil, &Error{"xattr.Get", path, name, err}
}
if size > 0 {
buf := make([]byte, size)
// Read into buffer of that size.
read, err := getxattr(path, name, &buf[0], size, 0, 0)
if err != nil {
return nil, &XAttrError{"getxattr", path, name, err}
return nil, &Error{"xattr.Get", path, name, err}
}
return buf[:read], nil
}
return []byte{}, nil
}
// Listxattr retrieves a list of names of extended attributes associated
// List retrieves a list of names of extended attributes associated
// with the given path in the file system.
func Listxattr(path string) ([]string, error) {
func List(path string) ([]string, error) {
// find size.
size, err := listxattr(path, nil, 0, 0)
if err != nil {
return nil, &XAttrError{"listxattr", path, "", err}
return nil, &Error{"xattr.List", path, "", err}
}
if size > 0 {
@ -35,25 +35,25 @@ func Listxattr(path string) ([]string, error) {
// Read into buffer of that size.
read, err := listxattr(path, &buf[0], size, 0)
if err != nil {
return nil, &XAttrError{"listxattr", path, "", err}
return nil, &Error{"xattr.List", path, "", err}
}
return nullTermToStrings(buf[:read]), nil
}
return []string{}, nil
}
// Setxattr associates name and data together as an attribute of path.
func Setxattr(path, name string, data []byte) error {
// Set associates name and data together as an attribute of path.
func Set(path, name string, data []byte) error {
if err := setxattr(path, name, &data[0], len(data), 0, 0); err != nil {
return &XAttrError{"setxattr", path, name, err}
return &Error{"xattr.Set", path, name, err}
}
return nil
}
// Removexattr removes the attribute associated with the given path.
func Removexattr(path, name string) error {
// Remove removes the attribute associated with the given path.
func Remove(path, name string) error {
if err := removexattr(path, name, 0); err != nil {
return &XAttrError{"removexattr", path, name, err}
return &Error{"xattr.Remove", path, name, err}
}
return nil
}

View file

@ -10,61 +10,61 @@ const (
EXTATTR_NAMESPACE_USER = 1
)
// Getxattr retrieves extended attribute data associated with path.
func Getxattr(path, name string) ([]byte, error) {
// Get retrieves extended attribute data associated with path.
func Get(path, name string) ([]byte, error) {
// find size.
size, err := extattr_get_file(path, EXTATTR_NAMESPACE_USER, name, nil, 0)
if err != nil {
return nil, &XAttrError{"extattr_get_file", path, name, err}
return nil, &Error{"xattr.Get", path, name, err}
}
if size > 0 {
buf := make([]byte, size)
// Read into buffer of that size.
read, err := extattr_get_file(path, EXTATTR_NAMESPACE_USER, name, &buf[0], size)
if err != nil {
return nil, &XAttrError{"extattr_get_file", path, name, err}
return nil, &Error{"xattr.Get", path, name, err}
}
return buf[:read], nil
}
return []byte{}, nil
}
// Listxattr retrieves a list of names of extended attributes associated
// List retrieves a list of names of extended attributes associated
// with the given path in the file system.
func Listxattr(path string) ([]string, error) {
func List(path string) ([]string, error) {
// find size.
size, err := extattr_list_file(path, EXTATTR_NAMESPACE_USER, nil, 0)
if err != nil {
return nil, &XAttrError{"extattr_list_file", path, "", err}
return nil, &Error{"xattr.List", path, "", err}
}
if size > 0 {
buf := make([]byte, size)
// Read into buffer of that size.
read, err := extattr_list_file(path, EXTATTR_NAMESPACE_USER, &buf[0], size)
if err != nil {
return nil, &XAttrError{"extattr_list_file", path, "", err}
return nil, &Error{"xattr.List", path, "", err}
}
return attrListToStrings(buf[:read]), nil
}
return []string{}, nil
}
// Setxattr associates name and data together as an attribute of path.
func Setxattr(path, name string, data []byte) error {
// Set associates name and data together as an attribute of path.
func Set(path, name string, data []byte) error {
written, err := extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, &data[0], len(data))
if err != nil {
return &XAttrError{"extattr_set_file", path, name, err}
return &Error{"xattr.Set", path, name, err}
}
if written != len(data) {
return &XAttrError{"extattr_set_file", path, name, syscall.E2BIG}
return &Error{"xattr.Set", path, name, syscall.E2BIG}
}
return nil
}
// Removexattr removes the attribute associated with the given path.
func Removexattr(path, name string) error {
// Remove removes the attribute associated with the given path.
func Remove(path, name string) error {
if err := extattr_delete_file(path, EXTATTR_NAMESPACE_USER, name); err != nil {
return &XAttrError{"extattr_delete_file", path, name, err}
return &Error{"xattr.Remove", path, name, err}
}
return nil
}

View file

@ -4,58 +4,58 @@ package xattr
import "syscall"
// Getxattr retrieves extended attribute data associated with path.
func Getxattr(path, name string) ([]byte, error) {
// Get retrieves extended attribute data associated with path.
func Get(path, name string) ([]byte, error) {
// find size.
size, err := syscall.Getxattr(path, name, nil)
if err != nil {
return nil, &XAttrError{"getxattr", path, name, err}
return nil, &Error{"xattr.Get", path, name, err}
}
if size > 0 {
data := make([]byte, size)
// Read into buffer of that size.
read, err := syscall.Getxattr(path, name, data)
if err != nil {
return nil, &XAttrError{"getxattr", path, name, err}
return nil, &Error{"xattr.Get", path, name, err}
}
return data[:read], nil
}
return []byte{}, nil
}
// Listxattr retrieves a list of names of extended attributes associated
// List retrieves a list of names of extended attributes associated
// with the given path in the file system.
func Listxattr(path string) ([]string, error) {
func List(path string) ([]string, error) {
// find size.
size, err := syscall.Listxattr(path, nil)
if err != nil {
return nil, &XAttrError{"listxattr", path, "", err}
return nil, &Error{"xattr.List", path, "", err}
}
if size > 0 {
buf := make([]byte, size)
// Read into buffer of that size.
read, err := syscall.Listxattr(path, buf)
if err != nil {
return nil, &XAttrError{"listxattr", path, "", err}
return nil, &Error{"xattr.List", path, "", err}
}
return nullTermToStrings(buf[:read]), nil
}
return []string{}, nil
}
// Setxattr associates name and data together as an attribute of path.
func Setxattr(path, name string, data []byte) error {
// Set associates name and data together as an attribute of path.
func Set(path, name string, data []byte) error {
if err := syscall.Setxattr(path, name, data, 0); err != nil {
return &XAttrError{"setxattr", path, name, err}
return &Error{"xattr.Set", path, name, err}
}
return nil
}
// Removexattr removes the attribute associated
// Remove removes the attribute associated
// with the given path.
func Removexattr(path, name string) error {
func Remove(path, name string) error {
if err := syscall.Removexattr(path, name); err != nil {
return &XAttrError{"removexattr", path, name, err}
return &Error{"xattr.Remove", path, name, err}
}
return nil
}

View file

@ -18,12 +18,12 @@ func Test_setxattr(t *testing.T) {
}
defer os.Remove(tmp.Name())
err = Setxattr(tmp.Name(), UserPrefix+"test", []byte("test-attr-value"))
err = Set(tmp.Name(), UserPrefix+"test", []byte("test-attr-value"))
if err != nil {
t.Fatal(err)
}
list, err := Listxattr(tmp.Name())
list, err := List(tmp.Name())
if err != nil {
t.Fatal(err)
}
@ -40,7 +40,7 @@ func Test_setxattr(t *testing.T) {
}
var data []byte
data, err = Getxattr(tmp.Name(), UserPrefix+"test")
data, err = Get(tmp.Name(), UserPrefix+"test")
if err != nil {
t.Fatal(err)
}
@ -50,7 +50,7 @@ func Test_setxattr(t *testing.T) {
t.Fail()
}
err = Removexattr(tmp.Name(), UserPrefix+"test")
err = Remove(tmp.Name(), UserPrefix+"test")
if err != nil {
t.Fatal(err)
}