mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-10-30 07:22:35 +01:00
Fixed: Extra checks in place to prevent config file corruption
This commit is contained in:
parent
013615a7a5
commit
ddb24549d4
@ -29,10 +29,8 @@ namespace NzbDrone.Common.Test
|
||||
const string key = "Port";
|
||||
const string value = "8989";
|
||||
|
||||
|
||||
var result = Subject.GetValue(key, value);
|
||||
|
||||
|
||||
result.Should().Be(value);
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Configuration.Events;
|
||||
@ -48,15 +49,22 @@ namespace NzbDrone.Core.Configuration
|
||||
public const string CONFIG_ELEMENT_NAME = "Config";
|
||||
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly ICached<string> _cache;
|
||||
|
||||
private readonly string _configFile;
|
||||
private static readonly Regex HiddenCharacterRegex = new Regex("[^a-z0-9]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public ConfigFileProvider(IAppFolderInfo appFolderInfo, ICacheManager cacheManager, IEventAggregator eventAggregator)
|
||||
private static readonly object Mutex = new object();
|
||||
|
||||
public ConfigFileProvider(IAppFolderInfo appFolderInfo,
|
||||
ICacheManager cacheManager,
|
||||
IEventAggregator eventAggregator,
|
||||
IDiskProvider diskProvider)
|
||||
{
|
||||
_cache = cacheManager.GetCache<string>(GetType());
|
||||
_eventAggregator = eventAggregator;
|
||||
_diskProvider = diskProvider;
|
||||
_configFile = appFolderInfo.GetConfigPath();
|
||||
}
|
||||
|
||||
@ -247,8 +255,6 @@ namespace NzbDrone.Core.Configuration
|
||||
{
|
||||
return _cache.Get(key, () =>
|
||||
{
|
||||
EnsureDefaultConfigFile();
|
||||
|
||||
var xDoc = LoadConfigFile();
|
||||
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
|
||||
|
||||
@ -274,8 +280,6 @@ namespace NzbDrone.Core.Configuration
|
||||
|
||||
public void SetValue(string key, object value)
|
||||
{
|
||||
EnsureDefaultConfigFile();
|
||||
|
||||
var valueString = value.ToString().Trim();
|
||||
var xDoc = LoadConfigFile();
|
||||
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
|
||||
@ -296,7 +300,7 @@ namespace NzbDrone.Core.Configuration
|
||||
|
||||
_cache.Set(key, valueString);
|
||||
|
||||
xDoc.Save(_configFile);
|
||||
SaveConfigFile(xDoc);
|
||||
}
|
||||
|
||||
public void SetValue(string key, Enum value)
|
||||
@ -308,18 +312,12 @@ namespace NzbDrone.Core.Configuration
|
||||
{
|
||||
if (!File.Exists(_configFile))
|
||||
{
|
||||
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
||||
xDoc.Add(new XElement(CONFIG_ELEMENT_NAME));
|
||||
xDoc.Save(_configFile);
|
||||
|
||||
SaveConfigDictionary(GetConfigDictionary());
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteOldValues()
|
||||
{
|
||||
EnsureDefaultConfigFile();
|
||||
|
||||
var xDoc = LoadConfigFile();
|
||||
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
|
||||
|
||||
@ -336,22 +334,41 @@ namespace NzbDrone.Core.Configuration
|
||||
}
|
||||
}
|
||||
|
||||
xDoc.Save(_configFile);
|
||||
SaveConfigFile(xDoc);
|
||||
}
|
||||
|
||||
private XDocument LoadConfigFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
lock (Mutex)
|
||||
{
|
||||
if (_diskProvider.FileExists(_configFile))
|
||||
{
|
||||
return XDocument.Load(_configFile);
|
||||
}
|
||||
|
||||
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
|
||||
xDoc.Add(new XElement(CONFIG_ELEMENT_NAME));
|
||||
|
||||
return xDoc;
|
||||
}
|
||||
}
|
||||
|
||||
catch (XmlException ex)
|
||||
{
|
||||
throw new InvalidConfigFileException(_configFile + " is invalid, please see the http://wiki.sonarr.tv for steps to resolve this issue.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveConfigFile(XDocument xDoc)
|
||||
{
|
||||
lock (Mutex)
|
||||
{
|
||||
_diskProvider.WriteAllText(_configFile, xDoc.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private string GenerateApiKey()
|
||||
{
|
||||
return Guid.NewGuid().ToString().Replace("-", "");
|
||||
@ -359,6 +376,7 @@ namespace NzbDrone.Core.Configuration
|
||||
|
||||
public void HandleAsync(ApplicationStartedEvent message)
|
||||
{
|
||||
EnsureDefaultConfigFile();
|
||||
DeleteOldValues();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user