-
-
Save wudi/5d7ef506e20cbb1c5997135cda6b4a82 to your computer and use it in GitHub Desktop.
GoLang: os.Rename() give error "invalid cross-device link" for Docker container with Volumes. MoveFile(source, destination) will work moving file between folders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
"io" | |
"os" | |
) | |
/* | |
GoLang: os.Rename() give error "invalid cross-device link" for Docker container with Volumes. | |
MoveFile(source, destination) will work moving file between folders | |
*/ | |
func MoveFile(sourcePath, destPath string) error { | |
inputFile, err := os.Open(sourcePath) | |
if err != nil { | |
return fmt.Errorf("Couldn't open source file: %s", err) | |
} | |
outputFile, err := os.Create(destPath) | |
if err != nil { | |
inputFile.Close() | |
return fmt.Errorf("Couldn't open dest file: %s", err) | |
} | |
defer outputFile.Close() | |
_, err = io.Copy(outputFile, inputFile) | |
inputFile.Close() | |
if err != nil { | |
return fmt.Errorf("Writing to output file failed: %s", err) | |
} | |
// The copy was successful, so now delete the original file | |
err = os.Remove(sourcePath) | |
if err != nil { | |
return fmt.Errorf("Failed removing original file: %s", err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment