1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* (C) 2003-2011 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
/*************************************************************************/
#include "module.h"
Anope::string DatabaseFile;
class DBFlatFile : public Module
{
/* Day the last backup was on */
int LastDay;
/* Backup file names */
std::list<Anope::string> Backups;
public:
DBFlatFile(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE)
{
this->SetAuthor("Anope");
Implementation i[] = { I_OnReload, I_OnLoadDatabase, I_OnSaveDatabase };
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
OnReload();
LastDay = 0;
}
void BackupDatabase()
{
/* Do not backup a database that doesn't exist */
if (!IsFile(DatabaseFile))
return;
time_t now = Anope::CurTime;
tm *tm = localtime(&now);
if (tm->tm_mday != LastDay)
{
LastDay = tm->tm_mday;
Anope::string newname = "backups/" + DatabaseFile + "." + stringify(tm->tm_year) + stringify(tm->tm_mon) + stringify(tm->tm_mday);
/* Backup already exists */
if (IsFile(newname))
return;
Log(LOG_DEBUG) << "db_flatfile: Attemping to rename " << DatabaseFile << " to " << newname;
if (rename(DatabaseFile.c_str(), newname.c_str()))
{
Log() << "Unable to back up database!";
if (!Config->NoBackupOkay)
quitting = true;
return;
}
Backups.push_back(newname);
if (Config->KeepBackups > 0 && Backups.size() > static_cast<unsigned>(Config->KeepBackups))
{
unlink(Backups.front().c_str());
Backups.pop_front();
}
}
}
void OnReload()
{
ConfigReader config;
DatabaseFile = config.ReadValue("db_flatfile", "database", "anope.db", 0);
}
EventReturn OnLoadDatabase()
{
std::fstream db;
db.open(DatabaseFile.c_str(), std::ios_base::in);
if (!db.is_open())
{
Log() << "Unable to open " << DatabaseFile << " for reading!";
return EVENT_CONTINUE;
}
SerializeType *st = NULL;
Serializable::serialized_data data;
std::multimap<SerializeType *, Serializable::serialized_data> objects;
for (Anope::string buf, token; std::getline(db, buf.str());)
{
spacesepstream sep(buf);
if (!sep.GetToken(token))
continue;
if (token == "OBJECT" && sep.GetToken(token))
{
st = SerializeType::Find(token);
data.clear();
}
else if (token == "DATA" && st != NULL && sep.GetToken(token))
data[token] << sep.GetRemaining();
else if (token == "END" && st != NULL)
{
objects.insert(std::make_pair(st, data));
st = NULL;
data.clear();
}
}
const std::vector<Anope::string> type_order = SerializeType::GetTypeOrder();
for (unsigned i = 0; i < type_order.size(); ++i)
{
SerializeType *stype = SerializeType::Find(type_order[i]);
std::multimap<SerializeType *, Serializable::serialized_data>::iterator it = objects.find(stype), it_end = objects.upper_bound(stype);
if (it == objects.end())
continue;
for (; it != it_end; ++it)
it->first->Create(it->second);
}
db.close();
return EVENT_STOP;
}
EventReturn OnSaveDatabase()
{
BackupDatabase();
Anope::string tmp_db = DatabaseFile + ".tmp";
if (IsFile(DatabaseFile))
rename(DatabaseFile.c_str(), tmp_db.c_str());
std::fstream db(DatabaseFile.c_str(), std::ios_base::out | std::ios_base::trunc);
if (!db.is_open())
{
Log() << "Unable to open " << DatabaseFile << " for writing";
if (IsFile(tmp_db))
rename(tmp_db.c_str(), DatabaseFile.c_str());
return EVENT_CONTINUE;
}
const std::list<Serializable *> &items = Serializable::GetItems();
for (std::list<Serializable *>::const_iterator it = items.begin(), it_end = items.end(); it != it_end; ++it)
{
Serializable *base = *it;
Serializable::serialized_data data = base->serialize();
db << "OBJECT " << base->serialize_name() << "\n";
for (Serializable::serialized_data::iterator dit = data.begin(), dit_end = data.end(); dit != dit_end; ++dit)
db << "DATA " << dit->first << " " << dit->second.astr() << "\n";
db << "END\n";
}
db.close();
if (db.good() == false)
{
Log() << "Unable to write database";
if (!Config->NoBackupOkay)
quitting = true;
if (IsFile(tmp_db))
rename(tmp_db.c_str(), DatabaseFile.c_str());
}
else
unlink(tmp_db.c_str());
return EVENT_CONTINUE;
}
};
MODULE_INIT(DBFlatFile)
|