summaryrefslogtreecommitdiff
path: root/modules/database/db_flatfile.cpp
blob: d1080b0099da113bf2d7eb65b5c5bf2aaa51c723 (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 *
 * (C) 2003-2025 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"

#ifndef _WIN32
#include <sys/wait.h>
#endif

class SaveData : public Serialize::Data
{
 public:
	Anope::string last;
	std::fstream *fs;

	SaveData() : fs(NULL) { }

	std::iostream& operator[](const Anope::string &key) anope_override
	{
		if (key != last)
		{
			*fs << "\nDATA " << key << " ";
			last = key;
		}

		return *fs;
	}
};

class LoadData : public Serialize::Data
{
 public:
	std::fstream *fs;
	unsigned int id;
	std::map<Anope::string, Anope::string> data;
	std::stringstream ss;
	bool read;

	LoadData() : fs(NULL), id(0), read(false) { }

	std::iostream& operator[](const Anope::string &key) anope_override
	{
		if (!read)
		{
			for (Anope::string token; std::getline(*this->fs, token.str());)
			{
				if (token.find("ID ") == 0)
				{
					try
					{
						this->id = convertTo<unsigned int>(token.substr(3));
					}
					catch (const ConvertException &) { }

					continue;
				}
				else if (token.find("DATA ") != 0)
					break;

				size_t sp = token.find(' ', 5); // Skip DATA
				if (sp != Anope::string::npos)
					data[token.substr(5, sp - 5)] = token.substr(sp + 1);
			}

			read = true;
		}

		ss.clear();
		this->ss << this->data[key];
		return this->ss;
	}

	std::set<Anope::string> KeySet() const anope_override
	{
		std::set<Anope::string> keys;
		for (std::map<Anope::string, Anope::string>::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it)
			keys.insert(it->first);
		return keys;
	}

	size_t Hash() const anope_override
	{
		size_t hash = 0;
		for (std::map<Anope::string, Anope::string>::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it)
			if (!it->second.empty())
				hash ^= Anope::hash_cs()(it->second);
		return hash;
	}

	void Reset()
	{
		id = 0;
		read = false;
		data.clear();
	}
};

class DBFlatFile : public Module, public Pipe
{
	/* Day the last backup was on */
	int last_day;
	/* Backup file names */
	std::map<Anope::string, std::list<Anope::string> > backups;
	bool loaded;

	int child_pid;

	void BackupDatabase()
	{
		tm *tm = localtime(&Anope::CurTime);

		if (tm->tm_mday != last_day)
		{
			last_day = tm->tm_mday;

			const std::vector<Anope::string> &type_order = Serialize::Type::GetTypeOrder();

			std::set<Anope::string> dbs;
			dbs.insert(Config->GetModule(this)->Get<const Anope::string>("database", "anope.db"));

			for (unsigned i = 0; i < type_order.size(); ++i)
			{
				Serialize::Type *stype = Serialize::Type::Find(type_order[i]);

				if (stype && stype->GetOwner())
					dbs.insert("module_" + stype->GetOwner()->name + ".db");
			}


			for (std::set<Anope::string>::const_iterator it = dbs.begin(), it_end = dbs.end(); it != it_end; ++it)
			{
				const Anope::string &oldname = Anope::DataDir + "/" + *it;
				Anope::string newname = Anope::DataDir + "/backups/" + *it + "-" + stringify(tm->tm_year + 1900) + Anope::printf("-%02i-", tm->tm_mon + 1) + Anope::printf("%02i", tm->tm_mday);

				/* Backup already exists or no database to backup */
				if (Anope::IsFile(newname) || !Anope::IsFile(oldname))
					continue;

				Log(LOG_DEBUG) << "db_flatfile: Attempting to rename " << *it << " to " << newname;
				if (rename(oldname.c_str(), newname.c_str()))
				{
					Anope::string err = Anope::LastError();
					Log(this) << "Unable to back up database " << *it << " (" << err << ")!";

					if (!Config->GetModule(this)->Get<bool>("nobackupokay"))
					{
						Anope::Quitting = true;
						Anope::QuitReason = "Unable to back up database " + *it + " (" + err + ")";
					}

					continue;
				}

				backups[*it].push_back(newname);

				unsigned keepbackups = Config->GetModule(this)->Get<unsigned>("keepbackups");
				if (keepbackups > 0 && backups[*it].size() > keepbackups)
				{
					unlink(backups[*it].front().c_str());
					backups[*it].pop_front();
				}
			}
		}
	}

 public:
	DBFlatFile(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE | VENDOR), last_day(0), loaded(false), child_pid(-1)
	{

	}

#ifndef _WIN32
	void OnRestart() anope_override
	{
		OnShutdown();
	}

	void OnShutdown() anope_override
	{
		if (child_pid > -1)
		{
			Log(this) << "Waiting for child to exit...";

			int status;
			waitpid(child_pid, &status, 0);

			Log(this) << "Done";
		}
	}
#endif

	void OnNotify() anope_override
	{
		char buf[512];
		int i = this->Read(buf, sizeof(buf) - 1);
		if (i <= 0)
			return;
		buf[i] = 0;

		child_pid = -1;

		if (!*buf)
		{
			Log(this) << "Finished saving databases";
			return;
		}

		Log(this) << "Error saving databases: " << buf;

		if (!Config->GetModule(this)->Get<bool>("nobackupokay"))
			Anope::Quitting = true;
	}

	EventReturn OnLoadDatabase() anope_override
	{
		const std::vector<Anope::string> &type_order = Serialize::Type::GetTypeOrder();
		std::set<Anope::string> tried_dbs;

		const Anope::string &db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get<const Anope::string>("database", "anope.db");

		std::fstream fd(db_name.c_str(), std::ios_base::in | std::ios_base::binary);
		if (!fd.is_open())
		{
			Log(this) << "Unable to open " << db_name << " for reading!";
			return EVENT_STOP;
		}

		std::map<Anope::string, std::vector<std::streampos> > positions;

		for (Anope::string buf; std::getline(fd, buf.str());)
			if (buf.find("OBJECT ") == 0)
				positions[buf.substr(7)].push_back(fd.tellg());

		LoadData ld;
		ld.fs = &fd;

		for (unsigned i = 0; i < type_order.size(); ++i)
		{
			Serialize::Type *stype = Serialize::Type::Find(type_order[i]);
			if (!stype || stype->GetOwner())
				continue;

			std::vector<std::streampos> &pos = positions[stype->GetName()];

			for (unsigned j = 0; j < pos.size(); ++j)
			{
				fd.clear();
				fd.seekg(pos[j]);

				Serializable *obj = stype->Unserialize(NULL, ld);
				if (obj != NULL)
					obj->id = ld.id;
				ld.Reset();
			}
		}

		fd.close();

		loaded = true;
		return EVENT_STOP;
	}


	void OnSaveDatabase() anope_override
	{
		if (child_pid > -1)
		{
			Log(this) << "Database save is already in progress!";
			return;
		}

		BackupDatabase();

		int i = -1;
#ifndef _WIN32
		if (!Anope::Quitting && Config->GetModule(this)->Get<bool>("fork"))
		{
			i = fork();
			if (i > 0)
			{
				child_pid = i;
				return;
			}
			else if (i < 0)
				Log(this) << "Unable to fork for database save";
		}
#endif

		try
		{
			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 + "/" + Config->GetModule(this)->Get<const Anope::string>("database", "anope.db");

				std::fstream *fs = databases[s_type->GetOwner()] = new std::fstream((db_name + ".tmp").c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);

				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)
			{
				Serializable *base = *it;
				Serialize::Type *s_type = base->GetSerializableType();
				if (!s_type)
					continue;

				data.fs = databases[s_type->GetOwner()];
				if (!data.fs || !data.fs->is_open())
					continue;

				*data.fs << "OBJECT " << s_type->GetName();
				if (base->id)
					*data.fs << "\nID " << base->id;
				base->Serialize(data);
				*data.fs << "\nEND\n";
			}

			for (std::map<Module *, std::fstream *>::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it)
			{
				std::fstream *f = it->second;
				const Anope::string &db_name = Anope::DataDir + "/" + (it->first ? (it->first->name + ".db") : Config->GetModule(this)->Get<const Anope::string>("database", "anope.db"));

				if (!f->is_open() || !f->good())
				{
					this->Write("Unable to write database " + db_name);

					f->close();
				}
				else
				{
					f->close();
#ifdef _WIN32
					/* Windows rename() fails if the file already exists. */
					remove(db_name.c_str());
#endif
					rename((db_name + ".tmp").c_str(), db_name.c_str());
				}

				delete f;
			}
		}
		catch (...)
		{
			if (i)
				throw;
		}

		if (!i)
		{
			this->Notify();
			exit(0);
		}
	}

	/* Load just one type. Done if a module is reloaded during runtime */
	void OnSerializeTypeCreate(Serialize::Type *stype) anope_override
	{
		if (!loaded)
			return;

		Anope::string db_name;
		if (stype->GetOwner())
			db_name = Anope::DataDir + "/module_" + stype->GetOwner()->name + ".db";
		else
			db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get<const Anope::string>("database", "anope.db");

		std::fstream fd(db_name.c_str(), std::ios_base::in | std::ios_base::binary);
		if (!fd.is_open())
		{
			Log(this) << "Unable to open " << db_name << " for reading!";
			return;
		}

		LoadData ld;
		ld.fs = &fd;

		for (Anope::string buf; std::getline(fd, buf.str());)
		{
			if (buf == "OBJECT " + stype->GetName())
			{
				stype->Unserialize(NULL, ld);
				ld.Reset();
			}
		}

		fd.close();
	}
};

MODULE_INIT(DBFlatFile)