Fixed: failed open default config file if path contains non English characters.

feature-httpd
XMRig 2017-08-16 12:22:35 +03:00
parent ad0d876b18
commit 1b0ddae4eb
4 changed files with 50 additions and 33 deletions

View File

@ -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) {

View File

@ -22,7 +22,40 @@
*/
#include <uv.h>
#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;
}

View File

@ -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;
};

View File

@ -81,6 +81,7 @@ void Platform::init()
void Platform::release()
{
delete [] m_defaultConfigName;
delete [] m_userAgent;
}