diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/download.cpp | 61 | ||||
-rw-r--r-- | src/common/i18n.cpp | 43 | ||||
-rw-r--r-- | src/common/stack_trace.cpp | 3 | ||||
-rw-r--r-- | src/common/threadpool.cpp | 8 | ||||
-rw-r--r-- | src/common/updates.cpp | 2 | ||||
-rw-r--r-- | src/common/util.cpp | 14 |
6 files changed, 100 insertions, 31 deletions
diff --git a/src/common/download.cpp b/src/common/download.cpp index 28aac5a59..87814fa5e 100644 --- a/src/common/download.cpp +++ b/src/common/download.cpp @@ -33,6 +33,7 @@ #include <boost/thread/thread.hpp> #include "cryptonote_config.h" #include "include_base_utils.h" +#include "file_io_utils.h" #include "net/http_client.h" #include "download.h" @@ -74,9 +75,20 @@ namespace tools try { boost::unique_lock<boost::mutex> lock(control->mutex); - MINFO("Downloading " << control->uri << " to " << control->path); + std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary; + uint64_t existing_size = 0; + if (epee::file_io_utils::get_file_size(control->path, existing_size) && existing_size > 0) + { + MINFO("Resuming downloading " << control->uri << " to " << control->path << " from " << existing_size); + mode |= std::ios_base::app; + } + else + { + MINFO("Downloading " << control->uri << " to " << control->path); + mode |= std::ios_base::trunc; + } std::ofstream f; - f.open(control->path, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); + f.open(control->path, mode); if (!f.good()) { MERROR("Failed to open file " << control->path); control->result_cb(control->path, control->uri, control->success); @@ -85,11 +97,13 @@ namespace tools class download_client: public epee::net_utils::http::http_simple_client { public: - download_client(download_async_handle control, std::ofstream &f): - control(control), f(f), content_length(-1), total(0) {} + download_client(download_async_handle control, std::ofstream &f, uint64_t offset = 0): + control(control), f(f), content_length(-1), total(0), offset(offset) {} virtual ~download_client() { f.close(); } virtual bool on_header(const epee::net_utils::http::http_response_info &headers) { + for (const auto &kv: headers.m_header_info.m_etc_fields) + MDEBUG("Header: " << kv.first << ": " << kv.second); ssize_t length; if (epee::string_tools::get_xtype_from_string(length, headers.m_header_info.m_content_length) && length >= 0) { @@ -104,6 +118,26 @@ namespace tools return false; } } + if (offset > 0) + { + // we requested a range, so check if we're getting it, otherwise truncate + bool got_range = false; + const std::string prefix = "bytes=" + std::to_string(offset) + "-"; + for (const auto &kv: headers.m_header_info.m_etc_fields) + { + if (kv.first == "Content-Range" && strncmp(kv.second.c_str(), prefix.c_str(), prefix.size())) + { + got_range = true; + break; + } + } + if (!got_range) + { + MWARNING("We did not get the requested range, downloading from start"); + f.close(); + f.open(control->path, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); + } + } return true; } virtual bool handle_target_data(std::string &piece_of_transfer) @@ -130,7 +164,8 @@ namespace tools std::ofstream &f; ssize_t content_length; size_t total; - } client(control, f); + uint64_t offset; + } client(control, f, existing_size); epee::net_utils::http::url_content u_c; if (!epee::net_utils::parse_url(control->uri, u_c)) { @@ -147,9 +182,10 @@ namespace tools lock.unlock(); - uint16_t port = u_c.port ? u_c.port : 80; + bool ssl = u_c.schema == "https"; + uint16_t port = u_c.port ? u_c.port : ssl ? 443 : 80; MDEBUG("Connecting to " << u_c.host << ":" << port); - client.set_server(u_c.host, std::to_string(port), boost::none); + client.set_server(u_c.host, std::to_string(port), boost::none, ssl); if (!client.connect(std::chrono::seconds(30))) { boost::lock_guard<boost::mutex> lock(control->mutex); @@ -159,7 +195,14 @@ namespace tools } MDEBUG("GETting " << u_c.uri); const epee::net_utils::http::http_response_info *info = NULL; - if (!client.invoke_get(u_c.uri, std::chrono::seconds(30), "", &info)) + epee::net_utils::http::fields_list fields; + if (existing_size > 0) + { + const std::string range = "bytes=" + std::to_string(existing_size) + "-"; + MDEBUG("Asking for range: " << range); + fields.push_back(std::make_pair("Range", range)); + } + if (!client.invoke_get(u_c.uri, std::chrono::seconds(30), "", &info, fields)) { boost::lock_guard<boost::mutex> lock(control->mutex); MERROR("Failed to connect to " << control->uri); @@ -189,7 +232,7 @@ namespace tools MDEBUG("response body: " << info->m_body); for (const auto &f: info->m_additional_fields) MDEBUG("additional field: " << f.first << ": " << f.second); - if (info->m_response_code != 200) + if (info->m_response_code != 200 && info->m_response_code != 206) { boost::lock_guard<boost::mutex> lock(control->mutex); MERROR("Status code " << info->m_response_code); diff --git a/src/common/i18n.cpp b/src/common/i18n.cpp index 4a76e76fc..28a186bf0 100644 --- a/src/common/i18n.cpp +++ b/src/common/i18n.cpp @@ -35,6 +35,7 @@ #include "file_io_utils.h" #include "common/util.h" #include "common/i18n.h" +#include "translation_files.h" #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "i18n" @@ -62,6 +63,7 @@ std::string i18n_get_language() e = "en"; std::string language = e; + language = language.substr(0, language.find(".")); std::transform(language.begin(), language.end(), language.begin(), tolower); return language; } @@ -137,25 +139,40 @@ int i18n_set_language(const char *directory, const char *base, std::string langu i18n_log("Loading translations for language " << language); boost::system::error_code ignored_ec; - if (!boost::filesystem::exists(filename, ignored_ec)) { + if (boost::filesystem::exists(filename, ignored_ec)) { + if (!epee::file_io_utils::load_file_to_string(filename, contents)) { + i18n_log("Failed to load translations file: " << filename); + return -1; + } + } else { i18n_log("Translations file not found: " << filename); - const char *underscore = strchr(language.c_str(), '_'); - if (underscore) { - std::string fallback_language = std::string(language, 0, underscore - language.c_str()); - filename = std::string(directory) + "/" + base + "_" + fallback_language + ".qm"; - i18n_log("Not found, loading translations for language " << fallback_language); - if (!boost::filesystem::exists(filename, ignored_ec)) { - i18n_log("Translations file not found: " << filename); + filename = std::string(base) + "_" + language + ".qm"; + if (!find_embedded_file(filename, contents)) { + i18n_log("Embedded translations file not found: " << filename); + const char *underscore = strchr(language.c_str(), '_'); + if (underscore) { + std::string fallback_language = std::string(language, 0, underscore - language.c_str()); + filename = std::string(directory) + "/" + base + "_" + fallback_language + ".qm"; + i18n_log("Loading translations for language " << fallback_language); + if (boost::filesystem::exists(filename, ignored_ec)) { + if (!epee::file_io_utils::load_file_to_string(filename, contents)) { + i18n_log("Failed to load translations file: " << filename); + return -1; + } + } else { + i18n_log("Translations file not found: " << filename); + filename = std::string(base) + "_" + fallback_language + ".qm"; + if (!find_embedded_file(filename, contents)) { + i18n_log("Embedded translations file not found: " << filename); + return -1; + } + } + } else { return -1; } } } - if (!epee::file_io_utils::load_file_to_string(filename, contents)) { - i18n_log("Failed to load translations file: " << filename); - return -1; - } - data = (const unsigned char*)contents.c_str(); datalen = contents.size(); idx = 0; diff --git a/src/common/stack_trace.cpp b/src/common/stack_trace.cpp index bcdf72b60..ed1093309 100644 --- a/src/common/stack_trace.cpp +++ b/src/common/stack_trace.cpp @@ -28,7 +28,10 @@ #if !defined __GNUC__ || defined __MINGW32__ || defined __MINGW64__ || defined __ANDROID__ #define USE_UNWIND +#else +#define ELPP_FEATURE_CRASH_LOG 1 #endif +#include "easylogging++/easylogging++.h" #include <stdexcept> #ifdef USE_UNWIND diff --git a/src/common/threadpool.cpp b/src/common/threadpool.cpp index 20c5765b0..5d749e08e 100644 --- a/src/common/threadpool.cpp +++ b/src/common/threadpool.cpp @@ -34,6 +34,8 @@ #include "cryptonote_config.h" #include "common/util.h" +static __thread int depth = 0; + namespace tools { threadpool::threadpool() : running(true), active(0) { @@ -60,11 +62,13 @@ threadpool::~threadpool() { void threadpool::submit(waiter *obj, std::function<void()> f) { entry e = {obj, f}; boost::unique_lock<boost::mutex> lock(mutex); - if (active == max && !queue.empty()) { + if ((active == max && !queue.empty()) || depth > 0) { // if all available threads are already running // and there's work waiting, just run in current thread lock.unlock(); + ++depth; f(); + --depth; } else { if (obj) obj->inc(); @@ -106,7 +110,9 @@ void threadpool::run() { e = queue.front(); queue.pop_front(); lock.unlock(); + ++depth; e.f(); + --depth; if (e.wo) e.wo->dec(); diff --git a/src/common/updates.cpp b/src/common/updates.cpp index eff6754af..2d9c2d89c 100644 --- a/src/common/updates.cpp +++ b/src/common/updates.cpp @@ -99,7 +99,7 @@ namespace tools std::string get_update_url(const std::string &software, const std::string &subdir, const std::string &buildtag, const std::string &version, bool user) { - const char *base = user ? "https://downloads.getmonero.org/" : "http://updates.getmonero.org/"; + const char *base = user ? "https://downloads.getmonero.org/" : "https://updates.getmonero.org/"; #ifdef _WIN32 static const char *extension = strncmp(buildtag.c_str(), "install-", 8) ? ".zip" : ".exe"; #else diff --git a/src/common/util.cpp b/src/common/util.cpp index d9e12ffe4..2a2f50c4f 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -635,13 +635,13 @@ std::string get_nix_version_display_string() int vercmp(const char *v0, const char *v1) { std::vector<std::string> f0, f1; - boost::split(f0, v0, boost::is_any_of(".")); - boost::split(f1, v1, boost::is_any_of(".")); - while (f0.size() < f1.size()) - f0.push_back("0"); - while (f1.size() < f0.size()) - f1.push_back("0"); - for (size_t i = 0; i < f0.size(); ++i) { + boost::split(f0, v0, boost::is_any_of(".-")); + boost::split(f1, v1, boost::is_any_of(".-")); + for (size_t i = 0; i < std::max(f0.size(), f1.size()); ++i) { + if (i >= f0.size()) + return -1; + if (i >= f1.size()) + return 1; int f0i = atoi(f0[i].c_str()), f1i = atoi(f1[i].c_str()); int n = f0i - f1i; if (n) |