Core/Updater: Convert an assertion into a fatal error

* The error is triggered when it fails to open an update for hashing
* See 'https://community.trinitycore.org/topic/12352-worldserver-crashes-on-database-update/'
  for details
* And a minor cleanup in the UpdateFetcher
This commit is contained in:
Naios
2016-02-21 03:30:47 +01:00
parent 719159e207
commit c43b808665
2 changed files with 23 additions and 14 deletions

View File

@@ -137,22 +137,33 @@ UpdateFetcher::AppliedFileStorage UpdateFetcher::ReceiveAppliedFiles() const
return map;
}
UpdateFetcher::SQLUpdate UpdateFetcher::ReadSQLUpdate(boost::filesystem::path const& file) const
std::string UpdateFetcher::ReadSQLUpdate(boost::filesystem::path const& file) const
{
std::ifstream in(file.c_str());
WPFatal(in.is_open(), "Could not read an update file.");
if (!in.is_open())
{
TC_LOG_FATAL("sql.updates", "Failed to open the sql update \"%s\" for reading! "
"Stopping the server to keep the database integrity, "
"try to identify and solve the issue or disabled the database updater.",
file.generic_string().c_str());
throw UpdateException("Opening the sql update failed!");
}
auto update = [&in] {
std::ostringstream ss;
ss << in.rdbuf();
return Trinity::make_unique<std::string>(ss.str());
return ss.str();
}();
in.close();
return update;
}
UpdateResult UpdateFetcher::Update(bool const redundancyChecks, bool const allowRehash, bool const archivedRedundancy, int32 const cleanDeadReferencesMaxCount) const
UpdateResult UpdateFetcher::Update(bool const redundancyChecks,
bool const allowRehash,
bool const archivedRedundancy,
int32 const cleanDeadReferencesMaxCount) const
{
LocaleFileStorage const available = GetFileList();
AppliedFileStorage applied = ReceiveAppliedFiles();
@@ -198,11 +209,9 @@ UpdateResult UpdateFetcher::Update(bool const redundancyChecks, bool const allow
}
}
// Read update from file
SQLUpdate const update = ReadSQLUpdate(availableQuery.first);
// Calculate hash
std::string const hash = CalculateHash(update);
std::string const hash =
CalculateHash(ReadSQLUpdate(availableQuery.first));
UpdateMode mode = MODE_APPLY;
@@ -325,11 +334,11 @@ UpdateResult UpdateFetcher::Update(bool const redundancyChecks, bool const allow
return UpdateResult(importedUpdates, countRecentUpdates, countArchivedUpdates);
}
std::string UpdateFetcher::CalculateHash(SQLUpdate const& query) const
std::string UpdateFetcher::CalculateHash(std::string const& query) const
{
// Calculate a Sha1 hash based on query content.
unsigned char digest[SHA_DIGEST_LENGTH];
SHA1((unsigned char*)query->c_str(), query->length(), (unsigned char*)&digest);
SHA1((unsigned char*)query.c_str(), query.length(), (unsigned char*)&digest);
return ByteArrayToHexStr(digest, SHA_DIGEST_LENGTH);
}

View File

@@ -103,16 +103,16 @@ private:
typedef std::unordered_map<std::string, std::string> HashToFileNameStorage;
typedef std::unordered_map<std::string, AppliedFileEntry> AppliedFileStorage;
typedef std::vector<UpdateFetcher::DirectoryEntry> DirectoryStorage;
typedef std::unique_ptr<std::string> SQLUpdate;
LocaleFileStorage GetFileList() const;
void FillFileListRecursively(Path const& path, LocaleFileStorage& storage, State const state, uint32 const depth) const;
void FillFileListRecursively(Path const& path, LocaleFileStorage& storage,
State const state, uint32 const depth) const;
DirectoryStorage ReceiveIncludedDirectories() const;
AppliedFileStorage ReceiveAppliedFiles() const;
SQLUpdate ReadSQLUpdate(Path const& file) const;
std::string CalculateHash(SQLUpdate const& query) const;
std::string ReadSQLUpdate(Path const& file) const;
std::string CalculateHash(std::string const& query) const;
uint32 Apply(Path const& path) const;