Check if file exists instead of catching exceptions

This commit is contained in:
Bond_009 2019-01-27 17:00:17 +01:00
parent 1a3543e5a5
commit ffe79c8982
6 changed files with 73 additions and 99 deletions

View File

@ -246,14 +246,15 @@ namespace Emby.Server.Implementations.AppBase
private object LoadConfiguration(string path, Type configurationType) private object LoadConfiguration(string path, Type configurationType)
{ {
if (!File.Exists(path))
{
return Activator.CreateInstance(configurationType);
}
try try
{ {
return XmlSerializer.DeserializeFromFile(configurationType, path); return XmlSerializer.DeserializeFromFile(configurationType, path);
} }
catch (FileNotFoundException)
{
return Activator.CreateInstance(configurationType);
}
catch (IOException) catch (IOException)
{ {
return Activator.CreateInstance(configurationType); return Activator.CreateInstance(configurationType);

View File

@ -286,9 +286,8 @@ namespace Emby.Server.Implementations.HttpClientManager
private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) 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); var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true);
@ -300,15 +299,6 @@ namespace Emby.Server.Implementations.HttpClientManager
ContentLength = stream.Length ContentLength = stream.Length
}; };
} }
}
catch (FileNotFoundException) // REVIEW: @bond Is this really faster?
{
}
catch (DirectoryNotFoundException)
{
}
return null; return null;
} }

View File

@ -394,6 +394,8 @@ namespace Emby.Server.Implementations.Library
var isRequiredForDelete = true; var isRequiredForDelete = true;
foreach (var fileSystemInfo in item.GetDeletePaths().ToList()) foreach (var fileSystemInfo in item.GetDeletePaths().ToList())
{
if (File.Exists(fileSystemInfo.FullName))
{ {
try try
{ {
@ -407,14 +409,6 @@ namespace Emby.Server.Implementations.Library
_fileSystem.DeleteFile(fileSystemInfo.FullName); _fileSystem.DeleteFile(fileSystemInfo.FullName);
} }
} }
catch (FileNotFoundException)
{
// may have already been deleted manually by user
}
catch (DirectoryNotFoundException)
{
// may have already been deleted manually by user
}
catch (IOException) catch (IOException)
{ {
if (isRequiredForDelete) if (isRequiredForDelete)
@ -429,6 +423,7 @@ namespace Emby.Server.Implementations.Library
throw; throw;
} }
} }
}
isRequiredForDelete = false; isRequiredForDelete = false;
} }

View File

@ -1029,6 +1029,11 @@ namespace Emby.Server.Implementations.Library
{ {
var path = GetPolicyFilePath(user); var path = GetPolicyFilePath(user);
if (!File.Exists(path))
{
return GetDefaultPolicy(user);
}
try try
{ {
lock (_policySyncLock) lock (_policySyncLock)
@ -1036,10 +1041,6 @@ namespace Emby.Server.Implementations.Library
return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path); return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path);
} }
} }
catch (FileNotFoundException)
{
return GetDefaultPolicy(user);
}
catch (IOException) catch (IOException)
{ {
return GetDefaultPolicy(user); return GetDefaultPolicy(user);
@ -1128,6 +1129,11 @@ namespace Emby.Server.Implementations.Library
{ {
var path = GetConfigurationFilePath(user); var path = GetConfigurationFilePath(user);
if (!File.Exists(path))
{
return new UserConfiguration();
}
try try
{ {
lock (_configSyncLock) lock (_configSyncLock)
@ -1135,10 +1141,6 @@ namespace Emby.Server.Implementations.Library
return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path); return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path);
} }
} }
catch (FileNotFoundException)
{
return new UserConfiguration();
}
catch (IOException) catch (IOException)
{ {
return new UserConfiguration(); return new UserConfiguration();

View File

@ -101,17 +101,20 @@ namespace Emby.Server.Implementations.ScheduledTasks
List<string> previouslyFailedImages; List<string> previouslyFailedImages;
if (File.Exists(failHistoryPath))
{
try try
{ {
previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath) previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
.ToList(); .ToList();
} }
catch (FileNotFoundException) catch (IOException)
{ {
previouslyFailedImages = new List<string>(); previouslyFailedImages = new List<string>();
} }
catch (IOException) }
else
{ {
previouslyFailedImages = new List<string>(); previouslyFailedImages = new List<string>();
} }

View File

@ -128,22 +128,17 @@ namespace Emby.Server.Implementations.ScheduledTasks
lock (_lastExecutionResultSyncLock) lock (_lastExecutionResultSyncLock)
{ {
if (_lastExecutionResult == null && !_readFromFile) if (_lastExecutionResult == null && !_readFromFile)
{
if (File.Exists(path))
{ {
try try
{ {
_lastExecutionResult = JsonSerializer.DeserializeFromFile<TaskResult>(path); _lastExecutionResult = JsonSerializer.DeserializeFromFile<TaskResult>(path);
} }
catch (DirectoryNotFoundException)
{
// File doesn't exist. No biggie
}
catch (FileNotFoundException)
{
// File doesn't exist. No biggie
}
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogError(ex, "Error deserializing {path}", path); Logger.LogError(ex, "Error deserializing {File}", path);
}
} }
_readFromFile = true; _readFromFile = true;
} }
@ -532,28 +527,16 @@ namespace Emby.Server.Implementations.ScheduledTasks
private TaskTriggerInfo[] LoadTriggerSettings() private TaskTriggerInfo[] LoadTriggerSettings()
{ {
try string path = GetConfigurationFilePath();
{ if (!File.Exists(path))
var list = JsonSerializer.DeserializeFromFile<IEnumerable<TaskTriggerInfo>>(GetConfigurationFilePath());
if (list != null)
{
return list.ToArray();
}
}
catch (FileNotFoundException)
{ {
// File doesn't exist. No biggie. Return defaults. // File doesn't exist. No biggie. Return defaults.
GetDefaultTriggers();
} }
catch (DirectoryNotFoundException)
{
// File doesn't exist. No biggie. Return defaults.
}
catch
{
} var list = JsonSerializer.DeserializeFromFile<TaskTriggerInfo[]>(path);
return GetDefaultTriggers();
return list ?? GetDefaultTriggers();
} }
private TaskTriggerInfo[] GetDefaultTriggers() private TaskTriggerInfo[] GetDefaultTriggers()