cross device rename: remove the source if copy suceeded

This commit is contained in:
Nicola Murino 2021-05-27 22:23:14 +02:00
parent e1bf46c6a5
commit fc7066a25c
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB

View file

@ -102,9 +102,14 @@ func (*OsFs) Create(name string, flag int) (File, *PipeWriter, func(), error) {
func (fs *OsFs) Rename(source, target string) error {
err := os.Rename(source, target)
if err != nil && isCrossDeviceError(err) {
fsLog(fs, logger.LevelWarn, "cross device error detected while renaming %#v -> %#v. Trying a copy, this could take a long time",
fsLog(fs, logger.LevelWarn, "cross device error detected while renaming %#v -> %#v. Trying a copy and remove, this could take a long time",
source, target)
return fscopy.Copy(source, target)
err = fscopy.Copy(source, target)
if err != nil {
fsLog(fs, logger.LevelDebug, "cross device copy error: %v", err)
return err
}
return os.RemoveAll(source)
}
return err
}