summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ingestor.cpp20
-rw-r--r--src/util.cpp11
-rw-r--r--src/util.hpp4
3 files changed, 26 insertions, 9 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp
index 200397b..d15940c 100644
--- a/src/ingestor.cpp
+++ b/src/ingestor.cpp
@@ -5,7 +5,9 @@
#include <connection.h>
#include <csignal>
#include <dbTypes.h>
+#include <fstream>
#include <modifycommand.h>
+#include <print>
#include <ranges>
#include <scn/scan.h>
#include <selectcommand.h>
@@ -451,18 +453,18 @@ namespace WebStat {
if (lines.empty()) {
return std::unexpected(0);
}
- const std::filesystem::path path {
- settings.fallbackDir / std::format("parked-{:s}.short", makeHash(lines.front()) | TO_HEX_RANGE)};
- if (auto parked = FilePtr(fopen(path.c_str(), "w"))) {
- fprintf(parked.get(), "%zu\n", lines.size());
+ const std::filesystem::path path
+ = settings.fallbackDir / std::format("parked-{:s}.short", makeHash(lines.front()) | TO_HEX_RANGE);
+ if (std::ofstream parked {path}; parked.good()) {
+ std::println(parked, "{}", lines.size());
for (const auto & line : lines) {
- fprintf(parked.get(), "%.*s\n", static_cast<int>(line.length()), line.data());
+ std::println(parked, "{}", line);
}
- if (fflush(parked.get()) == 0) {
+ if (parked.flush().good()) {
lines.clear();
- auto finalPath = std::filesystem::path {path}.replace_extension(".log");
- parked.reset();
- if (rename(path.c_str(), finalPath.c_str()) == 0) {
+ auto finalPath = auto {path}.replace_extension(".log");
+ parked.close();
+ if (!renameNoExcept(path, finalPath)) {
return finalPath;
}
}
diff --git a/src/util.cpp b/src/util.cpp
new file mode 100644
index 0000000..cdce30e
--- /dev/null
+++ b/src/util.cpp
@@ -0,0 +1,11 @@
+#include "util.hpp"
+
+namespace WebStat {
+ std::error_code
+ renameNoExcept(const std::filesystem::path & path, const std::filesystem::path & finalPath) noexcept
+ {
+ std::error_code error;
+ std::filesystem::rename(path, finalPath, error);
+ return error;
+ }
+}
diff --git a/src/util.hpp b/src/util.hpp
index 5cac5a3..d5e2482 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -2,6 +2,7 @@
#include <chrono>
#include <command.h>
+#include <filesystem>
#include <scn/scan.h>
#include <shared_mutex>
#include <tuple>
@@ -144,4 +145,7 @@ namespace WebStat {
ValueType value;
mutable MutexType mutex;
};
+
+ std::error_code renameNoExcept(
+ const std::filesystem::path & path, const std::filesystem::path & finalPath) noexcept;
}