From bbf604675e12c9ccd557c704dde940b62d449c17 Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Thu, 4 Jun 2020 10:55:43 +0200 Subject: [PATCH] Merge buffer and writer utils --- core-buffer/include/BufferUtils.hpp | 12 +------ core-buffer/src/BufferUtils.cpp | 36 +++++-------------- sf-buffer/include/WriterUtils.hpp | 11 ------ sf-buffer/src/BufferBinaryWriter.cpp | 3 +- sf-buffer/src/WriterUtils.cpp | 52 ---------------------------- 5 files changed, 10 insertions(+), 104 deletions(-) delete mode 100644 sf-buffer/include/WriterUtils.hpp delete mode 100644 sf-buffer/src/WriterUtils.cpp diff --git a/core-buffer/include/BufferUtils.hpp b/core-buffer/include/BufferUtils.hpp index a3feabb..5353ff0 100644 --- a/core-buffer/include/BufferUtils.hpp +++ b/core-buffer/include/BufferUtils.hpp @@ -17,17 +17,7 @@ namespace BufferUtils const std::string& latest_filename, const std::string& filename_to_write); - std::string get_latest_file(const std::string& latest_filename); - - struct path_sufix { - uint64_t start_pulse_id; - uint64_t stop_pulse_id; - std::string path; - }; - - std::vector get_path_suffixes( - const uint64_t start_pulse_id, - const uint64_t stop_pulse_id); + void create_destination_folder(const std::string& output_file); } #endif //BUFFER_UTILS_HPP diff --git a/core-buffer/src/BufferUtils.cpp b/core-buffer/src/BufferUtils.cpp index 3c92b34..de7ad8f 100644 --- a/core-buffer/src/BufferUtils.cpp +++ b/core-buffer/src/BufferUtils.cpp @@ -1,6 +1,6 @@ #include "BufferUtils.hpp" + #include -#include #include using namespace std; @@ -48,36 +48,16 @@ void BufferUtils::update_latest_file( system(str_latest_command.c_str()); } -string BufferUtils::get_latest_file(const string& latest_filename) +void BufferUtils::create_destination_folder(const string& output_file) { - std::ifstream latest_input_file; - latest_input_file.open(latest_filename); + auto file_separator_index = output_file.rfind('/'); - std::stringstream strStream; - strStream << latest_input_file.rdbuf(); - std::string filename = strStream.str(); + if (file_separator_index != string::npos) { - return filename.substr(0, filename.size()-1); -} + string output_folder(output_file.substr(0, file_separator_index)); -vector BufferUtils::get_path_suffixes ( - const uint64_t start_pulse_id, - const uint64_t stop_pulse_id) -{ - vector result; - - uint64_t start_file_base = start_pulse_id / core_buffer::FILE_MOD; - start_file_base *= core_buffer::FILE_MOD; - - for (uint64_t first_pulse_id=start_file_base; - first_pulse_id <= stop_pulse_id; - first_pulse_id += core_buffer::FILE_MOD) { - - result.emplace_back( - {first_pulse_id, - first_pulse_id + core_buffer::FILE_MOD - 1, - get_filename("", "", first_pulse_id)}); + // TODO: filesystem::create_directories(output_folder) + string create_folder_command("mkdir -p " + output_folder); + system(create_folder_command.c_str()); } - - return result; } \ No newline at end of file diff --git a/sf-buffer/include/WriterUtils.hpp b/sf-buffer/include/WriterUtils.hpp deleted file mode 100644 index 266518f..0000000 --- a/sf-buffer/include/WriterUtils.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef WRITERUTILS_H -#define WRITERUTILS_H - -#include - -namespace WriterUtils { - void set_process_effective_id(int user_id); - void create_destination_folder(const std::string& output_file); -} - -#endif // WRITERUTILS_H diff --git a/sf-buffer/src/BufferBinaryWriter.cpp b/sf-buffer/src/BufferBinaryWriter.cpp index 6e189a6..1553a82 100644 --- a/sf-buffer/src/BufferBinaryWriter.cpp +++ b/sf-buffer/src/BufferBinaryWriter.cpp @@ -9,7 +9,6 @@ #include #include "BufferUtils.hpp" -#include "WriterUtils.hpp" using namespace std; @@ -81,7 +80,7 @@ void BufferBinaryWriter::open_file(const std::string& filename) { close_current_file(); - WriterUtils::create_destination_folder(filename); + BufferUtils::create_destination_folder(filename); output_file_fd_ = ::open(filename.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); diff --git a/sf-buffer/src/WriterUtils.cpp b/sf-buffer/src/WriterUtils.cpp deleted file mode 100644 index 9b8d51a..0000000 --- a/sf-buffer/src/WriterUtils.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include - -#include "WriterUtils.hpp" -#include "date.h" - -using namespace std; - -void WriterUtils::set_process_effective_id(int user_id) -{ - - // TODO: use setfsuid and setfsgid - - if (setegid(user_id)) { - stringstream err_msg; - - using namespace date; - using namespace chrono; - err_msg << "[" << system_clock::now() << "]"; - err_msg << "[WriterUtils::set_process_effective_id]"; - err_msg << " Cannot set group_id to " << user_id << endl; - - throw runtime_error(err_msg.str()); - } - - if (seteuid(user_id)) { - stringstream err_msg; - - using namespace date; - using namespace chrono; - err_msg << "[" << system_clock::now() << "]"; - err_msg << "[WriterUtils::set_process_effective_id]"; - err_msg << " Cannot set user_id to " << user_id << endl; - - throw runtime_error(err_msg.str()); - } -} - -void WriterUtils::create_destination_folder(const string& output_file) -{ - auto file_separator_index = output_file.rfind('/'); - - if (file_separator_index != string::npos) { - - string output_folder(output_file.substr(0, file_separator_index)); - - // TODO: filesystem::create_directories(output_folder) - string create_folder_command("mkdir -p " + output_folder); - system(create_folder_command.c_str()); - } -}