forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomppackdb.cpp
More file actions
268 lines (227 loc) · 7.2 KB
/
Copy pathcomppackdb.cpp
File metadata and controls
268 lines (227 loc) · 7.2 KB
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
#include "comppackdb.hpp"
#include "pkgi.hpp"
#include "sqlite.hpp"
#include "utils.hpp"
#include <fmt/format.h>
#include <boost/scope_exit.hpp>
#include <algorithm>
#include <cstring>
#include <regex>
#include <stdexcept>
#include <string>
#include <stddef.h>
CompPackDatabase::CompPackDatabase(std::string const& dbPath) : _dbPath(dbPath)
{
reopen();
}
void CompPackDatabase::reopen()
{
LOG("Opening comppack database: %s", _dbPath.c_str());
sqlite3* db;
SQLITE_CHECK(sqlite3_open(_dbPath.c_str(), &db), "can't open database");
_sqliteDb.reset(db);
try
{
sqlite3_stmt* stmt;
SQLITE_CHECK(
sqlite3_prepare_v2(
_sqliteDb.get(),
R"(
SELECT titleid, app_version, path
FROM entries
WHERE 0)",
-1,
&stmt,
nullptr),
"sanity select failed");
sqlite3_finalize(stmt);
}
catch (const std::exception& e)
{
LOG_WARN("Schema migration needed: %s", e.what());
SQLITE_EXEC(
_sqliteDb,
R"(DROP TABLE IF EXISTS entries)",
"drop table failed");
}
SQLITE_EXEC(
_sqliteDb,
R"(
CREATE TABLE IF NOT EXISTS entries (
titleid TEXT NOT NULL,
app_version TEXT NOT NULL,
path TEXT NOT NULL,
PRIMARY KEY (titleid, app_version)
))",
"can't create comp pack table");
}
namespace
{
std::vector<const char*> pkgi_split_row(char** pptr, const char* end)
{
auto& ptr = *pptr;
std::vector<const char*> result;
while (ptr != end)
{
const char* field = ptr;
while (ptr != end && *ptr != '=' && *ptr != '\n')
++ptr;
if (ptr == end)
{
result.push_back(field);
break;
}
if (*ptr == '\n')
{
*ptr++ = 0;
break;
}
*ptr++ = 0;
result.push_back(field);
if (ptr == end)
{
result.push_back(field);
break;
}
}
return result;
}
}
void CompPackDatabase::parse_entries(std::string& db_data)
{
SQLITE_EXEC(_sqliteDb, "BEGIN", "can't begin transaction");
BOOST_SCOPE_EXIT_ALL(&)
{
if (std::uncaught_exceptions() == 0)
SQLITE_EXEC(_sqliteDb, "END", "can't end transaction");
else
{
char* errmsg;
auto err = sqlite3_exec(
_sqliteDb.get(), "ROLLBACK", nullptr, nullptr, &errmsg);
if (err != SQLITE_OK)
LOG_ERR("SQLite error during comppack save: %s", errmsg);
}
};
SQLITE_EXEC(_sqliteDb, "DELETE FROM entries", "can't truncate table");
sqlite3_stmt* stmt;
SQLITE_CHECK(
sqlite3_prepare_v2(
_sqliteDb.get(),
R"(INSERT INTO entries
(titleid, path, app_version)
VALUES (?, ?, ?))",
-1,
&stmt,
nullptr),
"can't prepare SQL statement");
BOOST_SCOPE_EXIT_ALL(&)
{
sqlite3_finalize(stmt);
};
char* ptr = db_data.data();
char* end = db_data.data() + db_data.size();
const auto regex = std::regex(
R"(([A-Z]{4}\d{5})-(\d{2}_\d{3})-(\d{2}_\d{2})-(\d{2}_\d{2}).ppk)");
const char* current_line = ptr;
while (ptr < end && *ptr)
{
try
{
current_line = ptr;
const auto fields = pkgi_split_row(&ptr, end);
if (fields.size() < 1)
throw std::runtime_error("failed to split line");
const auto path = std::string(fields[0]);
std::smatch matches;
if (!std::regex_search(path, matches, regex))
throw formatEx<std::runtime_error>("regex didn't match");
const auto titleid = matches.str(1);
const auto app_version = matches.str(3);
sqlite3_reset(stmt);
sqlite3_bind_text(stmt, 1, titleid.data(), titleid.size(), nullptr);
sqlite3_bind_text(stmt, 2, path.data(), path.size(), nullptr);
sqlite3_bind_text(
stmt, 3, app_version.data(), app_version.size(), nullptr);
auto err = sqlite3_step(stmt);
if (err != SQLITE_DONE)
throw std::runtime_error(fmt::format(
"can't execute SQL statement:\n{}",
sqlite3_errmsg(_sqliteDb.get())));
}
catch (const std::exception& e)
{
throw formatEx<std::runtime_error>(
"failed to parse line\n{}\n{}", current_line, e.what());
}
}
}
void CompPackDatabase::update(Http* http, const std::string& update_url)
{
std::string db_data;
db_data.resize(MAX_DB_SIZE);
uint64_t db_size = 0;
if (update_url.empty())
throw std::runtime_error("no comp pack url");
LOGF("Fetching comppack list from: {}", update_url);
http->start(update_url, 0);
const auto length = http->get_length();
if (length > (int64_t)db_data.size())
throw std::runtime_error(
"comp pack list is too large... check for newer pkgj version");
for (;;)
{
uint32_t want = (uint32_t)min64(64 * 1024, db_data.size() - db_size);
int read = http->read(
reinterpret_cast<uint8_t*>(db_data.data()) + db_size, want);
if (read == 0)
break;
db_size += read;
}
if (db_size == 0)
throw std::runtime_error(
"comp pack list is empty... check for newer pkgj version");
LOG("Parsing comppack list");
db_data.resize(db_size);
parse_entries(db_data);
LOG("Comppack list parsed successfully");
}
std::optional<CompPackDatabase::Item> CompPackDatabase::get(
const std::string& titleid)
{
// we need to reopen the db before every query because for some reason,
// after the app is suspended, all further query will return disk I/O error
reopen();
LOG("Reloading comppack database");
sqlite3_stmt* stmt;
SQLITE_CHECK(
sqlite3_prepare_v2(
_sqliteDb.get(),
"SELECT path, app_version "
"FROM entries "
"WHERE titleid = ? ",
-1,
&stmt,
nullptr),
"can't prepare SQL statement");
BOOST_SCOPE_EXIT_ALL(&)
{
sqlite3_finalize(stmt);
};
sqlite3_bind_text(stmt, 1, titleid.data(), titleid.size(), nullptr);
auto const err = sqlite3_step(stmt);
if (err == SQLITE_DONE)
return std::nullopt;
if (err != SQLITE_ROW)
throw std::runtime_error(fmt::format(
"can't execute SQL statement:\n{}",
sqlite3_errmsg(_sqliteDb.get())));
std::string app_version =
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
// replace _ by .
app_version[2] = '.';
return Item{
reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)),
app_version,
};
}