From 1e51424e8a3777b87fda3c68b5ea94fe9604444a Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 5 Sep 2023 20:22:23 -0700 Subject: [PATCH] use syscall.Rename() directly instead of os.Rename() (#17982) --- cmd/os-instrumented.go | 2 +- cmd/os-rename_linux.go | 7 +++++++ cmd/os-rename_nolinux.go | 10 +++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/os-instrumented.go b/cmd/os-instrumented.go index 08e752a40..adeb7ca99 100644 --- a/cmd/os-instrumented.go +++ b/cmd/os-instrumented.go @@ -137,7 +137,7 @@ func MkdirAll(dirPath string, mode os.FileMode) (err error) { // Rename captures time taken to call os.Rename func Rename(src, dst string) (err error) { defer updateOSMetrics(osMetricRename, src, dst)(err) - return os.Rename(src, dst) + return RenameSys(src, dst) } // OpenFile captures time taken to call os.OpenFile diff --git a/cmd/os-rename_linux.go b/cmd/os-rename_linux.go index 533fb35eb..952f8894f 100644 --- a/cmd/os-rename_linux.go +++ b/cmd/os-rename_linux.go @@ -21,6 +21,8 @@ package cmd import ( + "syscall" + "golang.org/x/sys/unix" ) @@ -29,3 +31,8 @@ func Rename2(src, dst string) (err error) { defer updateOSMetrics(osMetricRename2, src, dst)(err) return unix.Renameat2(unix.AT_FDCWD, src, unix.AT_FDCWD, dst, uint(2)) // RENAME_EXCHANGE from 'man renameat2' } + +// RenameSys is low level call in case of Linux this uses syscall.Rename() directly. +func RenameSys(src, dst string) (err error) { + return syscall.Rename(src, dst) +} diff --git a/cmd/os-rename_nolinux.go b/cmd/os-rename_nolinux.go index 836d140f6..8f566137e 100644 --- a/cmd/os-rename_nolinux.go +++ b/cmd/os-rename_nolinux.go @@ -20,10 +20,18 @@ package cmd -import "errors" +import ( + "errors" + "os" +) // Rename2 is not implemented in a non linux environment func Rename2(src, dst string) (err error) { defer updateOSMetrics(osMetricRename2, src, dst)(errors.New("not implemented, skipping")) return errSkipFile } + +// RenameSys is low level call in case of non-Linux this just uses os.Rename() +func RenameSys(src, dst string) (err error) { + return os.Rename(src, dst) +}