syscallcompat: Drop Unlinkat emulation on macOS.
This commit is contained in:
parent
42bf6d1c68
commit
229a9da74b
@ -11,29 +11,6 @@ import (
|
||||
|
||||
var chdirMutex sync.Mutex
|
||||
|
||||
// emulateUnlinkat emulates the syscall for platforms that don't have it
|
||||
// in the kernel (darwin).
|
||||
func emulateUnlinkat(dirfd int, path string, flags int) (err error) {
|
||||
if !filepath.IsAbs(path) {
|
||||
chdirMutex.Lock()
|
||||
defer chdirMutex.Unlock()
|
||||
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)
|
||||
}
|
||||
if (flags & unix.AT_REMOVEDIR) != 0 {
|
||||
return syscall.Rmdir(path)
|
||||
}
|
||||
return syscall.Unlink(path)
|
||||
}
|
||||
|
||||
// emulateMknodat emulates the syscall for platforms that don't have it
|
||||
// in the kernel (darwin).
|
||||
func emulateMknodat(dirfd int, path string, mode uint32, dev int) error {
|
||||
|
@ -9,59 +9,6 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestEmulateUnlinkat(t *testing.T) {
|
||||
os.Mkdir(tmpDir+"/unlink1", 0700)
|
||||
dirfd, err := os.Open(tmpDir + "/unlink1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dirfd.Close()
|
||||
// Try to delete file
|
||||
fd, err := os.Create(tmpDir + "/unlink1/f1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fd.Close()
|
||||
err = emulateUnlinkat(int(dirfd.Fd()), "f1", 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = os.Stat(tmpDir + "/unlink1/f1")
|
||||
if err == nil {
|
||||
t.Fatalf("file not deleted!")
|
||||
}
|
||||
// Try to delete dir
|
||||
err = os.Mkdir(tmpDir+"/unlink1/d1", 0700)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = emulateUnlinkat(int(dirfd.Fd()), "d1", 0)
|
||||
if err == nil {
|
||||
t.Fatalf("this should fail due to missing AT_REMOVEDIR flag")
|
||||
}
|
||||
err = emulateUnlinkat(int(dirfd.Fd()), "d1", unix.AT_REMOVEDIR)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = os.Stat(tmpDir + "/unlink1/d1")
|
||||
if err == nil {
|
||||
t.Fatalf("dir not deleted!")
|
||||
}
|
||||
// Test with absolute path
|
||||
err = os.Mkdir(tmpDir+"/unlink1/d1", 0700)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = emulateUnlinkat(-1, tmpDir+"/unlink1/d1", unix.AT_REMOVEDIR)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = os.Stat(tmpDir + "/unlink1/d1")
|
||||
if err == nil {
|
||||
t.Fatalf("dir not deleted!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmulateMknodat(t *testing.T) {
|
||||
err := emulateMknodat(tmpDirFd, "fifo1", unix.S_IFIFO, 0)
|
||||
if err != nil {
|
||||
|
@ -67,6 +67,11 @@ func Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err e
|
||||
return unix.Renameat(olddirfd, oldpath, newdirfd, newpath)
|
||||
}
|
||||
|
||||
// Unlinkat syscall.
|
||||
func Unlinkat(dirfd int, path string, flags int) (err error) {
|
||||
return unix.Unlinkat(dirfd, path, flags)
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestReadlinkat(t *testing.T) {
|
||||
@ -100,6 +102,59 @@ func TestRenameat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnlinkat(t *testing.T) {
|
||||
os.Mkdir(tmpDir+"/unlink1", 0700)
|
||||
dirfd, err := os.Open(tmpDir + "/unlink1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer dirfd.Close()
|
||||
// Try to delete file
|
||||
fd, err := os.Create(tmpDir + "/unlink1/f1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fd.Close()
|
||||
err = Unlinkat(int(dirfd.Fd()), "f1", 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = os.Stat(tmpDir + "/unlink1/f1")
|
||||
if err == nil {
|
||||
t.Fatalf("file not deleted!")
|
||||
}
|
||||
// Try to delete dir
|
||||
err = os.Mkdir(tmpDir+"/unlink1/d1", 0700)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = Unlinkat(int(dirfd.Fd()), "d1", 0)
|
||||
if err == nil {
|
||||
t.Fatalf("this should fail due to missing AT_REMOVEDIR flag")
|
||||
}
|
||||
err = Unlinkat(int(dirfd.Fd()), "d1", unix.AT_REMOVEDIR)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = os.Stat(tmpDir + "/unlink1/d1")
|
||||
if err == nil {
|
||||
t.Fatalf("dir not deleted!")
|
||||
}
|
||||
// Test with absolute path
|
||||
err = os.Mkdir(tmpDir+"/unlink1/d1", 0700)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = Unlinkat(-1, tmpDir+"/unlink1/d1", unix.AT_REMOVEDIR)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = os.Stat(tmpDir + "/unlink1/d1")
|
||||
if err == nil {
|
||||
t.Fatalf("dir not deleted!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFchmodat(t *testing.T) {
|
||||
regular := "TestFchmodat_Regular"
|
||||
f, err := os.OpenFile(tmpDir+"/"+regular, os.O_CREATE|os.O_WRONLY, 0000)
|
||||
|
@ -72,10 +72,6 @@ func OpenatUser(dirfd int, path string, flags int, mode uint32, context *fuse.Co
|
||||
return Openat(dirfd, path, flags, mode)
|
||||
}
|
||||
|
||||
func Unlinkat(dirfd int, path string, flags int) (err error) {
|
||||
return emulateUnlinkat(dirfd, path, flags)
|
||||
}
|
||||
|
||||
func Mknodat(dirfd int, path string, mode uint32, dev int) (err error) {
|
||||
return emulateMknodat(dirfd, path, mode, dev)
|
||||
}
|
||||
|
@ -80,11 +80,6 @@ func OpenatUser(dirfd int, path string, flags int, mode uint32, context *fuse.Co
|
||||
return Openat(dirfd, path, flags, mode)
|
||||
}
|
||||
|
||||
// Unlinkat syscall.
|
||||
func Unlinkat(dirfd int, path string, flags int) (err error) {
|
||||
return unix.Unlinkat(dirfd, path, flags)
|
||||
}
|
||||
|
||||
// Mknodat wraps the Mknodat syscall.
|
||||
func Mknodat(dirfd int, path string, mode uint32, dev int) (err error) {
|
||||
return syscall.Mknodat(dirfd, path, mode, dev)
|
||||
|
Loading…
Reference in New Issue
Block a user