Make sure to copy the permissions as well when moving files

This commit is contained in:
kolaente 2020-09-04 14:00:54 +02:00
parent e9b1786188
commit 5317a89623
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B

View file

@ -253,24 +253,36 @@ func copyFile(src, dst string) error {
// os.Rename has issues with moving files between docker volumes. // os.Rename has issues with moving files between docker volumes.
// Because of this limitaion, it fails in drone. // Because of this limitaion, it fails in drone.
// Source: https://gist.github.com/var23rav/23ae5d0d4d830aff886c3c970b8f6c6b // Source: https://gist.github.com/var23rav/23ae5d0d4d830aff886c3c970b8f6c6b
func moveFile(sourcePath, destPath string) error { func moveFile(src, dst string) error {
inputFile, err := os.Open(sourcePath) inputFile, err := os.Open(src)
defer inputFile.Close()
if err != nil { if err != nil {
return fmt.Errorf("couldn't open source file: %s", err) return fmt.Errorf("couldn't open source file: %s", err)
} }
outputFile, err := os.Create(destPath)
outputFile, err := os.Create(dst)
defer outputFile.Close()
if err != nil { if err != nil {
inputFile.Close()
return fmt.Errorf("couldn't open dest file: %s", err) return fmt.Errorf("couldn't open dest file: %s", err)
} }
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile) _, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil { if err != nil {
return fmt.Errorf("writing to output file failed: %s", err) return fmt.Errorf("writing to output file failed: %s", err)
} }
// Make sure to copy copy the permissions of the original file as well
si, err := os.Stat(src)
if err != nil {
return err
}
if err := os.Chmod(dst, si.Mode()); err != nil {
return err
}
// The copy was successful, so now delete the original file // The copy was successful, so now delete the original file
err = os.Remove(sourcePath) err = os.Remove(src)
if err != nil { if err != nil {
return fmt.Errorf("failed removing original file: %s", err) return fmt.Errorf("failed removing original file: %s", err)
} }