From c4aeb7ce582261e07a192bed8f70dbb14134145b Mon Sep 17 00:00:00 2001 From: JustinAJ Date: Thu, 6 Nov 2014 00:37:02 -0500 Subject: [PATCH] File position is now stored for ban entries; added BanDatabase::deactivate(). --- RenX.Core/RenX_BanDatabase.cpp | 20 ++++++++++++++++++++ RenX.Core/RenX_BanDatabase.h | 11 ++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/RenX.Core/RenX_BanDatabase.cpp b/RenX.Core/RenX_BanDatabase.cpp index 1d5d5d5..6f8c6e8 100644 --- a/RenX.Core/RenX_BanDatabase.cpp +++ b/RenX.Core/RenX_BanDatabase.cpp @@ -45,6 +45,7 @@ bool RenX::BanDatabase::load(const Jupiter::ReadableString &fname) while (!feof(file)) { entry = new Entry(); + fgetpos(file, &entry->pos); fread(&entry->active, 1, 1, file); fread(&entry->timestamp, sizeof(time_t), 1, file); fread(&entry->length, sizeof(time_t), 1, file); @@ -124,6 +125,7 @@ void RenX::BanDatabase::add(RenX::Server *server, const RenX::PlayerInfo *player FILE *file = fopen(RenX::BanDatabase::filename.c_str(), "ab"); if (file != nullptr) { + fgetpos(file, &entry->pos); fwrite(&entry->active, 1, 1, file); fwrite(&entry->timestamp, sizeof(time_t), 1, file); fwrite(&entry->length, sizeof(time_t), 1, file); @@ -150,6 +152,24 @@ void RenX::BanDatabase::add(RenX::Server *server, const RenX::PlayerInfo *player } } +bool RenX::BanDatabase::deactivate(size_t index) +{ + RenX::BanDatabase::Entry *entry = RenX::BanDatabase::entries.get(index); + if (entry->active) + { + entry->active = 0; + FILE *file = fopen(RenX::BanDatabase::filename.c_str(), "r+b"); + if (file != nullptr) + { + fsetpos(file, &entry->pos); + fputc(entry->active, file); + fclose(file); + } + return true; + } + return false; +} + uint8_t RenX::BanDatabase::getVersion() const { return RenX::BanDatabase::version; diff --git a/RenX.Core/RenX_BanDatabase.h b/RenX.Core/RenX_BanDatabase.h index ec24102..a8eb205 100644 --- a/RenX.Core/RenX_BanDatabase.h +++ b/RenX.Core/RenX_BanDatabase.h @@ -46,6 +46,7 @@ namespace RenX */ struct RENX_API Entry { + fpos_t pos; /** Position of the entry in the database */ unsigned char active; /** 1 if the ban is active, 0 otherwise */ time_t timestamp /** Time the ban was created */; time_t length /** Duration of the ban; 0 if permanent */; @@ -65,7 +66,7 @@ namespace RenX bool load(const Jupiter::ReadableString &fname); /** - * @param Adds a ban entry for a player and immediately writes it to the database. + * @brief Adds a ban entry for a player and immediately writes it to the database. * * @param server Server the player is playing in * @param player Data of the player to be banned @@ -73,6 +74,14 @@ namespace RenX */ void add(RenX::Server *server, const RenX::PlayerInfo *player, time_t length); + /** + * @brief Deactivates a ban entry. + * + * @param index Index of the entry to deactivate. + * @param True if the entry was active and is now inactive, false otherwise. + */ + bool deactivate(size_t index); + /** * @brief Fetches the version of the database file. *