commit aaccef7: [Minor] Add some more utilities

Vsevolod Stakhov vsevolod at rspamd.com
Sun Oct 16 14:28:03 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-10-15 15:33:53 +0100
URL: https://github.com/rspamd/rspamd/commit/aaccef79b94b624fbe91227dda40bb4331fee1ef

[Minor] Add some more utilities

---
 src/libutil/cxx/locked_file.cxx | 10 +++++++++-
 src/libutil/cxx/locked_file.hxx | 12 ++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/libutil/cxx/locked_file.cxx b/src/libutil/cxx/locked_file.cxx
index b4d865626..4c91f44ed 100644
--- a/src/libutil/cxx/locked_file.cxx
+++ b/src/libutil/cxx/locked_file.cxx
@@ -59,7 +59,7 @@ auto raii_file::create(const char *fname, int flags, int perms) -> tl::expected<
 {
 	int oflags = flags;
 #ifdef O_CLOEXEC
-	oflags |= O_CLOEXEC | O_CREAT | O_EXCL;
+	oflags |= O_CLOEXEC;
 #endif
 
 	if (fname == nullptr) {
@@ -179,6 +179,14 @@ auto raii_locked_file::lock_raii_file(raii_file &&unlocked) -> tl::expected<raii
 	return raii_locked_file{std::move(unlocked)};
 }
 
+auto raii_locked_file::unlock() -> raii_file {
+	if (fd != -1) {
+		(void) rspamd_file_unlock(fd, FALSE);
+	}
+
+	return raii_file{static_cast<raii_file&&>(std::move(*this))};
+}
+
 raii_mmaped_file::raii_mmaped_file(raii_file &&_file, void *_map)
 		: file(std::move(_file)), map(_map)
 {
diff --git a/src/libutil/cxx/locked_file.hxx b/src/libutil/cxx/locked_file.hxx
index 8690cfb64..c7d286cbb 100644
--- a/src/libutil/cxx/locked_file.hxx
+++ b/src/libutil/cxx/locked_file.hxx
@@ -28,6 +28,7 @@ namespace rspamd::util {
  * A file is unlocked and closed when not needed
  */
 struct raii_file {
+public:
 	virtual ~raii_file() noexcept;
 
 	static auto open(const char *fname, int flags) -> tl::expected<raii_file, std::string>;
@@ -92,6 +93,10 @@ struct raii_file {
 		*this = std::move(other);
 	}
 
+	auto make_immortal() noexcept {
+		temp = false;
+	}
+
 	/* Do not allow copy/default ctor */
 	const raii_file& operator=(const raii_file &other) = delete;
 	raii_file() = delete;
@@ -109,6 +114,7 @@ protected:
  * A file is unlocked and closed when not needed
  */
 struct raii_locked_file final : public raii_file {
+public:
 	~raii_locked_file() noexcept override;
 
 	static auto open(const char *fname, int flags) -> tl::expected<raii_locked_file, std::string> {
@@ -149,6 +155,12 @@ struct raii_locked_file final : public raii_file {
 		return *this;
 	}
 
+	/**
+	 * Unlock a locked file and return back unlocked file transferring ownership.
+	 * A locked file cannot be used after this method.
+	 */
+	auto unlock() -> raii_file;
+
 	raii_locked_file(raii_locked_file &&other) noexcept : raii_file(static_cast<raii_file &&>(std::move(other))) {}
 	/* Do not allow copy/default ctor */
 	const raii_locked_file& operator=(const raii_locked_file &other) = delete;


More information about the Commits mailing list