// GoLang: os.Rename() give error "invalid cross-device link" for Docker container with Volumes.
// MoveFile(source, destination) will work moving file between folders
// Therefore, we are using our own implementation (MoveFile) in order to rename files.
os.Rename() give error "invalid cross-device link" for Docker container with Volumes.
MoveFile(source, destination) will work moving file between folders
Therefore, we are using our own implementation (MoveFile) in order to rename files.
// GoLang : os . Rename () give error " invalid cross - device link " for Docker container with Volumes . // MoveFile ( source destination ) will work moving file between folders // Therefore we are using our own implementation ( MoveFile ) in order to rename files .
func MoveFile(sourcePath, destPath string) (err error) {
	inputFileOpen := true
	var inputFile *os.File
	inputFile, err = os.Open(sourcePath)
	if err != nil {
		return err
	}
	defer func() {
		if inputFileOpen {
			e := inputFile.Close()
			if err == nil {
				err = e
			}
		}
	}()
	inputFileInfo, err := inputFile.Stat()
	if err != nil {
		return err
	}
	var outputFile *os.File
	outputFile, err = os.Create(destPath)
	if err != nil {
		return err
	}
	defer func() {
		e := outputFile.Close()
		if err == nil {
			err = e
		}
	}()
	_, err = io.Copy(outputFile, inputFile)
	if err != nil {
		return err
	}
	err = os.Chmod(destPath, inputFileInfo.Mode())
	if err != nil {
		return err
	}
	err = inputFile.Close()
	if err != nil {
		return err
	}
	inputFileOpen = false
	err = os.Remove(sourcePath)
	return err
}
func MoveFile ( sourcePath , destPath string ) ( err error ) { inputFileOpen := true var inputFile * os . File inputFile , err = os . Open ( sourcePath ) if err != nil { return err } defer func ( ) { if inputFileOpen { e := inputFile . Close ( ) if err == nil { err = e } } } ( ) inputFileInfo , err := inputFile . Stat ( ) if err != nil { return err } var outputFile * os . File outputFile , err = os . Create ( destPath ) if err != nil { return err } defer func ( ) { e := outputFile . Close ( ) if err == nil { err = e } } ( ) _ , err = io . Copy ( outputFile , inputFile ) if err != nil { return err } err = os . Chmod ( destPath , inputFileInfo . Mode ( ) ) if err != nil { return err } err = inputFile . Close ( ) if err != nil { return err } inputFileOpen = false err = os . Remove ( sourcePath ) return err }
os.Rename() give error "invalid cross-device link" for Docker container with Volumes.
os . Rename () give error " invalid cross - device link " for Docker container with Volumes .
// The copy was successful, so now delete the original file