diff --git a/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs b/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs index 3eec15fcc..91a3b4fad 100644 --- a/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs +++ b/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs @@ -78,6 +78,23 @@ 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 bda56eb3e..060cdef74 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -234,13 +234,18 @@ namespace NzbDrone.Common.Disk throw new IOException(string.Format("Source and destination can't be the same {0}", source)); } - if (FileExists(destination) && overwrite) + var destExists = FileExists(destination); + + if (destExists && overwrite) { DeleteFile(destination); } RemoveReadOnly(source); - MoveFileInternal(source, destination); + + // 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); } public void MoveFolder(string source, string destination) @@ -251,9 +256,15 @@ namespace NzbDrone.Common.Disk Directory.Move(source, destination); } - protected virtual void MoveFileInternal(string source, string destination) + protected virtual void MoveFileInternal(string source, string destination, bool overwrite) { +#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 b09eb4601..5c0702a75 100644 --- a/src/NzbDrone.Mono/Disk/DiskProvider.cs +++ b/src/NzbDrone.Mono/Disk/DiskProvider.cs @@ -184,7 +184,7 @@ namespace NzbDrone.Mono.Disk } } - protected override void MoveFileInternal(string source, string destination) + protected override void MoveFileInternal(string source, string destination, bool overwrite) { var sourceInfo = UnixFileSystemInfo.GetFileSystemEntry(source); @@ -221,7 +221,7 @@ namespace NzbDrone.Mono.Disk } else { - base.MoveFileInternal(source, destination); + base.MoveFileInternal(source, destination, overwrite); } }