Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
34 lines
1 KiB
Go
34 lines
1 KiB
Go
package xos
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
|
|
)
|
|
|
|
// Rename copy oldPath to newPath and then delete oldPath.
|
|
// Unlike os.Rename, it doesn't fail when the oldPath and newPath are in
|
|
// different partitions (error: invalid cross-device link).
|
|
func Rename(oldPath, newPath string) error {
|
|
inputFile, err := os.Open(oldPath)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: couldn't open oldpath: %w", oldPath, newPath, err)
|
|
}
|
|
defer inputFile.Close()
|
|
outputFile, err := os.Create(newPath)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: couldn't open dest file: %w", oldPath, newPath, err)
|
|
}
|
|
defer outputFile.Close()
|
|
_, err = io.Copy(outputFile, inputFile)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: writing to output file failed: %w", oldPath, newPath, err)
|
|
}
|
|
// The copy was successful, so now delete the original file
|
|
err = os.Remove(oldPath)
|
|
if err != nil {
|
|
return errors.Errorf("rename %s %s: failed removing original file: %w", oldPath, newPath, err)
|
|
}
|
|
return nil
|
|
}
|