/// <summary> /// Recover a file that had a backup and remove it /// </summary> /// <param name="FileName">Name of file</param> /// <returns></returns> public static bool BackupRecovery(string FileName) { if (File.Exists(Configuration.TempName(FileName))) { string temp = Path.GetTempFileName(); File.Copy(Configuration.TempName(FileName), temp, true); Syslog.Log("Unfinished transaction from " + FileName + "~ was stored as " + temp); return(true); } return(false); }
private void FlushRows() { if (Recovering) { return; } // prevent multiple threads calling this function at same time lock (this) { string file = Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml"; if (File.Exists(file)) { Core.BackupData(file); if (!File.Exists(Configuration.TempName(file))) { Syslog.WarningLog("Unable to create backup file for " + file); return; } } try { File.Delete(file); XmlSerializer xs = new XmlSerializer(typeof(Unwritten)); StreamWriter writer = File.AppendText(file); lock (unwritten) { xs.Serialize(writer, unwritten); } writer.Close(); if (File.Exists(Configuration.TempName(file))) { File.Delete(Configuration.TempName(file)); } } catch (Exception fail) { Core.HandleException(fail); Syslog.WarningLog("Recovering the mysql unwritten dump because of exception to: " + file); Core.RecoverFile(file); } } }
/// <summary> /// Create a backup file /// /// This is very useful function in case you need to overwrite some file. In that case /// there is a chance for program to crash during the write and this would left the /// file corrupted, this function will create a copy of a file using config.tempName() /// which later needs to be deleted (after you finish your write operation). /// /// The file that is to be backed up doesn't need to exist, if it's not present the /// function just return false /// </summary> /// <param name="name">Full path of a file that you want to make a backup of</param> /// <returns>True on success or false</returns> public static bool BackupData(string name) { try { if (!File.Exists(name)) { return(false); } if (File.Exists(Configuration.TempName(name))) { BackupRecovery(name); } File.Copy(name, Configuration.TempName(name), true); } catch (Exception b) { HandleException(b); return(false); } return(true); }
/// <summary> /// Recover a file that was previously stored /// </summary> /// <param name="name"></param> /// <param name="ch"></param> /// <returns></returns> public static bool RecoverFile(string name, string ch = "unknown object") { try { if (File.Exists(Configuration.TempName(name))) { if (Core.GetTempFileName(name)) { Syslog.Log("Restoring unfinished transaction of " + ch + " for db_" + name); File.Copy(Configuration.TempName(name), name, true); return(true); } Syslog.Log("Unfinished transaction could not be restored! DB of " + name + " is probably broken", true); } return(false); } catch (Exception b) { HandleException(b); Syslog.Log("Unfinished transaction could not be restored! DB of " + name + " is now broken"); return(false); } }
/// <summary> /// Save config /// </summary> public void SaveConfig() { string fn = GetConfigFilePath(); try { XmlDocument data = new XmlDocument(); XmlNode xmlnode = data.CreateElement("channel"); InsertData("talkmode", Suppress.ToString(), ref data, ref xmlnode); InsertData("langcode", Language, ref data, ref xmlnode); InsertData("respond_message", RespondMessage.ToString(), ref data, ref xmlnode); InsertData("ignore-unknown", IgnoreUnknown.ToString(), ref data, ref xmlnode); InsertData("suppress-warnings", SuppressWarnings.ToString(), ref data, ref xmlnode); InsertData("respond_wait", RespondWait.ToString(), ref data, ref xmlnode); InsertData("sharedinfo", SharedDB, ref data, ref xmlnode); if (!String.IsNullOrEmpty(this.Password)) { InsertData("password", Password, ref data, ref xmlnode); } InsertData("defaultbot", DefaultInstance, ref data, ref xmlnode); if (!(SharedLinkedChan.Count < 1)) { foreach (Channel current in SharedLinkedChan) { InsertData("name", current.Name, ref data, ref xmlnode, "sharedch"); } } if (!(Infobot_IgnoredNames.Count < 1)) { foreach (string curr in Infobot_IgnoredNames) { InsertData("name", curr, ref data, ref xmlnode, "ignored"); } } if (ExtensionData.Count > 0) { foreach (KeyValuePair <string, string> item in ExtensionData) { InsertData(item.Key, item.Value, ref data, ref xmlnode, "extension"); } } lock (this.SystemUsers.Users) { foreach (SystemUser user in this.SystemUsers.Users) { XmlAttribute name = data.CreateAttribute("regex"); name.Value = user.Name; XmlAttribute kk = data.CreateAttribute("role"); kk.Value = user.Role; XmlNode db = data.CreateElement("user"); db.Attributes.Append(name); db.Attributes.Append(kk); xmlnode.AppendChild(db); } } if (File.Exists(fn)) { Core.BackupData(fn); if (!File.Exists(Configuration.TempName(fn))) { Syslog.WarningLog("Unable to create backup file for " + Name); } } data.AppendChild(xmlnode); data.Save(fn); if (File.Exists(Configuration.TempName(fn))) { File.Delete(Configuration.TempName(fn)); } } catch (Exception) { Core.RecoverFile(fn, Name); } }