From d6e47fde7e7a4049c2ab68950dc0022baa6a3eda Mon Sep 17 00:00:00 2001 From: ta264 Date: Thu, 14 May 2020 21:04:27 +0100 Subject: [PATCH] Fixed: NET Core not deleting source when moving across drives This reverts commit 10fc0b071fae9807fa456144b0708d95574d17ff. Use the mono fix from 43a35c84477fd194eaa816f1b3e4ade6cb4af42a in NET Core also --- .../DiskTests/DiskProviderFixtureBase.cs | 17 ----------------- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 17 +++-------------- src/NzbDrone.Mono/Disk/DiskProvider.cs | 14 ++++++++------ 3 files changed, 11 insertions(+), 37 deletions(-) diff --git a/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs b/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs index 91a3b4fad..3eec15fcc 100644 --- a/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs +++ b/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs @@ -78,23 +78,6 @@ namespace NzbDrone.Common.Test.DiskTests File.Exists(destination).Should().BeTrue(); } - [Test] - [Retry(5)] - public void MoveFile_should_not_overwrite_existing_file() - { - var source1 = GetTempFilePath(); - var source2 = GetTempFilePath(); - var destination = GetTempFilePath(); - - File.WriteAllText(source1, "SourceFile1"); - File.WriteAllText(source2, "SourceFile2"); - - Subject.MoveFile(source1, destination); - Assert.Throws(() => Subject.MoveFile(source2, destination, false)); - - File.ReadAllText(destination).Should().Be("SourceFile1"); - } - [Test] public void MoveFile_should_not_move_overwrite_itself() { diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 060cdef74..bda56eb3e 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -234,18 +234,13 @@ namespace NzbDrone.Common.Disk throw new IOException(string.Format("Source and destination can't be the same {0}", source)); } - var destExists = FileExists(destination); - - if (destExists && overwrite) + if (FileExists(destination) && overwrite) { DeleteFile(destination); } RemoveReadOnly(source); - - // NET Core is too eager to copy/delete if overwrite is false - // Therefore we also set overwrite if we know destination doesn't exist - MoveFileInternal(source, destination, overwrite || !destExists); + MoveFileInternal(source, destination); } public void MoveFolder(string source, string destination) @@ -256,15 +251,9 @@ namespace NzbDrone.Common.Disk Directory.Move(source, destination); } - protected virtual void MoveFileInternal(string source, string destination, bool overwrite) + protected virtual void MoveFileInternal(string source, string destination) { -#if NETCOREAPP - // This is a hack but the netcore specific overwrite parameter - // isn't availaboe via system.io.abstractions - File.Move(source, destination, overwrite); -#else _fileSystem.File.Move(source, destination); -#endif } public abstract bool TryCreateHardLink(string source, string destination); diff --git a/src/NzbDrone.Mono/Disk/DiskProvider.cs b/src/NzbDrone.Mono/Disk/DiskProvider.cs index cb8c21e5f..623363b6a 100644 --- a/src/NzbDrone.Mono/Disk/DiskProvider.cs +++ b/src/NzbDrone.Mono/Disk/DiskProvider.cs @@ -192,7 +192,7 @@ namespace NzbDrone.Mono.Disk } } - protected override void MoveFileInternal(string source, string destination, bool overwrite) + protected override void MoveFileInternal(string source, string destination) { var sourceInfo = UnixFileSystemInfo.GetFileSystemEntry(source); @@ -230,11 +230,11 @@ namespace NzbDrone.Mono.Disk else if ((PlatformInfo.Platform == PlatformType.Mono && PlatformInfo.GetVersion() >= new Version(6, 0)) || PlatformInfo.Platform == PlatformType.NetCore) { - TransferFilePatched(source, destination, overwrite, true); + TransferFilePatched(source, destination, false, true); } else { - base.MoveFileInternal(source, destination, overwrite); + base.MoveFileInternal(source, destination); } } @@ -246,7 +246,9 @@ namespace NzbDrone.Mono.Disk // Catch the exception and attempt to handle these edgecases // Mono 6.x till 6.10 doesn't properly try use rename first. - if (move && PlatformInfo.Platform == PlatformType.Mono && PlatformInfo.GetVersion() < new Version(6, 10)) + if (move && + ((PlatformInfo.Platform == PlatformType.Mono && PlatformInfo.GetVersion() < new Version(6, 10)) || + (PlatformInfo.Platform == PlatformType.NetCore))) { if (Syscall.lstat(source, out var sourcestat) == 0 && Syscall.lstat(destination, out var deststat) != 0 && @@ -261,11 +263,11 @@ namespace NzbDrone.Mono.Disk { if (move) { - base.MoveFileInternal(source, destination, overwrite); + base.MoveFileInternal(source, destination); } else { - base.CopyFileInternal(source, destination, overwrite); + base.CopyFileInternal(source, destination); } } catch (UnauthorizedAccessException)