From 2fd55de71e0df8401533184d4f96be529ea930e3 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Sun, 16 Jan 2022 23:14:47 -0600 Subject: [PATCH] Install and Remove Patch Fixed issue where packages would not install or remove correctly. --- include/constants.hpp | 3 +- include/http.hpp | 6 ++-- include/utils.hpp | 4 ++- meson.build | 3 +- src/http.cpp | 13 ++++---- src/package_manager.cpp | 73 +++++++++++++++++++++++++++-------------- 6 files changed, 64 insertions(+), 38 deletions(-) diff --git a/include/constants.hpp b/include/constants.hpp index 10aaa2c..455eb9c 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace gdpm::constants{ @@ -75,4 +76,4 @@ namespace gdpm::constants{ #ifndef GDPM_TIMESTAMP_FORMAT #define GDPM_TIMESTAMP_FORMAT ":%I:%M:%S %p; %Y-%m-%d" -#endif \ No newline at end of file +#endif diff --git a/include/http.hpp b/include/http.hpp index bdb6732..a73e1a7 100644 --- a/include/http.hpp +++ b/include/http.hpp @@ -11,8 +11,8 @@ namespace gdpm::http{ std::unordered_map headers{}; }; - response request_get(const std::string& url, size_t timeout = GDPM_CONFIG_TIMEOUT_MS); - response request_post(const std::string& url, const char *post_fields="", size_t timeout = GDPM_CONFIG_TIMEOUT_MS); - response download_file(const std::string& url, const std::string& storage_path, size_t timeout = GDPM_CONFIG_TIMEOUT_MS); + response request_get(const std::string& url, size_t timeout = GDPM_CONFIG_TIMEOUT_MS, int verbose = 0); + response request_post(const std::string& url, const char *post_fields="", size_t timeout = GDPM_CONFIG_TIMEOUT_MS, int verbose = 0); + response download_file(const std::string& url, const std::string& storage_path, size_t timeout = GDPM_CONFIG_TIMEOUT_MS, int verbose = 0); } \ No newline at end of file diff --git a/include/utils.hpp b/include/utils.hpp index 5ccc659..bd31cde 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -60,6 +60,7 @@ namespace gdpm::utils{ return fwrite(ptr, size, nmemb, (FILE*)userdata); } + /* Use ISO 8601 for default timestamp format. */ static inline auto timestamp(const std::string& format = GDPM_TIMESTAMP_FORMAT){ time_t t = std::time(nullptr); #if GDPM_ENABLE_TIMESTAMPS == 1 @@ -78,7 +79,7 @@ namespace gdpm::utils{ } // A make_tuple wrapper for enforcing certain requirements - template + template auto range(Args...args) { // Limit number of args to only 2 @@ -101,4 +102,5 @@ namespace gdpm::utils{ std::string prompt_user(const char *message); bool prompt_user_yn(const char *message); void delay(std::chrono::milliseconds milliseconds = GDPM_REQUEST_DELAY); + // TODO: Add function to get size of decompressed zip } \ No newline at end of file diff --git a/meson.build b/meson.build index 9ad6912..1c2cea1 100644 --- a/meson.build +++ b/meson.build @@ -44,7 +44,8 @@ cpp_args = [ '-DGDPM_REQUEST_DELAY=200ms', '-DGDPM_ENABLE_COLORS=1', '-DGDPM_ENABLE_TIMESTAMPS=1', - '-DGDPM_TIMESTAMP_FORMAT=":%I:%M:%S %p; %Y-%m-%d"' + '-DGDPM_TIMESTAMP_FORMAT=":%I:%M:%S %p; %Y-%m-%d"', + '-DRAPIDJSON_HAS_STDSTRING=1' ] lib = shared_library( meson.project_name(), diff --git a/src/http.cpp b/src/http.cpp index 3a5f476..b4d7fb1 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -2,14 +2,13 @@ #include "http.hpp" #include "utils.hpp" #include "log.hpp" - #include #include #include namespace gdpm::http{ - response request_get(const std::string& url, size_t timeout){ + response request_get(const std::string& url, size_t timeout, int verbose){ CURL *curl = nullptr; CURLcode res; utils::memory_buffer buf = utils::make_buffer(); @@ -32,7 +31,7 @@ namespace gdpm::http{ curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r.code); - if(res != CURLE_OK) + if(res != CURLE_OK && verbose > 0) log::error("_make_request.curl_easy_perform(): {}", curl_easy_strerror(res)); curl_easy_cleanup(curl); } @@ -43,7 +42,7 @@ namespace gdpm::http{ return r; } - response request_post(const std::string& url, const char *post_fields, size_t timeout){ + response request_post(const std::string& url, const char *post_fields, size_t timeout, int verbose){ CURL *curl = nullptr; CURLcode res; utils::memory_buffer buf = utils::make_buffer(); @@ -66,7 +65,7 @@ namespace gdpm::http{ curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r.code); - if(res != CURLE_OK) + if(res != CURLE_OK && verbose > 0) log::error("_make_request.curl_easy_perform(): {}", curl_easy_strerror(res)); curl_easy_cleanup(curl); } @@ -77,7 +76,7 @@ namespace gdpm::http{ return r; } - response download_file(const std::string& url, const std::string& storage_path, size_t timeout){ + response download_file(const std::string& url, const std::string& storage_path, size_t timeout, int verbose){ CURL *curl = nullptr; CURLcode res; response r; @@ -114,7 +113,7 @@ namespace gdpm::http{ curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r.code); - if(res != CURLE_OK){ + if(res != CURLE_OK && verbose > 0){ log::error("download_file.curl_easy_perform() failed: {}", curl_easy_strerror(res)); } fclose(fp); diff --git a/src/package_manager.cpp b/src/package_manager.cpp index c6e4cea..8af497c 100644 --- a/src/package_manager.cpp +++ b/src/package_manager.cpp @@ -134,7 +134,9 @@ namespace gdpm::package_manager{ /* Retrieve necessary asset data if it was found already in cache */ Document doc; + // log::debug("download_url: {}\ncategory: {}\ndescription: {}\nsupport_level: {}", p.download_url, p.category, p.description, p.support_level); if(p.download_url.empty() || p.category.empty() || p.description.empty() || p.support_level.empty()){ + params.verbose = config.verbose; doc = rest_api::get_asset(url, p.asset_id, params); if(doc.HasParseError() || doc.IsNull()){ log::println(""); @@ -147,7 +149,23 @@ namespace gdpm::package_manager{ p.download_url = doc["download_url"].GetString(); p.download_hash = doc["download_hash"].GetString(); } - else{ + else{ + /* Package for in cache so no remote request. Still need to populate RapidJson::Document to write to package.json. + NOTE: This may not be necessary at all! + */ + // doc["asset_id"].SetUint64(p.asset_id + // doc["type"].SetString(p.type, doc.GetAllocator()); + // doc["title"].SetString(p.title, doc.GetAllocator()); + // doc["author"].SetString(p.author, doc.GetAllocator()); + // doc["author_id"].SetUint64(p.author_id); + // doc["version"].SetString(p.version, doc.GetAllocator()); + // doc["category"].SetString(p.category, doc.GetAllocator()); + // doc["godot_version"].SetString(p.godot_version, doc.GetAllocator()); + // doc["cost"].SetString(p.cost, doc.GetAllocator()); + // doc["description"].SetString(p.description, doc.GetAllocator()); + // doc["support_level"].SetString(p.support_level, doc.GetAllocator()); + // doc["download_url"].SetString(p.download_url, doc.GetAllocator()); + // doc["download_hash"].SetString(p.download_hash, doc.GetAllocator; } /* Set directory and temp paths for storage */ @@ -164,6 +182,7 @@ namespace gdpm::package_manager{ /* Dump asset information for lookup into JSON in package directory */ if(!std::filesystem::exists(package_dir)) std::filesystem::create_directory(package_dir); + std::ofstream ofs(package_dir + "/package.json"); OStreamWrapper osw(ofs); PrettyWriter writer(osw); @@ -176,8 +195,8 @@ namespace gdpm::package_manager{ else{ /* Download all the package files and place them in tmp directory. */ log::print("Downloading \"{}\"...", p.title); - std::string download_url = doc["download_url"].GetString(); - std::string title = doc["title"].GetString(); + std::string download_url = p.download_url;// doc["download_url"].GetString(); + std::string title = p.title;// doc["title"].GetString(); http::response response = http::download_file(download_url, tmp_zip); if(response.code == 200){ log::println("Done."); @@ -209,6 +228,7 @@ namespace gdpm::package_manager{ using namespace std::filesystem; if(package_titles.empty()){ + log::println(""); log::error("No packages to remove."); return; } @@ -216,6 +236,7 @@ namespace gdpm::package_manager{ /* Find the packages to remove if they're is_installed and show them to the user */ std::vector p_cache = cache::get_package_info_by_title(package_titles); if(p_cache.empty()){ + log::println(""); log::error("Could not find any packages to remove."); return; } @@ -227,6 +248,7 @@ namespace gdpm::package_manager{ }); if(p_count == 0){ + log::println(""); log::error("No packages to remove."); return; } @@ -251,29 +273,30 @@ namespace gdpm::package_manager{ } /* Traverse the package directory */ - for(const auto& entry : recursive_directory_iterator(path)){ - if(entry.is_directory()){ - } - else if(entry.is_regular_file()){ - std::string filename = entry.path().filename().string(); - std::string pkg_path = entry.path().lexically_normal().string(); + // for(const auto& entry : recursive_directory_iterator(path)){ + // if(entry.is_directory()){ + // } + // else if(entry.is_regular_file()){ + // std::string filename = entry.path().filename().string(); + // std::string pkg_path = entry.path().lexically_normal().string(); - // pkg_path = utils::replace_all(pkg_path, " ", "\\ "); - if(filename == "package.json"){ - std::string contents = utils::readfile(pkg_path); - Document doc; - if(config.verbose > 0){ - log::debug("package path: {}", pkg_path); - log::debug("contents: \n{}", contents); - } - doc.Parse(contents.c_str()); - if(doc.IsNull()){ - log::error("Could not remove packages. Parsing 'package.json' returned NULL."); - return; - } - } - } - } + // // pkg_path = utils::replace_all(pkg_path, " ", "\\ "); + // if(filename == "package.json"){ + // std::string contents = utils::readfile(pkg_path); + // Document doc; + // if(config.verbose > 0){ + // log::debug("package path: {}", pkg_path); + // log::debug("contents: \n{}", contents); + // } + // doc.Parse(contents.c_str()); + // if(doc.IsNull()){ + // log::println(""); + // log::error("Could not remove packages. Parsing 'package.json' returned NULL."); + // return; + // } + // } + // } + // } p.is_installed = false; } log::println("Done.");