public void SaveState()
{
/* The configuration regarding the various sources is stored in config.xml and the list of hosts who are
* allowed despite invalid certificates is stored in allowed.xml. This was done to allow one to easily 'reset'
* the security allowances by deleting a single file. */
string configFolderRoot = Path.Combine (System.Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "splatter");
if (!System.IO.Directory.Exists (configFolderRoot)) {
System.IO.Directory.CreateDirectory (configFolderRoot);
}
string dataFilePath = Path.Combine (configFolderRoot, dataFileName);
string allowedFilePath = Path.Combine (configFolderRoot, allowFileName);
Console.WriteLine ("Saving configuration to " + dataFilePath);
Console.WriteLine ("Saving list of allowed thumbprints to " + dataFilePath);
/*XmlAttributes attrs = new XmlAttributes();
TODO ??? Why is this here?
XmlElementAttribute attr1 = new XmlElementAttribute("ReportedByQuery",typeof(ReportedByQuery));
attrs.XmlElements.Add(attr1);
XmlAttributeOverrides attrOverRides = new XmlAttributeOverrides();
attrOverRides.Add(typeof(Query), "Generator", attrs);*/
if(File.Exists (dataFilePath)) {
File.Move(dataFilePath, dataFilePath + ".bak");
// Pray this works
}
if(File.Exists (allowedFilePath)) {
File.Move(allowedFilePath, allowedFilePath + ".bak");
}
XmlSerializer configurationSerializer = new XmlSerializer(typeof(SplatterCore));
FileStream configFile = new FileStream(dataFilePath, FileMode.Create, FileAccess.Write);
TextWriter configFileWriter = new StreamWriter(configFile);
XmlSerializer thumbprintSerializer = new XmlSerializer(typeof(List<string>));
FileStream allowedFile = new FileStream(allowedFilePath, FileMode.Create, FileAccess.Write);
TextWriter allowedFileWriter = new StreamWriter(allowedFile);
configurationSerializer.Serialize(configFileWriter, this);
thumbprintSerializer.Serialize (allowedFileWriter, SecurityCertificateHandler.Instance.AllowedThumbPrints);
configFileWriter.Close();
configFile.Close();
allowedFileWriter.Close();
allowedFile.Close();
/* @Andy Why delete the backup file at all ? */
// Otherwise, we get an error like this:
// System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.IOException: Win32 IO returned ERROR_ALREADY_EXISTS. Path:
// - Andy
if(File.Exists (dataFilePath + ".bak"))
{
File.Delete (dataFilePath + ".bak");
}
if(File.Exists (allowedFilePath + ".bak"))
{
File.Delete (allowedFilePath + ".bak");
}
}