aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/cxx/file_util.cxx
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2023-07-26 10:49:23 +0100
committerVsevolod Stakhov <vsevolod@rspamd.com>2023-07-26 10:49:23 +0100
commit537a7180a0d5132c11636c4fd8b1450cd99d352c (patch)
treefb9f8c84955a411bdffbd6371ea32f2716fb3687 /src/libutil/cxx/file_util.cxx
parent5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff)
downloadrspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz
rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'src/libutil/cxx/file_util.cxx')
-rw-r--r--src/libutil/cxx/file_util.cxx296
1 files changed, 154 insertions, 142 deletions
diff --git a/src/libutil/cxx/file_util.cxx b/src/libutil/cxx/file_util.cxx
index bc9028aa8..9baf062a5 100644
--- a/src/libutil/cxx/file_util.cxx
+++ b/src/libutil/cxx/file_util.cxx
@@ -32,7 +32,7 @@ auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, er
#endif
if (fname == nullptr) {
- return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL});
+ return tl::make_unexpected(error{"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL});
}
auto fd = ::open(fname, oflags);
@@ -44,7 +44,7 @@ auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, er
auto ret = raii_file{fname, fd, false};
if (fstat(ret.fd, &ret.st) == -1) {
- return tl::make_unexpected(error {fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno});
+ return tl::make_unexpected(error{fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno});
}
return ret;
@@ -52,13 +52,13 @@ auto raii_file::open(const char *fname, int flags) -> tl::expected<raii_file, er
auto raii_file::create(const char *fname, int flags, int perms) -> tl::expected<raii_file, error>
{
- int oflags = flags|O_CREAT;
+ int oflags = flags | O_CREAT;
#ifdef O_CLOEXEC
oflags |= O_CLOEXEC;
#endif
if (fname == nullptr) {
- return tl::make_unexpected(error {"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL});
+ return tl::make_unexpected(error{"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL});
}
auto fd = ::open(fname, oflags, perms);
@@ -83,19 +83,19 @@ auto raii_file::create_temp(const char *fname, int flags, int perms) -> tl::expe
oflags |= O_CLOEXEC | O_CREAT | O_EXCL;
#endif
if (fname == nullptr) {
- return tl::make_unexpected(error {"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL});
+ return tl::make_unexpected(error{"cannot open file; filename is nullptr", EINVAL, error_category::CRITICAL});
}
auto fd = ::open(fname, oflags, perms);
if (fd == -1) {
- return tl::make_unexpected(error {fmt::format("cannot create file {}: {}", fname, ::strerror(errno)), errno});
+ return tl::make_unexpected(error{fmt::format("cannot create file {}: {}", fname, ::strerror(errno)), errno});
}
auto ret = raii_file{fname, fd, true};
if (fstat(ret.fd, &ret.st) == -1) {
- return tl::make_unexpected(error {fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno});
+ return tl::make_unexpected(error{fmt::format("cannot stat file {}: {}", fname, ::strerror(errno)), errno});
}
return ret;
@@ -108,8 +108,7 @@ auto raii_file::mkstemp(const char *pattern, int flags, int perms) -> tl::expect
oflags |= O_CLOEXEC | O_CREAT | O_EXCL;
#endif
if (pattern == nullptr) {
- return tl::make_unexpected(error {"cannot open file; pattern is nullptr", EINVAL, error_category::CRITICAL});
-
+ return tl::make_unexpected(error{"cannot open file; pattern is nullptr", EINVAL, error_category::CRITICAL});
}
std::string mutable_pattern = pattern;
@@ -117,14 +116,15 @@ auto raii_file::mkstemp(const char *pattern, int flags, int perms) -> tl::expect
auto fd = g_mkstemp_full(mutable_pattern.data(), oflags, perms);
if (fd == -1) {
- return tl::make_unexpected(error {fmt::format("cannot create file {}: {}", pattern, ::strerror(errno)), errno});
+ return tl::make_unexpected(error{fmt::format("cannot create file {}: {}", pattern, ::strerror(errno)), errno});
}
auto ret = raii_file{mutable_pattern.c_str(), fd, true};
if (fstat(ret.fd, &ret.st) == -1) {
- return tl::make_unexpected(error { fmt::format("cannot stat file {}: {}",
- mutable_pattern, ::strerror(errno)), errno} );
+ return tl::make_unexpected(error{fmt::format("cannot stat file {}: {}",
+ mutable_pattern, ::strerror(errno)),
+ errno});
}
return ret;
@@ -134,7 +134,7 @@ raii_file::~raii_file() noexcept
{
if (fd != -1) {
if (temp) {
- (void)unlink(fname.c_str());
+ (void) unlink(fname.c_str());
}
close(fd);
}
@@ -145,7 +145,8 @@ auto raii_file::update_stat() noexcept -> bool
return fstat(fd, &st) != -1;
}
-raii_file::raii_file(const char *fname, int fd, bool temp) : fd(fd), temp(temp)
+raii_file::raii_file(const char *fname, int fd, bool temp)
+ : fd(fd), temp(temp)
{
std::size_t nsz;
@@ -167,22 +168,23 @@ auto raii_locked_file::lock_raii_file(raii_file &&unlocked) -> tl::expected<raii
{
if (!rspamd_file_lock(unlocked.get_fd(), TRUE)) {
return tl::make_unexpected(
- error { fmt::format("cannot lock file {}: {}", unlocked.get_name(), ::strerror(errno)), errno});
+ error{fmt::format("cannot lock file {}: {}", unlocked.get_name(), ::strerror(errno)), errno});
}
return raii_locked_file{std::move(unlocked)};
}
-auto raii_locked_file::unlock() -> raii_file {
+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))};
+ return raii_file{static_cast<raii_file &&>(std::move(*this))};
}
raii_mmaped_file::raii_mmaped_file(raii_file &&file, void *map, std::size_t sz)
- : file(std::move(file)), map(map), map_size(sz)
+ : file(std::move(file)), map(map), map_size(sz)
{
}
@@ -192,21 +194,22 @@ auto raii_mmaped_file::mmap_shared(raii_file &&file,
void *map;
if (file.get_stat().st_size < offset || offset < 0) {
- return tl::make_unexpected(error {
+ return tl::make_unexpected(error{
fmt::format("cannot mmap file {} due to incorrect offset; offset={}, size={}",
- file.get_name(), offset, file.get_size()), EINVAL});
+ file.get_name(), offset, file.get_size()),
+ EINVAL});
}
/* Update stat on file to ensure it is up-to-date */
file.update_stat();
map = mmap(nullptr, (std::size_t)(file.get_size() - offset), flags, MAP_SHARED, file.get_fd(), offset);
if (map == MAP_FAILED) {
- return tl::make_unexpected(error { fmt::format("cannot mmap file {}: {}",
- file.get_name(), ::strerror(errno)), errno });
-
+ return tl::make_unexpected(error{fmt::format("cannot mmap file {}: {}",
+ file.get_name(), ::strerror(errno)),
+ errno});
}
- return raii_mmaped_file{std::move(file), map, (std::size_t)(file.get_size() - offset)};
+ return raii_mmaped_file{std::move(file), map, (std::size_t)(file.get_size() - offset)};
}
auto raii_mmaped_file::mmap_shared(const char *fname, int open_flags,
@@ -229,7 +232,7 @@ raii_mmaped_file::~raii_mmaped_file()
}
raii_mmaped_file::raii_mmaped_file(raii_mmaped_file &&other) noexcept
- : file(std::move(other.file))
+ : file(std::move(other.file))
{
std::swap(map, other.map);
std::swap(map_size, other.map_size);
@@ -239,7 +242,7 @@ auto raii_file_sink::create(const char *fname, int flags, int perms,
const char *suffix) -> tl::expected<raii_file_sink, error>
{
if (!fname || !suffix) {
- return tl::make_unexpected(error {"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL});
+ return tl::make_unexpected(error{"cannot create file; filename is nullptr", EINVAL, error_category::CRITICAL});
}
auto tmp_fname = fmt::format("{}.{}", fname, suffix);
@@ -277,34 +280,37 @@ raii_file_sink::~raii_file_sink()
}
raii_file_sink::raii_file_sink(raii_locked_file &&_file, const char *_output, std::string &&_tmp_fname)
- : file(std::move(_file)), output_fname(_output), tmp_fname(std::move(_tmp_fname)), success(false)
+ : file(std::move(_file)), output_fname(_output), tmp_fname(std::move(_tmp_fname)), success(false)
{
}
raii_file_sink::raii_file_sink(raii_file_sink &&other) noexcept
- : file(std::move(other.file)),
- output_fname(std::move(other.output_fname)),
- tmp_fname(std::move(other.tmp_fname)),
- success(other.success)
+ : file(std::move(other.file)),
+ output_fname(std::move(other.output_fname)),
+ tmp_fname(std::move(other.tmp_fname)),
+ success(other.success)
{
}
namespace tests {
template<class T>
-static auto test_read_file(const T& f) {
+static auto test_read_file(const T &f)
+{
auto fd = f.get_fd();
- (void)::lseek(fd, 0, SEEK_SET);
- std::string buf('\0', (std::size_t)f.get_size());
+ (void) ::lseek(fd, 0, SEEK_SET);
+ std::string buf('\0', (std::size_t) f.get_size());
::read(fd, buf.data(), buf.size());
return buf;
}
template<class T>
-static auto test_write_file(const T& f, const std::string_view &buf) {
+static auto test_write_file(const T &f, const std::string_view &buf)
+{
auto fd = f.get_fd();
- (void)::lseek(fd, 0, SEEK_SET);
+ (void) ::lseek(fd, 0, SEEK_SET);
return ::write(fd, buf.data(), buf.size());
}
-auto random_fname(std::string_view extension) {
+auto random_fname(std::string_view extension)
+{
const auto *tmpdir = getenv("TMPDIR");
if (tmpdir == nullptr) {
tmpdir = G_DIR_SEPARATOR_S "tmp";
@@ -315,7 +321,7 @@ auto random_fname(std::string_view extension) {
unsigned char hexbuf[32];
rspamd_random_hex(hexbuf, sizeof(hexbuf));
- out_fname.append((const char *)hexbuf, sizeof(hexbuf));
+ out_fname.append((const char *) hexbuf, sizeof(hexbuf));
if (!extension.empty()) {
out_fname.append(".");
out_fname.append(extension);
@@ -323,123 +329,129 @@ auto random_fname(std::string_view extension) {
return out_fname;
}
-TEST_SUITE("loked files utils") {
+TEST_SUITE("loked files utils")
+{
-TEST_CASE("create and delete file") {
- auto fname = random_fname("tmp");
+ TEST_CASE("create and delete file")
{
- auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600);
- CHECK(raii_locked_file.has_value());
- CHECK(raii_locked_file.value().get_extension() == "tmp");
- CHECK(::access(fname.c_str(), R_OK) == 0);
- }
- // File must be deleted after this call
- auto ret = ::access(fname.c_str(), R_OK);
- auto serrno = errno;
- CHECK(ret == -1);
- CHECK(serrno == ENOENT);
- // Create one more time
+ auto fname = random_fname("tmp");
+ {
+ auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600);
+ CHECK(raii_locked_file.has_value());
+ CHECK(raii_locked_file.value().get_extension() == "tmp");
+ CHECK(::access(fname.c_str(), R_OK) == 0);
+ }
+ // File must be deleted after this call
+ auto ret = ::access(fname.c_str(), R_OK);
+ auto serrno = errno;
+ CHECK(ret == -1);
+ CHECK(serrno == ENOENT);
+ // Create one more time
+ {
+ auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600);
+ CHECK(raii_locked_file.has_value());
+ CHECK(::access(fname.c_str(), R_OK) == 0);
+ }
+ ret = ::access(fname.c_str(), R_OK);
+ serrno = errno;
+ CHECK(ret == -1);
+ CHECK(serrno == ENOENT);
+ }
+
+ TEST_CASE("check lock")
{
- auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600);
- CHECK(raii_locked_file.has_value());
- CHECK(::access(fname.c_str(), R_OK) == 0);
- }
- ret = ::access(fname.c_str(), R_OK);
- serrno = errno;
- CHECK(ret == -1);
- CHECK(serrno == ENOENT);
-}
+ auto fname = random_fname("");
+ {
+ auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600);
+ CHECK(raii_locked_file.has_value());
+ CHECK(raii_locked_file.value().get_extension() == "");
+ CHECK(::access(fname.c_str(), R_OK) == 0);
+ auto raii_locked_file2 = raii_locked_file::open(fname.c_str(), O_RDONLY);
+ CHECK(!raii_locked_file2.has_value());
+ CHECK(::access(fname.c_str(), R_OK) == 0);
+ }
+ // File must be deleted after this call
+ auto ret = ::access(fname.c_str(), R_OK);
+ auto serrno = errno;
+ CHECK(ret == -1);
+ CHECK(serrno == ENOENT);
+ }
-TEST_CASE("check lock") {
- auto fname = random_fname("");
+ auto get_tmpdir()->std::string
{
- auto raii_locked_file = raii_locked_file::create_temp(fname.c_str(), O_RDONLY, 00600);
- CHECK(raii_locked_file.has_value());
- CHECK(raii_locked_file.value().get_extension() == "");
- CHECK(::access(fname.c_str(), R_OK) == 0);
- auto raii_locked_file2 = raii_locked_file::open(fname.c_str(), O_RDONLY);
- CHECK(!raii_locked_file2.has_value());
- CHECK(::access(fname.c_str(), R_OK) == 0);
- }
- // File must be deleted after this call
- auto ret = ::access(fname.c_str(), R_OK);
- auto serrno = errno;
- CHECK(ret == -1);
- CHECK(serrno == ENOENT);
-}
+ const auto *tmpdir = getenv("TMPDIR");
+ if (tmpdir == nullptr) {
+ tmpdir = G_DIR_SEPARATOR_S "tmp";
+ }
-auto get_tmpdir() -> std::string {
- const auto *tmpdir = getenv("TMPDIR");
- if (tmpdir == nullptr) {
- tmpdir = G_DIR_SEPARATOR_S "tmp";
- }
+ std::size_t sz;
+ std::string mut_fname = tmpdir;
+ rspamd_normalize_path_inplace(mut_fname.data(), mut_fname.size(), &sz);
+ mut_fname.resize(sz);
- std::size_t sz;
- std::string mut_fname = tmpdir;
- rspamd_normalize_path_inplace(mut_fname.data(), mut_fname.size(), &sz);
- mut_fname.resize(sz);
+ if (!mut_fname.ends_with(G_DIR_SEPARATOR)) {
+ mut_fname += G_DIR_SEPARATOR;
+ }
- if (!mut_fname.ends_with(G_DIR_SEPARATOR)) {
- mut_fname += G_DIR_SEPARATOR;
+ return mut_fname;
}
- return mut_fname;
-}
-
-TEST_CASE("tempfile") {
- std::string tmpname;
- const std::string tmpdir{get_tmpdir()};
+ TEST_CASE("tempfile")
{
- auto raii_locked_file = raii_locked_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(),
- O_RDONLY, 00600);
- CHECK(raii_locked_file.has_value());
- CHECK(raii_locked_file.value().get_dir() == tmpdir);
- CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0);
- auto raii_locked_file2 = raii_locked_file::open(raii_locked_file.value().get_name().data(), O_RDONLY);
- CHECK(!raii_locked_file2.has_value());
- CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0);
- tmpname = raii_locked_file.value().get_name();
- }
- // File must be deleted after this call
- auto ret = ::access(tmpname.c_str(), R_OK);
- auto serrno = errno;
- CHECK(ret == -1);
- CHECK(serrno == ENOENT);
-}
+ std::string tmpname;
+ const std::string tmpdir{get_tmpdir()};
+ {
+ auto raii_locked_file = raii_locked_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(),
+ O_RDONLY, 00600);
+ CHECK(raii_locked_file.has_value());
+ CHECK(raii_locked_file.value().get_dir() == tmpdir);
+ CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0);
+ auto raii_locked_file2 = raii_locked_file::open(raii_locked_file.value().get_name().data(), O_RDONLY);
+ CHECK(!raii_locked_file2.has_value());
+ CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0);
+ tmpname = raii_locked_file.value().get_name();
+ }
+ // File must be deleted after this call
+ auto ret = ::access(tmpname.c_str(), R_OK);
+ auto serrno = errno;
+ CHECK(ret == -1);
+ CHECK(serrno == ENOENT);
+ }
-TEST_CASE("mmap") {
- std::string tmpname;
- const std::string tmpdir{get_tmpdir()};
+ TEST_CASE("mmap")
{
- auto raii_file = raii_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(),
- O_RDWR|O_CREAT|O_EXCL, 00600);
- CHECK(raii_file.has_value());
- CHECK(raii_file->get_dir() == tmpdir);
- CHECK(access(raii_file->get_name().data(), R_OK) == 0);
- tmpname = std::string{raii_file->get_name()};
- char payload[] = {'1', '2', '3'};
- CHECK(write(raii_file->get_fd(), payload, sizeof(payload)) == sizeof(payload));
- auto mmapped_file1 = raii_mmaped_file::mmap_shared(std::move(raii_file.value()), PROT_READ|PROT_WRITE);
- CHECK(mmapped_file1.has_value());
- CHECK(!raii_file->is_valid());
- CHECK(mmapped_file1->get_size() == sizeof(payload));
- CHECK(memcmp(mmapped_file1->get_map(), payload, sizeof(payload)) == 0);
- *(char *)mmapped_file1->get_map() = '2';
- auto mmapped_file2 = raii_mmaped_file::mmap_shared(tmpname.c_str(), O_RDONLY, PROT_READ);
- CHECK(mmapped_file2.has_value());
- CHECK(mmapped_file2->get_size() == sizeof(payload));
- CHECK(memcmp(mmapped_file2->get_map(), payload, sizeof(payload)) != 0);
- CHECK(memcmp(mmapped_file2->get_map(), mmapped_file1->get_map(), sizeof(payload)) == 0);
- }
- // File must be deleted after this call
- auto ret = ::access(tmpname.c_str(), R_OK);
- auto serrno = errno;
- CHECK(ret == -1);
- CHECK(serrno == ENOENT);
-}
+ std::string tmpname;
+ const std::string tmpdir{get_tmpdir()};
+ {
+ auto raii_file = raii_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(),
+ O_RDWR | O_CREAT | O_EXCL, 00600);
+ CHECK(raii_file.has_value());
+ CHECK(raii_file->get_dir() == tmpdir);
+ CHECK(access(raii_file->get_name().data(), R_OK) == 0);
+ tmpname = std::string{raii_file->get_name()};
+ char payload[] = {'1', '2', '3'};
+ CHECK(write(raii_file->get_fd(), payload, sizeof(payload)) == sizeof(payload));
+ auto mmapped_file1 = raii_mmaped_file::mmap_shared(std::move(raii_file.value()), PROT_READ | PROT_WRITE);
+ CHECK(mmapped_file1.has_value());
+ CHECK(!raii_file->is_valid());
+ CHECK(mmapped_file1->get_size() == sizeof(payload));
+ CHECK(memcmp(mmapped_file1->get_map(), payload, sizeof(payload)) == 0);
+ *(char *) mmapped_file1->get_map() = '2';
+ auto mmapped_file2 = raii_mmaped_file::mmap_shared(tmpname.c_str(), O_RDONLY, PROT_READ);
+ CHECK(mmapped_file2.has_value());
+ CHECK(mmapped_file2->get_size() == sizeof(payload));
+ CHECK(memcmp(mmapped_file2->get_map(), payload, sizeof(payload)) != 0);
+ CHECK(memcmp(mmapped_file2->get_map(), mmapped_file1->get_map(), sizeof(payload)) == 0);
+ }
+ // File must be deleted after this call
+ auto ret = ::access(tmpname.c_str(), R_OK);
+ auto serrno = errno;
+ CHECK(ret == -1);
+ CHECK(serrno == ENOENT);
+ }
-} // TEST_SUITE
+}// TEST_SUITE
-} // namespace tests
+}// namespace tests
-} // namespace rspamd::util
+}// namespace rspamd::util