+ dirty = true;
+ }
+
+ void OnExpireLine(XLine *line)
+ {
+ dirty = true;
+ }
+
+ void OnBackgroundTimer(time_t now)
+ {
+ if (dirty)
+ {
+ if (WriteDatabase())
+ dirty = false;
+ }
+ }
+
+ bool WriteDatabase()
+ {
+ FILE *f;
+
+ /*
+ * We need to perform an atomic write so as not to fuck things up.
+ * So, let's write to a temporary file, flush and sync the FD, then rename the file..
+ * Technically, that means that this can block, but I have *never* seen that.
+ * -- w00t
+ */
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opening temporary database");
+ std::string xlinenewdbpath = xlinedbpath + ".new";
+ f = fopen(xlinenewdbpath.c_str(), "w");
+ if (!f)
+ {
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot create database! %s (%d)", strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot create new db: %s (%d)", strerror(errno), errno);
+ return false;
+ }
+
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Opened. Writing..");
+
+ /*
+ * Now, much as I hate writing semi-unportable formats, additional
+ * xline types may not have a conf tag, so let's just write them.
+ * In addition, let's use a file version, so we can maintain some
+ * semblance of backwards compatibility for reading on startup..
+ * -- w00t
+ */
+ fprintf(f, "VERSION 1\n");
+
+ // Now, let's write.
+ std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
+ for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
+ {
+ XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
+ if (!lookup)
+ continue; // Not possible as we just obtained the list from XLineManager
+
+ for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
+ {
+ XLine* line = i->second;
+ fprintf(f, "LINE %s %s %s %lu %lu :%s\n", line->type.c_str(), line->Displayable(),
+ ServerInstance->Config->ServerName.c_str(), (unsigned long)line->set_time, (unsigned long)line->duration, line->reason.c_str());
+ }
+ }
+
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Finished writing XLines. Checking for error..");
+
+ int write_error = 0;
+ write_error = ferror(f);
+ write_error |= fclose(f);
+ if (write_error)
+ {
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot write to new database! %s (%d)", strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot write to new db: %s (%d)", strerror(errno), errno);
+ return false;
+ }
+
+#ifdef _WIN32
+ if (remove(xlinedbpath.c_str()))
+ {
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot remove old database! %s (%d)", strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot remove old database: %s (%d)", strerror(errno), errno);
+ return false;
+ }
+#endif
+ // Use rename to move temporary to new db - this is guarenteed not to fuck up, even in case of a crash.
+ if (rename(xlinenewdbpath.c_str(), xlinedbpath.c_str()) < 0)
+ {
+ ServerInstance->Logs->Log("m_xline_db",DEBUG, "xlinedb: Cannot move new to old database! %s (%d)", strerror(errno), errno);
+ ServerInstance->SNO->WriteToSnoMask('a', "database: cannot replace old with new db: %s (%d)", strerror(errno), errno);
+ return false;
+ }
+
+ return true;
+ }
+
+ bool ReadDatabase()
+ {
+ FILE *f;
+ char linebuf[MAXBUF];
+
+ f = fopen(xlinedbpath.c_str(), "r");
+ if (!f)