mirror of
https://github.com/monero-project/monero.git
synced 2024-12-23 03:59:33 +00:00
Add drop_and_recreate in privatefile class.
When creating a private file we need to delete the file if exist.
This commit is contained in:
parent
c8214782fb
commit
267e31f5b7
3 changed files with 26 additions and 5 deletions
|
@ -121,7 +121,7 @@ namespace tools
|
||||||
private_file::private_file(std::FILE* handle, std::string&& filename) noexcept
|
private_file::private_file(std::FILE* handle, std::string&& filename) noexcept
|
||||||
: m_handle(handle), m_filename(std::move(filename)) {}
|
: m_handle(handle), m_filename(std::move(filename)) {}
|
||||||
|
|
||||||
private_file private_file::create(std::string name)
|
private_file private_file::create(std::string name, uint32_t extra_flags)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
struct close_handle
|
struct close_handle
|
||||||
|
@ -174,7 +174,7 @@ namespace tools
|
||||||
name.c_str(),
|
name.c_str(),
|
||||||
GENERIC_WRITE, FILE_SHARE_READ,
|
GENERIC_WRITE, FILE_SHARE_READ,
|
||||||
std::addressof(attributes),
|
std::addressof(attributes),
|
||||||
CREATE_NEW, (FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE),
|
CREATE_NEW, (FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | extra_flags),
|
||||||
nullptr
|
nullptr
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
@ -193,7 +193,7 @@ namespace tools
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
const int fdr = open(name.c_str(), (O_RDONLY | O_CREAT), S_IRUSR);
|
const int fdr = open(name.c_str(), (O_RDONLY | O_CREAT | extra_flags), S_IRUSR);
|
||||||
if (0 <= fdr)
|
if (0 <= fdr)
|
||||||
{
|
{
|
||||||
struct stat rstats = {};
|
struct stat rstats = {};
|
||||||
|
@ -224,6 +224,23 @@ namespace tools
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private_file private_file::drop_and_recreate(std::string filename)
|
||||||
|
{
|
||||||
|
if (epee::file_io_utils::is_file_exist(filename)) {
|
||||||
|
boost::system::error_code ec{};
|
||||||
|
boost::filesystem::remove(filename, ec);
|
||||||
|
if (ec) {
|
||||||
|
MERROR("Failed to remove " << filename << ": " << ec.message());
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef WIN32
|
||||||
|
return create(filename);
|
||||||
|
#else
|
||||||
|
return create(filename, O_EXCL);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
private_file::~private_file() noexcept
|
private_file::~private_file() noexcept
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
|
@ -80,7 +80,11 @@ namespace tools
|
||||||
|
|
||||||
/*! \return File only readable by owner and only used by this process
|
/*! \return File only readable by owner and only used by this process
|
||||||
OR `private_file{}` on error. */
|
OR `private_file{}` on error. */
|
||||||
static private_file create(std::string filename);
|
static private_file create(std::string filename, uint32_t extra_flags = 0);
|
||||||
|
|
||||||
|
/*! \return Drop and create file only readable by owner and only used
|
||||||
|
by this process OR `private_file{}` on error. */
|
||||||
|
static private_file drop_and_recreate(std::string filename);
|
||||||
|
|
||||||
private_file(private_file&&) = default;
|
private_file(private_file&&) = default;
|
||||||
private_file& operator=(private_file&&) = default;
|
private_file& operator=(private_file&&) = default;
|
||||||
|
|
|
@ -245,7 +245,7 @@ namespace tools
|
||||||
);
|
);
|
||||||
|
|
||||||
std::string temp = "monero-wallet-rpc." + bind_port + ".login";
|
std::string temp = "monero-wallet-rpc." + bind_port + ".login";
|
||||||
rpc_login_file = tools::private_file::create(temp);
|
rpc_login_file = tools::private_file::drop_and_recreate(temp);
|
||||||
if (!rpc_login_file.handle())
|
if (!rpc_login_file.handle())
|
||||||
{
|
{
|
||||||
LOG_ERROR(tr("Failed to create file ") << temp << tr(". Check permissions or remove file"));
|
LOG_ERROR(tr("Failed to create file ") << temp << tr(". Check permissions or remove file"));
|
||||||
|
|
Loading…
Reference in a new issue