2016-07-03 17:51:40 +02:00
|
|
|
package syscallcompat
|
2016-01-07 21:39:41 +01:00
|
|
|
|
2016-07-03 19:14:12 +02:00
|
|
|
import (
|
2017-06-18 15:40:38 +02:00
|
|
|
"log"
|
2019-01-16 15:38:32 +01:00
|
|
|
"path/filepath"
|
2019-01-13 18:12:00 +01:00
|
|
|
"runtime"
|
2016-07-03 19:14:12 +02:00
|
|
|
"syscall"
|
2019-01-16 15:38:32 +01:00
|
|
|
"time"
|
|
|
|
"unsafe"
|
2017-12-03 17:53:14 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2020-05-17 14:18:23 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2016-07-03 19:14:12 +02:00
|
|
|
)
|
|
|
|
|
2018-09-08 18:06:33 +02:00
|
|
|
const (
|
|
|
|
// O_DIRECT means oncached I/O on Linux. No direct equivalent on MacOS and defined
|
|
|
|
// to zero there.
|
|
|
|
O_DIRECT = 0
|
|
|
|
|
|
|
|
// O_PATH is only defined on Linux
|
|
|
|
O_PATH = 0
|
2019-01-13 18:12:00 +01:00
|
|
|
|
2020-09-09 11:16:29 +02:00
|
|
|
// RENAME_NOREPLACE is only defined on Linux
|
|
|
|
RENAME_NOREPLACE = 0
|
|
|
|
|
2019-01-13 18:12:00 +01:00
|
|
|
// KAUTH_UID_NONE and KAUTH_GID_NONE are special values to
|
|
|
|
// revert permissions to the process credentials.
|
|
|
|
KAUTH_UID_NONE = ^uint32(0) - 100
|
|
|
|
KAUTH_GID_NONE = ^uint32(0) - 100
|
2018-09-08 18:06:33 +02:00
|
|
|
)
|
2018-07-04 09:04:00 +02:00
|
|
|
|
2019-01-13 18:12:00 +01:00
|
|
|
// Unfortunately pthread_setugid_np does not have a syscall wrapper yet.
|
|
|
|
func pthread_setugid_np(uid uint32, gid uint32) (err error) {
|
|
|
|
_, _, e1 := syscall.RawSyscall(syscall.SYS_SETTID, uintptr(uid), uintptr(gid), 0)
|
|
|
|
if e1 != 0 {
|
|
|
|
err = e1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:38:32 +01:00
|
|
|
// Unfortunately fsetattrlist does not have a syscall wrapper yet.
|
|
|
|
func fsetattrlist(fd int, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error) {
|
|
|
|
_, _, e1 := syscall.Syscall6(syscall.SYS_FSETATTRLIST, uintptr(fd), uintptr(list), uintptr(buf), uintptr(size), uintptr(options), 0)
|
|
|
|
if e1 != 0 {
|
|
|
|
err = e1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setattrlist already has a syscall wrapper, but it is not exported.
|
|
|
|
func setattrlist(path *byte, list unsafe.Pointer, buf unsafe.Pointer, size uintptr, options int) (err error) {
|
|
|
|
_, _, e1 := syscall.Syscall6(syscall.SYS_SETATTRLIST, uintptr(unsafe.Pointer(path)), uintptr(list), uintptr(buf), uintptr(size), uintptr(options), 0)
|
|
|
|
if e1 != 0 {
|
|
|
|
err = e1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:05:32 +02:00
|
|
|
// Sorry, fallocate is not available on OSX at all and
|
|
|
|
// fcntl F_PREALLOCATE is not accessible from Go.
|
2021-08-30 11:31:01 +02:00
|
|
|
// See https://github.com/rfjakob/gocryptfs/issues/18 if you want to help.
|
2017-02-16 19:23:17 +01:00
|
|
|
func EnospcPrealloc(fd int, off int64, len int64) error {
|
2016-01-09 13:04:48 +01:00
|
|
|
return nil
|
2016-01-07 21:39:41 +01:00
|
|
|
}
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2016-07-03 20:05:32 +02:00
|
|
|
// See above.
|
2017-02-16 19:23:17 +01:00
|
|
|
func Fallocate(fd int, mode uint32, off int64, len int64) error {
|
2016-07-03 20:05:32 +02:00
|
|
|
return syscall.EOPNOTSUPP
|
|
|
|
}
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2017-11-30 17:07:55 +01:00
|
|
|
// Dup3 is not available on Darwin, so we use Dup2 instead.
|
|
|
|
func Dup3(oldfd int, newfd int, flags int) (err error) {
|
|
|
|
if flags != 0 {
|
|
|
|
log.Panic("darwin does not support dup3 flags")
|
2016-07-03 20:05:32 +02:00
|
|
|
}
|
2017-11-30 17:07:55 +01:00
|
|
|
return syscall.Dup2(oldfd, newfd)
|
2016-07-03 20:05:32 +02:00
|
|
|
}
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2017-11-30 17:07:55 +01:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//// Emulated Syscalls (see emulate.go) ////////////////
|
|
|
|
////////////////////////////////////////////////////////
|
2016-07-03 19:14:12 +02:00
|
|
|
|
2019-01-12 20:42:05 +01:00
|
|
|
func OpenatUser(dirfd int, path string, flags int, mode uint32, context *fuse.Context) (fd int, err error) {
|
2019-01-13 18:12:00 +01:00
|
|
|
if context != nil {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
2019-01-13 14:05:03 +01:00
|
|
|
|
2019-01-12 20:42:05 +01:00
|
|
|
return Openat(dirfd, path, flags, mode)
|
|
|
|
}
|
|
|
|
|
2017-11-30 17:07:55 +01:00
|
|
|
func Mknodat(dirfd int, path string, mode uint32, dev int) (err error) {
|
|
|
|
return emulateMknodat(dirfd, path, mode, dev)
|
2017-06-18 15:40:38 +02:00
|
|
|
}
|
2017-11-26 21:59:24 +01:00
|
|
|
|
2019-01-12 21:19:23 +01:00
|
|
|
func MknodatUser(dirfd int, path string, mode uint32, dev int, context *fuse.Context) (err error) {
|
2019-01-13 18:12:00 +01:00
|
|
|
if context != nil {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
2019-01-13 14:05:03 +01:00
|
|
|
|
2019-01-12 21:19:23 +01:00
|
|
|
return Mknodat(dirfd, path, mode, dev)
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:54:16 +01:00
|
|
|
func FchmodatNofollow(dirfd int, path string, mode uint32) (err error) {
|
|
|
|
return unix.Fchmodat(dirfd, path, mode, unix.AT_SYMLINK_NOFOLLOW)
|
2017-11-29 12:54:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 21:22:52 +01:00
|
|
|
func SymlinkatUser(oldpath string, newdirfd int, newpath string, context *fuse.Context) (err error) {
|
2019-01-13 18:12:00 +01:00
|
|
|
if context != nil {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
|
|
|
|
2021-06-05 15:06:30 +02:00
|
|
|
return unix.Symlinkat(oldpath, newdirfd, newpath)
|
2019-01-12 21:22:52 +01:00
|
|
|
}
|
|
|
|
|
2021-05-22 22:01:46 +02:00
|
|
|
func MkdiratUser(dirfd int, path string, mode uint32, context *fuse.Context) (err error) {
|
|
|
|
if context != nil {
|
2019-01-13 18:12:00 +01:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2021-05-22 22:01:46 +02:00
|
|
|
err = pthread_setugid_np(context.Owner.Uid, context.Owner.Gid)
|
2019-01-13 18:12:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pthread_setugid_np(KAUTH_UID_NONE, KAUTH_GID_NONE)
|
|
|
|
}
|
2019-01-13 14:05:03 +01:00
|
|
|
|
2021-06-05 15:06:30 +02:00
|
|
|
return unix.Mkdirat(dirfd, path, mode)
|
2019-01-12 20:57:31 +01:00
|
|
|
}
|
|
|
|
|
2019-01-16 15:38:32 +01:00
|
|
|
type attrList struct {
|
|
|
|
bitmapCount uint16
|
|
|
|
_ uint16
|
|
|
|
CommonAttr uint32
|
|
|
|
VolAttr uint32
|
|
|
|
DirAttr uint32
|
|
|
|
FileAttr uint32
|
|
|
|
Forkattr uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func timesToAttrList(a *time.Time, m *time.Time) (attrList attrList, attributes [2]unix.Timespec) {
|
|
|
|
attrList.bitmapCount = unix.ATTR_BIT_MAP_COUNT
|
|
|
|
attrList.CommonAttr = 0
|
|
|
|
i := 0
|
|
|
|
if m != nil {
|
|
|
|
attributes[i] = unix.Timespec(fuse.UtimeToTimespec(m))
|
|
|
|
attrList.CommonAttr |= unix.ATTR_CMN_MODTIME
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
if a != nil {
|
|
|
|
attributes[i] = unix.Timespec(fuse.UtimeToTimespec(a))
|
|
|
|
attrList.CommonAttr |= unix.ATTR_CMN_ACCTIME
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
return attrList, attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutimesNano syscall.
|
|
|
|
func FutimesNano(fd int, a *time.Time, m *time.Time) (err error) {
|
|
|
|
attrList, attributes := timesToAttrList(a, m)
|
|
|
|
return fsetattrlist(fd, unsafe.Pointer(&attrList), unsafe.Pointer(&attributes),
|
|
|
|
unsafe.Sizeof(attributes), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UtimesNanoAtNofollow is like UtimesNanoAt but never follows symlinks.
|
|
|
|
//
|
|
|
|
// Unfortunately we cannot use unix.UtimesNanoAt since it is broken and just
|
|
|
|
// ignores the provided 'dirfd'. In addition, it also lacks handling of 'nil'
|
|
|
|
// pointers (used to preserve one of both timestamps).
|
|
|
|
func UtimesNanoAtNofollow(dirfd int, path string, a *time.Time, m *time.Time) (err error) {
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
chdirMutex.Lock()
|
|
|
|
defer chdirMutex.Unlock()
|
|
|
|
var cwd int
|
|
|
|
cwd, err = syscall.Open(".", syscall.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer syscall.Close(cwd)
|
|
|
|
err = syscall.Fchdir(dirfd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer syscall.Fchdir(cwd)
|
|
|
|
}
|
|
|
|
|
|
|
|
_p0, err := syscall.BytePtrFromString(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
attrList, attributes := timesToAttrList(a, m)
|
|
|
|
return setattrlist(_p0, unsafe.Pointer(&attrList), unsafe.Pointer(&attributes),
|
|
|
|
unsafe.Sizeof(attributes), unix.FSOPT_NOFOLLOW)
|
|
|
|
}
|
|
|
|
|
2017-12-03 17:53:14 +01:00
|
|
|
func Getdents(fd int) ([]fuse.DirEntry, error) {
|
2021-05-23 12:00:09 +02:00
|
|
|
entries, _, err := emulateGetdents(fd)
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetdentsSpecial(fd int) (entries []fuse.DirEntry, entriesSpecial []fuse.DirEntry, err error) {
|
2017-12-03 17:53:14 +01:00
|
|
|
return emulateGetdents(fd)
|
|
|
|
}
|
2020-09-09 11:16:29 +02:00
|
|
|
|
|
|
|
// Renameat2 does not exist on Darwin, so we call Renameat and ignore the flags.
|
|
|
|
func Renameat2(olddirfd int, oldpath string, newdirfd int, newpath string, flags uint) (err error) {
|
|
|
|
return unix.Renameat(olddirfd, oldpath, newdirfd, newpath)
|
|
|
|
}
|