diff options
author | Adam <Adam@anope.org> | 2013-01-27 01:59:38 -0500 |
---|---|---|
committer | Adam <Adam@anope.org> | 2013-01-27 01:59:38 -0500 |
commit | 0052dd29a7b0f8286990a49af6e4947e4bc784cf (patch) | |
tree | 0602378c773b581bdbc5d7f6973945bf4908cddf | |
parent | 50a42d2cbf2ff00ecab6bd432e88c8ca17a47219 (diff) |
Fix db_flatfile not clearing databases on save if there are no objects left of that type (it will leave the old database with old objects currently)
-rw-r--r-- | include/serialize.h | 2 | ||||
-rw-r--r-- | modules/database/db_flatfile.cpp | 48 | ||||
-rw-r--r-- | src/serialize.cpp | 5 |
3 files changed, 31 insertions, 24 deletions
diff --git a/include/serialize.h b/include/serialize.h index 918944804..b136412e4 100644 --- a/include/serialize.h +++ b/include/serialize.h @@ -176,6 +176,8 @@ class CoreExport Serialize::Type static Serialize::Type *Find(const Anope::string &name); static const std::vector<Anope::string> &GetTypeOrder(); + + static const std::map<Anope::string, Serialize::Type *>& GetTypes(); }; /** Should be used to hold lists and other objects of a specific type, diff --git a/modules/database/db_flatfile.cpp b/modules/database/db_flatfile.cpp index b1f86d984..a2db7c1b4 100644 --- a/modules/database/db_flatfile.cpp +++ b/modules/database/db_flatfile.cpp @@ -246,6 +246,29 @@ class DBFlatFile : public Module, public Pipe { std::map<Module *, std::fstream *> databases; + /* First open the databases of all of the registered types. This way, if we have a type with 0 objects, that database will be properly cleared */ + for (std::map<Anope::string, Serialize::Type *>::const_iterator it = Serialize::Type::GetTypes().begin(), it_end = Serialize::Type::GetTypes().end(); it != it_end; ++it) + { + Serialize::Type *s_type = it->second; + + if (databases[s_type->GetOwner()]) + continue; + + Anope::string db_name; + if (s_type->GetOwner()) + db_name = Anope::DataDir + "/module_" + s_type->GetOwner()->name + ".db"; + else + db_name = Anope::DataDir + "/" + database_file; + + if (Anope::IsFile(db_name)) + rename(db_name.c_str(), (db_name + ".tmp").c_str()); + + std::fstream *fs = databases[s_type->GetOwner()] = new std::fstream(db_name.c_str(), std::ios_base::out | std::ios_base::trunc); + + if (!fs->is_open()) + Log(this) << "Unable to open " << db_name << " for writing"; + } + SaveData data; const std::list<Serializable *> &items = Serializable::GetItems(); for (std::list<Serializable *>::const_iterator it = items.begin(), it_end = items.end(); it != it_end; ++it) @@ -253,31 +276,8 @@ class DBFlatFile : public Module, public Pipe Serializable *base = *it; Serialize::Type *s_type = base->GetSerializableType(); - if (!s_type) - continue; - data.fs = databases[s_type->GetOwner()]; - - if (!data.fs) - { - Anope::string db_name; - if (s_type->GetOwner()) - db_name = Anope::DataDir + "/module_" + s_type->GetOwner()->name + ".db"; - else - db_name = Anope::DataDir + "/" + database_file; - - if (Anope::IsFile(db_name)) - rename(db_name.c_str(), (db_name + ".tmp").c_str()); - - data.fs = databases[s_type->GetOwner()] = new std::fstream(db_name.c_str(), std::ios_base::out | std::ios_base::trunc); - - if (!data.fs->is_open()) - { - Log(this) << "Unable to open " << db_name << " for writing"; - continue; - } - } - else if (!data.fs->is_open()) + if (!data.fs || !data.fs->is_open()) continue; *data.fs << "OBJECT " << s_type->GetName(); diff --git a/src/serialize.cpp b/src/serialize.cpp index 5594cf93e..4f0068a48 100644 --- a/src/serialize.cpp +++ b/src/serialize.cpp @@ -170,3 +170,8 @@ const std::vector<Anope::string> &Type::GetTypeOrder() return TypeOrder; } +const std::map<Anope::string, Serialize::Type *>& Type::GetTypes() +{ + return Types; +} + |