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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
/*
* 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 "config.h"
#include <filesystem>
#include <gio/gio.h>
#include <cstdint>
#include <stdlib.h>
#include <iostream>
#include <zlib.h>
#include <curl/curl.h>
#include <glib/gstdio.h>
#include <sqlite3.h>
#include <forward_list>
#include <array>
#include <string>
#include <list>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <regex>
export module katja.job;
import katja.utils;
import katja.pkgtools;
import katja.slackpkg;
namespace katja
{
static std::forward_list<std::unique_ptr<Pkgtools>> repos;
void pk_backend_initialize(GKeyFile *conf)
{
char **groups;
int ret;
std::uint8_t i;
std::size_t groups_len;
GKeyFile *key_conf;
GError *err = nullptr;
sqlite3 *db;
sqlite3_stmt *stmt;
curl_global_init(CURL_GLOBAL_DEFAULT);
/* Open the database. We will need it to save the time the configuration file was last modified. */
auto path = std::filesystem::path(LOCALSTATEDIR) / "cache" / "katja" / "metadata.db";
if (sqlite3_open(path.c_str(), &db) != SQLITE_OK)
{
std::cerr << path.native() << ": " << sqlite3_errmsg(db) << std::endl;
}
/* Read the configuration file */
key_conf = g_key_file_new();
path = std::filesystem::path(SYSCONFDIR) / "PackageKit" / "Slackware.conf";
g_key_file_load_from_file(key_conf, path.c_str(), G_KEY_FILE_NONE, &err);
if (err)
{
std::cerr << path.native() << ": " << err->message << std::endl;
g_error_free(err);
}
std::chrono::time_point<std::chrono::file_clock> file_info = std::filesystem::last_write_time(path);
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(file_info.time_since_epoch());
if ((ret = sqlite3_prepare_v2(db,
"UPDATE cache_info SET value = ? WHERE key LIKE 'last_modification'",
-1,
&stmt,
nullptr)) == SQLITE_OK) {
ret = sqlite3_bind_int(stmt, 1, microseconds.count());
if (ret == SQLITE_OK)
{
ret = sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
}
if ((ret != SQLITE_OK) && (ret != SQLITE_DONE))
{
std::cerr << path.native() << ": " << sqlite3_errstr(ret) << std::endl;
}
else if (!sqlite3_changes(db))
{
std::cerr << "Failed to update database: " << path.native() << std::endl;
}
sqlite3_close_v2(db);
/* Initialize an object for each well-formed repository */
groups = g_key_file_get_groups(key_conf, &groups_len);
for (i = 0; i < groups_len; i++)
{
char *blacklist = g_key_file_get_string(key_conf, groups[i], "Blacklist", nullptr);
char *mirror = g_key_file_get_string(key_conf, groups[i], "Mirror", nullptr);
if (g_key_file_has_key(key_conf, groups[i], "Priority", nullptr))
{
auto repo = std::make_unique<Slackpkg>(groups[i], mirror, i + 1, blacklist,
g_key_file_get_string_list(key_conf, groups[i], "Priority", nullptr, nullptr));
repos.emplace_front(std::move(repo));
}
g_free(mirror);
g_free(blacklist);
}
g_free(groups);
g_key_file_free(key_conf);
}
void pk_backend_destroy()
{
repos.clear();
curl_global_cleanup();
}
JobData *pk_backend_start_job()
{
JobData *job_data = new DummyJobData();
auto db_filename = std::filesystem::path(LOCALSTATEDIR) / "cache" / "katja" / "metadata.db";
if (sqlite3_open(db_filename.native().c_str(), &job_data->db) == SQLITE_OK) { /* Some SQLite settings */
sqlite3_exec(job_data->db, "PRAGMA foreign_keys = ON", nullptr, nullptr, nullptr);
}
else
{
std::cerr << db_filename << ": " << sqlite3_errmsg(job_data->db) << std::endl;
}
return job_data;
}
void pk_backend_stop_job(JobData *job_data)
{
if (job_data->curl)
{
curl_easy_cleanup(job_data->curl);
}
sqlite3_close(job_data->db);
delete job_data;
}
// for filters -1 means requesting not installed, 1 - installed, 0 - no filters.
void pk_backend_search_thread(JobData *job_data, std::uint64_t filters, const std::vector<std::string>& vals,
const char *user_data)
{
std::string search = boost::algorithm::join(vals, "%");
const char *generate_query = "SELECT (p1.name || ';' || p1.ver || ';' || p1.arch || ';' || r.repo), p1.summary, "
"p1.full_name FROM pkglist AS p1 NATURAL JOIN repos AS r "
"WHERE p1.%s LIKE '%%%q%%' AND p1.ext NOT LIKE 'obsolete' AND p1.repo_order = "
"(SELECT MIN(p2.repo_order) FROM pkglist AS p2 WHERE p2.name = p1.name GROUP BY p2.name)";
char *query = sqlite3_mprintf(generate_query, user_data, search.c_str());
sqlite3_stmt *stmt;
if ((sqlite3_prepare_v2(job_data->db, query, -1, &stmt, nullptr) == SQLITE_OK))
{
/* Now we're ready to output all packages */
while (sqlite3_step(stmt) == SQLITE_ROW)
{
katja::Info info = katja::is_installed(
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)));
if ((info == katja::Info::installed || info == katja::Info::updating)
&& filters != -1)
{
job_data->package(katja::Info::installed,
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)));
}
else if (info == katja::Info::installing && filters != 1)
{
job_data->package(katja::Info::available,
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)),
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)));
}
}
sqlite3_finalize(stmt);
}
else
{
std::cerr << sqlite3_errmsg(job_data->db) << std::endl;
}
sqlite3_free(query);
}
void pk_backend_search_files(JobData *job_data, const std::vector<std::string>& values)
{
char *query;
sqlite3_stmt *stmt;
Info ret;
std::string search = boost::algorithm::join(values, "%");
query = sqlite3_mprintf("SELECT (p.name || ';' || p.ver || ';' || p.arch || ';' || r.repo), p.summary, "
"p.full_name FROM filelist AS f NATURAL JOIN pkglist AS p NATURAL JOIN repos AS r "
"WHERE f.filename LIKE '%%%q%%' GROUP BY f.full_name", search.c_str());
if ((sqlite3_prepare_v2(job_data->db, query, -1, &stmt, nullptr) == SQLITE_OK))
{
/* Now we're ready to output all packages */
while (sqlite3_step(stmt) == SQLITE_ROW)
{
ret = is_installed((char*) sqlite3_column_text(stmt, 2));
if ((ret == Info::installed) || (ret == Info::updating))
{
job_data->package(katja::Info::installed,
(char*) sqlite3_column_text(stmt, 0),
(char*) sqlite3_column_text(stmt, 1));
}
else if (ret == katja::Info::installing)
{
job_data->package(katja::Info::available,
(char*) sqlite3_column_text(stmt, 0),
(char*) sqlite3_column_text(stmt, 1));
}
}
sqlite3_finalize(stmt);
}
else
{
std::cerr << sqlite3_errmsg(job_data->db) << std::endl;
}
sqlite3_free(query);
}
void pk_backend_get_details(JobData *job_data, char **package_ids)
{
std::optional<std::string> homepage;
std::size_t i;
std::regex expr;
std::smatch match_info;
GError *err = nullptr;
sqlite3_stmt *stmt;
std::string desc;
std::vector<std::string> tokens;
boost::split(tokens, package_ids[i], boost::is_any_of(";"));
if ((sqlite3_prepare_v2(job_data->db,
"SELECT p.desc, p.cat, p.uncompressed FROM pkglist AS p NATURAL JOIN repos AS r "
"WHERE name LIKE @name AND r.repo LIKE @repo",
-1,
&stmt,
nullptr) != SQLITE_OK)) {
std::cerr << sqlite3_errmsg(job_data->db) << std::endl;
goto out;
}
sqlite3_bind_text(stmt, 1, tokens[0].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, tokens[3].c_str(), -1, SQLITE_TRANSIENT);
if (sqlite3_step(stmt) != SQLITE_ROW)
{
goto out;
}
desc = reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0));
/* Regular expression for searching a homepage */
expr = std::regex("(?:http|ftp):\\/\\/[[:word:]\\/\\-\\.]+[[:word:]\\/](?=\\.?$)",
std::regex::optimize);
if (std::regex_match(std::cbegin(desc), std::cend(desc), match_info, expr))
{
homepage = match_info[0]; /* URL */
/* Remove the last sentence with the copied URL */
for (i = desc.size() - 1; i > 0; i--)
{
if ((desc[i - 1] == '.') && (desc[i] == ' '))
{
desc.erase(std::cbegin(desc) + i);
break;
}
}
}
/* Ready */
job_data->details(package_ids[0],
(char *) sqlite3_column_text(stmt, 1),
desc.c_str(),
homepage.has_value() ? homepage.value().c_str() : nullptr,
sqlite3_column_int(stmt, 2));
out:
sqlite3_finalize(stmt);
}
void pk_backend_resolve(JobData *job_data, char **packages)
{
char **val;
sqlite3_stmt *stmt;
if ((sqlite3_prepare_v2(job_data->db,
"SELECT (p1.name || ';' || p1.ver || ';' || p1.arch || ';' || r.repo), p1.summary, "
"p1.full_name FROM pkglist AS p1 NATURAL JOIN repos AS r "
"WHERE p1.name LIKE @search AND p1.repo_order = "
"(SELECT MIN(p2.repo_order) FROM pkglist AS p2 WHERE p2.name = p1.name GROUP BY p2.name)",
-1,
&stmt,
nullptr) == SQLITE_OK)) {
/* Output packages matching each pattern */
for (val = packages; *val; val++)
{
sqlite3_bind_text(stmt, 1, *val, -1, SQLITE_TRANSIENT);
while (sqlite3_step(stmt) == SQLITE_ROW)
{
job_data->package(katja::Info::available,
(char *) sqlite3_column_text(stmt, 0),
(char *) sqlite3_column_text(stmt, 1));
}
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
}
else
{
std::cerr << sqlite3_errmsg(job_data->db) << std::endl;
}
}
void pk_backend_download_packages(JobData *job_data, char **package_ids, const char *directory)
{
unsigned i;
sqlite3_stmt *stmt;
if ((sqlite3_prepare_v2(job_data->db,
"SELECT summary, (full_name || '.' || ext) FROM pkglist NATURAL JOIN repos "
"WHERE name LIKE @name AND ver LIKE @ver AND arch LIKE @arch AND repo LIKE @repo",
-1,
&stmt,
nullptr) != SQLITE_OK))
{
std::cerr << sqlite3_errmsg(job_data->db) << std::endl;
goto out;
}
for (i = 0; package_ids[i]; ++i)
{
std::vector<std::string> tokens;
boost::split(tokens, package_ids[i], boost::is_any_of(";"));
sqlite3_bind_text(stmt, 1, tokens[0].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, tokens[1].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, tokens[2].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, tokens[3].c_str(), -1, SQLITE_TRANSIENT);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
auto repo = cmp_repo(std::begin(repos), std::end(repos), tokens[3].c_str());
if (repo != std::end(repos))
{
(*repo)->download(job_data, directory, tokens[0].c_str());
auto path = std::filesystem::path(directory)
/ reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1));
std::vector<std::string> to_strv{ path };
job_data->files(to_strv);
}
}
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
}
out:
sqlite3_finalize(stmt);
}
void pk_backend_install_packages(JobData *job_data, const std::vector<std::string> package_ids)
{
unsigned i;
gdouble percent_step;
std::list<std::string> install_list;
sqlite3_stmt *pkglist_stmt = nullptr, *collection_stmt = nullptr;
if ((sqlite3_prepare_v2(job_data->db,
"SELECT summary, cat FROM pkglist NATURAL JOIN repos "
"WHERE name LIKE @name AND ver LIKE @ver AND arch LIKE @arch AND repo LIKE @repo",
-1,
&pkglist_stmt,
nullptr) != SQLITE_OK) ||
(sqlite3_prepare_v2(job_data->db,
"SELECT (c.collection_pkg || ';' || p.ver || ';' || p.arch || ';' || r.repo), p.summary, "
"p.full_name, p.ext FROM collections AS c "
"JOIN pkglist AS p ON c.collection_pkg = p.name "
"JOIN repos AS r ON p.repo_order = r.repo_order "
"WHERE c.name LIKE @name AND r.repo LIKE @repo",
-1,
&collection_stmt,
nullptr) != SQLITE_OK))
{
std::cerr << sqlite3_errmsg(job_data->db) << std::endl;
goto out;
}
for (const std::string& package_id : package_ids)
{
std::vector<std::string> tokens;
boost::split(tokens, package_id, boost::is_any_of(";"));
sqlite3_bind_text(pkglist_stmt, 1, tokens[0].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(pkglist_stmt, 2, tokens[1].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(pkglist_stmt, 3, tokens[2].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(pkglist_stmt, 4, tokens[3].c_str(), -1, SQLITE_TRANSIENT);
if (sqlite3_step(pkglist_stmt) == SQLITE_ROW)
{
/* If it isn't a collection */
if (strcmp(reinterpret_cast<const char *>(sqlite3_column_text(pkglist_stmt, 1)), "collections"))
{
install_list.push_back(package_id);
}
else
{
sqlite3_bind_text(collection_stmt, 1, tokens[0].c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(collection_stmt, 2, tokens[3].c_str(), -1, SQLITE_TRANSIENT);
while (sqlite3_step(collection_stmt) == SQLITE_ROW)
{
katja::Info ret = is_installed((char*) sqlite3_column_text(collection_stmt, 2));
if ((ret == Info::installing) || (ret == Info::updating))
{
install_list.push_back(
reinterpret_cast<const char *>(sqlite3_column_text(collection_stmt, 0)));
}
}
sqlite3_clear_bindings(collection_stmt);
sqlite3_reset(collection_stmt);
}
}
sqlite3_clear_bindings(pkglist_stmt);
sqlite3_reset(pkglist_stmt);
}
if (!install_list.empty())
{
/* / 2 means total percentage for installing and for downloading */
percent_step = 100.0 / install_list.size() / 2;
/* Download the packages */
auto dest_dir_name = std::filesystem::path(LOCALSTATEDIR) / "cache" / "katja" / "downloads";
for (const auto& l : install_list)
{
job_data->set_percentage(percent_step * i);
std::vector<std::string> tokens;
boost::split(tokens, l, boost::is_any_of(";"));
auto repo = cmp_repo(std::begin(repos), std::end(repos), tokens[3].c_str());
if (repo != std::end(repos))
{
(*repo)->download(job_data, dest_dir_name.native().c_str(), tokens[0].c_str());
}
}
/* Install the packages */
for (const auto& l : install_list)
{
job_data->set_percentage(percent_step * i);
std::vector<std::string> tokens;
boost::split(tokens, l, boost::is_any_of(";"));
auto repo = cmp_repo(std::begin(repos), std::end(repos), tokens[3].c_str());
if (repo != std::end(repos))
{
(*repo)->install(job_data, tokens[0].c_str());
}
}
}
out:
sqlite3_finalize(pkglist_stmt);
sqlite3_finalize(collection_stmt);
}
void pk_backend_remove_packages(JobData* job_data, const std::vector<std::string>& package_ids)
{
unsigned i;
double percent_step;
GError *err = nullptr;
/* Add percent_step percents per removed package */
percent_step = 100.0 / package_ids.size();
for (i = 0; i < package_ids[i].size(); i++)
{
job_data->set_percentage(percent_step * i);
std::vector<std::string> tokens;
boost::split(tokens, package_ids[i], boost::is_any_of(";"));
std::string cmd_line = "/sbin/removepkg " + tokens[0];
/* Pkgtools return always 0 */
g_spawn_command_line_sync(cmd_line.c_str(), nullptr, nullptr, nullptr, &err);
if (err)
{
std::cerr << err->message << std::endl;
g_error_free(err);
return;
}
job_data->set_percentage(100);
}
}
void pk_backend_get_updates(JobData *job_data)
{
std::filesystem::path pkg_metadata_filename;
std::filesystem::path pkg_metadata_dir;
sqlite3_stmt *stmt;
if ((sqlite3_prepare_v2(job_data->db,
"SELECT p1.full_name, p1.name, p1.ver, p1.arch, r.repo, p1.summary, p1.ext "
"FROM pkglist AS p1 NATURAL JOIN repos AS r "
"WHERE p1.name LIKE @name AND p1.repo_order = "
"(SELECT MIN(p2.repo_order) FROM pkglist AS p2 WHERE p2.name = p1.name GROUP BY p2.name)",
-1,
&stmt,
nullptr) != SQLITE_OK))
{
std::cerr << sqlite3_errmsg(job_data->db) << std::endl;
goto out;
}
/* Read the package metadata directory and comprare all installed packages with ones in the cache */
pkg_metadata_dir = std::filesystem::path("/var/log/packages");
for (const auto& pkg_metadata_file_info : std::filesystem::directory_iterator{ pkg_metadata_dir })
{
pkg_metadata_filename = pkg_metadata_file_info.path();
std::vector<std::string> tokens = split_package_name(pkg_metadata_filename).value();
/* Select the package from the database */
sqlite3_bind_text(stmt, 1, tokens[0].c_str(), -1, SQLITE_TRANSIENT);
/* If there are more packages with the same name, remember the one from the
* repository with the lowest order. */
if ((sqlite3_step(stmt) == SQLITE_ROW)
|| cmp_repo(std::begin(repos), std::end(repos), (const char *) sqlite3_column_text(stmt, 4)) != std::end(repos))
{
std::string full_name{ reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)) };
if (full_name != pkg_metadata_filename)
{ /* Update available */
std::array<std::string, 4> id_parts{
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)),
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 2)),
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 3)),
reinterpret_cast<const char *>(sqlite3_column_text(stmt, 4))
};
std::string pkg_id = boost::algorithm::join(id_parts, ";");
std::string desc{ reinterpret_cast<const char *>(sqlite3_column_text(stmt, 5)) };
job_data->package(katja::Info::updating, pkg_id.c_str(), desc.c_str());
}
}
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
}
out:
sqlite3_finalize(stmt);
}
void pk_backend_update_packages(JobData *job_data, char **package_ids)
{
char *cmd_line;
unsigned i;
/* Download the packages */
auto dest_dir_name = std::filesystem::path(LOCALSTATEDIR) / "cache" / "katja" / "downloads";
for (i = 0; package_ids[i]; i++)
{
std::vector<std::string> tokens;
boost::split(tokens, package_ids[i], boost::is_any_of(";"));
auto repo = cmp_repo(std::begin(repos), std::end(repos), tokens[3].c_str());
if (repo != std::end(repos))
{
(*repo)->download(job_data, dest_dir_name.c_str(), tokens[0].c_str());
}
}
/* Install the packages */
for (i = 0; package_ids[i]; i++)
{
std::vector<std::string> tokens;
boost::split(tokens, package_ids[i], boost::is_any_of(";"));
auto repo = cmp_repo(std::begin(repos), std::end(repos), tokens[3].c_str());
if (repo != std::end(repos))
{
(*repo)->install(job_data, tokens[0].c_str());
}
}
}
void pk_backend_refresh_cache(JobData *job_data, bool force)
{
char *db_err;
int ret;
std::forward_list<katja::cache_entry> file_list;
sqlite3_stmt *stmt = nullptr;
/* Create temporary directory */
std::filesystem::path tmp_dir_name =
std::filesystem::temp_directory_path() / boost::filesystem::unique_path("katja.%%%%%%").native();
std::filesystem::create_directories(tmp_dir_name);
/* Force the complete cache refresh if the read configuration file is newer than the metadata cache */
if (!force)
{
auto path = std::filesystem::path(LOCALSTATEDIR) / "cache" / "katja" / "metadata.db";
std::chrono::time_point<std::chrono::file_clock> file_info = std::filesystem::last_write_time(path);
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(file_info.time_since_epoch());
ret = sqlite3_prepare_v2(job_data->db,
"SELECT value FROM cache_info WHERE key LIKE 'last_modification'",
-1,
&stmt,
nullptr);
if ((ret != SQLITE_OK) || ((ret = sqlite3_step(stmt)) != SQLITE_ROW))
{
std::cerr << path << ": " << sqlite3_errstr(ret) << std::endl;
goto out;
}
if ((std::uint32_t) sqlite3_column_int(stmt, 0) > microseconds.count())
{
force = true;
}
}
if (force) /* It should empty all tables */
{
if (sqlite3_exec(job_data->db, "DELETE FROM repos", nullptr, 0, &db_err) != SQLITE_OK)
{
std::cerr << db_err << std::endl;
sqlite3_free(db_err);
goto out;
}
}
// Get list of files that should be downloaded.
for (auto& l : repos)
{
file_list.prepend_range(l->collect_cache_info(tmp_dir_name));
}
/* Download repository */
for (auto& l : file_list)
{
get_file(&job_data->curl, l.first.c_str(), l.second);
}
/* Refresh cache */
for (auto& l : repos)
{
l->generate_cache(job_data, tmp_dir_name);
}
out:
sqlite3_finalize(stmt);
std::filesystem::remove_all(tmp_dir_name);
}
}
|