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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
module;
#include <bzlib.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <cstdint>
#include <cstddef>
#include <string.h>
#include <gio/gio.h>
#include <curl/curl.h>
#include <string>
#include <regex>
#include <vector>
#include <forward_list>
#include <filesystem>
export module katja.slackpkg;
import katja.utils;
import katja.pkgtools;
export namespace katja
{
class Slackpkg final : public Pkgtools
{
public:
/**
* katja::Slackpkg::Slackpkg:
* @name: Repository name.
* @mirror: Repository mirror.
* @order: Repository order.
* @blacklist: Blacklist.
* @priority: Groups priority.
*
* Constructor.
*
* Returns: New #katja::Slackpkg.
**/
Slackpkg(const std::string& name, const std::string& mirror,
std::uint8_t order, const char *blacklist, char **priority) noexcept
: Pkgtools(name, mirror, order)
{
if (blacklist != nullptr)
{
this->blacklist = std::regex(blacklist);
}
if (priority != nullptr)
{
for (char **cur_priority = priority; *cur_priority; cur_priority++)
{
this->priority.emplace_back(*cur_priority);
}
}
}
/**
* katja::Slackpkg::collect_cache_info:
* @tmpl: temporary directory for downloading the files.
*
* Download files needed to get the information like the list of packages
* in available repositories, updates, package descriptions and so on.
*
* Returns: List of files needed for building the cache.
**/
std::forward_list<cache_entry> collect_cache_info(const std::filesystem::path& tmpl) noexcept
{
CURL *curl = nullptr;
cache_entry source_dest;
std::forward_list<cache_entry> file_list;
GFile *tmp_dir, *repo_tmp_dir;
/* Create the temporary directory for the repository */
tmp_dir = g_file_new_for_path(tmpl.native().c_str());
repo_tmp_dir = g_file_get_child(tmp_dir, this->name.c_str());
g_file_make_directory(repo_tmp_dir, nullptr, nullptr);
/* Download PACKAGES.TXT. These files are most important, break if some of them couldn't be found */
for (const std::string& current_priority : this->priority)
{
source_dest.first = this->mirror + current_priority + "/PACKAGES.TXT";
source_dest.second = tmpl / this->name / "PACKAGES.TXT";
if (get_file(&curl, source_dest.first.c_str(), nullptr) == CURLE_OK)
{
file_list.emplace_front(std::move(source_dest));
}
else
{
goto out;
}
/* Download file lists if available */
source_dest.first = this->mirror + current_priority + "/MANIFEST.bz2";
source_dest.second = tmpl / this->name / (current_priority + "-MANIFEST.bz2");
if (get_file(&curl, source_dest.first.c_str(), nullptr) == CURLE_OK)
{
file_list.emplace_front(std::move(source_dest));
}
}
out:
g_object_unref(repo_tmp_dir);
g_object_unref(tmp_dir);
if (curl)
{
curl_easy_cleanup(curl);
}
return file_list;
}
/**
* katja::Slackpkg::generate_cache:
* @job_data: A #JobData.
* @tmpl: temporary directory for downloading the files.
*
* Download files needed to get the information like the list of packages
* in available repositories, updates, package descriptions and so on.
*
* Returns: List of files needed for building the cache.
**/
void generate_cache(JobData *job_data, const std::filesystem::path& tmpl) noexcept
{
std::vector<std::string> pkg_tokens;
char *query = nullptr, *filename = nullptr, *location = nullptr, *summary = nullptr, *line;
unsigned pkg_compressed = 0, pkg_uncompressed = 0;
std::uint8_t pkg_name_len;
GString *desc;
GFile *list_file;
GFileInputStream *fin = nullptr;
GDataInputStream *data_in = nullptr;
sqlite3_stmt *insert_statement = nullptr, *update_statement = nullptr, *insert_default_statement = nullptr;
sqlite3_stmt *statement;
/* Check if the temporary directory for this repository exists, then the file metadata have to be generated */
std::filesystem::path packages_txt = tmpl / this->name / "PACKAGES.TXT";
list_file = g_file_new_for_path(packages_txt.native().c_str());
fin = g_file_read(list_file, nullptr, nullptr);
g_object_unref(list_file);
if (!fin)
{
goto out;
}
/* Remove the old entries from this repository */
if (sqlite3_prepare_v2(job_data->db,
"DELETE FROM repos WHERE repo LIKE @repo",
-1,
&statement,
nullptr) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, this->name.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_finalize(statement);
}
if (sqlite3_prepare_v2(job_data->db,
"INSERT INTO repos (repo_order, repo) VALUES (@repo_order, @repo)",
-1,
&statement,
nullptr) != SQLITE_OK)
{
goto out;
}
sqlite3_bind_int(statement, 1, this->order);
sqlite3_bind_text(statement, 2, this->name.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_finalize(statement);
/* Insert new records */
if ((sqlite3_prepare_v2(job_data->db,
"INSERT OR REPLACE INTO pkglist (full_name, ver, arch, ext, location, "
"summary, desc, compressed, uncompressed, name, repo_order, cat) "
"VALUES (@full_name, @ver, @arch, @ext, @location, @summary, "
"@desc, @compressed, @uncompressed, @name, @repo_order, @cat)",
-1,
&insert_statement,
nullptr) != SQLITE_OK)
|| (sqlite3_prepare_v2(job_data->db,
"INSERT OR REPLACE INTO pkglist (full_name, ver, arch, ext, location, "
"summary, desc, compressed, uncompressed, name, repo_order) "
"VALUES (@full_name, @ver, @arch, @ext, @location, @summary, "
"@desc, @compressed, @uncompressed, @name, @repo_order)",
-1,
&insert_default_statement,
nullptr) != SQLITE_OK))
{
goto out;
}
query = sqlite3_mprintf("UPDATE pkglist SET full_name = @full_name, ver = @ver, arch = @arch, "
"ext = @ext, location = @location, summary = @summary, "
"desc = @desc, compressed = @compressed, uncompressed = @uncompressed "
"WHERE name LIKE @name AND repo_order = %u",
this->order);
if (sqlite3_prepare_v2(job_data->db, query, -1, &update_statement, nullptr) != SQLITE_OK)
{
goto out;
}
data_in = g_data_input_stream_new(G_INPUT_STREAM(fin));
desc = g_string_new("");
sqlite3_exec(job_data->db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
while ((line = g_data_input_stream_read_line(data_in, nullptr, nullptr, nullptr)))
{
if (!strncmp(line, "PACKAGE NAME: ", 15))
{
filename = g_strdup(line + 15);
if (this->is_blacklisted(filename))
{
g_free(filename);
filename = nullptr;
}
}
else if (filename && !strncmp(line, "PACKAGE LOCATION: ", 19))
{
location = g_strdup(line + 21); /* Exclude ./ at the path beginning */
}
else if (filename && !strncmp(line, "PACKAGE SIZE (compressed): ", 28))
{
/* Remove the unit (kilobytes) */
pkg_compressed = atoi(g_strndup(line + 28, strlen(line + 28) - 2)) * 1024;
}
else if (filename && !strncmp(line, "PACKAGE SIZE (uncompressed): ", 30))
{
/* Remove the unit (kilobytes) */
pkg_uncompressed = atoi(g_strndup(line + 30, strlen(line + 30) - 2)) * 1024;
}
else if (filename && !g_strcmp0(line, "PACKAGE DESCRIPTION:"))
{
g_free(line);
line = g_data_input_stream_read_line(data_in, nullptr, nullptr, nullptr); /* Short description */
summary = g_strstr_len(line, -1, "(");
if (summary) /* Else summary = nullptr */
{
summary = g_strndup(summary + 1, strlen(summary) - 2); /* Without ( ) */
}
pkg_tokens = split_package_name(filename).value();
pkg_name_len = pkg_tokens[0].size(); /* Description begins with pkg_name: */
}
else if (filename && !strncmp(line, pkg_tokens[0].c_str(), pkg_name_len))
{
g_string_append(desc, line + pkg_name_len + 1);
}
else if (filename && !g_strcmp0(line, ""))
{
if (g_strcmp0(location, "patches/packages")) /* Insert a new package */
{
/* Get the package group based on its location */
const char *cat = g_strrstr(location, "/");
if (cat)
{
++cat;
statement = insert_statement;
sqlite3_bind_text(insert_statement, 12, cat, -1, SQLITE_TRANSIENT);
}
else
{
statement = insert_default_statement;
}
sqlite3_bind_int(statement, 11, this->order);
}
else /* Update package information if it is a patch */
{
statement = update_statement;
}
sqlite3_bind_text(statement, 1, pkg_tokens[3].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, pkg_tokens[1].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, pkg_tokens[2].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, pkg_tokens[4].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, location, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, summary, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, desc->str, -1, SQLITE_TRANSIENT);
sqlite3_bind_int(statement, 8, pkg_compressed);
sqlite3_bind_int(statement, 9, pkg_uncompressed);
sqlite3_bind_text(statement, 10, pkg_tokens[0].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_clear_bindings(statement);
sqlite3_reset(statement);
/* Reset for the next package */
g_free(filename);
g_free(location);
g_free(summary);
filename = location = summary = nullptr;
g_string_assign(desc, "");
pkg_compressed = pkg_uncompressed = 0;
}
g_free(line);
}
sqlite3_exec(job_data->db, "END TRANSACTION", nullptr, nullptr, nullptr);
g_string_free(desc, true);
g_object_unref(data_in);
/* Parse MANIFEST.bz2 */
for (const std::string& current_priority : this->priority)
{
manifest(job_data, tmpl, current_priority + "-MANIFEST.bz2");
}
out:
sqlite3_finalize(update_statement);
sqlite3_free(query);
sqlite3_finalize(insert_default_statement);
sqlite3_finalize(insert_statement);
if (fin)
{
g_object_unref(fin);
}
}
private:
static const std::size_t max_buf_size = 8192;
std::vector<std::string> priority;
/*
* katja::Slackpkg::manifest:
* @job: a #JobData.
* @tmpl: temporary directory.
* @filename: manifest filename
*
* Parse the manifest file and save the file list in the database.
*/
void manifest(JobData *job_data, const std::filesystem::path& tmpl, const std::string& filename) noexcept
{
FILE *manifest;
int err, read_len;
unsigned pos;
char buf[max_buf_size], *pkg_filename, *rest = nullptr, *start;
std::filesystem::path path;
char *full_name = nullptr;
char **line, **lines;
BZFILE *manifest_bz2;
GRegex *pkg_expr = nullptr, *file_expr = nullptr;
GMatchInfo *match_info;
sqlite3_stmt *statement = nullptr;
path = tmpl / this->name / filename;
manifest = fopen(path.native().c_str(), "rb");
if (!manifest)
{
return;
}
if (!(manifest_bz2 = BZ2_bzReadOpen(&err, manifest, 0, 0, nullptr, 0)))
{
goto out;
}
/* Prepare regular expressions */
pkg_expr = g_regex_new("^\\|\\|[[:blank:]]+Package:[[:blank:]]+.+\\/(.+)\\.(t[blxg]z$)?",
static_cast<GRegexCompileFlags> (G_REGEX_OPTIMIZE | G_REGEX_DUPNAMES),
static_cast<GRegexMatchFlags> (0),
nullptr);
file_expr = g_regex_new("^[-bcdlps][-r][-w][-xsS][-r][-w][-xsS][-r][-w]"
"[-xtT][[:space:]][^[:space:]]+[[:space:]]+"
"[[:digit:]]+[[:space:]][[:digit:]-]+[[:space:]]"
"[[:digit:]:]+[[:space:]](?!install\\/|\\.)(.*)",
static_cast<GRegexCompileFlags> (G_REGEX_OPTIMIZE | G_REGEX_DUPNAMES),
static_cast<GRegexMatchFlags> (0),
nullptr);
if (!(file_expr) || !(pkg_expr))
{
goto out;
}
/* Prepare SQL statements */
if (sqlite3_prepare_v2(job_data->db,
"INSERT INTO filelist (full_name, filename) VALUES (@full_name, @filename)",
-1,
&statement,
nullptr) != SQLITE_OK)
{
goto out;
}
sqlite3_exec(job_data->db, "BEGIN TRANSACTION", nullptr, nullptr, nullptr);
while ((read_len = BZ2_bzRead(&err, manifest_bz2, buf, max_buf_size - 1)))
{
if ((err != BZ_OK) && (err != BZ_STREAM_END))
{
break;
}
buf[read_len] = '\0';
/* Split the read text into lines */
lines = g_strsplit(buf, "\n", 0);
if (rest)
{ /* Add to the first line rest characters from the previous read operation */
start = lines[0];
lines[0] = g_strconcat(rest, lines[0], nullptr);
g_free(start);
g_free(rest);
}
if (err != BZ_STREAM_END) /* The last line can be incomplete */
{
pos = g_strv_length(lines) - 1;
rest = lines[pos];
lines[pos] = nullptr;
}
for (line = lines; *line; line++)
{
if (g_regex_match(pkg_expr, *line, static_cast<GRegexMatchFlags> (0), &match_info))
{
if (g_match_info_get_match_count(match_info) > 2)
{ /* If the extension matches */
g_free(full_name);
full_name = g_match_info_fetch(match_info, 1);
}
else
{
full_name = nullptr;
}
}
g_match_info_free(match_info);
match_info = nullptr;
if (full_name && g_regex_match(file_expr, *line, static_cast<GRegexMatchFlags> (0), &match_info))
{
pkg_filename = g_match_info_fetch(match_info, 1);
sqlite3_bind_text(statement, 1, full_name, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, pkg_filename, -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_clear_bindings(statement);
sqlite3_reset(statement);
g_free(pkg_filename);
}
g_match_info_free(match_info);
}
g_strfreev(lines);
}
sqlite3_exec(job_data->db, "END TRANSACTION", nullptr, nullptr, nullptr);
g_free(full_name);
BZ2_bzReadClose(&err, manifest_bz2);
out:
sqlite3_finalize(statement);
if (file_expr)
{
g_regex_unref(file_expr);
}
if (pkg_expr)
{
g_regex_unref(pkg_expr);
}
fclose(manifest);
}
};
}
|