From 353e3123f363405978de1472376f8a90d5ab916e Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Mon, 16 Oct 2023 20:29:24 +0200 Subject: [PATCH] file saving and managing --- Makefile | 2 +- mail.h | 30 ++++++++++++++++++++++++++---- server.cpp | 33 +++++++++++++++++++++++++++++++++ user.cpp | 1 + user.h | 4 ++-- user_handler.h | 1 + 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 75fbf26..072b595 100644 --- a/Makefile +++ b/Makefile @@ -25,4 +25,4 @@ $(BUILD_DIR)/%.o: %.cpp $(CC) $(CFLAGS) -c $< -o $@ clean: - rm -f twmailer-client twmailer-server \ No newline at end of file + rm -rf $(BUILD_DIR) twmailer-client twmailer-server \ No newline at end of file diff --git a/mail.h b/mail.h index 5a57f2c..48f19e1 100644 --- a/mail.h +++ b/mail.h @@ -1,21 +1,43 @@ #pragma once -#include +#include "user_handler.h" + #include #include +#include +#include + +namespace fs = std::filesystem; struct mail { std::string filename; /* metadata */ + u_int id; int64_t timestamp; std::string sender; - std::vector receipient; + std::vector recipients; std::string subject; - const bool operator<(const mail& left) { + bool operator()(const u_int& id) const { + return id == this->id; + } + + bool operator<(const mail& left) const { return this->timestamp < left.timestamp; } - void remove(); + fs::path getPath() { + if (this->filename.empty()) + return fs::path(); + return fs::path(this->filename.insert(2, "/")); + }; + + void remove() { + if (this->filename.empty()) + return; + std::remove((user_handler::getInstance()->getSpoolDir()/"objects"/fs::path(this->filename.insert(2, "/"))).c_str()); + + this->filename = ""; + }; }; \ No newline at end of file diff --git a/server.cpp b/server.cpp index 17bdc39..d66b0fd 100644 --- a/server.cpp +++ b/server.cpp @@ -6,6 +6,10 @@ #include #include #include +#include + +#include +#include #include #include @@ -31,6 +35,9 @@ int new_socket = -1; void printUsage(); inline bool isInteger(const std::string & s); +void saveToFile(fs::path object_dir, std::string message); +std::string get_sha1(const std::string& p_arg); + // from myserver.c void *clientCommunication(void *data); void signalHandler(int sig); @@ -281,3 +288,29 @@ void signalHandler(int sig) exit(sig); } } + +void saveToFile(fs::path object_dir, std::string message) +{ + std::string sha1 = get_sha1(message); + std::ofstream ofs(object_dir/sha1); // possible issues with path length or file length limitations + ofs << message; +} + +// https://stackoverflow.com/questions/28489153/how-to-portably-compute-a-sha1-hash-in-c +std::string get_sha1(const std::string& p_arg) +{ + boost::uuids::detail::sha1 sha1; + sha1.process_bytes(p_arg.data(), p_arg.size()); + unsigned hash[5] = {0}; + sha1.get_digest(hash); + + // Back to string + char buf[41] = {0}; + + for (int i = 0; i < 5; i++) + { + std::sprintf(buf + (i << 3), "%08x", hash[i]); + } + + return std::string(buf); +} \ No newline at end of file diff --git a/user.cpp b/user.cpp index 563b8b5..95f853d 100644 --- a/user.cpp +++ b/user.cpp @@ -1,5 +1,6 @@ #include "user.h" #include "user_handler.h" +#include "mail.h" #include #include diff --git a/user.h b/user.h index 4e818eb..59c75f8 100644 --- a/user.h +++ b/user.h @@ -1,7 +1,5 @@ #pragma once -#include "mail.h" - #include #include #include @@ -11,6 +9,8 @@ namespace fs = std::filesystem; using json = nlohmann::json; +struct mail; + template static const bool ptr_cmp(T* left, T* right) { return *left < *right; }; diff --git a/user_handler.h b/user_handler.h index be5a5f8..691edb4 100644 --- a/user_handler.h +++ b/user_handler.h @@ -25,6 +25,7 @@ public: ~user_handler(); void setSpoolDir(fs::path p) { this->spool_dir = p; }; + fs::path getSpoolDir() { return this->spool_dir; }; user* getUser(std::string name) { return this->users[name]; }; private: