diff --git a/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs b/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs index d9e39b787..617cba13a 100644 --- a/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs +++ b/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs @@ -10,6 +10,16 @@ namespace NzbDrone.Common.Test.DiskTests public abstract class DiskProviderFixtureBase : TestBase where TSubject : class, IDiskProvider { + [Test] + public void writealltext_should_truncate_existing() + { + var file = GetTempFilePath(); + + Subject.WriteAllText(file, "A pretty long string"); + Subject.WriteAllText(file, "A short string"); + Subject.ReadAllText(file).Should().Be("A short string"); + } + [Test] public void directory_exist_should_be_able_to_find_existing_folder() { diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 632cad342..75eb8a9dd 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -138,7 +138,7 @@ namespace NzbDrone.Common.Disk { var testPath = Path.Combine(path, "lidarr_write_test.txt"); var testContent = $"This file was created to verify if '{path}' is writable. It should've been automatically deleted. Feel free to delete it."; - _fileSystem.File.WriteAllText(testPath, testContent); + WriteAllText(testPath, testContent); _fileSystem.File.Delete(testPath); return true; } @@ -314,7 +314,16 @@ namespace NzbDrone.Common.Disk { Ensure.That(filename, () => filename).IsValidPath(); RemoveReadOnly(filename); - _fileSystem.File.WriteAllText(filename, contents); + + // File.WriteAllText is broken on net core when writing to some CIFS mounts + // This workaround from https://github.com/dotnet/runtime/issues/42790#issuecomment-700362617 + using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)) + { + using (var writer = new StreamWriter(fs)) + { + writer.Write(contents); + } + } } public void FolderSetLastWriteTime(string path, DateTime dateTime)