diff --git a/src/Options.cpp b/src/Options.cpp index 832ed09a..9d2e3504 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -38,6 +38,7 @@ #include "donate.h" #include "net/Url.h" #include "Options.h" +#include "Platform.h" #include "version.h" @@ -154,34 +155,6 @@ static const char *algo_names[] = { }; -static char *defaultConfigName() -{ - size_t size = 512; - char *buf = new char[size]; - - if (uv_exepath(buf, &size) < 0) { - delete [] buf; - return nullptr; - } - - if (size < 500) { -# ifdef WIN32 - char *p = strrchr(buf, '\\'); -# else - char *p = strrchr(buf, '/'); -# endif - - if (p) { - strcpy(p + 1, "config.json"); - return buf; - } - } - - delete [] buf; - return nullptr; -} - - Options *Options::parse(int argc, char **argv) { Options *options = new Options(argc, argv); @@ -241,9 +214,7 @@ Options::Options(int argc, char **argv) : } if (!m_pools[0]->isValid()) { - char *fileName = defaultConfigName(); - parseConfig(fileName); - delete [] fileName; + parseConfig(Platform::defaultConfigName()); } if (!m_pools[0]->isValid()) { @@ -508,8 +479,18 @@ Url *Options::parseUrl(const char *arg) const void Options::parseConfig(const char *fileName) { + 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; + } + json_error_t err; - json_t *config = json_load_file(fileName, 0, &err); + json_t *config = json_loadfd(fd, 0, &err); + + uv_fs_close(uv_default_loop(), &req, fd, nullptr); + uv_fs_req_cleanup(&req); if (!json_is_object(config)) { if (config) { diff --git a/src/Platform.cpp b/src/Platform.cpp index aa0c5706..57c655fa 100644 --- a/src/Platform.cpp +++ b/src/Platform.cpp @@ -22,7 +22,40 @@ */ +#include + + #include "Platform.h" -char *Platform::m_userAgent = nullptr; +char *Platform::m_defaultConfigName = nullptr; +char *Platform::m_userAgent = nullptr; + + +const char *Platform::defaultConfigName() +{ + size_t size = 520; + + if (m_defaultConfigName == nullptr) { + m_defaultConfigName = new char[size]; + } + + if (uv_exepath(m_defaultConfigName, &size) < 0) { + return nullptr; + } + + if (size < 500) { +# ifdef WIN32 + char *p = strrchr(m_defaultConfigName, '\\'); +# else + char *p = strrchr(m_defaultConfigName, '/'); +# endif + + if (p) { + strcpy(p + 1, "config.json"); + return m_defaultConfigName; + } + } + + return nullptr; +} diff --git a/src/Platform.h b/src/Platform.h index cc6fd879..1c1d5ce9 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -28,6 +28,7 @@ class Platform { public: + static const char *defaultConfigName(); static void init(); static void release(); static void setProcessPriority(int priority); @@ -36,6 +37,7 @@ public: static inline const char *userAgent() { return m_userAgent; } private: + static char *m_defaultConfigName; static char *m_userAgent; }; diff --git a/src/Platform_win.cpp b/src/Platform_win.cpp index 7943b662..3d520586 100644 --- a/src/Platform_win.cpp +++ b/src/Platform_win.cpp @@ -81,6 +81,7 @@ void Platform::init() void Platform::release() { + delete [] m_defaultConfigName; delete [] m_userAgent; }