From 0345cc08307c01da635ea5546952a584480c9a93 Mon Sep 17 00:00:00 2001 From: Sebastian Lackner Date: Mon, 14 Jan 2019 02:45:21 +0100 Subject: syscallcompat: Drop Fchmodat emulation on macOS. On macOS the function has a flags argument, so we don't need the /proc/self/fd trick used on Linux. --- internal/syscallcompat/emulate.go | 31 -------------------- internal/syscallcompat/emulate_test.go | 53 ---------------------------------- internal/syscallcompat/sys_darwin.go | 9 +++++- 3 files changed, 8 insertions(+), 85 deletions(-) diff --git a/internal/syscallcompat/emulate.go b/internal/syscallcompat/emulate.go index 360c0ac..8a538fb 100644 --- a/internal/syscallcompat/emulate.go +++ b/internal/syscallcompat/emulate.go @@ -1,7 +1,6 @@ package syscallcompat import ( - "os" "path/filepath" "sync" "syscall" @@ -31,36 +30,6 @@ func emulateMknodat(dirfd int, path string, mode uint32, dev int) error { return syscall.Mknod(path, mode, dev) } -// emulateFchmodat emulates the syscall for platforms that don't have it -// in the kernel (darwin). -func emulateFchmodat(dirfd int, path string, mode uint32, 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) - } - // We also don't have Lchmod, so emulate it (poorly). - if flags&unix.AT_SYMLINK_NOFOLLOW != 0 { - fi, err := os.Lstat(path) - if err != nil { - return err - } - if fi.Mode()&os.ModeSymlink != 0 { - return nil - } - } - return syscall.Chmod(path, mode) -} - // emulateFchownat emulates the syscall for platforms that don't have it // in the kernel (darwin). func emulateFchownat(dirfd int, path string, uid int, gid int, flags int) (err error) { diff --git a/internal/syscallcompat/emulate_test.go b/internal/syscallcompat/emulate_test.go index 2747cc1..3f23d91 100644 --- a/internal/syscallcompat/emulate_test.go +++ b/internal/syscallcompat/emulate_test.go @@ -29,59 +29,6 @@ func TestEmulateMknodat(t *testing.T) { } } -func TestEmulateFchmodat(t *testing.T) { - fd, err := os.Create(tmpDir + "/chmod") - if err != nil { - t.Fatal(err) - } - err = fd.Chmod(0654) - if err != nil { - t.Fatal(err) - } - fd.Close() - // Chmod a normal file - err = emulateFchmodat(tmpDirFd, "chmod", 0600, 0) - if err != nil { - t.Fatal(err) - } - var st syscall.Stat_t - err = syscall.Lstat(tmpDir+"/chmod", &st) - if err != nil { - t.Fatal(err) - } - if st.Mode != 0100600 { - t.Fatalf("Wrong mode: have %o, want %o", st.Mode, 0100600) - } - // Chmod a symlink (original file should not change) - err = os.Symlink(tmpDir+"/chmod", tmpDir+"/chmodSymlink") - if err != nil { - t.Fatal(err) - } - err = emulateFchmodat(tmpDirFd, "chmodSymlink", 0123, unix.AT_SYMLINK_NOFOLLOW) - if err != nil { - t.Fatal(err) - } - err = syscall.Lstat(tmpDir+"/chmod", &st) - if err != nil { - t.Fatal(err) - } - if st.Mode != 0100600 { - t.Fatalf("Wrong mode: have %o, want %o", st.Mode, 0100600) - } - // Test with absolute path - err = emulateFchmodat(-1, tmpDir+"/chmod", 0400, 0) - if err != nil { - t.Fatal(err) - } - err = syscall.Lstat(tmpDir+"/chmod", &st) - if err != nil { - t.Fatal(err) - } - if st.Mode != 0100400 { - t.Fatalf("Wrong mode: have %o, want %o", st.Mode, 0100400) - } -} - func TestEmulateFchownat(t *testing.T) { t.Skipf("TODO") } diff --git a/internal/syscallcompat/sys_darwin.go b/internal/syscallcompat/sys_darwin.go index 92639cf..89d93e7 100644 --- a/internal/syscallcompat/sys_darwin.go +++ b/internal/syscallcompat/sys_darwin.go @@ -8,6 +8,8 @@ import ( "golang.org/x/sys/unix" "github.com/hanwen/go-fuse/fuse" + + "github.com/rfjakob/gocryptfs/internal/tlog" ) const ( @@ -92,7 +94,12 @@ func MknodatUser(dirfd int, path string, mode uint32, dev int, context *fuse.Con } func Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) { - return emulateFchmodat(dirfd, path, mode, flags) + // Why would we ever want to call this without AT_SYMLINK_NOFOLLOW? + if flags&unix.AT_SYMLINK_NOFOLLOW == 0 { + tlog.Warn.Printf("Fchmodat: adding missing AT_SYMLINK_NOFOLLOW flag") + flags |= unix.AT_SYMLINK_NOFOLLOW + } + return unix.Fchmodat(dirfd, path, mode, flags) } func Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) { -- cgit v1.2.3