aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorluigi1111 <luigi1111w@gmail.com>2024-05-20 23:37:41 -0500
committerluigi1111 <luigi1111w@gmail.com>2024-05-20 23:37:41 -0500
commit19fa7dceac6de9a2c8c5f6231284a26a5bc60ed0 (patch)
treecd84d2e098035650ddedf7c96f632326797d77a0 /src/common
parentf48fc72fb6ba7def014a68044300fdb86be8603c (diff)
parent100a7d06eee6d3f60a1c2d00af53e4665c5f5690 (diff)
downloadmonzero-core-19fa7dceac6de9a2c8c5f6231284a26a5bc60ed0.tar.gz
monzero-core-19fa7dceac6de9a2c8c5f6231284a26a5bc60ed0.tar.xz
monzero-core-19fa7dceac6de9a2c8c5f6231284a26a5bc60ed0.zip
Merge pull request #9292
100a7d0 Add drop_and_recreate in privatefile class. When creating a private file we need to delete the file if exist. (0xFFFC0000)
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.cpp23
-rw-r--r--src/common/util.h6
2 files changed, 25 insertions, 4 deletions
diff --git a/src/common/util.cpp b/src/common/util.cpp
index 4b5e2adb8..829f4605c 100644
--- a/src/common/util.cpp
+++ b/src/common/util.cpp
@@ -122,7 +122,7 @@ namespace tools
private_file::private_file(std::FILE* handle, std::string&& filename) noexcept
: 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
struct close_handle
@@ -175,7 +175,7 @@ namespace tools
name.c_str(),
GENERIC_WRITE, FILE_SHARE_READ,
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
)
};
@@ -194,7 +194,7 @@ namespace tools
}
}
#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)
{
struct stat rstats = {};
@@ -225,6 +225,23 @@ namespace tools
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
{
try
diff --git a/src/common/util.h b/src/common/util.h
index f489594e8..35b897017 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -80,7 +80,11 @@ namespace tools
/*! \return File only readable by owner and only used by this process
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& operator=(private_file&&) = default;