add database file and file locking

This commit is contained in:
Syping 2024-02-06 16:19:08 +01:00
parent 94251d7dde
commit bb10b8ad59
8 changed files with 242 additions and 50 deletions

View file

@ -23,8 +23,10 @@ include(GNUInstallDirs)
# dtranslatebot Source files # dtranslatebot Source files
set(DTRANSLATEBOT_HEADERS set(DTRANSLATEBOT_HEADERS
src/database_core.h src/database_core.h
src/database_file.h
src/message_queue.h src/message_queue.h
src/settings.h src/settings.h
src/settings_types.h
src/slashcommands.h src/slashcommands.h
src/submit_queue.h src/submit_queue.h
src/translator_core.h src/translator_core.h
@ -33,6 +35,7 @@ set(DTRANSLATEBOT_HEADERS
) )
set(DTRANSLATEBOT_SOURCES set(DTRANSLATEBOT_SOURCES
src/database_core.cpp src/database_core.cpp
src/database_file.cpp
src/main.cpp src/main.cpp
src/message_queue.cpp src/message_queue.cpp
src/settings.cpp src/settings.cpp
@ -55,7 +58,7 @@ find_package(Threads REQUIRED)
# dtranslatebot Target + Installs # dtranslatebot Target + Installs
add_executable(dtranslatebot ${DTRANSLATEBOT_HEADERS} ${DTRANSLATEBOT_SOURCES}) add_executable(dtranslatebot ${DTRANSLATEBOT_HEADERS} ${DTRANSLATEBOT_SOURCES})
target_link_libraries(dtranslatebot Threads::Threads ${DPP_LIBRARIES}) target_link_libraries(dtranslatebot PRIVATE Threads::Threads ${DPP_LIBRARIES})
target_include_directories(dtranslatebot PRIVATE ${DPP_INCLUDE_DIR}) target_include_directories(dtranslatebot PRIVATE ${DPP_INCLUDE_DIR})
set_target_properties(dtranslatebot PROPERTIES set_target_properties(dtranslatebot PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17

View file

@ -19,8 +19,7 @@
#ifndef DATABASE_CORE_H #ifndef DATABASE_CORE_H
#define DATABASE_CORE_H #define DATABASE_CORE_H
#include <vector> #include "settings_types.h"
#include "settings.h"
namespace bot { namespace bot {
namespace database { namespace database {

118
src/database_file.cpp Normal file
View file

@ -0,0 +1,118 @@
/*****************************************************************************
* dtranslatebot Discord Translate Bot
* Copyright (C) 2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifdef __unix__
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <iostream>
#include "database_file.h"
using namespace bot::database;
file::file(const std::filesystem::path &storage_path) : m_storagePath(storage_path)
{
std::cout << "[Launch] Checking working directory..." << std::endl;
if (!std::filesystem::exists(storage_path)) {
std::cerr << "[Error] Storage directory " << storage_path << " can not be found" << std::endl;
throw std::runtime_error("Storage directory can not be found");
}
#ifdef __unix__
struct flock lock;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = getpid();
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
const std::string lock_file = storage_path / ".lock";
fd = open(lock_file.c_str(), O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1) {
std::cerr << "[Error] Storage directory " << storage_path << " can not be locked" << std::endl;
throw std::system_error(errno, std::system_category());
}
if (fcntl(fd, F_SETLK, &lock) == -1) {
std::cerr << "[Error] Storage directory " << storage_path << " can not be locked" << std::endl;
throw std::system_error(errno, std::system_category());
}
#endif
}
file::~file()
{
#ifdef __unix__
close(fd);
#endif
}
bool file::add_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const bot::settings::target &target)
{
#ifndef NDEBUG
std::cerr << "[Debug] database::add_channel_target(dpp::snowflake, dpp::snowflake, const bot::settings::target&) have being called." << std::endl;
#endif
return false;
}
std::variant<std::monostate,bot::settings::target> file::find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target)
{
#ifndef NDEBUG
std::cerr << "[Debug] database::find_channel_target(dpp::snowflake, dpp::snowflake, const std::string&) have being called." << std::endl;
#endif
return {};
}
std::vector<dpp::snowflake> file::get_channels(dpp::snowflake guild_id)
{
#ifndef NDEBUG
std::cerr << "[Debug] database::get_channels(dpp::snowflake) have being called." << std::endl;
#endif
return {};
}
std::string file::get_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id)
{
#ifndef NDEBUG
std::cerr << "[Debug] database::get_channel_source(dpp::snowflake, dpp::snowflake) have being called." << std::endl;
#endif
return {};
}
std::vector<bot::settings::target> file::get_channel_targets(dpp::snowflake guild_id, dpp::snowflake channel_id)
{
#ifndef NDEBUG
std::cerr << "[Debug] database::get_channel_targets(dpp::snowflake, dpp::snowflake) have being called." << std::endl;
#endif
return {};
}
std::vector<dpp::snowflake> file::get_guilds()
{
#ifndef NDEBUG
std::cerr << "[Debug] database::get_guilds() have being called." << std::endl;
#endif
return {};
}
bool file::set_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &source)
{
#ifndef NDEBUG
std::cerr << "[Debug] database::set_channel_source(dpp::snowflake, dpp::snowflake, const std::string&) have being called." << std::endl;
#endif
return false;
}

50
src/database_file.h Normal file
View file

@ -0,0 +1,50 @@
/*****************************************************************************
* dtranslatebot Discord Translate Bot
* Copyright (C) 2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef DATABASE_FILE_H
#define DATABASE_FILE_H
#include <mutex>
#include <filesystem>
#include "database_core.h"
namespace bot {
namespace database {
class file : public database {
public:
explicit file(const std::filesystem::path &storage_path);
~file();
bool add_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const bot::settings::target &target) override;
std::variant<std::monostate,bot::settings::target> find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) override;
std::vector<dpp::snowflake> get_channels(dpp::snowflake guild_id) override;
std::string get_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id) override;
std::vector<bot::settings::target> get_channel_targets(dpp::snowflake guild_id, dpp::snowflake channel_id) override;
std::vector<dpp::snowflake> get_guilds() override;
bool set_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &source) override;
private:
#ifdef __unix__
int fd;
#endif
std::mutex m_mutex;
std::filesystem::path m_storagePath;
};
}
}
#endif // DATABASE_FILE_H

View file

@ -42,12 +42,6 @@ int main(int argc, char* argv[]) {
return 2; return 2;
} }
std::cout << "[Launch] Checking working directory..." << std::endl;
if (!std::filesystem::exists(settings.storage_path())) {
std::cerr << "[Error] Storage directory " << settings.storage_path() << " can not be found" << std::endl;
return 2;
}
dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content); dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content);
bot.on_log([&bot](const dpp::log_t &event) { bot.on_log([&bot](const dpp::log_t &event) {
std::cerr << "[Log] " << event.message << std::endl; std::cerr << "[Log] " << event.message << std::endl;

View file

@ -17,10 +17,9 @@
*****************************************************************************/ *****************************************************************************/
#include <dpp/json.h> #include <dpp/json.h>
#include <mutex>
#include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include "database_file.h"
#include "settings.h" #include "settings.h"
#include "translator_libretranslate.h" #include "translator_libretranslate.h"
using namespace bot::settings; using namespace bot::settings;
@ -260,18 +259,15 @@ const std::vector<std::string> settings::preferred_languages() const
return m_prefLangs; return m_prefLangs;
} }
const std::filesystem::path settings::storage_path() const std::shared_ptr<bot::database::database> settings::get_database() const
{ {
const std::lock_guard<std::recursive_mutex> guard(m_mutex); return m_database;
return m_storagePath;
} }
std::unique_ptr<bot::translator::translator> settings::get_translator() const std::unique_ptr<bot::translator::translator> settings::get_translator() const
{ {
const std::lock_guard<std::recursive_mutex> guard(m_mutex); const std::lock_guard<std::recursive_mutex> guard(m_mutex);
std::unique_ptr<bot::translator::libretranslate> libretranslate( return std::make_unique<bot::translator::libretranslate>(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey);
new bot::translator::libretranslate(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey));
return libretranslate;
} }
const std::string settings::token() const const std::string settings::token() const
@ -318,14 +314,18 @@ bool settings::parse(const std::string &data)
const std::lock_guard<std::recursive_mutex> guard(m_mutex); const std::lock_guard<std::recursive_mutex> guard(m_mutex);
m_token = *json_token; m_token = *json_token;
// Support more Database systems in the future
std::filesystem::path storage_path;
auto json_storage = json.find("storage"); auto json_storage = json.find("storage");
if (json_storage != json.end()) if (json_storage != json.end())
m_storagePath = std::string(*json_storage); storage_path = std::string(*json_storage);
else if (char *storagepath = getenv("DTRANSLATEBOT_STORAGE")) else if (char *storagepath = getenv("DTRANSLATEBOT_STORAGE"))
m_storagePath = storagepath; storage_path = storagepath;
if (m_storagePath.empty()) if (storage_path.empty())
m_storagePath = std::filesystem::current_path(); storage_path = std::filesystem::current_path();
m_database = std::make_shared<bot::database::file>(storage_path);
auto json_translator = json.find("translator"); auto json_translator = json.find("translator");
if (json_translator == json.end()) { if (json_translator == json.end()) {

View file

@ -18,38 +18,13 @@
#ifndef SETTINGS_H #ifndef SETTINGS_H
#define SETTINGS_H #define SETTINGS_H
#include <cstdint>
#include <dpp/snowflake.h>
#include <dpp/webhook.h>
#include <filesystem>
#include <mutex> #include <mutex>
#include <string> #include "database_core.h"
#include <vector> #include "settings_types.h"
#include "translator_core.h" #include "translator_core.h"
namespace bot { namespace bot {
namespace settings { namespace settings {
struct target {
std::string target;
dpp::webhook webhook;
};
struct channel {
dpp::snowflake id;
std::string source;
std::vector<bot::settings::target> targets;
};
struct guild {
dpp::snowflake id;
std::vector<bot::settings::channel> channel;
};
struct translator {
std::string hostname;
uint16_t port;
std::string url;
bool tls;
std::string apiKey;
};
class settings { class settings {
public: public:
/* add functions */ /* add functions */
@ -63,7 +38,7 @@ namespace bot {
const channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const; const channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const;
const guild* get_guild(dpp::snowflake guild_id) const; const guild* get_guild(dpp::snowflake guild_id) const;
const std::vector<std::string> preferred_languages() const; const std::vector<std::string> preferred_languages() const;
const std::filesystem::path storage_path() const; std::shared_ptr<bot::database::database> get_database() const;
std::unique_ptr<bot::translator::translator> get_translator() const; std::unique_ptr<bot::translator::translator> get_translator() const;
const std::string token() const; const std::string token() const;
@ -82,9 +57,9 @@ namespace bot {
mutable std::recursive_mutex m_mutex; mutable std::recursive_mutex m_mutex;
size_t m_externallyLockedCount; size_t m_externallyLockedCount;
uint16_t m_avatarSize; uint16_t m_avatarSize;
std::shared_ptr<bot::database::database> m_database;
std::vector<guild> m_guilds; std::vector<guild> m_guilds;
std::vector<std::string> m_prefLangs; std::vector<std::string> m_prefLangs;
std::filesystem::path m_storagePath;
bot::settings::translator m_translator; bot::settings::translator m_translator;
std::string m_token; std::string m_token;
std::vector<dpp::snowflake> m_webhookIds; std::vector<dpp::snowflake> m_webhookIds;

53
src/settings_types.h Normal file
View file

@ -0,0 +1,53 @@
/*****************************************************************************
* dtranslatebot Discord Translate Bot
* Copyright (C) 2023-2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef SETTINGS_TYPES_H
#define SETTINGS_TYPES_H
#include <cstdint>
#include <dpp/snowflake.h>
#include <dpp/webhook.h>
#include <string>
#include <vector>
namespace bot {
namespace settings {
struct target {
std::string target;
dpp::webhook webhook;
};
struct channel {
dpp::snowflake id;
std::string source;
std::vector<bot::settings::target> targets;
};
struct guild {
dpp::snowflake id;
std::vector<bot::settings::channel> channel;
};
struct translator {
std::string hostname;
uint16_t port;
std::string url;
bool tls;
std::string apiKey;
};
}
}
#endif // SETTINGS_TYPES_H