diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c39776b..41e809a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ include (cmake/cpu.cmake) set(HEADERS src/api/NetworkState.h src/App.h + src/base/io/Json.h src/base/tools/String.h src/common/config/CommonConfig.h src/common/config/ConfigLoader.h @@ -98,6 +99,7 @@ endif() set(SOURCES src/api/NetworkState.cpp src/App.cpp + src/base/io/Json.cpp src/base/tools/String.cpp src/common/config/CommonConfig.cpp src/common/config/ConfigLoader.cpp @@ -143,6 +145,7 @@ if (WIN32) set(SOURCES_OS res/app.rc src/App_win.cpp + src/base/io/Json_win.cpp src/common/Platform_win.cpp src/Mem_win.cpp ) @@ -152,12 +155,14 @@ if (WIN32) elseif (APPLE) set(SOURCES_OS src/App_unix.cpp + src/base/io/Json_unix.cpp src/common/Platform_mac.cpp src/Mem_unix.cpp ) else() set(SOURCES_OS src/App_unix.cpp + src/base/io/Json_unix.cpp src/common/Platform_unix.cpp src/Mem_unix.cpp ) diff --git a/src/base/io/Json.cpp b/src/base/io/Json.cpp new file mode 100644 index 00000000..13a0d7e3 --- /dev/null +++ b/src/base/io/Json.cpp @@ -0,0 +1,38 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "base/io/Json.h" +#include "rapidjson/document.h" + + +bool xmrig::Json::getBool(const rapidjson::Value &obj, const char *key, bool defaultValue) +{ + auto i = obj.FindMember(key); + if (i != obj.MemberEnd() && i->value.IsBool()) { + return i->value.GetBool(); + } + + return defaultValue; +} diff --git a/src/base/io/Json.h b/src/base/io/Json.h new file mode 100644 index 00000000..54f92758 --- /dev/null +++ b/src/base/io/Json.h @@ -0,0 +1,48 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef XMRIG_JSON_H +#define XMRIG_JSON_H + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class Json +{ +public: + static bool getBool(const rapidjson::Value &obj, const char *key, bool defaultValue = false); + + static bool get(const char *fileName, rapidjson::Document &doc); + static bool save(const char *fileName, const rapidjson::Document &doc); +}; + + +} /* namespace xmrig */ + + +#endif /* XMRIG_JSON_H */ diff --git a/src/base/io/Json_unix.cpp b/src/base/io/Json_unix.cpp new file mode 100644 index 00000000..da3902d8 --- /dev/null +++ b/src/base/io/Json_unix.cpp @@ -0,0 +1,62 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#include "base/io/Json.h" +#include "rapidjson/document.h" +#include "rapidjson/istreamwrapper.h" +#include "rapidjson/ostreamwrapper.h" +#include "rapidjson/prettywriter.h" + + +bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc) +{ + std::ifstream ifs(fileName, std::ios_base::in | std::ios_base::binary); + if (!ifs.is_open()) { + return false; + } + + rapidjson::IStreamWrapper isw(ifs); + doc.ParseStream(isw); + + return !doc.HasParseError() && doc.IsObject(); +} + + +bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc) +{ + std::ofstream ofs(fileName, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); + if (!ofs.is_open()) { + return false; + } + + rapidjson::OStreamWrapper osw(ofs); + rapidjson::PrettyWriter writer(osw); + doc.Accept(writer); + + return true; +} diff --git a/src/base/io/Json_win.cpp b/src/base/io/Json_win.cpp new file mode 100644 index 00000000..4a1c5266 --- /dev/null +++ b/src/base/io/Json_win.cpp @@ -0,0 +1,124 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include + + +#ifdef __GNUC__ +# include +# include +#endif + + +#include + + +#include "base/io/Json.h" +#include "rapidjson/document.h" +#include "rapidjson/istreamwrapper.h" +#include "rapidjson/ostreamwrapper.h" +#include "rapidjson/prettywriter.h" + + +#if defined(_MSC_VER) || defined (__GNUC__) +static std::wstring toUtf16(const char *str) +{ + const int size = static_cast(strlen(str)); + std::wstring ret; + + int len = MultiByteToWideChar(CP_UTF8, 0, str, size, nullptr, 0); + if (len > 0) { + ret.resize(static_cast(len)); + MultiByteToWideChar(CP_UTF8, 0, str, size, &ret[0], len); + } + + return ret; +} +#endif + + +bool xmrig::Json::get(const char *fileName, rapidjson::Document &doc) +{ + using namespace rapidjson; + constexpr const std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary; + +# if defined(_MSC_VER) + std::ifstream ifs(toUtf16(fileName), mode); + if (!ifs.is_open()) { + return false; + } +# elif defined(__GNUC__) + const int fd = _wopen(toUtf16(fileName).c_str(), _O_RDONLY | _O_BINARY); + if (fd == -1) { + return false; + } + + __gnu_cxx::stdio_filebuf buf(fd, mode); + std::istream ifs(&buf); +# else + std::ifstream ifs(fileName, mode); + if (!ifs.is_open()) { + return false; + } +# endif + + IStreamWrapper isw(ifs); + doc.ParseStream(isw); + + return !doc.HasParseError() && doc.IsObject(); +} + + +bool xmrig::Json::save(const char *fileName, const rapidjson::Document &doc) +{ + using namespace rapidjson; + constexpr const std::ios_base::openmode mode = std::ios_base::out | std::ios_base::binary | std::ios_base::trunc; + +# if defined(_MSC_VER) + std::ofstream ofs(toUtf16(fileName), mode); + if (!ofs.is_open()) { + return false; + } +# elif defined(__GNUC__) + const int fd = _wopen(toUtf16(fileName).c_str(), _O_WRONLY | _O_BINARY | _O_TRUNC); + if (fd == -1) { + return false; + } + + __gnu_cxx::stdio_filebuf buf(fd, mode); + std::ostream ofs(&buf); +# else + std::ofstream ofs(fileName, mode); + if (!ofs.is_open()) { + return false; + } +# endif + + OStreamWrapper osw(ofs); + PrettyWriter writer(osw); + doc.Accept(writer); + + return true; +} diff --git a/src/common/config/CommonConfig.cpp b/src/common/config/CommonConfig.cpp index f2a00182..1207db48 100644 --- a/src/common/config/CommonConfig.cpp +++ b/src/common/config/CommonConfig.cpp @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -53,6 +54,7 @@ #endif +#include "base/io/Json.h" #include "common/config/CommonConfig.h" #include "common/log/Log.h" #include "donate.h" @@ -203,31 +205,15 @@ bool xmrig::CommonConfig::save() return false; } - uv_fs_t req; - const int fd = uv_fs_open(uv_default_loop(), &req, m_fileName.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644, nullptr); - if (fd < 0) { - return false; - } - - uv_fs_req_cleanup(&req); - rapidjson::Document doc; getJSON(doc); - FILE *fp = fdopen(fd, "w"); + if (Json::save(m_fileName, doc)) { + LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data()); + return true; + } - char buf[4096]; - rapidjson::FileWriteStream os(fp, buf, sizeof(buf)); - rapidjson::PrettyWriter writer(os); - doc.Accept(writer); - - fflush(fp); - - uv_fs_close(uv_default_loop(), &req, fd, nullptr); - uv_fs_req_cleanup(&req); - - LOG_NOTICE("configuration saved to: \"%s\"", m_fileName.data()); - return true; + return false; } diff --git a/src/common/config/CommonConfig.h b/src/common/config/CommonConfig.h index a864033b..40124b37 100644 --- a/src/common/config/CommonConfig.h +++ b/src/common/config/CommonConfig.h @@ -5,7 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2016-2018 XMRig , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,9 +29,9 @@ #include +#include "base/tools/String.h" #include "common/interfaces/IConfig.h" #include "common/net/Pool.h" -#include "common/utils/c_str.h" #include "common/xmrig.h" @@ -103,12 +104,12 @@ protected: State m_state; std::vector m_activePools; std::vector m_pools; - xmrig::c_str m_apiId; - xmrig::c_str m_apiToken; - xmrig::c_str m_apiWorkerId; - xmrig::c_str m_fileName; - xmrig::c_str m_logFile; - xmrig::c_str m_userAgent; + xmrig::String m_apiId; + xmrig::String m_apiToken; + xmrig::String m_apiWorkerId; + xmrig::String m_fileName; + xmrig::String m_logFile; + xmrig::String m_userAgent; private: bool parseInt(int key, int arg); diff --git a/src/common/config/ConfigLoader.cpp b/src/common/config/ConfigLoader.cpp index b3b3ecb0..6ce918d6 100644 --- a/src/common/config/ConfigLoader.cpp +++ b/src/common/config/ConfigLoader.cpp @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify @@ -37,6 +38,7 @@ #endif +#include "base/io/Json.h" #include "common/config/ConfigLoader.h" #include "common/config/ConfigWatcher.h" #include "common/interfaces/IConfig.h" @@ -151,7 +153,7 @@ xmrig::IConfig *xmrig::ConfigLoader::load(int argc, char **argv, IConfigCreator int key; while (1) { - key = getopt_long(argc, argv, short_options, options, NULL); + key = getopt_long(argc, argv, short_options, options, nullptr); if (key < 0) { break; } @@ -207,30 +209,18 @@ void xmrig::ConfigLoader::release() bool xmrig::ConfigLoader::getJSON(const char *fileName, rapidjson::Document &doc) { - uv_fs_t req; - const int fd = uv_fs_open(uv_default_loop(), &req, fileName, O_RDONLY, 0644, nullptr); - if (fd < 0) { - fprintf(stderr, "unable to open %s: %s\n", fileName, uv_strerror(fd)); - return false; + if (Json::get(fileName, doc)) { + return true; } - uv_fs_req_cleanup(&req); - - FILE *fp = fdopen(fd, "rb"); - char buf[8192]; - rapidjson::FileReadStream is(fp, buf, sizeof(buf)); - - doc.ParseStream(is); - - uv_fs_close(uv_default_loop(), &req, fd, nullptr); - uv_fs_req_cleanup(&req); - if (doc.HasParseError()) { - printf("%s<%d>: %s\n", fileName, (int) doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError())); - return false; + printf("%s: \"%s\"\n", fileName, doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError())); + } + else { + fprintf(stderr, "unable to open \"%s\".\n", fileName); } - return doc.IsObject(); + return false; } diff --git a/src/common/config/ConfigLoader.h b/src/common/config/ConfigLoader.h index b9e04537..b0bd67eb 100644 --- a/src/common/config/ConfigLoader.h +++ b/src/common/config/ConfigLoader.h @@ -5,6 +5,7 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify diff --git a/src/common/interfaces/IConfig.h b/src/common/interfaces/IConfig.h index 0c8cfc28..48809fa1 100644 --- a/src/common/interfaces/IConfig.h +++ b/src/common/interfaces/IConfig.h @@ -4,7 +4,9 @@ * Copyright 2014 Lucas Jones * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee - * Copyright 2016-2018 XMRig + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -125,7 +127,7 @@ public: CudaMaxUsageKey = 1206, }; - virtual ~IConfig() {} + virtual ~IConfig() = default; virtual bool finalize() = 0; virtual bool isWatch() const = 0;