From aaccef79b94b624fbe91227dda40bb4331fee1ef Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 15 Oct 2022 15:33:53 +0100 Subject: [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(-) (limited to 'src/libutil') 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_file { + if (fd != -1) { + (void) rspamd_file_unlock(fd, FALSE); + } + + return raii_file{static_cast(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; @@ -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 { @@ -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(std::move(other))) {} /* Do not allow copy/default ctor */ const raii_locked_file& operator=(const raii_locked_file &other) = delete; -- cgit v1.2.3