From 1b8b1d7261f08c6f8448e777e7f9ccf78d1aeeaa Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Sun, 4 Jun 2023 13:42:44 -0600 Subject: [PATCH] Replace os.Rename with copy and delete --- main.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 26c97de..4ca069d 100644 --- a/main.go +++ b/main.go @@ -172,7 +172,7 @@ func moveBuildArtifacts(outDirectory, romDirectory string) bool { } newROMs = true log.Printf("new build found - moving file %s", v.Name()) - err := os.Rename(fmt.Sprintf("%s/%s", outDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name())) + err := moveBuildFile(fmt.Sprintf("%s/%s", outDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name())) if err != nil { log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err) continue @@ -222,3 +222,39 @@ func hashFile(filename string) (string, error) { return fmt.Sprintf("%x", h.Sum(nil)), nil } + +// A custom "move file" function because in docker container the mounted folders are different overlay filesystems +// Instead of os.Rename, we must copy and delete +func moveBuildFile(src, dst string) error { + sourceFileStat, err := os.Stat(src) + if err != nil { + return err + } + + if !sourceFileStat.Mode().IsRegular() { + return fmt.Errorf("%s is not a regular file", src) + } + + source, err := os.Open(src) + if err != nil { + return err + } + defer source.Close() + + destination, err := os.Create(dst) + if err != nil { + return err + } + defer destination.Close() + _, err = io.Copy(destination, source) + if err != nil { + return fmt.Errorf("failed to copy file: %v", err) + } + + err = os.Remove(src) + if err != nil { + return fmt.Errorf("failed to delete source file after copy: %v", err) + } + + return nil +}