Install and Remove Patch

Fixed issue where packages would not install or remove correctly.
This commit is contained in:
David Allen 2022-01-16 23:14:47 -06:00
parent 2b250d9a2d
commit 2fd55de71e
6 changed files with 64 additions and 38 deletions

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <rapidjson/rapidjson.h>
#include <string> #include <string>
namespace gdpm::constants{ namespace gdpm::constants{

View file

@ -11,8 +11,8 @@ namespace gdpm::http{
std::unordered_map<std::string, std::string> headers{}; std::unordered_map<std::string, std::string> headers{};
}; };
response request_get(const std::string& url, 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); 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); response download_file(const std::string& url, const std::string& storage_path, size_t timeout = GDPM_CONFIG_TIMEOUT_MS, int verbose = 0);
} }

View file

@ -60,6 +60,7 @@ namespace gdpm::utils{
return fwrite(ptr, size, nmemb, (FILE*)userdata); 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){ static inline auto timestamp(const std::string& format = GDPM_TIMESTAMP_FORMAT){
time_t t = std::time(nullptr); time_t t = std::time(nullptr);
#if GDPM_ENABLE_TIMESTAMPS == 1 #if GDPM_ENABLE_TIMESTAMPS == 1
@ -101,4 +102,5 @@ namespace gdpm::utils{
std::string prompt_user(const char *message); std::string prompt_user(const char *message);
bool prompt_user_yn(const char *message); bool prompt_user_yn(const char *message);
void delay(std::chrono::milliseconds milliseconds = GDPM_REQUEST_DELAY); void delay(std::chrono::milliseconds milliseconds = GDPM_REQUEST_DELAY);
// TODO: Add function to get size of decompressed zip
} }

View file

@ -44,7 +44,8 @@ cpp_args = [
'-DGDPM_REQUEST_DELAY=200ms', '-DGDPM_REQUEST_DELAY=200ms',
'-DGDPM_ENABLE_COLORS=1', '-DGDPM_ENABLE_COLORS=1',
'-DGDPM_ENABLE_TIMESTAMPS=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( lib = shared_library(
meson.project_name(), meson.project_name(),

View file

@ -2,14 +2,13 @@
#include "http.hpp" #include "http.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "log.hpp" #include "log.hpp"
#include <curl/curl.h> #include <curl/curl.h>
#include <stdio.h> #include <stdio.h>
#include <chrono> #include <chrono>
namespace gdpm::http{ 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; CURL *curl = nullptr;
CURLcode res; CURLcode res;
utils::memory_buffer buf = utils::make_buffer(); utils::memory_buffer buf = utils::make_buffer();
@ -32,7 +31,7 @@ namespace gdpm::http{
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r.code); 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)); log::error("_make_request.curl_easy_perform(): {}", curl_easy_strerror(res));
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
@ -43,7 +42,7 @@ namespace gdpm::http{
return r; 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; CURL *curl = nullptr;
CURLcode res; CURLcode res;
utils::memory_buffer buf = utils::make_buffer(); utils::memory_buffer buf = utils::make_buffer();
@ -66,7 +65,7 @@ namespace gdpm::http{
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r.code); 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)); log::error("_make_request.curl_easy_perform(): {}", curl_easy_strerror(res));
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
@ -77,7 +76,7 @@ namespace gdpm::http{
return r; 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; CURL *curl = nullptr;
CURLcode res; CURLcode res;
response r; response r;
@ -114,7 +113,7 @@ namespace gdpm::http{
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout); curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r.code); 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)); log::error("download_file.curl_easy_perform() failed: {}", curl_easy_strerror(res));
} }
fclose(fp); fclose(fp);

View file

@ -134,7 +134,9 @@ namespace gdpm::package_manager{
/* Retrieve necessary asset data if it was found already in cache */ /* Retrieve necessary asset data if it was found already in cache */
Document doc; 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()){ 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); doc = rest_api::get_asset(url, p.asset_id, params);
if(doc.HasParseError() || doc.IsNull()){ if(doc.HasParseError() || doc.IsNull()){
log::println(""); log::println("");
@ -148,6 +150,22 @@ namespace gdpm::package_manager{
p.download_hash = doc["download_hash"].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 */ /* 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 */ /* Dump asset information for lookup into JSON in package directory */
if(!std::filesystem::exists(package_dir)) if(!std::filesystem::exists(package_dir))
std::filesystem::create_directory(package_dir); std::filesystem::create_directory(package_dir);
std::ofstream ofs(package_dir + "/package.json"); std::ofstream ofs(package_dir + "/package.json");
OStreamWrapper osw(ofs); OStreamWrapper osw(ofs);
PrettyWriter<OStreamWrapper> writer(osw); PrettyWriter<OStreamWrapper> writer(osw);
@ -176,8 +195,8 @@ namespace gdpm::package_manager{
else{ else{
/* Download all the package files and place them in tmp directory. */ /* Download all the package files and place them in tmp directory. */
log::print("Downloading \"{}\"...", p.title); log::print("Downloading \"{}\"...", p.title);
std::string download_url = doc["download_url"].GetString(); std::string download_url = p.download_url;// doc["download_url"].GetString();
std::string title = doc["title"].GetString(); std::string title = p.title;// doc["title"].GetString();
http::response response = http::download_file(download_url, tmp_zip); http::response response = http::download_file(download_url, tmp_zip);
if(response.code == 200){ if(response.code == 200){
log::println("Done."); log::println("Done.");
@ -209,6 +228,7 @@ namespace gdpm::package_manager{
using namespace std::filesystem; using namespace std::filesystem;
if(package_titles.empty()){ if(package_titles.empty()){
log::println("");
log::error("No packages to remove."); log::error("No packages to remove.");
return; 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 */ /* Find the packages to remove if they're is_installed and show them to the user */
std::vector<package_info> p_cache = cache::get_package_info_by_title(package_titles); std::vector<package_info> p_cache = cache::get_package_info_by_title(package_titles);
if(p_cache.empty()){ if(p_cache.empty()){
log::println("");
log::error("Could not find any packages to remove."); log::error("Could not find any packages to remove.");
return; return;
} }
@ -227,6 +248,7 @@ namespace gdpm::package_manager{
}); });
if(p_count == 0){ if(p_count == 0){
log::println("");
log::error("No packages to remove."); log::error("No packages to remove.");
return; return;
} }
@ -251,29 +273,30 @@ namespace gdpm::package_manager{
} }
/* Traverse the package directory */ /* Traverse the package directory */
for(const auto& entry : recursive_directory_iterator(path)){ // for(const auto& entry : recursive_directory_iterator(path)){
if(entry.is_directory()){ // if(entry.is_directory()){
} // }
else if(entry.is_regular_file()){ // else if(entry.is_regular_file()){
std::string filename = entry.path().filename().string(); // std::string filename = entry.path().filename().string();
std::string pkg_path = entry.path().lexically_normal().string(); // std::string pkg_path = entry.path().lexically_normal().string();
// pkg_path = utils::replace_all(pkg_path, " ", "\\ "); // // pkg_path = utils::replace_all(pkg_path, " ", "\\ ");
if(filename == "package.json"){ // if(filename == "package.json"){
std::string contents = utils::readfile(pkg_path); // std::string contents = utils::readfile(pkg_path);
Document doc; // Document doc;
if(config.verbose > 0){ // if(config.verbose > 0){
log::debug("package path: {}", pkg_path); // log::debug("package path: {}", pkg_path);
log::debug("contents: \n{}", contents); // log::debug("contents: \n{}", contents);
} // }
doc.Parse(contents.c_str()); // doc.Parse(contents.c_str());
if(doc.IsNull()){ // if(doc.IsNull()){
log::error("Could not remove packages. Parsing 'package.json' returned NULL."); // log::println("");
return; // log::error("Could not remove packages. Parsing 'package.json' returned NULL.");
} // return;
} // }
} // }
} // }
// }
p.is_installed = false; p.is_installed = false;
} }
log::println("Done."); log::println("Done.");