From 616a468180c5594283415e4a033f1536c955c77c Mon Sep 17 00:00:00 2001 From: Sebastian Lackner Date: Thu, 30 Nov 2017 21:19:45 +0100 Subject: syscallcompat: Improve the Openat and Mknodat syscall emulation This avoids the conversion to an absolute path. --- internal/syscallcompat/emulate.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'internal/syscallcompat') diff --git a/internal/syscallcompat/emulate.go b/internal/syscallcompat/emulate.go index 0aca28e..96776e2 100644 --- a/internal/syscallcompat/emulate.go +++ b/internal/syscallcompat/emulate.go @@ -14,21 +14,19 @@ 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) { - chdirMutex.Lock() - defer chdirMutex.Unlock() if !filepath.IsAbs(path) { - // Save the old working directory - oldWd, err := os.Getwd() + chdirMutex.Lock() + defer chdirMutex.Unlock() + cwd, err := syscall.Open(".", syscall.O_RDONLY, 0) if err != nil { return -1, err } - // Chdir to target directory + defer syscall.Close(cwd) err = syscall.Fchdir(dirfd) if err != nil { return -1, err } - // Chdir back at the end - defer os.Chdir(oldWd) + defer syscall.Fchdir(cwd) } return syscall.Open(path, flags, mode) } @@ -88,18 +86,19 @@ func emulateUnlinkat(dirfd int, path string, flags int) (err error) { // 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 { - chdirMutex.Lock() - defer chdirMutex.Unlock() if !filepath.IsAbs(path) { - oldWd, err := os.Getwd() + chdirMutex.Lock() + defer chdirMutex.Unlock() + cwd, err := syscall.Open(".", syscall.O_RDONLY, 0) if err != nil { return err } - defer os.Chdir(oldWd) - } - path, err := dirfdAbs(dirfd, path) - if err != nil { - return err + defer syscall.Close(cwd) + err = syscall.Fchdir(dirfd) + if err != nil { + return err + } + defer syscall.Fchdir(cwd) } return syscall.Mknod(path, mode, dev) } -- cgit v1.2.3