From 1e12f2af8b9ec50144421d998e98e4809071e118 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 12 Jun 2026 13:30:37 +0200 Subject: Replace allocations with RAII in pkgtools.cpp --- backend/slackpkg.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'backend/slackpkg.cpp') diff --git a/backend/slackpkg.cpp b/backend/slackpkg.cpp index d546b5e..b179da0 100644 --- a/backend/slackpkg.cpp +++ b/backend/slackpkg.cpp @@ -67,7 +67,7 @@ public: * * Returns: List of files needed for building the cache. **/ - std::forward_list collect_cache_info(const char *tmpl) noexcept + std::forward_list collect_cache_info(const std::filesystem::path& tmpl) noexcept { CURL *curl = nullptr; cache_entry source_dest; @@ -75,7 +75,7 @@ public: GFile *tmp_dir, *repo_tmp_dir; /* Create the temporary directory for the repository */ - tmp_dir = g_file_new_for_path(tmpl); + 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); @@ -83,7 +83,7 @@ public: for (const std::string& current_priority : this->priority) { source_dest.first = this->mirror + current_priority + "/PACKAGES.TXT"; - source_dest.second = std::filesystem::path(tmpl) / this->name / "PACKAGES.TXT"; + source_dest.second = tmpl / this->name / "PACKAGES.TXT"; if (get_file(&curl, source_dest.first.c_str(), nullptr) == CURLE_OK) { @@ -96,7 +96,7 @@ public: /* Download file lists if available */ source_dest.first = this->mirror + current_priority + "/MANIFEST.bz2"; - source_dest.second = std::filesystem::path(tmpl) / this->name / (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) { @@ -124,10 +124,10 @@ 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 std::filesystem::path& tmpl) noexcept { char **pkg_tokens = nullptr; - char *query = nullptr, *filename = nullptr, *location = nullptr, *summary = nullptr, *line, *packages_txt; + 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; @@ -138,11 +138,11 @@ 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->name.c_str(), "PACKAGES.TXT", nullptr); - list_file = g_file_new_for_path(packages_txt); + 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); - g_free(packages_txt); + if (!fin) { goto out; @@ -304,9 +304,7 @@ public: /* Parse MANIFEST.bz2 */ for (const std::string& current_priority : this->priority) { - filename = g_strconcat(current_priority.c_str(), "-MANIFEST.bz2", nullptr); - manifest(job_data, tmpl, filename); - g_free(filename); + manifest(job_data, tmpl, current_priority + "-MANIFEST.bz2"); } out: sqlite3_finalize(update_statement); @@ -333,12 +331,13 @@ 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 std::filesystem::path& tmpl, const std::string& filename) noexcept { FILE *manifest; int err, read_len; unsigned pos; - char buf[max_buf_size], *path, *pkg_filename, *rest = nullptr, *start; + char buf[max_buf_size], *pkg_filename, *rest = nullptr, *start; + std::filesystem::path path; char *full_name = nullptr; char **line, **lines; BZFILE *manifest_bz2; @@ -346,9 +345,8 @@ private: GMatchInfo *match_info; sqlite3_stmt *statement = nullptr; - path = g_build_filename(tmpl, this->name.c_str(), filename, nullptr); - manifest = fopen(path, "rb"); - g_free(path); + path = tmpl / this->name / filename; + manifest = fopen(path.native().c_str(), "rb"); if (!manifest) { -- cgit v1.2.3