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
414
415
416
417
418
419
420
421
422
423
424
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/TdDb.h"
#include "td/telegram/DialogDb.h"
#include "td/telegram/files/FileDb.h"
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/MessagesDb.h"
#include "td/telegram/TdParameters.h"
#include "td/actor/actor.h"
#include "td/actor/MultiPromise.h"
#include "td/db/BinlogKeyValue.h"
#include "td/utils/logging.h"
#include "td/utils/port/path.h"
#include "td/utils/Random.h"
namespace td {
namespace {
std::string get_binlog_path(const TdParameters ¶meters) {
return PSTRING() << parameters.database_directory << "td" << (parameters.use_test_dc ? "_test" : "") << ".binlog";
}
std::string get_sqlite_path(const TdParameters ¶meters) {
const string db_name = "db" + (parameters.use_test_dc ? string("_test") : string());
return parameters.database_directory + db_name + ".sqlite";
}
Result<EncryptionInfo> check_encryption(string path) {
Binlog binlog;
auto status = binlog.init(path, Binlog::Callback());
if (status.is_error() && status.code() != Binlog::Error::WrongPassword) {
return Status::Error(400, status.message());
}
EncryptionInfo info;
info.is_encrypted = binlog.get_info().wrong_password;
binlog.close(false /*need_sync*/).ensure();
return info;
}
Status init_binlog(Binlog &binlog, string path, BinlogKeyValue<Binlog> &binlog_pmc, BinlogKeyValue<Binlog> &config_pmc,
TdDb::Events &events, DbKey key) {
auto callback = [&](const BinlogEvent &event) {
switch (event.type_) {
case LogEvent::HandlerType::SecretChats:
events.to_secret_chats_manager.push_back(event.clone());
break;
case LogEvent::HandlerType::Users:
events.user_events.push_back(event.clone());
break;
case LogEvent::HandlerType::Chats:
events.chat_events.push_back(event.clone());
break;
case LogEvent::HandlerType::Channels:
events.channel_events.push_back(event.clone());
break;
case LogEvent::HandlerType::SecretChatInfos:
events.secret_chat_events.push_back(event.clone());
break;
case LogEvent::HandlerType::WebPages:
events.web_page_events.push_back(event.clone());
break;
case LogEvent::HandlerType::SendMessage:
case LogEvent::HandlerType::DeleteMessage:
case LogEvent::HandlerType::DeleteMessagesFromServer:
case LogEvent::HandlerType::ReadHistoryOnServer:
case LogEvent::HandlerType::ReadMessageContentsOnServer:
case LogEvent::HandlerType::ForwardMessages:
case LogEvent::HandlerType::SendBotStartMessage:
case LogEvent::HandlerType::SendScreenshotTakenNotificationMessage:
case LogEvent::HandlerType::SendInlineQueryResultMessage:
case LogEvent::HandlerType::DeleteDialogHistoryFromServer:
case LogEvent::HandlerType::ReadAllDialogMentionsOnServer:
case LogEvent::HandlerType::DeleteAllChannelMessagesFromUserOnServer:
case LogEvent::HandlerType::ToggleDialogIsPinnedOnServer:
case LogEvent::HandlerType::ReorderPinnedDialogsOnServer:
case LogEvent::HandlerType::SaveDialogDraftMessageOnServer:
case LogEvent::HandlerType::GetChannelDifference:
events.to_messages_manager.push_back(event.clone());
break;
case LogEvent::HandlerType::BinlogPmcMagic:
binlog_pmc.external_init_handle(event);
break;
case LogEvent::HandlerType::ConfigPmcMagic:
config_pmc.external_init_handle(event);
break;
default:
LOG(FATAL) << "Unsupported logevent type " << event.type_;
}
};
auto binlog_info = binlog.init(std::move(path), callback, std::move(key));
if (binlog_info.is_error()) {
return binlog_info.move_as_error();
}
return Status::OK();
}
Status init_db(SqliteDb &db) {
TRY_STATUS(db.exec("PRAGMA encoding=\"UTF-8\""));
TRY_STATUS(db.exec("PRAGMA journal_mode=WAL"));
TRY_STATUS(db.exec("PRAGMA synchronous=NORMAL"));
TRY_STATUS(db.exec("PRAGMA temp_store=MEMORY"));
TRY_STATUS(db.exec("PRAGMA secure_delete=1"));
return Status::OK();
}
} // namespace
std::shared_ptr<FileDbInterface> TdDb::get_file_db_shared() {
return file_db_;
}
std::shared_ptr<SqliteConnectionSafe> &TdDb::get_sqlite_connection_safe() {
return sql_connection_;
}
ConcurrentBinlog *TdDb::get_binlog() {
CHECK(binlog_);
return binlog_.get();
}
BinlogPmc TdDb::get_binlog_pmc_shared() {
return binlog_pmc_;
}
BinlogPmcPtr TdDb::get_binlog_pmc() {
CHECK(binlog_pmc_);
return binlog_pmc_.get();
}
BinlogPmcPtr TdDb::get_config_pmc() {
CHECK(config_pmc_);
return config_pmc_.get();
}
BigPmcPtr TdDb::get_sqlite_sync_pmc() {
CHECK(common_kv_safe_);
return &common_kv_safe_->get();
}
SqliteKeyValueAsyncInterface *TdDb::get_sqlite_pmc() {
CHECK(common_kv_async_);
return common_kv_async_.get();
}
MessagesDbSyncInterface *TdDb::get_messages_db_sync() {
return &messages_db_sync_safe_->get();
}
MessagesDbAsyncInterface *TdDb::get_messages_db_async() {
return messages_db_async_.get();
}
DialogDbSyncInterface *TdDb::get_dialog_db_sync() {
return &dialog_db_sync_safe_->get();
}
DialogDbAsyncInterface *TdDb::get_dialog_db_async() {
return dialog_db_async_.get();
}
CSlice TdDb::binlog_path() const {
return binlog_->get_path();
}
CSlice TdDb::sqlite_path() const {
return sqlite_path_;
}
void TdDb::flush_all() {
if (messages_db_async_) {
messages_db_async_->force_flush();
}
binlog_->force_flush();
}
void TdDb::close_all(Promise<> on_finished) {
do_close(std::move(on_finished), false /*destroy_flag*/);
}
void TdDb::close_and_destroy_all(Promise<> on_finished) {
do_close(std::move(on_finished), true /*destroy_flag*/);
}
void TdDb::do_close(Promise<> on_finished, bool destroy_flag) {
MultiPromiseActorSafe mpas;
mpas.add_promise(PromiseCreator::lambda(
[promise = std::move(on_finished), sql_connection = std::move(sql_connection_), destroy_flag](Unit) mutable {
if (sql_connection) {
CHECK(sql_connection.unique()) << sql_connection.use_count();
if (destroy_flag) {
sql_connection->close_and_destroy();
} else {
sql_connection->close();
}
sql_connection.reset();
}
promise.set_value(Unit());
}));
auto lock = mpas.get_promise();
if (file_db_) {
file_db_->close(mpas.get_promise());
file_db_.reset();
}
common_kv_safe_.reset();
if (common_kv_async_) {
common_kv_async_->close(mpas.get_promise());
}
messages_db_sync_safe_.reset();
if (messages_db_async_) {
messages_db_async_->close(mpas.get_promise());
}
dialog_db_sync_safe_.reset();
if (dialog_db_async_) {
dialog_db_async_->close(mpas.get_promise());
}
// binlog_pmc is dependent on binlog_ and anyway it doesn't support close_and_destroy
CHECK(binlog_pmc_.unique());
binlog_pmc_.reset();
CHECK(config_pmc_.unique());
config_pmc_.reset();
if (binlog_) {
if (destroy_flag) {
binlog_->close_and_destroy(mpas.get_promise());
} else {
binlog_->close(mpas.get_promise());
}
binlog_.reset();
}
}
Status TdDb::init_sqlite(int32 scheduler_id, const TdParameters ¶meters, DbKey key, DbKey old_key,
BinlogKeyValue<Binlog> &binlog_pmc) {
CHECK(!parameters.use_message_db || parameters.use_chat_info_db);
CHECK(!parameters.use_chat_info_db || parameters.use_file_db);
const string sql_db_name = get_sqlite_path(parameters);
bool use_sqlite = parameters.use_file_db;
bool use_file_db = parameters.use_file_db;
bool use_dialog_db = parameters.use_message_db;
bool use_message_db = parameters.use_message_db;
if (!use_sqlite) {
unlink(sql_db_name).ignore();
return Status::OK();
}
sqlite_path_ = sql_db_name;
TRY_STATUS(SqliteDb::change_key(sqlite_path_, key, old_key));
sql_connection_ = std::make_shared<SqliteConnectionSafe>(sql_db_name, key);
auto &db = sql_connection_->get();
TRY_STATUS(init_db(db));
// Init databases
// Do initialization once and before everything else to avoid "database is locked" error.
// Must be in a transaction
// NB: when database is dropped we should also drop corresponding binlog events
TRY_STATUS(db.exec("BEGIN TRANSACTION"));
// Get 'PRAGMA user_version'
TRY_RESULT(user_version, db.user_version());
LOG(WARNING) << "got PRAGMA user_version = " << user_version;
// init DialogDb
bool dialog_db_was_created = false;
if (use_dialog_db) {
TRY_STATUS(init_dialog_db(db, user_version, dialog_db_was_created));
} else {
TRY_STATUS(drop_dialog_db(db, user_version));
}
// init MessagesDb
if (use_message_db) {
TRY_STATUS(init_messages_db(db, user_version));
} else {
TRY_STATUS(drop_messages_db(db, user_version));
}
// init filesDb
if (use_file_db) {
TRY_STATUS(init_file_db(db, user_version));
} else {
TRY_STATUS(drop_file_db(db, user_version));
}
// Update 'PRAGMA user_version'
auto db_version = current_db_version();
if (db_version != user_version) {
LOG(WARNING) << "set PRAGMA user_version = " << db_version;
TRY_STATUS(db.set_user_version(db_version));
}
if (dialog_db_was_created) {
binlog_pmc.erase("unread_message_count");
binlog_pmc.erase("last_server_dialog_date");
}
if (db_version == 0) {
binlog_pmc.erase_by_prefix("top_dialogs");
binlog_pmc.erase("next_contacts_sync_date");
}
binlog_pmc.force_sync({});
TRY_STATUS(db.exec("COMMIT TRANSACTION"));
file_db_ = create_file_db(sql_connection_, scheduler_id);
common_kv_safe_ = std::make_shared<SqliteKeyValueSafe>("common", sql_connection_);
common_kv_async_ = create_sqlite_key_value_async(common_kv_safe_, scheduler_id);
if (use_dialog_db) {
dialog_db_sync_safe_ = create_dialog_db_sync(sql_connection_);
dialog_db_async_ = create_dialog_db_async(dialog_db_sync_safe_, scheduler_id);
}
if (use_message_db) {
messages_db_sync_safe_ = create_messages_db_sync(sql_connection_);
messages_db_async_ = create_messages_db_async(messages_db_sync_safe_, scheduler_id);
}
return Status::OK();
}
Status TdDb::init(int32 scheduler_id, const TdParameters ¶meters, DbKey key, Events &events) {
// Init pmc
Binlog *binlog_ptr = nullptr;
auto binlog = std::shared_ptr<Binlog>(new Binlog, [&](Binlog *ptr) { binlog_ptr = ptr; });
auto binlog_pmc = std::make_unique<BinlogKeyValue<Binlog>>();
auto config_pmc = std::make_unique<BinlogKeyValue<Binlog>>();
binlog_pmc->external_init_begin(static_cast<int32>(LogEvent::HandlerType::BinlogPmcMagic));
config_pmc->external_init_begin(static_cast<int32>(LogEvent::HandlerType::ConfigPmcMagic));
bool encrypt_binlog = !key.is_empty();
TRY_STATUS(init_binlog(*binlog, get_binlog_path(parameters), *binlog_pmc, *config_pmc, events, std::move(key)));
binlog_pmc->external_init_finish(binlog);
config_pmc->external_init_finish(binlog);
DbKey new_sqlite_key;
DbKey old_sqlite_key;
bool encrypt_sqlite = encrypt_binlog;
bool drop_sqlite_key = false;
auto sqlite_key = binlog_pmc->get("sqlite_key");
if (encrypt_sqlite) {
if (sqlite_key.empty()) {
sqlite_key = string(32, ' ');
Random::secure_bytes(sqlite_key);
binlog_pmc->set("sqlite_key", sqlite_key);
binlog_pmc->force_sync(Auto());
}
new_sqlite_key = DbKey::raw_key(std::move(sqlite_key));
} else {
if (!sqlite_key.empty()) {
old_sqlite_key = DbKey::raw_key(std::move(sqlite_key));
drop_sqlite_key = true;
}
}
auto init_sqlite_status = init_sqlite(scheduler_id, parameters, new_sqlite_key, old_sqlite_key, *binlog_pmc);
if (init_sqlite_status.is_error()) {
LOG(ERROR) << "Destroy bad sqlite db because of: " << init_sqlite_status;
SqliteDb::destroy(get_sqlite_path(parameters)).ignore();
TRY_STATUS(init_sqlite(scheduler_id, parameters, new_sqlite_key, old_sqlite_key, *binlog_pmc));
}
if (drop_sqlite_key) {
binlog_pmc->erase("sqlite_key");
binlog_pmc->force_sync(Auto());
}
auto concurrent_binlog_pmc = std::make_shared<BinlogKeyValue<ConcurrentBinlog>>();
concurrent_binlog_pmc->external_init_begin(binlog_pmc->get_magic());
concurrent_binlog_pmc->external_init_handle(std::move(*binlog_pmc));
auto concurrent_config_pmc = std::make_shared<BinlogKeyValue<ConcurrentBinlog>>();
concurrent_config_pmc->external_init_begin(config_pmc->get_magic());
concurrent_config_pmc->external_init_handle(std::move(*config_pmc));
binlog.reset();
binlog_pmc.reset();
config_pmc.reset();
CHECK(binlog_ptr != nullptr);
auto concurrent_binlog = std::make_shared<ConcurrentBinlog>(std::unique_ptr<Binlog>(binlog_ptr), scheduler_id);
concurrent_binlog_pmc->external_init_finish(concurrent_binlog);
concurrent_config_pmc->external_init_finish(concurrent_binlog);
binlog_pmc_ = std::move(concurrent_binlog_pmc);
config_pmc_ = std::move(concurrent_config_pmc);
binlog_ = std::move(concurrent_binlog);
return Status::OK();
}
TdDb::TdDb() = default;
TdDb::~TdDb() = default;
Result<std::unique_ptr<TdDb>> TdDb::open(int32 scheduler_id, const TdParameters ¶meters, DbKey key,
Events &events) {
auto db = std::make_unique<TdDb>();
TRY_STATUS(db->init(scheduler_id, parameters, std::move(key), events));
return std::move(db);
}
Result<EncryptionInfo> TdDb::check_encryption(const TdParameters ¶meters) {
return ::td::check_encryption(get_binlog_path(parameters));
}
void TdDb::change_key(DbKey key, Promise<> promise) {
get_binlog()->change_key(std::move(key), std::move(promise));
}
Status TdDb::destroy(const TdParameters ¶meters) {
SqliteDb::destroy(get_sqlite_path(parameters)).ignore();
Binlog::destroy(get_binlog_path(parameters)).ignore();
return Status::OK();
}
void TdDb::with_db_path(std::function<void(CSlice)> callback) {
SqliteDb::with_db_path(sqlite_path(), callback);
callback(binlog_path());
}
} // namespace td
|