yq/pkg/yqlib/file_utils.go

86 lines
2.0 KiB
Go
Raw Normal View History

2020-11-29 09:25:47 +00:00
package yqlib
import (
2022-01-14 04:22:55 +00:00
"fmt"
2020-11-29 09:25:47 +00:00
"io"
"os"
)
func tryRenameFile(from string, to string) error {
2020-11-29 09:25:47 +00:00
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())
2022-01-14 04:22:55 +00:00
log.Debug("going to try copying instead")
2020-11-29 09:25:47 +00:00
// 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 {
return fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError)
2020-11-29 09:25:47 +00:00
}
tryRemoveTempFile(from)
2020-11-29 09:25:47 +00:00
}
return nil
2020-11-29 09:25:47 +00:00
}
2022-01-14 04:22:55 +00:00
func tryRemoveTempFile(filename string) {
2021-07-18 02:28:46 +00:00
log.Debug("Removing temp file: %v", filename)
removeErr := os.Remove(filename)
if removeErr != nil {
log.Errorf("Failed to remove temp file: %v", filename)
}
}
2020-11-29 09:25:47 +00:00
// thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
func copyFileContents(src, dst string) (err error) {
2021-11-25 09:24:51 +00:00
// ignore CWE-22 gosec issue - that's more targeted for http based apps that run in a public directory,
// and ensuring that it's not possible to give a path to a file outside thar directory.
in, err := os.Open(src) // #nosec
2020-11-29 09:25:47 +00:00
if err != nil {
return err
}
defer safelyCloseFile(in)
out, err := os.Create(dst)
if err != nil {
return err
}
defer safelyCloseFile(out)
if _, err = io.Copy(out, in); err != nil {
return err
}
return out.Sync()
}
2021-07-18 02:28:46 +00:00
func SafelyCloseReader(reader io.Reader) {
switch reader := reader.(type) {
case *os.File:
safelyCloseFile(reader)
}
}
2020-11-29 09:25:47 +00:00
func safelyCloseFile(file *os.File) {
err := file.Close()
if err != nil {
log.Error("Error closing file!")
log.Error(err.Error())
}
}
2021-07-18 02:28:46 +00:00
func createTempFile() (*os.File, error) {
_, err := os.Stat(os.TempDir())
if os.IsNotExist(err) {
err = os.Mkdir(os.TempDir(), 0700)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
file, err := os.CreateTemp("", "temp")
2021-07-18 02:28:46 +00:00
if err != nil {
return nil, err
}
return file, err
}