diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-06-16 11:37:59 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-06-16 11:37:59 +0200 |
| commit | 1d8a03a7b9121bb2c61e8ec315920c452516afcf (patch) | |
| tree | a6eda85bc7d0f76b8f6d089820c2d083acf4ec54 /backend/utils.cpp | |
| parent | 8140da6d6e1a7584f02a2e8f8a5a9f75a10e776a (diff) | |
| download | katja-1d8a03a7b9121bb2c61e8ec315920c452516afcf.tar.gz | |
Query the modification time with std::filesystem
Diffstat (limited to 'backend/utils.cpp')
| -rw-r--r-- | backend/utils.cpp | 76 |
1 files changed, 29 insertions, 47 deletions
diff --git a/backend/utils.cpp b/backend/utils.cpp index 871fad8..816fcfd 100644 --- a/backend/utils.cpp +++ b/backend/utils.cpp @@ -14,6 +14,8 @@ module; #include <string> #include <vector> #include <optional> +#include <filesystem> +#include <boost/algorithm/string.hpp> export module katja.utils; @@ -76,12 +78,12 @@ struct DummyJobData final : JobData * * Returns: CURLE_OK (zero) on success, non-zero otherwise. **/ -CURLcode get_file(CURL **curl, const char *source_url, const char *dest) +CURLcode get_file(CURL **curl, const char *source_url, const std::optional<std::filesystem::path>& dest) { - const char *dest_dir_name; FILE *fout = nullptr; CURLcode ret; glong response_code; + std::string dest_dir_name; if ((*curl == nullptr) && (!(*curl = curl_easy_init()))) { @@ -91,7 +93,7 @@ CURLcode get_file(CURL **curl, const char *source_url, const char *dest) curl_easy_setopt(*curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(*curl, CURLOPT_URL, source_url); - if (dest == nullptr) + if (!dest.has_value()) { curl_easy_setopt(*curl, CURLOPT_NOBODY, 1L); curl_easy_setopt(*curl, CURLOPT_HEADER, 1L); @@ -105,13 +107,12 @@ CURLcode get_file(CURL **curl, const char *source_url, const char *dest) } else { - if (g_file_test(dest, G_FILE_TEST_IS_DIR)) + dest_dir_name = dest.value().native(); + if (std::filesystem::is_directory(dest.value())) { - dest_dir_name = dest; - dest = g_strconcat(dest_dir_name, g_strrstr(source_url, "/"), nullptr); - g_free(const_cast<char *>(dest_dir_name)); + dest_dir_name += strrchr(source_url, '/'); } - if ((fout = fopen(dest, "ab")) == nullptr) + if ((fout = fopen(dest_dir_name.c_str(), "ab")) == nullptr) { return CURLE_WRITE_ERROR; } @@ -132,7 +133,7 @@ CURLcode get_file(CURL **curl, const char *source_url, const char *dest) **/ std::optional<std::vector<std::string>> split_package_name(const std::string& pkg_filename) { - char *pkg_full_name; + std::string pkg_full_name; int len = pkg_filename.size(); if (len < 4) @@ -144,28 +145,23 @@ std::optional<std::vector<std::string>> split_package_name(const std::string& pk if (pkg_filename[len - 4] == '.') { /* Full name without extension */ - len -= 4; - pkg_full_name = g_strndup(pkg_filename.data(), len); - pkg_tokens[3] = g_strdup(pkg_full_name); + pkg_tokens[3] = pkg_filename; + pkg_full_name = std::string(std::crbegin(pkg_filename) + 4, std::crend(pkg_filename)); /* The last 3 characters should be the file extension */ - pkg_tokens[4] = g_strdup(pkg_filename.data() + len + 1); + pkg_tokens[4] = std::string(std::cbegin(pkg_filename) + len + 1, std::cend(pkg_filename)); } else { - pkg_full_name = g_strdup(pkg_filename.c_str()); + pkg_full_name = std::string(std::crbegin(pkg_filename), std::crend(pkg_filename)); } /* Reverse all of the bytes in the package filename to get the name, version and the architecture */ - g_strreverse (pkg_full_name); - char **reversed_tokens = g_strsplit(pkg_full_name, "-", 4); - pkg_tokens[0] = g_strreverse(reversed_tokens[3]); /* Name */ - pkg_tokens[1] = g_strreverse(reversed_tokens[2]); /* Version */ - pkg_tokens[2] = g_strreverse(reversed_tokens[1]); /* Architecture */ - - g_free(reversed_tokens[0]); /* Build number */ - g_free(reversed_tokens); - g_free(pkg_full_name); + std::vector<std::string> reversed_tokens; + boost::algorithm::split(reversed_tokens, pkg_full_name, boost::is_any_of("-")); + pkg_tokens[0] = std::string(std::crbegin(reversed_tokens[3]), std::crend(reversed_tokens[3])); /* Name */ + pkg_tokens[1] = std::string(std::crbegin(reversed_tokens[2]), std::crend(reversed_tokens[2])); /* Version */ + pkg_tokens[2] = std::string(std::crbegin(reversed_tokens[1]), std::crend(reversed_tokens[1])); /* Architecture */ return pkg_tokens; } @@ -180,20 +176,18 @@ std::optional<std::vector<std::string>> split_package_name(const std::string& pk **/ Info is_installed(const char *pkg_fullname) { - GFileEnumerator *pkg_metadata_enumerator; - GFileInfo *pkg_metadata_file_info; - GFile *pkg_metadata_dir; Info ret = Info::installing; const char *it; std::uint8_t dashes = 0; ptrdiff_t pkg_name; - g_return_val_if_fail(pkg_fullname != nullptr, Info::unknown); + if (pkg_fullname == nullptr) + { + return Info::unknown; + } // We want to find the package name without version for the package we're // looking for. - g_debug("Looking if %s is installed", pkg_fullname); - for (it = pkg_fullname + strlen(pkg_fullname); it != pkg_fullname; --it) { if (*it == '-') @@ -213,29 +207,21 @@ Info is_installed(const char *pkg_fullname) // 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"); - if (!(pkg_metadata_enumerator = g_file_enumerate_children(pkg_metadata_dir, - "standard::name", - G_FILE_QUERY_INFO_NONE, - nullptr, - nullptr))) - { - g_object_unref(pkg_metadata_dir); - return Info::unknown; - } + auto pkg_metadata_dir = std::filesystem::path("/var/log/packages"); - while ((pkg_metadata_file_info = g_file_enumerator_next_file(pkg_metadata_enumerator, nullptr, nullptr))) + for (const auto& pkg_metadata_file_info : std::filesystem::directory_iterator{ pkg_metadata_dir }) { - const char *dir = g_file_info_get_name(pkg_metadata_file_info); + std::string filename = pkg_metadata_file_info.path().native(); + const char *dir = filename.c_str(); dashes = 0; - if (strcmp(dir, pkg_fullname) == 0) + if (dir == pkg_fullname) { ret = Info::installed; } else { - for (it = dir + strlen(dir); it != dir; --it) + for (it = dir + filename.size(); it != dir; --it) { if (*it == '-') { @@ -251,15 +237,11 @@ Info is_installed(const char *pkg_fullname) ret = Info::updating; } } - g_object_unref(pkg_metadata_file_info); - if (ret != Info::installing) /* If installed */ { break; } } - g_object_unref(pkg_metadata_enumerator); - g_object_unref(pkg_metadata_dir); return ret; } |
