From 17be7e97d312c7b07f75bfe72228ed8328923eda Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:29:16 +1000 Subject: [PATCH] Emit error in SafeMove if remove from source fails (#5251) --- pkg/fsutil/file.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/fsutil/file.go b/pkg/fsutil/file.go index 19cf5ced6..db5a13155 100644 --- a/pkg/fsutil/file.go +++ b/pkg/fsutil/file.go @@ -8,8 +8,6 @@ import ( "regexp" "runtime" "strings" - - "github.com/stashapp/stash/pkg/logger" ) // 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. +// If the copy fails, or the delete fails, the function will return an error. func SafeMove(src, dst string) error { 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) } - err = os.Remove(src) - if err != nil { - logger.Errorf("error removing old file %s during SafeMove: %v", src, err) + removeErr := os.Remove(src) + if removeErr != nil { + // 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) } }