diff options
Diffstat (limited to 'backend/slackpkg.cpp')
| -rw-r--r-- | backend/slackpkg.cpp | 89 |
1 files changed, 31 insertions, 58 deletions
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); |
