use syscall.Rename() directly instead of os.Rename() (#17982)

This commit is contained in:
Harshavardhana 2023-09-05 20:22:23 -07:00 committed by GitHub
parent 5b114b43f7
commit 1e51424e8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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)
}