Better fix #1062 (return error instead of panic)

This commit is contained in:
Mike Farah
2022-01-14 15:40:24 +11:00
parent 78b45a3eb0
commit 606ef91cc6
4 changed files with 22 additions and 13 deletions
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"os"
)
func tryRenameFile(from string, to string) {
func tryRenameFile(from string, to string) error {
if renameError := os.Rename(from, to); renameError != nil {
log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to)
log.Debug(renameError.Error())
@@ -15,11 +15,11 @@ func tryRenameFile(from string, to string) {
// can't do this rename when running in docker to a file targeted in a mounted volume,
// so gracefully degrade to copying the entire contents.
if copyError := copyFileContents(from, to); copyError != nil {
panic(fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError))
} else {
tryRemoveTempFile(from)
return fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError)
}
tryRemoveTempFile(from)
}
return nil
}
func tryRemoveTempFile(filename string) {
+6 -5
View File
@@ -6,7 +6,7 @@ import (
type writeInPlaceHandler interface {
CreateTempFile() (*os.File, error)
FinishWriteInPlace(evaluatedSuccessfully bool)
FinishWriteInPlace(evaluatedSuccessfully bool) error
}
type writeInPlaceHandlerImpl struct {
@@ -39,13 +39,14 @@ func (w *writeInPlaceHandlerImpl) CreateTempFile() (*os.File, error) {
return file, err
}
func (w *writeInPlaceHandlerImpl) FinishWriteInPlace(evaluatedSuccessfully bool) {
func (w *writeInPlaceHandlerImpl) FinishWriteInPlace(evaluatedSuccessfully bool) error {
log.Debug("Going to write-inplace, evaluatedSuccessfully=%v, target=%v", evaluatedSuccessfully, w.inputFilename)
safelyCloseFile(w.tempFile)
if evaluatedSuccessfully {
log.Debug("Moving temp file to target")
tryRenameFile(w.tempFile.Name(), w.inputFilename)
} else {
tryRemoveTempFile(w.tempFile.Name())
return tryRenameFile(w.tempFile.Name(), w.inputFilename)
}
tryRemoveTempFile(w.tempFile.Name())
return nil
}