From 33d3bb28701fd97c309a5bbba7b8d4e1d6090262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Liru=20F=C3=A6rs?= Date: Sun, 19 Apr 2020 20:04:25 -0400 Subject: [PATCH] [Chat Logs] When migrating file to new name, respect possible new name file If a new name file exists, copy its contents into our currently tracked file, because we update our tracked file, the new file can only contain more recent text (unless a name change back and forth and back happened since last run, but then the out of order text isn't entirely our fault) So we copy the new file's text to the bottom of our old file and then remove the new file, so we can rename our old tracked file to the updated name. If we cannot perform the copy, or the delete, we bypass the migration, leaving our old tracked file as a remnant and begin to track the new name file because that is more proper than clinging to the old one, and thus no history is lost to failure. --- indra/newview/lllogchat.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/indra/newview/lllogchat.cpp b/indra/newview/lllogchat.cpp index 2ebc4c971..7da15ec61 100644 --- a/indra/newview/lllogchat.cpp +++ b/indra/newview/lllogchat.cpp @@ -65,6 +65,20 @@ bool LLLogChat::migrateFile(const std::string& old_name, const std::string& file { std::string oldfile = makeLogFileNameInternal(old_name); if (!LLFile::isfile(oldfile)) return false; // An old file by this name doesn't exist + + if (LLFile::isfile(filename)) // A file by the new name also exists, but wasn't being tracked yet + { + auto&& new_untracked_log = llifstream(filename); + auto&& tracked_log = llofstream(oldfile, llofstream::out|llofstream::app); + // Append new to old and find out if it failed + bool failed = !(tracked_log << new_untracked_log.rdbuf()); + // Close streams + new_untracked_log.close(); + tracked_log.close(); + if (failed || LLFile::remove(filename)) // Delete the untracked new file so that reclaiming its name won't fail + return true; // We failed to remove it or update the old file, let's just use the new file and leave the old one alone + } + LLFile::rename(oldfile, filename); // Move the existing file to the new name return true; // Report success }