Commit 83a4148b authored by Dustin L. Howett's avatar Dustin L. Howett

Go 1.1 on Linux has syscall.(Get|Set)xattr. Keep the cgo implementation for Darwin.

parent 73f26b8d
......@@ -5,49 +5,31 @@ package xattr
#include <sys/types.h>
#include <sys/xattr.h>
#include <stdlib.h>
#cgo linux CFLAGS: -DCGO_LINUX=1
#cgo darwin CFLAGS: -DCGO_DARWIN=1
#if CGO_LINUX == 1
#define XATTR_SHOWCOMPRESSION 0
#define XATTR_NOFOLLOW 0
static int _cgo_setxattr_thunk(const char *path, const char *attr, void *val, size_t size, uint32_t offset, int opts) {
return setxattr(path, attr, val, size, opts);
}
static ssize_t _cgo_getxattr_thunk(const char *path, const char *attr, void *val, size_t size, uint32_t offset, int opts) {
return getxattr(path, attr, val, size);
}
#elif CGO_DARWIN == 1
static int _cgo_setxattr_thunk(const char *path, const char *attr, void *val, size_t size, uint32_t offset, int opts) {
return setxattr(path, attr, val, size, offset, opts);
}
static ssize_t _cgo_getxattr_thunk(const char *path, const char *attr, void *val, size_t size, uint32_t offset, int opts) {
return getxattr(path, attr, val, size, offset, opts);
}
#endif
*/
import "C"
import "unsafe"
const (
XattrNoFollow = C.XATTR_NOFOLLOW
XattrShowCompression = C.XATTR_SHOWCOMPRESSION
XattrReplace = C.XATTR_REPLACE
XattrCreate = C.XATTR_CREATE
XattrShowCompression = C.XATTR_SHOWCOMPRESSION
)
func Setxattr(path, name string, data []byte, offset uint32, options int) error {
func Setxattr(path, name string, data []byte, offset uint32, options int) (err error) {
cpath, cattrname := C.CString(path), C.CString(name)
defer func() {
C.free(unsafe.Pointer(cpath))
C.free(unsafe.Pointer(cattrname))
}()
ret, err := C._cgo_setxattr_thunk(cpath, cattrname, unsafe.Pointer(&data[0]), C.size_t(len(data)), C.uint32_t(offset), C.int(options))
ret, err := C.setxattr(cpath, cattrname, unsafe.Pointer(&data[0]), C.size_t(len(data)), C.u_int32_t(offset), C.int(options))
if ret == -1 {
return err
return
}
return nil
return
}
func Getxattr(path, name string, offset uint32, options int) ([]byte, error) {
......@@ -60,14 +42,14 @@ func Getxattr(path, name string, offset uint32, options int) ([]byte, error) {
var ret C.ssize_t
var err error
ret, err = C._cgo_getxattr_thunk(cpath, cattrname, nil, C.size_t(0), C.uint32_t(offset), C.int(options))
ret, err = C.getxattr(cpath, cattrname, nil, C.size_t(0), C.u_int32_t(offset), C.int(options))
if ret == -1 {
return nil, err
}
data := make([]byte, int(ret))
ret, err = C._cgo_getxattr_thunk(cpath, cattrname, unsafe.Pointer(&data[0]), C.size_t(len(data)), C.uint32_t(offset), C.int(options))
ret, err = C.getxattr(cpath, cattrname, unsafe.Pointer(&data[0]), C.size_t(len(data)), C.u_int32_t(offset), C.int(options))
if ret == -1 {
return nil, err
}
......
package xattr
import "syscall"
const (
XattrNoFollow = 0x00 // Stubbed; Exists on Darwin
XattrShowCompression = 0x00 // Stubbed; Exists on Darwin
XattrReplace = 0x02
XattrCreate = 0x01
)
func Setxattr(path, name string, data []byte, offset uint32, options int) (err error) {
err = syscall.Setxattr(path, name, data, options)
return
}
func Getxattr(path, name string, offset uint32, options int) ([]byte, error) {
var err error
sz, err := syscall.Getxattr(path, name, nil)
if err != nil {
return nil, err
}
data := make([]byte, int(sz))
sz, err = syscall.Getxattr(path, name, data)
if err != nil {
return nil, err
}
return data, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment