From ffe79c89829fb232e8ccb4ae4caf4b732ce51600 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sun, 27 Jan 2019 17:00:17 +0100 Subject: [PATCH] Check if file exists instead of catching exceptions --- .../AppBase/BaseConfigurationManager.cs | 9 ++-- .../HttpClientManager/HttpClientManager.cs | 30 ++++-------- .../Library/LibraryManager.cs | 47 +++++++++---------- .../Library/UserManager.cs | 18 +++---- .../ScheduledTasks/ChapterImagesTask.cs | 21 +++++---- .../ScheduledTasks/ScheduledTaskWorker.cs | 47 ++++++------------- 6 files changed, 73 insertions(+), 99 deletions(-) diff --git a/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs b/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs index 59c7c655f..9740f5ee3 100644 --- a/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs +++ b/Emby.Server.Implementations/AppBase/BaseConfigurationManager.cs @@ -246,14 +246,15 @@ namespace Emby.Server.Implementations.AppBase private object LoadConfiguration(string path, Type configurationType) { + if (!File.Exists(path)) + { + return Activator.CreateInstance(configurationType); + } + try { return XmlSerializer.DeserializeFromFile(configurationType, path); } - catch (FileNotFoundException) - { - return Activator.CreateInstance(configurationType); - } catch (IOException) { return Activator.CreateInstance(configurationType); diff --git a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs index ea620cb2e..217f366e0 100644 --- a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs +++ b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs @@ -286,28 +286,18 @@ namespace Emby.Server.Implementations.HttpClientManager private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) { - try + if (File.Exists(responseCachePath) + && _fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow) { - if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow) + var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true); + + return new HttpResponseInfo { - var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true); - - return new HttpResponseInfo - { - ResponseUrl = url, - Content = stream, - StatusCode = HttpStatusCode.OK, - ContentLength = stream.Length - }; - } - } - catch (FileNotFoundException) // REVIEW: @bond Is this really faster? - { - - } - catch (DirectoryNotFoundException) - { - + ResponseUrl = url, + Content = stream, + StatusCode = HttpStatusCode.OK, + ContentLength = stream.Length + }; } return null; diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index ad070ed79..9688ab385 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -395,38 +395,33 @@ namespace Emby.Server.Implementations.Library foreach (var fileSystemInfo in item.GetDeletePaths().ToList()) { - try + if (File.Exists(fileSystemInfo.FullName)) { - _logger.LogDebug("Deleting path {path}", fileSystemInfo.FullName); - if (fileSystemInfo.IsDirectory) + try { - _fileSystem.DeleteDirectory(fileSystemInfo.FullName, true); + _logger.LogDebug("Deleting path {path}", fileSystemInfo.FullName); + if (fileSystemInfo.IsDirectory) + { + _fileSystem.DeleteDirectory(fileSystemInfo.FullName, true); + } + else + { + _fileSystem.DeleteFile(fileSystemInfo.FullName); + } } - else + catch (IOException) { - _fileSystem.DeleteFile(fileSystemInfo.FullName); + if (isRequiredForDelete) + { + throw; + } } - } - catch (FileNotFoundException) - { - // may have already been deleted manually by user - } - catch (DirectoryNotFoundException) - { - // may have already been deleted manually by user - } - catch (IOException) - { - if (isRequiredForDelete) + catch (UnauthorizedAccessException) { - throw; - } - } - catch (UnauthorizedAccessException) - { - if (isRequiredForDelete) - { - throw; + if (isRequiredForDelete) + { + throw; + } } } diff --git a/Emby.Server.Implementations/Library/UserManager.cs b/Emby.Server.Implementations/Library/UserManager.cs index 6139659b7..d6f844650 100644 --- a/Emby.Server.Implementations/Library/UserManager.cs +++ b/Emby.Server.Implementations/Library/UserManager.cs @@ -1029,6 +1029,11 @@ namespace Emby.Server.Implementations.Library { var path = GetPolicyFilePath(user); + if (!File.Exists(path)) + { + return GetDefaultPolicy(user); + } + try { lock (_policySyncLock) @@ -1036,10 +1041,6 @@ namespace Emby.Server.Implementations.Library return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path); } } - catch (FileNotFoundException) - { - return GetDefaultPolicy(user); - } catch (IOException) { return GetDefaultPolicy(user); @@ -1128,6 +1129,11 @@ namespace Emby.Server.Implementations.Library { var path = GetConfigurationFilePath(user); + if (!File.Exists(path)) + { + return new UserConfiguration(); + } + try { lock (_configSyncLock) @@ -1135,10 +1141,6 @@ namespace Emby.Server.Implementations.Library return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path); } } - catch (FileNotFoundException) - { - return new UserConfiguration(); - } catch (IOException) { return new UserConfiguration(); diff --git a/Emby.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs b/Emby.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs index 0ae7ae96c..57bc61c7a 100644 --- a/Emby.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs @@ -101,17 +101,20 @@ namespace Emby.Server.Implementations.ScheduledTasks List previouslyFailedImages; - try + if (File.Exists(failHistoryPath)) { - previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath) - .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) - .ToList(); + try + { + previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath) + .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) + .ToList(); + } + catch (IOException) + { + previouslyFailedImages = new List(); + } } - catch (FileNotFoundException) - { - previouslyFailedImages = new List(); - } - catch (IOException) + else { previouslyFailedImages = new List(); } diff --git a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs index 93a9a0d81..88b3dd07f 100644 --- a/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs +++ b/Emby.Server.Implementations/ScheduledTasks/ScheduledTaskWorker.cs @@ -129,21 +129,16 @@ namespace Emby.Server.Implementations.ScheduledTasks { if (_lastExecutionResult == null && !_readFromFile) { - try + if (File.Exists(path)) { - _lastExecutionResult = JsonSerializer.DeserializeFromFile(path); - } - catch (DirectoryNotFoundException) - { - // File doesn't exist. No biggie - } - catch (FileNotFoundException) - { - // File doesn't exist. No biggie - } - catch (Exception ex) - { - Logger.LogError(ex, "Error deserializing {path}", path); + try + { + _lastExecutionResult = JsonSerializer.DeserializeFromFile(path); + } + catch (Exception ex) + { + Logger.LogError(ex, "Error deserializing {File}", path); + } } _readFromFile = true; } @@ -532,28 +527,16 @@ namespace Emby.Server.Implementations.ScheduledTasks private TaskTriggerInfo[] LoadTriggerSettings() { - try - { - var list = JsonSerializer.DeserializeFromFile>(GetConfigurationFilePath()); - - if (list != null) - { - return list.ToArray(); - } - } - catch (FileNotFoundException) + string path = GetConfigurationFilePath(); + if (!File.Exists(path)) { // File doesn't exist. No biggie. Return defaults. + GetDefaultTriggers(); } - catch (DirectoryNotFoundException) - { - // File doesn't exist. No biggie. Return defaults. - } - catch - { - } - return GetDefaultTriggers(); + var list = JsonSerializer.DeserializeFromFile(path); + + return list ?? GetDefaultTriggers(); } private TaskTriggerInfo[] GetDefaultTriggers()