summaryrefslogtreecommitdiff
path: root/backend/job.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'backend/job.cpp')
-rw-r--r--backend/job.cpp107
1 files changed, 24 insertions, 83 deletions
diff --git a/backend/job.cpp b/backend/job.cpp
index 68eda1a..f95b33a 100644
--- a/backend/job.cpp
+++ b/backend/job.cpp
@@ -21,6 +21,7 @@ module;
#include <list>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
+#include <regex>
export module katja.job;
@@ -38,21 +39,18 @@ void pk_backend_initialize(GKeyFile *conf)
int ret;
std::uint8_t i;
std::size_t groups_len;
- GFile *conf_file;
- GFileInfo *file_info;
GKeyFile *key_conf;
GError *err = nullptr;
sqlite3 *db;
sqlite3_stmt *stmt;
- g_debug("backend: initialize");
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)
{
- g_error("%s: %s", path.c_str(), sqlite3_errmsg(db));
+ std::cerr << path.native() << ": " << sqlite3_errmsg(db) << std::endl;
}
/* Read the configuration file */
@@ -61,27 +59,19 @@ void pk_backend_initialize(GKeyFile *conf)
g_key_file_load_from_file(key_conf, path.c_str(), G_KEY_FILE_NONE, &err);
if (err)
{
- g_error("%s: %s", path.c_str(), err->message);
+ std::cerr << path.native() << ": " << err->message << std::endl;
g_error_free(err);
}
- conf_file = g_file_new_for_path(path.c_str());
- if (!(file_info = g_file_query_info(conf_file,
- "time::modified-usec",
- G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
- nullptr,
- &err)))
- {
- g_error("%s", err->message);
- 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, g_file_info_get_attribute_uint32(file_info, "time::modified-usec"));
+ ret = sqlite3_bind_int(stmt, 1, microseconds.count());
if (ret == SQLITE_OK)
{
ret = sqlite3_step(stmt);
@@ -90,15 +80,12 @@ void pk_backend_initialize(GKeyFile *conf)
}
if ((ret != SQLITE_OK) && (ret != SQLITE_DONE))
{
- g_error("%s: %s", path.c_str(), sqlite3_errstr(ret));
+ std::cerr << path.native() << ": " << sqlite3_errstr(ret) << std::endl;
}
else if (!sqlite3_changes(db))
{
- g_error("Failed to update database: %s", path.c_str());
+ std::cerr << "Failed to update database: " << path.native() << std::endl;
}
-
- g_object_unref(file_info);
- g_object_unref(conf_file);
sqlite3_close_v2(db);
/* Initialize an object for each well-formed repository */
@@ -124,8 +111,6 @@ void pk_backend_initialize(GKeyFile *conf)
void pk_backend_destroy()
{
- g_debug("backend: destroy");
-
repos.clear();
curl_global_cleanup();
}
@@ -243,8 +228,8 @@ void pk_backend_get_details(JobData *job_data, char **package_ids)
{
std::optional<std::string> homepage;
std::size_t i;
- GRegex *expr;
- GMatchInfo *match_info;
+ std::regex expr;
+ std::smatch match_info;
GError *err = nullptr;
sqlite3_stmt *stmt;
std::string desc;
@@ -271,19 +256,12 @@ void pk_backend_get_details(JobData *job_data, char **package_ids)
desc = reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0));
/* Regular expression for searching a homepage */
- expr = g_regex_new("(?:http|ftp):\\/\\/[[:word:]\\/\\-\\.]+[[:word:]\\/](?=\\.?$)",
- (GRegexCompileFlags)(G_REGEX_OPTIMIZE | G_REGEX_DUPNAMES),
- (GRegexMatchFlags)(0),
- &err);
- if (err)
- {
- std::cerr << err->message << std::endl;
- g_error_free(err);
- goto out;
- }
- if (g_regex_match(expr, desc.c_str(), (GRegexMatchFlags) 0, &match_info))
+ expr = std::regex("(?:http|ftp):\\/\\/[[:word:]\\/\\-\\.]+[[:word:]\\/](?=\\.?$)",
+ std::regex::optimize);
+
+ if (std::regex_match(std::cbegin(desc), std::cend(desc), match_info, expr))
{
- homepage = g_match_info_fetch(match_info, 0); /* URL */
+ homepage = match_info[0]; /* URL */
/* Remove the last sentence with the copied URL */
for (i = desc.size() - 1; i > 0; i--)
{
@@ -293,9 +271,7 @@ void pk_backend_get_details(JobData *job_data, char **package_ids)
break;
}
}
- g_match_info_free(match_info);
}
- g_regex_unref(expr);
/* Ready */
job_data->details(package_ids[0],
@@ -531,11 +507,8 @@ void pk_backend_remove_packages(JobData* job_data, const std::vector<std::string
void pk_backend_get_updates(JobData *job_data)
{
- const char *pkg_metadata_filename;
- GFile *pkg_metadata_dir;
- GFileEnumerator *pkg_metadata_enumerator;
- GFileInfo *pkg_metadata_file_info;
- GError *err = nullptr;
+ std::filesystem::path pkg_metadata_filename;
+ std::filesystem::path pkg_metadata_dir;
sqlite3_stmt *stmt;
if ((sqlite3_prepare_v2(job_data->db,
@@ -552,22 +525,10 @@ void pk_backend_get_updates(JobData *job_data)
}
/* Read the package metadata directory and comprare all installed packages with ones in the cache */
- pkg_metadata_dir = g_file_new_for_path("/var/log/packages");
- pkg_metadata_enumerator = g_file_enumerate_children(pkg_metadata_dir, "standard::name",
- G_FILE_QUERY_INFO_NONE,
- nullptr,
- &err);
- g_object_unref(pkg_metadata_dir);
- if (err)
+ pkg_metadata_dir = std::filesystem::path("/var/log/packages");
+ for (const auto& pkg_metadata_file_info : std::filesystem::directory_iterator{ pkg_metadata_dir })
{
- std::cerr << "/var/log/packages: " << err->message << std::endl;
- g_error_free(err);
- goto out;
- }
-
- while ((pkg_metadata_file_info = g_file_enumerator_next_file(pkg_metadata_enumerator, nullptr, nullptr)))
- {
- pkg_metadata_filename = g_file_info_get_name(pkg_metadata_file_info);
+ 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 */
@@ -597,10 +558,7 @@ void pk_backend_get_updates(JobData *job_data)
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
-
- g_object_unref(pkg_metadata_file_info);
}
- g_object_unref(pkg_metadata_enumerator);
out:
sqlite3_finalize(stmt);
@@ -644,9 +602,6 @@ void pk_backend_refresh_cache(JobData *job_data, bool force)
char *db_err;
int ret;
std::forward_list<katja::cache_entry> file_list;
- GFile *db_file = nullptr;
- GFileInfo *file_info = nullptr;
- GError *err = nullptr;
sqlite3_stmt *stmt = nullptr;
/* Create temporary directory */
@@ -658,14 +613,8 @@ void pk_backend_refresh_cache(JobData *job_data, bool force)
if (!force)
{
auto path = std::filesystem::path(LOCALSTATEDIR) / "cache" / "katja" / "metadata.db";
- db_file = g_file_new_for_path(path.native().c_str());
- file_info = g_file_query_info(db_file, "time::modified-usec", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, nullptr, &err);
- if (err)
- {
- std::cerr << path << ": " << err->message;
- g_error_free(err);
- goto out;
- }
+ 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,
@@ -676,7 +625,7 @@ void pk_backend_refresh_cache(JobData *job_data, bool force)
std::cerr << path << ": " << sqlite3_errstr(ret) << std::endl;
goto out;
}
- if ((std::uint32_t) sqlite3_column_int(stmt, 0) > g_file_info_get_attribute_uint32(file_info, "time::modified-usec"))
+ if ((std::uint32_t) sqlite3_column_int(stmt, 0) > microseconds.count())
{
force = true;
}
@@ -700,7 +649,7 @@ void pk_backend_refresh_cache(JobData *job_data, bool force)
/* Download repository */
for (auto& l : file_list)
{
- get_file(&job_data->curl, l.first.c_str(), l.second.native().c_str());
+ get_file(&job_data->curl, l.first.c_str(), l.second);
}
/* Refresh cache */
@@ -711,14 +660,6 @@ void pk_backend_refresh_cache(JobData *job_data, bool force)
out:
sqlite3_finalize(stmt);
- if (file_info)
- {
- g_object_unref(file_info);
- }
- if (db_file)
- {
- g_object_unref(db_file);
- }
std::filesystem::remove_all(tmp_dir_name);
}
}