Fixed: Extra checks in place to prevent config file corruption

pull/185/head
Mark McDowall 10 years ago
parent 013615a7a5
commit ddb24549d4

@ -29,10 +29,8 @@ namespace NzbDrone.Common.Test
const string key = "Port"; const string key = "Port";
const string value = "8989"; const string value = "8989";
var result = Subject.GetValue(key, value); var result = Subject.GetValue(key, value);
result.Should().Be(value); result.Should().Be(value);
} }

@ -6,6 +6,7 @@ using System.Text.RegularExpressions;
using System.Xml; using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration.Events; using NzbDrone.Core.Configuration.Events;
@ -48,15 +49,22 @@ namespace NzbDrone.Core.Configuration
public const string CONFIG_ELEMENT_NAME = "Config"; public const string CONFIG_ELEMENT_NAME = "Config";
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
private readonly IDiskProvider _diskProvider;
private readonly ICached<string> _cache; private readonly ICached<string> _cache;
private readonly string _configFile; private readonly string _configFile;
private static readonly Regex HiddenCharacterRegex = new Regex("[^a-z0-9]", RegexOptions.Compiled | RegexOptions.IgnoreCase); 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()); _cache = cacheManager.GetCache<string>(GetType());
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_diskProvider = diskProvider;
_configFile = appFolderInfo.GetConfigPath(); _configFile = appFolderInfo.GetConfigPath();
} }
@ -247,8 +255,6 @@ namespace NzbDrone.Core.Configuration
{ {
return _cache.Get(key, () => return _cache.Get(key, () =>
{ {
EnsureDefaultConfigFile();
var xDoc = LoadConfigFile(); var xDoc = LoadConfigFile();
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single(); var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
@ -274,8 +280,6 @@ namespace NzbDrone.Core.Configuration
public void SetValue(string key, object value) public void SetValue(string key, object value)
{ {
EnsureDefaultConfigFile();
var valueString = value.ToString().Trim(); var valueString = value.ToString().Trim();
var xDoc = LoadConfigFile(); var xDoc = LoadConfigFile();
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single(); var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
@ -296,7 +300,7 @@ namespace NzbDrone.Core.Configuration
_cache.Set(key, valueString); _cache.Set(key, valueString);
xDoc.Save(_configFile); SaveConfigFile(xDoc);
} }
public void SetValue(string key, Enum value) public void SetValue(string key, Enum value)
@ -308,18 +312,12 @@ namespace NzbDrone.Core.Configuration
{ {
if (!File.Exists(_configFile)) 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()); SaveConfigDictionary(GetConfigDictionary());
} }
} }
private void DeleteOldValues() private void DeleteOldValues()
{ {
EnsureDefaultConfigFile();
var xDoc = LoadConfigFile(); var xDoc = LoadConfigFile();
var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single(); var config = xDoc.Descendants(CONFIG_ELEMENT_NAME).Single();
@ -336,14 +334,25 @@ namespace NzbDrone.Core.Configuration
} }
} }
xDoc.Save(_configFile); SaveConfigFile(xDoc);
} }
private XDocument LoadConfigFile() private XDocument LoadConfigFile()
{ {
try try
{ {
return XDocument.Load(_configFile); 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) catch (XmlException ex)
@ -352,6 +361,14 @@ namespace NzbDrone.Core.Configuration
} }
} }
private void SaveConfigFile(XDocument xDoc)
{
lock (Mutex)
{
_diskProvider.WriteAllText(_configFile, xDoc.ToString());
}
}
private string GenerateApiKey() private string GenerateApiKey()
{ {
return Guid.NewGuid().ToString().Replace("-", ""); return Guid.NewGuid().ToString().Replace("-", "");
@ -359,6 +376,7 @@ namespace NzbDrone.Core.Configuration
public void HandleAsync(ApplicationStartedEvent message) public void HandleAsync(ApplicationStartedEvent message)
{ {
EnsureDefaultConfigFile();
DeleteOldValues(); DeleteOldValues();
} }

Loading…
Cancel
Save