From 141f1597dc9445b611f01955bb28eea172a121b0 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 3 Aug 2023 19:54:44 -0700 Subject: [PATCH] New: Ignore inaccessible files with getting files (cherry picked from commit e5aa8584100d96a2077c57f74ae5b2ceab63de19) --- .../DiskTests/DiskTransferServiceFixture.cs | 6 ++-- src/NzbDrone.Common/ArchiveService.cs | 5 +-- src/NzbDrone.Common/Disk/DiskProviderBase.cs | 31 ++++++++++++------- src/NzbDrone.Common/Disk/IDiskProvider.cs | 6 ++-- .../EnvironmentInfo/AppFolderFactory.cs | 2 +- src/NzbDrone.Core/Backup/BackupService.cs | 6 ++-- .../MacOsVersionAdapterFixture.cs | 6 ++-- .../ReleaseFileVersionAdapterFixture.cs | 10 +++--- .../IssueFileVersionAdapter.cs | 3 +- .../VersionAdapters/MacOsVersionAdapter.cs | 3 +- .../ReleaseFileVersionAdapter.cs | 3 +- .../VersionAdapters/SynologyVersionAdapter.cs | 3 +- src/Prowlarr.Api.V1/Logs/LogFileController.cs | 2 +- .../Logs/UpdateLogFileController.cs | 2 +- 14 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/NzbDrone.Common.Test/DiskTests/DiskTransferServiceFixture.cs b/src/NzbDrone.Common.Test/DiskTests/DiskTransferServiceFixture.cs index 4fca6ca40..b01ac65db 100644 --- a/src/NzbDrone.Common.Test/DiskTests/DiskTransferServiceFixture.cs +++ b/src/NzbDrone.Common.Test/DiskTests/DiskTransferServiceFixture.cs @@ -837,7 +837,7 @@ namespace NzbDrone.Common.Test.DiskTests // Note: never returns anything. Mocker.GetMock() - .Setup(v => v.GetFileInfos(It.IsAny(), SearchOption.TopDirectoryOnly)) + .Setup(v => v.GetFileInfos(It.IsAny(), false)) .Returns(new List()); Mocker.GetMock() @@ -875,8 +875,8 @@ namespace NzbDrone.Common.Test.DiskTests .Returns(v => new DirectoryInfo(v).GetDirectories().ToList()); Mocker.GetMock() - .Setup(v => v.GetFileInfos(It.IsAny(), SearchOption.TopDirectoryOnly)) - .Returns((v, _) => new DirectoryInfo(v).GetFiles().ToList()); + .Setup(v => v.GetFileInfos(It.IsAny(), false)) + .Returns((v, _) => new DirectoryInfo(v).GetFiles().ToList()); Mocker.GetMock() .Setup(v => v.GetFileSize(It.IsAny())) diff --git a/src/NzbDrone.Common/ArchiveService.cs b/src/NzbDrone.Common/ArchiveService.cs index 4cd6f6835..800d240ab 100644 --- a/src/NzbDrone.Common/ArchiveService.cs +++ b/src/NzbDrone.Common/ArchiveService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.GZip; @@ -11,7 +12,7 @@ namespace NzbDrone.Common public interface IArchiveService { void Extract(string compressedFile, string destination); - void CreateZip(string path, params string[] files); + void CreateZip(string path, IEnumerable files); } public class ArchiveService : IArchiveService @@ -39,7 +40,7 @@ namespace NzbDrone.Common _logger.Debug("Extraction complete."); } - public void CreateZip(string path, params string[] files) + public void CreateZip(string path, IEnumerable files) { using (var zipFile = ZipFile.Create(path)) { diff --git a/src/NzbDrone.Common/Disk/DiskProviderBase.cs b/src/NzbDrone.Common/Disk/DiskProviderBase.cs index 521cb61df..c386aa001 100644 --- a/src/NzbDrone.Common/Disk/DiskProviderBase.cs +++ b/src/NzbDrone.Common/Disk/DiskProviderBase.cs @@ -46,7 +46,7 @@ namespace NzbDrone.Common.Disk { CheckFolderExists(path); - var dirFiles = GetFiles(path, SearchOption.AllDirectories).ToList(); + var dirFiles = GetFiles(path, true).ToList(); if (!dirFiles.Any()) { @@ -149,25 +149,29 @@ namespace NzbDrone.Common.Disk return Directory.EnumerateFileSystemEntries(path).Empty(); } - public string[] GetDirectories(string path) + public IEnumerable GetDirectories(string path) { Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs); - return Directory.GetDirectories(path); + return Directory.EnumerateDirectories(path); } - public string[] GetFiles(string path, SearchOption searchOption) + public IEnumerable GetFiles(string path, bool recursive) { Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs); - return Directory.GetFiles(path, "*.*", searchOption); + return Directory.EnumerateFiles(path, "*", new EnumerationOptions + { + RecurseSubdirectories = recursive, + IgnoreInaccessible = true + }); } public long GetFolderSize(string path) { Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs); - return GetFiles(path, SearchOption.AllDirectories).Sum(e => new FileInfo(e).Length); + return GetFiles(path, true).Sum(e => new FileInfo(e).Length); } public long GetFileSize(string path) @@ -288,8 +292,9 @@ namespace NzbDrone.Common.Disk { Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs); - var files = Directory.GetFiles(path, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); - Array.ForEach(files, RemoveReadOnly); + var files = GetFiles(path, recursive); + + files.ToList().ForEach(RemoveReadOnly); Directory.Delete(path, recursive); } @@ -414,7 +419,7 @@ namespace NzbDrone.Common.Disk { Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs); - foreach (var file in GetFiles(path, SearchOption.TopDirectoryOnly)) + foreach (var file in GetFiles(path, false)) { DeleteFile(file); } @@ -515,13 +520,17 @@ namespace NzbDrone.Common.Disk return new FileInfo(path); } - public List GetFileInfos(string path, SearchOption searchOption = SearchOption.TopDirectoryOnly) + public List GetFileInfos(string path, bool recursive = false) { Ensure.That(path, () => path).IsValidPath(PathValidationType.CurrentOs); var di = new DirectoryInfo(path); - return di.GetFiles("*", searchOption).ToList(); + return di.EnumerateFiles("*", new EnumerationOptions + { + RecurseSubdirectories = recursive, + IgnoreInaccessible = true + }).ToList(); } public void RemoveEmptySubfolders(string path) diff --git a/src/NzbDrone.Common/Disk/IDiskProvider.cs b/src/NzbDrone.Common/Disk/IDiskProvider.cs index 6c33692c3..46589411a 100644 --- a/src/NzbDrone.Common/Disk/IDiskProvider.cs +++ b/src/NzbDrone.Common/Disk/IDiskProvider.cs @@ -22,8 +22,8 @@ namespace NzbDrone.Common.Disk bool FileExists(string path, StringComparison stringComparison); bool FolderWritable(string path); bool FolderEmpty(string path); - string[] GetDirectories(string path); - string[] GetFiles(string path, SearchOption searchOption); + IEnumerable GetDirectories(string path); + IEnumerable GetFiles(string path, bool recursive); long GetFolderSize(string path); long GetFileSize(string path); void CreateFolder(string path); @@ -52,7 +52,7 @@ namespace NzbDrone.Common.Disk IMount GetMount(string path); List GetDirectoryInfos(string path); FileInfo GetFileInfo(string path); - List GetFileInfos(string path, SearchOption searchOption = SearchOption.TopDirectoryOnly); + List GetFileInfos(string path, bool recursive = false); void RemoveEmptySubfolders(string path); void SaveStream(Stream stream, string path); bool IsValidFolderPermissionMask(string mask); diff --git a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs index 78caf0b12..178ce7a0f 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/AppFolderFactory.cs @@ -159,7 +159,7 @@ namespace NzbDrone.Common.EnvironmentInfo private void CleanupSqLiteRollbackFiles() { - _diskProvider.GetFiles(_appFolderInfo.AppDataFolder, SearchOption.TopDirectoryOnly) + _diskProvider.GetFiles(_appFolderInfo.AppDataFolder, false) .Where(f => Path.GetFileName(f).StartsWith("nzbdrone.db")) .ToList() .ForEach(_diskProvider.DeleteFile); diff --git a/src/NzbDrone.Core/Backup/BackupService.cs b/src/NzbDrone.Core/Backup/BackupService.cs index 97f13ee15..14fc91efc 100644 --- a/src/NzbDrone.Core/Backup/BackupService.cs +++ b/src/NzbDrone.Core/Backup/BackupService.cs @@ -89,7 +89,7 @@ namespace NzbDrone.Core.Backup // Delete journal file created during database backup _diskProvider.DeleteFile(Path.Combine(_backupTempFolder, "prowlarr.db-journal")); - _archiveService.CreateZip(backupPath, _diskProvider.GetFiles(_backupTempFolder, SearchOption.TopDirectoryOnly)); + _archiveService.CreateZip(backupPath, _diskProvider.GetFiles(_backupTempFolder, false)); Cleanup(); @@ -128,7 +128,7 @@ namespace NzbDrone.Core.Backup _archiveService.Extract(backupFileName, temporaryPath); - foreach (var file in _diskProvider.GetFiles(temporaryPath, SearchOption.TopDirectoryOnly)) + foreach (var file in _diskProvider.GetFiles(temporaryPath, false)) { var fileName = Path.GetFileName(file); @@ -243,7 +243,7 @@ namespace NzbDrone.Core.Backup private IEnumerable GetBackupFiles(string path) { - var files = _diskProvider.GetFiles(path, SearchOption.TopDirectoryOnly); + var files = _diskProvider.GetFiles(path, false); return files.Where(f => BackupFileRegex.IsMatch(f)); } diff --git a/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/MacOsVersionAdapterFixture.cs b/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/MacOsVersionAdapterFixture.cs index f0dbe0a85..b4df1027a 100644 --- a/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/MacOsVersionAdapterFixture.cs +++ b/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/MacOsVersionAdapterFixture.cs @@ -25,7 +25,7 @@ namespace NzbDrone.Mono.Test.EnvironmentInfo.VersionAdapters .Setup(c => c.FolderExists("/System/Library/CoreServices/")).Returns(true); Mocker.GetMock() - .Setup(c => c.GetFiles("/System/Library/CoreServices/", SearchOption.TopDirectoryOnly)) + .Setup(c => c.GetFiles("/System/Library/CoreServices/", false)) .Returns(new[] { plistPath }); Mocker.GetMock() @@ -49,7 +49,7 @@ namespace NzbDrone.Mono.Test.EnvironmentInfo.VersionAdapters .Setup(c => c.FolderExists("/System/Library/CoreServices/")).Returns(true); Mocker.GetMock() - .Setup(c => c.GetFiles("/System/Library/CoreServices/", SearchOption.TopDirectoryOnly)) + .Setup(c => c.GetFiles("/System/Library/CoreServices/", false)) .Returns(new[] { plistPath }); Mocker.GetMock() @@ -69,7 +69,7 @@ namespace NzbDrone.Mono.Test.EnvironmentInfo.VersionAdapters Subject.Read().Should().BeNull(); Mocker.GetMock() - .Verify(c => c.GetFiles(It.IsAny(), SearchOption.TopDirectoryOnly), Times.Never()); + .Verify(c => c.GetFiles(It.IsAny(), false), Times.Never()); } } } diff --git a/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapterFixture.cs b/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapterFixture.cs index 0c3257657..496f4eefa 100644 --- a/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapterFixture.cs +++ b/src/NzbDrone.Mono.Test/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapterFixture.cs @@ -29,25 +29,25 @@ namespace NzbDrone.Mono.Test.EnvironmentInfo.VersionAdapters } [Test] - public void should_return_null_if_etc_doestn_exist() + public void should_return_null_if_etc_doesnt_exist() { Mocker.GetMock().Setup(c => c.FolderExists("/etc/")).Returns(false); Subject.Read().Should().BeNull(); Mocker.GetMock() - .Verify(c => c.GetFiles(It.IsAny(), SearchOption.TopDirectoryOnly), Times.Never()); + .Verify(c => c.GetFiles(It.IsAny(), false), Times.Never()); Subject.Read().Should().BeNull(); } [Test] - public void should_return_null_if_release_file_doestn_exist() + public void should_return_null_if_release_file_doesnt_exist() { Mocker.GetMock().Setup(c => c.FolderExists("/etc/")).Returns(true); Subject.Read().Should().BeNull(); Mocker.GetMock() - .Setup(c => c.GetFiles(It.IsAny(), SearchOption.TopDirectoryOnly)).Returns(Array.Empty()); + .Setup(c => c.GetFiles(It.IsAny(), false)).Returns(Array.Empty()); Subject.Read().Should().BeNull(); } @@ -59,7 +59,7 @@ namespace NzbDrone.Mono.Test.EnvironmentInfo.VersionAdapters Subject.Read().Should().BeNull(); Mocker.GetMock() - .Setup(c => c.GetFiles(It.IsAny(), SearchOption.TopDirectoryOnly)).Returns(new[] + .Setup(c => c.GetFiles(It.IsAny(), false)).Returns(new[] { "/etc/lsb-release", "/etc/os-release" diff --git a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/IssueFileVersionAdapter.cs b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/IssueFileVersionAdapter.cs index 2965f5a1a..4113c3993 100644 --- a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/IssueFileVersionAdapter.cs +++ b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/IssueFileVersionAdapter.cs @@ -1,4 +1,3 @@ -using System.IO; using System.Linq; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; @@ -21,7 +20,7 @@ namespace NzbDrone.Mono.EnvironmentInfo.VersionAdapters return null; } - var issueFile = _diskProvider.GetFiles("/etc/", SearchOption.TopDirectoryOnly).SingleOrDefault(c => c.EndsWith("/issue")); + var issueFile = _diskProvider.GetFiles("/etc/", false).SingleOrDefault(c => c.EndsWith("/issue")); if (issueFile == null) { diff --git a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/MacOsVersionAdapter.cs b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/MacOsVersionAdapter.cs index 59f743da2..1171334b5 100644 --- a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/MacOsVersionAdapter.cs +++ b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/MacOsVersionAdapter.cs @@ -1,4 +1,3 @@ -using System.IO; using System.Linq; using System.Text.RegularExpressions; using NLog; @@ -33,7 +32,7 @@ namespace NzbDrone.Mono.EnvironmentInfo.VersionAdapters return null; } - var allFiles = _diskProvider.GetFiles(PLIST_DIR, SearchOption.TopDirectoryOnly); + var allFiles = _diskProvider.GetFiles(PLIST_DIR, false); var versionFile = allFiles.SingleOrDefault(c => c.EndsWith("/SystemVersion.plist") || diff --git a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapter.cs b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapter.cs index 9347176b2..77c4b9edc 100644 --- a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapter.cs +++ b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/ReleaseFileVersionAdapter.cs @@ -1,4 +1,3 @@ -using System.IO; using System.Linq; using System.Text.RegularExpressions; using NzbDrone.Common.Disk; @@ -22,7 +21,7 @@ namespace NzbDrone.Mono.EnvironmentInfo.VersionAdapters return null; } - var releaseFiles = _diskProvider.GetFiles("/etc/", SearchOption.TopDirectoryOnly).Where(c => c.EndsWith("release")).ToList(); + var releaseFiles = _diskProvider.GetFiles("/etc/", false).Where(c => c.EndsWith("release")).ToList(); var name = "Linux"; var fullName = ""; diff --git a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/SynologyVersionAdapter.cs b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/SynologyVersionAdapter.cs index 994867527..b27c0d7e4 100644 --- a/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/SynologyVersionAdapter.cs +++ b/src/NzbDrone.Mono/EnvironmentInfo/VersionAdapters/SynologyVersionAdapter.cs @@ -1,4 +1,3 @@ -using System.IO; using System.Linq; using System.Text.RegularExpressions; using NzbDrone.Common.Disk; @@ -24,7 +23,7 @@ namespace NzbDrone.Mono.EnvironmentInfo.VersionAdapters return null; } - var versionFile = _diskProvider.GetFiles("/etc.defaults/", SearchOption.TopDirectoryOnly).SingleOrDefault(c => c.EndsWith("VERSION")); + var versionFile = _diskProvider.GetFiles("/etc.defaults/", false).SingleOrDefault(c => c.EndsWith("VERSION")); if (versionFile == null) { diff --git a/src/Prowlarr.Api.V1/Logs/LogFileController.cs b/src/Prowlarr.Api.V1/Logs/LogFileController.cs index 9f43b0a21..d4c901df9 100644 --- a/src/Prowlarr.Api.V1/Logs/LogFileController.cs +++ b/src/Prowlarr.Api.V1/Logs/LogFileController.cs @@ -25,7 +25,7 @@ namespace Prowlarr.Api.V1.Logs protected override IEnumerable GetLogFiles() { - return _diskProvider.GetFiles(_appFolderInfo.GetLogFolder(), SearchOption.TopDirectoryOnly); + return _diskProvider.GetFiles(_appFolderInfo.GetLogFolder(), false); } protected override string GetLogFilePath(string filename) diff --git a/src/Prowlarr.Api.V1/Logs/UpdateLogFileController.cs b/src/Prowlarr.Api.V1/Logs/UpdateLogFileController.cs index f2a07f8c5..c7e4a2cdf 100644 --- a/src/Prowlarr.Api.V1/Logs/UpdateLogFileController.cs +++ b/src/Prowlarr.Api.V1/Logs/UpdateLogFileController.cs @@ -32,7 +32,7 @@ namespace Prowlarr.Api.V1.Logs return Enumerable.Empty(); } - return _diskProvider.GetFiles(_appFolderInfo.GetUpdateLogFolder(), SearchOption.TopDirectoryOnly) + return _diskProvider.GetFiles(_appFolderInfo.GetUpdateLogFolder(), false) .Where(f => Regex.IsMatch(Path.GetFileName(f), LOGFILE_ROUTE.TrimStart('/'), RegexOptions.IgnoreCase)) .ToList(); }