]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Add some more utilities
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 15 Oct 2022 14:33:53 +0000 (15:33 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 15 Oct 2022 14:33:53 +0000 (15:33 +0100)
src/libutil/cxx/locked_file.cxx
src/libutil/cxx/locked_file.hxx

index b4d865626cafa6de2f45a2f0e270a149a8e5c84b..4c91f44ed560a4a6e818a9386f788b7a58963f64 100644 (file)
@@ -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)
 {
index 8690cfb64c685acc2e0c5055579dc8ea1bce3534..c7d286cbb725e2c19c3576777286afa4c721a7a3 100644 (file)
@@ -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;