From d9acbf5682a10312583e3846b10b2a9fc83f95ee Mon Sep 17 00:00:00 2001 From: ta264 Date: Wed, 23 Dec 2020 22:01:18 +0100 Subject: [PATCH] Fixed: FolderWritable check for CIFS shares mounted in Unix This reverts commit 8c892a732ed57af9bb1f39743e0c16361f41b50f. (cherry picked from commit 96384521c59233dab5bd8289e7c84043f75b84a2) --- .../DiskTests/DiskProviderFixtureBase.cs | 10 ++++++++++ src/NzbDrone.Common/Disk/DiskProviderBase.cs | 13 +++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs b/src/NzbDrone.Common.Test/DiskTests/DiskProviderFixtureBase.cs index b1994d103..421ffbcf6 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] [Retry(5)] 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 5d30c074f..9c206709f 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -131,7 +131,7 @@ namespace NzbDrone.Common.Disk { var testPath = Path.Combine(path, "sonarr_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."; - File.WriteAllText(testPath, testContent); + WriteAllText(testPath, testContent); File.Delete(testPath); return true; } @@ -311,7 +311,16 @@ namespace NzbDrone.Common.Disk { Ensure.That(filename, () => filename).IsValidPath(PathValidationType.CurrentOs); RemoveReadOnly(filename); - 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)