Skip to content

Commit

Permalink
Fixed #1062
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Jan 14, 2022
1 parent 84ddf18 commit 78b45a3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
14 changes: 6 additions & 8 deletions pkg/yqlib/file_utils.go
@@ -1,30 +1,28 @@
package yqlib

import (
"fmt"
"io"
"io/ioutil"
"os"
)

func safelyRenameFile(from string, to string) {
func tryRenameFile(from string, to string) {
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())
log.Debug("going to try copying instead")
// 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 {
log.Errorf("Failed copying from %v to %v", from, to)
log.Error(copyError.Error())
panic(fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError))
} else {
removeErr := os.Remove(from)
if removeErr != nil {
log.Errorf("failed removing original file: %s", from)
}
tryRemoveTempFile(from)
}
}
}

func tryRemoveFile(filename string) {
func tryRemoveTempFile(filename string) {
log.Debug("Removing temp file: %v", filename)
removeErr := os.Remove(filename)
if removeErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/yqlib/front_matter.go
Expand Up @@ -33,7 +33,7 @@ func (f *frontMatterHandlerImpl) GetContentReader() io.Reader {
}

func (f *frontMatterHandlerImpl) CleanUp() {
tryRemoveFile(f.yamlFrontMatterFilename)
tryRemoveTempFile(f.yamlFrontMatterFilename)
}

// Splits the given file by yaml front matter
Expand Down
6 changes: 3 additions & 3 deletions pkg/yqlib/front_matter_test.go
Expand Up @@ -66,7 +66,7 @@ yaml: doc
}
test.AssertResult(t, expectedContent, string(contentBytes))

tryRemoveFile(file)
tryRemoveTempFile(file)
fmHandler.CleanUp()
}

Expand Down Expand Up @@ -103,7 +103,7 @@ yaml: doc
}
test.AssertResult(t, expectedContent, string(contentBytes))

tryRemoveFile(file)
tryRemoveTempFile(file)
fmHandler.CleanUp()
}

Expand Down Expand Up @@ -137,6 +137,6 @@ yaml: doc
}
test.AssertResult(t, expectedContent, string(contentBytes))

tryRemoveFile(file)
tryRemoveTempFile(file)
fmHandler.CleanUp()
}
4 changes: 2 additions & 2 deletions pkg/yqlib/write_in_place_handler.go
Expand Up @@ -44,8 +44,8 @@ func (w *writeInPlaceHandlerImpl) FinishWriteInPlace(evaluatedSuccessfully bool)
safelyCloseFile(w.tempFile)
if evaluatedSuccessfully {
log.Debug("Moving temp file to target")
safelyRenameFile(w.tempFile.Name(), w.inputFilename)
tryRenameFile(w.tempFile.Name(), w.inputFilename)
} else {
tryRemoveFile(w.tempFile.Name())
tryRemoveTempFile(w.tempFile.Name())
}
}

0 comments on commit 78b45a3

Please sign in to comment.