Fixed possible out of order write to log file.

This commit is contained in:
XMRig 2021-03-01 18:54:20 +07:00
parent 4a8e7510e1
commit 6f8ffb7660
No known key found for this signature in database
GPG key ID: 446A53638BE94409
2 changed files with 36 additions and 15 deletions

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -37,9 +37,6 @@ static void fsWriteCallback(uv_fs_t *req)
}
static const char *kNewLine = "\n";
} // namespace xmrig
@ -50,11 +47,24 @@ bool xmrig::FileLogWriter::open(const char *fileName)
return false;
}
uv_fs_t req;
m_file = uv_fs_open(uv_default_loop(), &req, Env::expand(fileName), O_CREAT | O_APPEND | O_WRONLY, 0644, nullptr);
uv_fs_t req{};
const auto path = Env::expand(fileName);
m_file = uv_fs_open(uv_default_loop(), &req, path, O_CREAT | O_WRONLY, 0644, nullptr);
if (req.result < 0 || !isOpen()) {
uv_fs_req_cleanup(&req);
m_file = -1;
return false;
}
uv_fs_req_cleanup(&req);
return isOpen();
uv_fs_stat(uv_default_loop(), &req, path, nullptr);
m_pos = req.statbuf.st_size;
uv_fs_req_cleanup(&req);
return true;
}
@ -70,7 +80,8 @@ bool xmrig::FileLogWriter::write(const char *data, size_t size)
auto req = new uv_fs_t;
req->data = buf.base;
uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, -1, fsWriteCallback);
uv_fs_write(uv_default_loop(), req, m_file, &buf, 1, m_pos, fsWriteCallback);
m_pos += size;
return true;
}
@ -78,9 +89,9 @@ bool xmrig::FileLogWriter::write(const char *data, size_t size)
bool xmrig::FileLogWriter::writeLine(const char *data, size_t size)
{
uv_buf_t buf[2] = {
const uv_buf_t buf[2] = {
uv_buf_init(new char[size], size),
uv_buf_init(const_cast<char *>(kNewLine), 1)
uv_buf_init(m_endl, sizeof(m_endl) - 1)
};
memcpy(buf[0].base, data, size);
@ -88,7 +99,8 @@ bool xmrig::FileLogWriter::writeLine(const char *data, size_t size)
auto req = new uv_fs_t;
req->data = buf[0].base;
uv_fs_write(uv_default_loop(), req, m_file, buf, 2, -1, fsWriteCallback);
uv_fs_write(uv_default_loop(), req, m_file, buf, 2, m_pos, fsWriteCallback);
m_pos += (buf[0].len + buf[1].len);
return true;
}

View file

@ -1,6 +1,6 @@
/* XMRig
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
@ -21,6 +21,7 @@
#include <cstddef>
#include <cstdint>
namespace xmrig {
@ -33,13 +34,21 @@ public:
FileLogWriter(const char *fileName) { open(fileName); }
inline bool isOpen() const { return m_file >= 0; }
inline int64_t pos() const { return m_pos; }
bool open(const char *fileName);
bool write(const char *data, size_t size);
bool writeLine(const char *data, size_t size);
private:
# ifdef XMRIG_OS_WIN
char m_endl[3] = "\r\n";
# else
char m_endl[2] = "\n";
# endif
int m_file = -1;
int64_t m_pos = 0;
};