Emit error in SafeMove if remove from source fails (#5251)

This commit is contained in:
WithoutPants 2024-09-11 14:29:16 +10:00 committed by GitHub
parent 71e39e5cb8
commit 17be7e97d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 5 deletions

View File

@ -8,8 +8,6 @@ import (
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
"github.com/stashapp/stash/pkg/logger"
) )
// CopyFile copies the contents of the file at srcpath to a regular file at dstpath. // CopyFile copies the contents of the file at srcpath to a regular file at dstpath.
@ -56,6 +54,7 @@ func CopyFile(srcpath, dstpath string) (err error) {
} }
// SafeMove attempts to move the file with path src to dest using os.Rename. If this fails, then it copies src to dest, then deletes src. // SafeMove attempts to move the file with path src to dest using os.Rename. If this fails, then it copies src to dest, then deletes src.
// If the copy fails, or the delete fails, the function will return an error.
func SafeMove(src, dst string) error { func SafeMove(src, dst string) error {
err := os.Rename(src, dst) err := os.Rename(src, dst)
@ -65,9 +64,11 @@ func SafeMove(src, dst string) error {
return fmt.Errorf("copying file during SaveMove failed with: '%w'; renaming file failed previously with: '%v'", copyErr, err) return fmt.Errorf("copying file during SaveMove failed with: '%w'; renaming file failed previously with: '%v'", copyErr, err)
} }
err = os.Remove(src) removeErr := os.Remove(src)
if err != nil { if removeErr != nil {
logger.Errorf("error removing old file %s during SafeMove: %v", src, err) // if we can't remove the old file, remove the new one and fail
_ = os.Remove(dst)
return fmt.Errorf("removing old file during SafeMove failed with: '%w'; renaming file failed previously with: '%v'", removeErr, err)
} }
} }