From fc7066a25c1bf651b128e1d599883500d7433577 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Thu, 27 May 2021 22:23:14 +0200 Subject: [PATCH] cross device rename: remove the source if copy suceeded --- vfs/osfs.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vfs/osfs.go b/vfs/osfs.go index c2c913e2..a8414214 100644 --- a/vfs/osfs.go +++ b/vfs/osfs.go @@ -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 }