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.
This commit is contained in:
Sebastian Lackner 2019-01-14 02:45:21 +01:00 committed by rfjakob
parent 229a9da74b
commit 0345cc0830
3 changed files with 8 additions and 85 deletions

View File

@ -1,7 +1,6 @@
package syscallcompat package syscallcompat
import ( import (
"os"
"path/filepath" "path/filepath"
"sync" "sync"
"syscall" "syscall"
@ -31,36 +30,6 @@ func emulateMknodat(dirfd int, path string, mode uint32, dev int) error {
return syscall.Mknod(path, mode, dev) 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 // emulateFchownat emulates the syscall for platforms that don't have it
// in the kernel (darwin). // in the kernel (darwin).
func emulateFchownat(dirfd int, path string, uid int, gid int, flags int) (err error) { func emulateFchownat(dirfd int, path string, uid int, gid int, flags int) (err error) {

View File

@ -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) { func TestEmulateFchownat(t *testing.T) {
t.Skipf("TODO") t.Skipf("TODO")
} }

View File

@ -8,6 +8,8 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
"github.com/rfjakob/gocryptfs/internal/tlog"
) )
const ( 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) { 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) { func Fchownat(dirfd int, path string, uid int, gid int, flags int) (err error) {