aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Lackner2017-11-30 21:19:45 +0100
committerrfjakob2017-12-01 09:41:52 +0100
commit616a468180c5594283415e4a033f1536c955c77c (patch)
tree262826c6b5734f9c1d49a886f047fa5c0f8e952f
parentf30522a0c14218ad236831d1c229968ea46ec8aa (diff)
syscallcompat: Improve the Openat and Mknodat syscall emulation
This avoids the conversion to an absolute path.
-rw-r--r--internal/syscallcompat/emulate.go29
1 files changed, 14 insertions, 15 deletions
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)
}