commit 600452b: [Minor] Add a simple utility to deal with locked files

Vsevolod Stakhov vsevolod at rspamd.com
Sat Apr 30 19:21:08 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-04-02 12:50:59 +0100
URL: https://github.com/rspamd/rspamd/commit/600452bacff96da2470dd70917c38e5b4de95d1c

[Minor] Add a simple utility to deal with locked files

---
 src/libutil/cxx/locked_file.cxx                    | 36 ++++++++++++++++++++++
 .../cxx/locked_file.hxx}                           | 28 +++++++++--------
 2 files changed, 52 insertions(+), 12 deletions(-)

diff --git a/src/libutil/cxx/locked_file.cxx b/src/libutil/cxx/locked_file.cxx
new file mode 100644
index 000000000..8bbb51bf3
--- /dev/null
+++ b/src/libutil/cxx/locked_file.cxx
@@ -0,0 +1,36 @@
+//
+// Created by Vsevolod Stakhov on 02/04/2022.
+//
+
+#include "locked_file.hxx"
+#include <fmt/core.h>
+#include "libutil/util.h"
+#include "libutil/unix-std.h"
+
+auto raii_locked_file::open(const char *fname, int flags) -> tl::expected<raii_locked_file, std::string>
+{
+	int oflags = flags;
+#ifdef O_CLOEXEC
+	oflags |= O_CLOEXEC;
+#endif
+	auto fd = ::open(fname, oflags);
+
+	if (fd == -1) {
+		return tl::make_unexpected(fmt::format("cannot open file {}: {}", fname, ::strerror(errno)));
+	}
+
+	if (!rspamd_file_lock(fd, FALSE)) {
+		close(fd);
+		return tl::make_unexpected(fmt::format("cannot lock file {}: {}", fname, ::strerror(errno)));
+	}
+
+	return raii_locked_file{fd};
+}
+
+raii_locked_file::~raii_locked_file()
+{
+	if (fd != -1) {
+		(void) rspamd_file_unlock(fd, FALSE);
+		close(fd);
+	}
+}
diff --git a/src/libserver/html/html_entities.hxx b/src/libutil/cxx/locked_file.hxx
similarity index 53%
copy from src/libserver/html/html_entities.hxx
copy to src/libutil/cxx/locked_file.hxx
index 68084bf92..63239fcd9 100644
--- a/src/libserver/html/html_entities.hxx
+++ b/src/libutil/cxx/locked_file.hxx
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2021 Vsevolod Stakhov
+ * Copyright 2022 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -13,19 +13,23 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-#ifndef RSPAMD_HTML_ENTITIES_H
-#define RSPAMD_HTML_ENTITIES_H
+#ifndef RSPAMD_LOCKED_FILE_HXX
+#define RSPAMD_LOCKED_FILE_HXX
 #pragma once
 
-#include <utility>
+#include "contrib/expected/expected.hpp"
 #include <string>
 
-namespace rspamd::html {
-
-auto decode_html_entitles_inplace(char *s, std::size_t len, bool norm_spaces = false) -> std::size_t ;
-auto decode_html_entitles_inplace(std::string &st) -> void;
-
-}
+/**
+ * A simple RAII object to contain a file descriptor with an flock wrap
+ * A file is unlocked and closed when not needed
+ */
+struct raii_locked_file final {
+	~raii_locked_file();
+	static auto open(const char *fname, int flags) -> tl::expected<raii_locked_file, std::string>;
+private:
+	int fd;
+	explicit raii_locked_file(int _fd) : fd(_fd) {}
+};
 
-#endif
+#endif //RSPAMD_LOCKED_FILE_HXX


More information about the Commits mailing list