2017-12-06 21:07:24 +01:00
|
|
|
package syscallcompat
|
|
|
|
|
|
|
|
import (
|
2019-01-02 16:56:23 +01:00
|
|
|
"bytes"
|
2017-12-07 00:05:28 +01:00
|
|
|
"syscall"
|
|
|
|
|
2017-12-06 21:07:24 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2018-06-19 20:13:56 +02:00
|
|
|
// PATH_MAX is the maximum allowed path length on Linux.
|
|
|
|
// It is not defined on Darwin, so we use the Linux value.
|
|
|
|
const PATH_MAX = 4096
|
2018-06-08 00:47:48 +02:00
|
|
|
|
2018-11-04 22:01:18 +01:00
|
|
|
// Readlinkat is a convenience wrapper around unix.Readlinkat() that takes
|
|
|
|
// care of buffer sizing. Implemented like os.Readlink().
|
2017-12-06 21:07:24 +01:00
|
|
|
func Readlinkat(dirfd int, path string) (string, error) {
|
|
|
|
// Allocate the buffer exponentially like os.Readlink does.
|
|
|
|
for bufsz := 128; ; bufsz *= 2 {
|
|
|
|
buf := make([]byte, bufsz)
|
|
|
|
n, err := unix.Readlinkat(dirfd, path, buf)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if n < bufsz {
|
|
|
|
return string(buf[0:n]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 00:05:28 +01:00
|
|
|
|
|
|
|
// Faccessat exists both in Linux and in MacOS 10.10+, but the Linux version
|
|
|
|
// DOES NOT support any flags. Emulate AT_SYMLINK_NOFOLLOW like glibc does.
|
|
|
|
func Faccessat(dirfd int, path string, mode uint32) error {
|
|
|
|
var st unix.Stat_t
|
|
|
|
err := Fstatat(dirfd, path, &st, unix.AT_SYMLINK_NOFOLLOW)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if st.Mode&syscall.S_IFMT == syscall.S_IFLNK {
|
|
|
|
// Pretend that a symlink is always accessible
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return unix.Faccessat(dirfd, path, mode, 0)
|
|
|
|
}
|
2017-12-12 14:42:49 +01:00
|
|
|
|
|
|
|
// Linkat exists both in Linux and in MacOS 10.10+.
|
|
|
|
func Linkat(olddirfd int, oldpath string, newdirfd int, newpath string, flags int) (err error) {
|
|
|
|
return unix.Linkat(olddirfd, oldpath, newdirfd, newpath, flags)
|
|
|
|
}
|
2019-01-02 16:56:23 +01:00
|
|
|
|
|
|
|
const XATTR_SIZE_MAX = 65536
|
|
|
|
|
|
|
|
// Make the buffer 1kB bigger so we can detect overflows
|
|
|
|
const XATTR_BUFSZ = XATTR_SIZE_MAX + 1024
|
|
|
|
|
|
|
|
// Fgetxattr is a wrapper around unix.Fgetxattr that handles the buffer sizing.
|
|
|
|
func Fgetxattr(fd int, attr string) (val []byte, err error) {
|
|
|
|
// If the buffer is too small to fit the value, Linux and MacOS react
|
|
|
|
// differently:
|
|
|
|
// Linux: returns an ERANGE error and "-1" bytes.
|
|
|
|
// MacOS: truncates the value and returns "size" bytes.
|
|
|
|
//
|
|
|
|
// We choose the simple approach of buffer that is bigger than the limit on
|
|
|
|
// Linux, and return an error for everything that is bigger (which can
|
|
|
|
// only happen on MacOS).
|
|
|
|
//
|
|
|
|
// See https://github.com/pkg/xattr for a smarter solution.
|
2019-01-02 20:48:46 +01:00
|
|
|
// TODO: smarter buffer sizing?
|
2019-01-02 16:56:23 +01:00
|
|
|
buf := make([]byte, XATTR_BUFSZ)
|
|
|
|
sz, err := unix.Fgetxattr(fd, attr, buf)
|
|
|
|
if err == syscall.ERANGE {
|
|
|
|
// Do NOT return ERANGE - the user might retry ad inifinitum!
|
|
|
|
return nil, syscall.EOVERFLOW
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sz >= XATTR_SIZE_MAX {
|
|
|
|
return nil, syscall.EOVERFLOW
|
|
|
|
}
|
|
|
|
// Copy only the actually used bytes to a new (smaller) buffer
|
|
|
|
// so "buf" never leaves the function and can be allocated on the stack.
|
|
|
|
val = make([]byte, sz)
|
|
|
|
copy(val, buf)
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2019-01-02 20:48:46 +01:00
|
|
|
// Flistxattr is a wrapper for unix.Flistxattr that handles buffer sizing and
|
2019-01-02 16:56:23 +01:00
|
|
|
// parsing the returned blob to a string slice.
|
|
|
|
func Flistxattr(fd int) (attrs []string, err error) {
|
|
|
|
// See the buffer sizing comments in Fgetxattr.
|
2019-01-02 20:48:46 +01:00
|
|
|
// TODO: smarter buffer sizing?
|
2019-01-02 16:56:23 +01:00
|
|
|
buf := make([]byte, XATTR_BUFSZ)
|
|
|
|
sz, err := unix.Flistxattr(fd, buf)
|
|
|
|
if err == syscall.ERANGE {
|
|
|
|
// Do NOT return ERANGE - the user might retry ad inifinitum!
|
|
|
|
return nil, syscall.EOVERFLOW
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sz >= XATTR_SIZE_MAX {
|
|
|
|
return nil, syscall.EOVERFLOW
|
|
|
|
}
|
2019-01-02 20:48:46 +01:00
|
|
|
attrs = parseListxattrBlob(buf[:sz])
|
|
|
|
return attrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Llistxattr is a wrapper for unix.Llistxattr that handles buffer sizing and
|
|
|
|
// parsing the returned blob to a string slice.
|
|
|
|
func Llistxattr(path string) (attrs []string, err error) {
|
|
|
|
// TODO: smarter buffer sizing?
|
|
|
|
buf := make([]byte, XATTR_BUFSZ)
|
|
|
|
sz, err := unix.Llistxattr(path, buf)
|
|
|
|
if err == syscall.ERANGE {
|
|
|
|
// Do NOT return ERANGE - the user might retry ad inifinitum!
|
|
|
|
return nil, syscall.EOVERFLOW
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sz >= XATTR_SIZE_MAX {
|
|
|
|
return nil, syscall.EOVERFLOW
|
|
|
|
}
|
|
|
|
attrs = parseListxattrBlob(buf[:sz])
|
|
|
|
return attrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseListxattrBlob(buf []byte) (attrs []string) {
|
2019-01-02 16:56:23 +01:00
|
|
|
parts := bytes.Split(buf, []byte{0})
|
|
|
|
for _, part := range parts {
|
|
|
|
if len(part) == 0 {
|
|
|
|
// Last part is empty, ignore
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attrs = append(attrs, string(part))
|
|
|
|
}
|
2019-01-02 20:48:46 +01:00
|
|
|
return attrs
|
2019-01-02 16:56:23 +01:00
|
|
|
}
|