summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-05-12 16:32:40 +0200
committerEugen Wissner <belka@caraus.de>2026-05-12 16:32:40 +0200
commitc04278ff84c2c4d9c54bbd43c1542e4eace40419 (patch)
tree5d24863c391b2c34b187f2e101faab2d4fd46bb5
parent8dd9efa88da99f602a6848731a115f10ec3a5b31 (diff)
downloadkatja-c04278ff84c2c4d9c54bbd43c1542e4eace40419.tar.gz
Migrate Pkgtools char pointer fields to strings
-rw-r--r--backend/CMakeLists.txt13
-rw-r--r--backend/config.h.in5
-rw-r--r--backend/job.cpp12
-rw-r--r--backend/pkgtools.cpp68
-rw-r--r--backend/slackpkg.cpp89
-rw-r--r--backend/tests/definitions.cc98
-rw-r--r--backend/tests/dl-test.cc30
-rw-r--r--backend/tests/slackpkg-test.cc29
8 files changed, 81 insertions, 263 deletions
diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt
index 259a3c6..c1153a5 100644
--- a/backend/CMakeLists.txt
+++ b/backend/CMakeLists.txt
@@ -17,15 +17,4 @@ include_directories(${CMAKE_BINARY_DIR}/generated/)
target_link_libraries(backend PkgConfig::deps CURL::libcurl)
-# configure_file(
-# input: 'Slackware.conf.in',
-# output: 'Slackware.conf',
-# configuration: slackware_config_data,
-# install: true,
-# install_dir: join_paths(get_option('sysconfdir'), 'PackageKit'),
-# )
-
-# install_data(
-# 'metadata.db',
-# install_dir: join_paths(get_option('localstatedir'), 'cache', 'PackageKit', 'metadata'),
-# )
+file(COPY metadata.db DESTINATION ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LOCALSTATEDIR})
diff --git a/backend/config.h.in b/backend/config.h.in
index 2f6da9a..07c3a60 100644
--- a/backend/config.h.in
+++ b/backend/config.h.in
@@ -1,3 +1,8 @@
+/*
+ * 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/.
+ */
#ifndef CONFIG_H
#define CONFIG_H
diff --git a/backend/job.cpp b/backend/job.cpp
index 669e02a..9aaabc7 100644
--- a/backend/job.cpp
+++ b/backend/job.cpp
@@ -44,7 +44,7 @@ void pk_backend_initialize(GKeyFile *conf)
curl_global_init(CURL_GLOBAL_DEFAULT);
/* Open the database. We will need it to save the time the configuration file was last modified. */
- path = g_build_filename(LOCALSTATEDIR, "cache", "PackageKit", "metadata", "metadata.db", nullptr);
+ path = g_build_filename(LOCALSTATEDIR, "cache", "katja", "metadata.db", nullptr);
if (sqlite3_open(path, &db) != SQLITE_OK)
{
g_error("%s: %s", path, sqlite3_errmsg(db));
@@ -137,7 +137,7 @@ JobData *pk_backend_start_job()
char *db_filename = nullptr;
JobData *job_data = g_new0(JobData, 1);
- db_filename = g_build_filename(LOCALSTATEDIR, "cache", "PackageKit", "metadata", "metadata.db", nullptr);
+ db_filename = g_build_filename(LOCALSTATEDIR, "cache", "katja", "metadata.db", nullptr);
if (sqlite3_open(db_filename, &job_data->db) == SQLITE_OK) { /* Some SQLite settings */
sqlite3_exec(job_data->db, "PRAGMA foreign_keys = ON", nullptr, nullptr, nullptr);
}
@@ -484,7 +484,7 @@ void pk_backend_install_packages(JobData *job_data, char **package_ids)
percent_step = 100.0 / g_slist_length(install_list) / 2;
/* Download the packages */
- dest_dir_name = g_build_filename(LOCALSTATEDIR, "cache", "PackageKit", "downloads", nullptr);
+ dest_dir_name = g_build_filename(LOCALSTATEDIR, "cache", "katja", "downloads", nullptr);
for (l = install_list, i = 0; l; l = g_slist_next(l), i++)
{
char **tokens;
@@ -652,7 +652,7 @@ void pk_backend_update_packages(JobData *job_data, char **package_ids)
unsigned i;
/* Download the packages */
- dest_dir_name = g_build_filename(LOCALSTATEDIR, "cache", "PackageKit", "downloads", nullptr);
+ dest_dir_name = g_build_filename(LOCALSTATEDIR, "cache", "katja", "downloads", nullptr);
for (i = 0; package_ids[i]; i++)
{
char **tokens = g_strsplit(package_ids[i], ";", 4);
@@ -691,7 +691,7 @@ void pk_backend_refresh_cache(JobData *job_data, bool force)
sqlite3_stmt *stmt = nullptr;
/* Create temporary directory */
- tmp_dir_name = g_dir_make_tmp("PackageKit.XXXXXX", &err);
+ tmp_dir_name = g_dir_make_tmp("katja.XXXXXX", &err);
if (!tmp_dir_name)
{
std::cerr << err->message << std::endl;
@@ -702,7 +702,7 @@ void pk_backend_refresh_cache(JobData *job_data, bool force)
/* Force the complete cache refresh if the read configuration file is newer than the metadata cache */
if (!force)
{
- path = g_build_filename(LOCALSTATEDIR, "cache", "PackageKit", "metadata", "metadata.db", nullptr);
+ path = g_build_filename(LOCALSTATEDIR, "cache", "katja", "metadata.db", nullptr);
db_file = g_file_new_for_path(path);
file_info = g_file_query_info(db_file, "time::modified-usec", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, nullptr, &err);
if (err)
diff --git a/backend/pkgtools.cpp b/backend/pkgtools.cpp
index bb04a10..58a752f 100644
--- a/backend/pkgtools.cpp
+++ b/backend/pkgtools.cpp
@@ -10,6 +10,8 @@ module;
#include <sqlite3.h>
#include <cstdint>
#include <glib-object.h>
+#include <string>
+#include <regex>
export module katja.pkgtools;
@@ -21,39 +23,29 @@ class Pkgtools
{
public:
/**
- * katja::Pkgtools::get_name:
- *
- * Retrieves the repository name.
- *
- * Returns: Repository name.
- **/
- const char *get_name() const noexcept
- {
- return this->name;
- }
+ * Repository name.
+ */
+ const std::string name;
/**
- * katja::Pkgtools::get_mirror:
- *
- * Retrieves the repository mirror.
- *
- * Returns: Repository mirror.
- **/
- const char *get_mirror() const noexcept
- {
- return this->mirror;
- }
+ * Repository mirror.
+ */
+ const std::string mirror;
/**
- * katja::Pkgtools::get_order:
- *
- * Retrieves the repository order.
- *
- * Returns: Repository order.
- **/
- std::uint8_t get_order() const noexcept
+ * Repository order.
+ */
+ const std::uint8_t order;
+
+ /**
+ * katja::Pkgtools::Pkgtools:
+ * @name: Repository name.
+ * @mirror: Repository mirror.
+ * @order: Repository order.
+ */
+ Pkgtools(const std::string& name, const std::string& mirror, std::uint8_t order)
+ : name(name), mirror(mirror), order(order)
{
- return this->order;
}
/**
@@ -64,10 +56,9 @@ public:
*
* Returns: %TRUE if the package is blacklisted, %FALSE otherwise.
**/
- bool is_blacklisted(const char *pkg) const noexcept
+ bool is_blacklisted(const std::string_view& pkg) const noexcept
{
- return this->blacklist
- && g_regex_match (this->blacklist, pkg, static_cast<GRegexMatchFlags>(0), nullptr);
+ return std::regex_match(std::cbegin(pkg), std::cend(pkg), this->blacklist);
}
virtual ~Pkgtools() noexcept
@@ -101,12 +92,12 @@ public:
return false;
}
sqlite3_bind_text(statement, 1, pkg_name, -1, SQLITE_TRANSIENT);
- sqlite3_bind_int(statement, 2, this->get_order());
+ sqlite3_bind_int(statement, 2, this->order);
if (sqlite3_step(statement) == SQLITE_ROW)
{
dest_filename = g_build_filename(dest_dir_name, sqlite3_column_text(statement, 1), nullptr);
- source_url = g_strconcat(this->get_mirror(),
+ source_url = g_strconcat(this->mirror.c_str(),
sqlite3_column_text(statement, 0),
"/",
sqlite3_column_text(statement, 1),
@@ -150,13 +141,13 @@ public:
}
sqlite3_bind_text(statement, 1, pkg_name, -1, SQLITE_TRANSIENT);
- sqlite3_bind_int(statement, 2, this->get_order());
+ sqlite3_bind_int(statement, 2, this->order);
if (sqlite3_step(statement) == SQLITE_ROW)
{
pkg_filename = g_build_filename(LOCALSTATEDIR,
"cache",
- "PackageKit",
+ "katja",
"downloads",
sqlite3_column_text(statement, 0),
nullptr);
@@ -173,10 +164,7 @@ public:
virtual void generate_cache(JobData *job_data, const char *tmpl) noexcept = 0;
protected:
- char *name = nullptr;
- char *mirror = nullptr;
- std::uint8_t order;
- GRegex *blacklist = nullptr;
+ std::regex blacklist;
};
/**
@@ -186,6 +174,6 @@ int cmp_repo(const void *a, const void *b)
{
auto repo = static_cast<const Pkgtools *> (a);
- return g_strcmp0(repo->get_name(), (char *) b);
+ return g_strcmp0(repo->name.c_str(), (char *) b);
}
}
diff --git a/backend/slackpkg.cpp b/backend/slackpkg.cpp
index a053b76..fd7ea22 100644
--- a/backend/slackpkg.cpp
+++ b/backend/slackpkg.cpp
@@ -13,6 +13,9 @@ module;
#include <string.h>
#include <gio/gio.h>
#include <curl/curl.h>
+#include <string>
+#include <regex>
+#include <vector>
export module katja.slackpkg;
@@ -36,30 +39,18 @@ public:
*
* Returns: New #katja::Slackpkg.
**/
- Slackpkg(const char *name, const char *mirror,
- std::uint8_t order, const char *blacklist, char **priority) noexcept
+ Slackpkg(const std::string& name, const std::string& mirror,
+ std::uint8_t order, const char *blacklist, char **priority) noexcept
+ : Pkgtools(name, mirror, order)
{
- GRegex *regex;
-
- if (blacklist)
+ if (blacklist != nullptr)
{
- regex = static_cast<GRegex *>(g_regex_new(blacklist,
- G_REGEX_OPTIMIZE, static_cast<GRegexMatchFlags>(0), nullptr));
+ this->blacklist = std::regex(blacklist);
}
- else
+ for (char **cur_priority = priority; *cur_priority; cur_priority++)
{
- regex = nullptr;
+ this->priority.emplace_back(*cur_priority);
}
-
- this->name = g_strdup (name);
- this->mirror = g_strdup (mirror);
-
- this->order = order;
-
- this->blacklist = regex;
-
- this->priority = priority;
-
// Initialize category map
if (cat_map == nullptr)
{
@@ -83,21 +74,6 @@ public:
}
}
- ~Slackpkg() noexcept
- {
- if (this->blacklist)
- {
- g_regex_unref(this->blacklist);
- }
-
- g_free(this->name);
- g_free(this->mirror);
- if (this->priority)
- {
- g_strfreev(this->priority);
- }
- }
-
/**
* katja::Slackpkg::collect_cache_info:
* @tmpl: temporary directory for downloading the files.
@@ -107,7 +83,7 @@ public:
*
* Returns: List of files needed for building the cache.
**/
- GSList *collect_cache_info(const char *tmpl) noexcept
+ GSList *collect_cache_info(const char *tmpl) noexcept
{
CURL *curl = nullptr;
char **source_dest;
@@ -116,15 +92,15 @@ public:
/* Create the temporary directory for the repository */
tmp_dir = g_file_new_for_path(tmpl);
- repo_tmp_dir = g_file_get_child(tmp_dir, this->get_name());
+ 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 (char **cur_priority = this->priority; *cur_priority; cur_priority++)
+ for (const std::string& current_priority : this->priority)
{
source_dest = static_cast<char **> (g_malloc_n(3, sizeof(char *)));
- source_dest[0] = g_strconcat(this->get_mirror(), *cur_priority, "/PACKAGES.TXT", nullptr);
- source_dest[1] = g_build_filename(tmpl, this->get_name(), "PACKAGES.TXT", nullptr);
+ source_dest[0] = g_strconcat(this->mirror.c_str(), current_priority.c_str(), "/PACKAGES.TXT", nullptr);
+ source_dest[1] = g_build_filename(tmpl, this->name.c_str(), "PACKAGES.TXT", nullptr);
source_dest[2] = nullptr;
if (get_file(&curl, source_dest[0], nullptr) == CURLE_OK)
@@ -140,8 +116,8 @@ public:
/* Download file lists if available */
source_dest = static_cast<char **> (g_malloc_n(3, sizeof(char *)));
- source_dest[0] = g_strconcat(this->get_mirror(), *cur_priority, "/MANIFEST.bz2", nullptr);
- source_dest[1] = g_strconcat(tmpl, "/", this->get_name(), "/", *cur_priority, "-MANIFEST.bz2", nullptr);
+ source_dest[0] = g_strconcat(this->mirror.c_str(), current_priority.c_str(), "/MANIFEST.bz2", nullptr);
+ source_dest[1] = g_strconcat(tmpl, "/", this->name.c_str(), "/", *current_priority.c_str(), "-MANIFEST.bz2", nullptr);
source_dest[2] = nullptr;
if (get_file(&curl, source_dest[0], nullptr) == CURLE_OK)
{
@@ -173,7 +149,7 @@ public:
*
* Returns: List of files needed for building the cache.
**/
- void generate_cache(JobData *job_data, const char *tmpl) noexcept
+ void generate_cache(JobData *job_data, const char *tmpl) noexcept
{
char **pkg_tokens = nullptr;
char *query = nullptr, *filename = nullptr, *location = nullptr, *summary = nullptr, *line, *packages_txt;
@@ -187,7 +163,7 @@ public:
sqlite3_stmt *statement;
/* Check if the temporary directory for this repository exists, then the file metadata have to be generated */
- packages_txt = g_build_filename(tmpl, this->get_name(), "PACKAGES.TXT", nullptr);
+ packages_txt = g_build_filename(tmpl, this->name.c_str(), "PACKAGES.TXT", nullptr);
list_file = g_file_new_for_path(packages_txt);
fin = g_file_read(list_file, nullptr, nullptr);
g_object_unref(list_file);
@@ -203,7 +179,7 @@ public:
&statement,
nullptr) == SQLITE_OK)
{
- sqlite3_bind_text(statement, 1, this->get_name(), -1, SQLITE_TRANSIENT);
+ sqlite3_bind_text(statement, 1, this->name.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_step(statement);
sqlite3_finalize(statement);
}
@@ -215,8 +191,8 @@ public:
{
goto out;
}
- sqlite3_bind_int(statement, 1, this->get_order());
- sqlite3_bind_text(statement, 2, this->get_name(), -1, SQLITE_TRANSIENT);
+ 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);
@@ -244,7 +220,7 @@ public:
"ext = @ext, location = @location, summary = @summary, "
"desc = @desc, compressed = @compressed, uncompressed = @uncompressed "
"WHERE name LIKE @name AND repo_order = %u",
- this->get_order());
+ this->order);
if (sqlite3_prepare_v2(job_data->db, query, -1, &update_statement, nullptr) != SQLITE_OK)
{
goto out;
@@ -316,7 +292,7 @@ public:
{
statement = insert_default_statement;
}
- sqlite3_bind_int(statement, 11, this->get_order());
+ sqlite3_bind_int(statement, 11, this->order);
}
else /* Update package information if it is a patch */
{
@@ -354,9 +330,9 @@ public:
g_object_unref(data_in);
/* Parse MANIFEST.bz2 */
- for (char **p = this->priority; *p; p++)
+ for (const std::string& current_priority : this->priority)
{
- filename = g_strconcat(*p, "-MANIFEST.bz2", nullptr);
+ filename = g_strconcat(current_priority.c_str(), "-MANIFEST.bz2", nullptr);
manifest(job_data, tmpl, filename);
g_free(filename);
}
@@ -373,9 +349,9 @@ public:
}
private:
- static inline GHashTable *cat_map{ nullptr };
- static const std::size_t max_buf_size = 8192;
- char **priority = nullptr;
+ static inline GHashTable *cat_map{ nullptr };
+ static const std::size_t max_buf_size = 8192;
+ std::vector<std::string> priority;
/*
* katja::Slackpkg::manifest:
@@ -386,7 +362,7 @@ private:
* Parse the manifest file and save the file list in the database.
*/
- void manifest(JobData *job_data, const char *tmpl, char *filename) noexcept
+ void manifest(JobData *job_data, const char *tmpl, char *filename) noexcept
{
FILE *manifest;
int err, read_len;
@@ -399,10 +375,7 @@ private:
GMatchInfo *match_info;
sqlite3_stmt *statement = nullptr;
- path = g_build_filename(tmpl,
- this->get_name(),
- filename,
- nullptr);
+ path = g_build_filename(tmpl, this->name.c_str(), filename, nullptr);
manifest = fopen(path, "rb");
g_free(path);
diff --git a/backend/tests/definitions.cc b/backend/tests/definitions.cc
deleted file mode 100644
index 37f6134..0000000
--- a/backend/tests/definitions.cc
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * 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/.
- */
-#include <cstdint>
-#include "pk-backend.h"
-#include <pk-backend-job.h>
-
-void *
-pk_backend_job_get_user_data (PkBackendJob *job)
-{
- return nullptr;
-}
-
-void
-pk_backend_job_set_user_data (PkBackendJob *job, void *user_data)
-{
-}
-
-void
-pk_backend_job_set_allow_cancel (PkBackendJob *job, bool allow_cancel)
-{
-}
-
-void
-pk_backend_job_package (PkBackendJob *job,
- PkInfoEnum info,
- const char *package_id,
- const char *summary)
-{
-}
-
-void
-pk_backend_job_set_status (PkBackendJob *job, PkStatusEnum status)
-{
-}
-
-void
-pk_backend_job_set_percentage (PkBackendJob *job, unsigned percentage)
-{
-}
-
-void
-pk_backend_job_error_code (PkBackendJob *job,
- PkErrorEnum error_code, const char *format, ...)
-{
-}
-
-void
-pk_backend_job_files (PkBackendJob *job,
- const char *package_id, char **files)
-{
-}
-
-void
-pk_backend_job_details (PkBackendJob *job,
- const char *package_id,
- const char *summary,
- const char *license,
- PkGroupEnum group,
- const char *description,
- const char *url,
- unsigned long size,
- std::uint64_t download_size)
-{
-}
-
-void
-pk_backend_job_update_detail (PkBackendJob *job,
- const char *package_id,
- char **updates,
- char **obsoletes,
- char **vendor_urls,
- char **bugzilla_urls,
- char **cve_urls,
- PkRestartEnum restart,
- const char *update_text,
- const char *changelog,
- PkUpdateStateEnum state,
- const char *issued,
- const char *updated)
-{
-}
-
-bool
-pk_backend_job_thread_create (PkBackendJob *job,
- PkBackendJobThreadFunc func,
- void *user_data,
- GDestroyNotify destroy_func)
-{
- return false;
-}
-
-bool pk_directory_remove_contents (const char *directory)
-{
- return true;
-}
diff --git a/backend/tests/dl-test.cc b/backend/tests/dl-test.cc
deleted file mode 100644
index d04b2b5..0000000
--- a/backend/tests/dl-test.cc
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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/.
- */
-#include "dl.h"
-
-using namespace slack;
-
-static void
-slack_test_dl_construct()
-{
- auto dl = new Dl ("some", "mirror", 1, nullptr, nullptr);
-
- g_assert_cmpstr (dl->get_name (), ==, "some");
- g_assert_cmpstr (dl->get_mirror (), ==, "mirror");
- g_assert_cmpuint (dl->get_order (), ==, 1);
- g_assert_false (dl->is_blacklisted ("pattern"));
-
- delete dl;
-}
-
-int main(int argc, char *argv[])
-{
- g_test_init(&argc, &argv, nullptr);
-
- g_test_add_func("/slack/dl/construct", slack_test_dl_construct);
-
- return g_test_run();
-}
diff --git a/backend/tests/slackpkg-test.cc b/backend/tests/slackpkg-test.cc
index bc2e227..89406af 100644
--- a/backend/tests/slackpkg-test.cc
+++ b/backend/tests/slackpkg-test.cc
@@ -3,28 +3,19 @@
* 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/.
*/
-#include "slackpkg.h"
+#define BOOST_TEST_MODULE slackpkg tests
-using namespace slack;
+#include <boost/test/unit_test.hpp>
+import katja.slackpkg;
-static void
-slack_test_slackpkg_construct()
+BOOST_AUTO_TEST_CASE(slack_test_slackpkg_construct)
{
- auto slackpkg = new Slackpkg ("some", "mirror", 1, nullptr, nullptr);
+ auto slackpkg = new Slackpkg("some", "mirror", 1, nullptr, nullptr);
- g_assert_cmpstr(slackpkg->get_name (), ==, "some");
- g_assert_cmpstr(slackpkg->get_mirror (), ==, "mirror");
- g_assert_cmpuint(slackpkg->get_order (), ==, 1);
- g_assert_false (slackpkg->is_blacklisted ("pattern"));
+ BOOST_TEST(strcmp(slackpkg->get_name(), "some") == 0);
+ BOOST_TEST(strcmp(slackpkg->get_mirror(), "mirror") == 0);
+ BOOST_TEST(slackpkg->get_order() == 1);
+ BOOST_TEST(!slackpkg->is_blacklisted("pattern"));
- delete slackpkg;
-}
-
-int main(int argc, char *argv[])
-{
- g_test_init(&argc, &argv, nullptr);
-
- g_test_add_func("/slack/slackpkg/construct", slack_test_slackpkg_construct);
-
- return g_test_run();
+ delete slackpkg;
}