diff options
-rw-r--r-- | internal/syscallcompat/emulate.go | 23 | ||||
-rw-r--r-- | internal/syscallcompat/emulate_test.go | 53 | ||||
-rw-r--r-- | internal/syscallcompat/sys_common.go | 5 | ||||
-rw-r--r-- | internal/syscallcompat/sys_common_test.go | 55 | ||||
-rw-r--r-- | internal/syscallcompat/sys_darwin.go | 4 | ||||
-rw-r--r-- | internal/syscallcompat/sys_linux.go | 5 |
6 files changed, 60 insertions, 85 deletions
diff --git a/internal/syscallcompat/emulate.go b/internal/syscallcompat/emulate.go index 7fea3a8..360c0ac 100644 --- a/internal/syscallcompat/emulate.go +++ b/internal/syscallcompat/emulate.go @@ -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 { diff --git a/internal/syscallcompat/emulate_test.go b/internal/syscallcompat/emulate_test.go index 7d34c0c..2747cc1 100644 --- a/internal/syscallcompat/emulate_test.go +++ b/internal/syscallcompat/emulate_test.go @@ -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 { diff --git a/internal/syscallcompat/sys_common.go b/internal/syscallcompat/sys_common.go index d4b2e0d..75d2509 100644 --- a/internal/syscallcompat/sys_common.go +++ b/internal/syscallcompat/sys_common.go @@ -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) diff --git a/internal/syscallcompat/sys_common_test.go b/internal/syscallcompat/sys_common_test.go index 3e38eb2..4556279 100644 --- a/internal/syscallcompat/sys_common_test.go +++ b/internal/syscallcompat/sys_common_test.go @@ -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) diff --git a/internal/syscallcompat/sys_darwin.go b/internal/syscallcompat/sys_darwin.go index f49e41a..92639cf 100644 --- a/internal/syscallcompat/sys_darwin.go +++ b/internal/syscallcompat/sys_darwin.go @@ -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) } diff --git a/internal/syscallcompat/sys_linux.go b/internal/syscallcompat/sys_linux.go index c58f51d..28a7149 100644 --- a/internal/syscallcompat/sys_linux.go +++ b/internal/syscallcompat/sys_linux.go @@ -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) |