Replace os.Rename with copy and delete
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-04 13:42:44 -06:00
parent 575157c759
commit 1b8b1d7261

38
main.go
View File

@ -172,7 +172,7 @@ func moveBuildArtifacts(outDirectory, romDirectory string) bool {
} }
newROMs = true newROMs = true
log.Printf("new build found - moving file %s", v.Name()) 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 { if err != nil {
log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err) log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err)
continue continue
@ -222,3 +222,39 @@ func hashFile(filename string) (string, error) {
return fmt.Sprintf("%x", h.Sum(nil)), nil 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
}