syscallcompat: Drop Openat emulation on macOS.

This commit is contained in:
Sebastian Lackner 2019-01-14 02:32:28 +01:00 committed by rfjakob
parent d7be766851
commit da557702d7
6 changed files with 49 additions and 71 deletions

View File

@ -11,26 +11,6 @@ import (
var chdirMutex sync.Mutex
// emulateOpenat emulates the syscall for platforms that don't have it
// in the kernel (darwin).
func emulateOpenat(dirfd int, path string, flags int, mode uint32) (int, error) {
if !filepath.IsAbs(path) {
chdirMutex.Lock()
defer chdirMutex.Unlock()
cwd, err := syscall.Open(".", syscall.O_RDONLY, 0)
if err != nil {
return -1, err
}
defer syscall.Close(cwd)
err = syscall.Fchdir(dirfd)
if err != nil {
return -1, err
}
defer syscall.Fchdir(cwd)
}
return syscall.Open(path, flags, mode)
}
// emulateRenameat emulates the syscall for platforms that don't have it
// in the kernel (darwin).
func emulateRenameat(olddirfd int, oldpath string, newdirfd int, newpath string) error {

View File

@ -9,35 +9,6 @@ import (
"golang.org/x/sys/unix"
)
func TestEmulateOpenat(t *testing.T) {
_, err := emulateOpenat(tmpDirFd, "testOpenAt", 0, 0)
if err == nil {
t.Errorf("should have failed")
}
fd, err := os.Create(tmpDir + "/testOpenAt")
if err != nil {
t.Fatal(err)
}
fd.Close()
rawFd, err := emulateOpenat(tmpDirFd, "testOpenAt", 0, 0)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(rawFd)
if rawFd < 0 {
t.Fatalf("rawFd=%d", rawFd)
}
// Test with absolute path
rawFd, err = emulateOpenat(-1, tmpDir+"/testOpenAt", 0, 0)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(rawFd)
if rawFd < 0 {
t.Fatalf("rawFd=%d", rawFd)
}
}
func TestEmulateRenameat(t *testing.T) {
os.Mkdir(tmpDir+"/dir1", 0700)
dir1, err := os.Open(tmpDir + "/dir1")

View File

@ -5,6 +5,8 @@ import (
"syscall"
"golang.org/x/sys/unix"
"github.com/rfjakob/gocryptfs/internal/tlog"
)
// PATH_MAX is the maximum allowed path length on Linux.
@ -42,6 +44,24 @@ func Faccessat(dirfd int, path string, mode uint32) error {
return unix.Faccessat(dirfd, path, mode, 0)
}
// Openat wraps the Openat syscall.
func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
if flags&syscall.O_CREAT != 0 {
// O_CREAT should be used with O_EXCL. O_NOFOLLOW has no effect with O_EXCL.
if flags&syscall.O_EXCL == 0 {
tlog.Warn.Printf("Openat: O_CREAT without O_EXCL: flags = %#x", flags)
flags |= syscall.O_EXCL
}
} else {
// If O_CREAT is not used, we should use O_NOFOLLOW
if flags&syscall.O_NOFOLLOW == 0 {
tlog.Warn.Printf("Openat: O_NOFOLLOW missing: flags = %#x", flags)
flags |= syscall.O_NOFOLLOW
}
}
return unix.Openat(dirfd, path, flags, mode)
}
// 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)

View File

@ -34,6 +34,35 @@ func TestReadlinkat(t *testing.T) {
}
}
func TestOpenat(t *testing.T) {
_, err := Openat(tmpDirFd, "testOpenAt", 0, 0)
if err == nil {
t.Errorf("should have failed")
}
fd, err := os.Create(tmpDir + "/testOpenAt")
if err != nil {
t.Fatal(err)
}
fd.Close()
rawFd, err := Openat(tmpDirFd, "testOpenAt", 0, 0)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(rawFd)
if rawFd < 0 {
t.Fatalf("rawFd=%d", rawFd)
}
// Test with absolute path
rawFd, err = Openat(-1, tmpDir+"/testOpenAt", 0, 0)
if err != nil {
t.Fatal(err)
}
defer syscall.Close(rawFd)
if rawFd < 0 {
t.Fatalf("rawFd=%d", rawFd)
}
}
func TestFchmodat(t *testing.T) {
regular := "TestFchmodat_Regular"
f, err := os.OpenFile(tmpDir+"/"+regular, os.O_CREATE|os.O_WRONLY, 0000)

View File

@ -57,10 +57,6 @@ func Dup3(oldfd int, newfd int, flags int) (err error) {
//// Emulated Syscalls (see emulate.go) ////////////////
////////////////////////////////////////////////////////
func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
return emulateOpenat(dirfd, path, flags, mode)
}
func OpenatUser(dirfd int, path string, flags int, mode uint32, context *fuse.Context) (fd int, err error) {
if context != nil {
runtime.LockOSThread()

View File

@ -58,24 +58,6 @@ func Fallocate(fd int, mode uint32, off int64, len int64) (err error) {
return syscall.Fallocate(fd, mode, off, len)
}
// Openat wraps the Openat syscall.
func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
if flags&syscall.O_CREAT != 0 {
// O_CREAT should be used with O_EXCL. O_NOFOLLOW has no effect with O_EXCL.
if flags&syscall.O_EXCL == 0 {
tlog.Warn.Printf("Openat: O_CREAT without O_EXCL: flags = %#x", flags)
flags |= syscall.O_EXCL
}
} else {
// If O_CREAT is not used, we should use O_NOFOLLOW
if flags&syscall.O_NOFOLLOW == 0 {
tlog.Warn.Printf("Openat: O_NOFOLLOW missing: flags = %#x", flags)
flags |= syscall.O_NOFOLLOW
}
}
return syscall.Openat(dirfd, path, flags, mode)
}
// OpenatUser runs the Openat syscall in the context of a different user.
func OpenatUser(dirfd int, path string, flags int, mode uint32, context *fuse.Context) (fd int, err error) {
if context != nil {