Move user.json.txt instead of copy then delete (#25)

This commit is contained in:
Edgar Handal 2020-07-13 18:07:55 -05:00 committed by R2DLiu
commit 8b17f0cfca

View file

@ -68,26 +68,21 @@ bool SlippiUser::AttemptLogin()
INFO_LOG(SLIPPI_ONLINE, "Looking for file at: %s", userFilePath.c_str()); INFO_LOG(SLIPPI_ONLINE, "Looking for file at: %s", userFilePath.c_str());
{ {
std::string userFilePathTxt = // Put the filename here in its own scope because we don't really need it elsewhere
userFilePath + ".txt"; // Put the filename here in its own scope because we don't really need it elsewhere std::string userFilePathTxt = userFilePath + ".txt";
// If both files exist we just log they exist and take no further action if (File::Exists(userFilePathTxt))
if (File::Exists(userFilePathTxt) && File::Exists(userFilePath))
{ {
INFO_LOG(SLIPPI_ONLINE, // If both files exist we just log they exist and take no further action
"Found both .json.txt and .json file for user data. Using .json and ignoring the .json.txt"); if (File::Exists(userFilePath))
}
// If only the .txt file exists copy the contents to a json file and delete the text file
else if (File::Exists(userFilePathTxt))
{
// Attempt to copy the txt file to the json file path. If it fails log a warning
if (!File::Copy(userFilePathTxt, userFilePath))
{ {
WARN_LOG(SLIPPI_ONLINE, "Could not copy file %s to %s", userFilePathTxt.c_str(), userFilePath.c_str()); INFO_LOG(SLIPPI_ONLINE, "Found both .json.txt and .json file for user data. Using .json "
"and ignoring the .json.txt");
} }
// Attempt to delete the txt file. If it fails log an info because this isn't as critical // If only the .txt file exists move the contents to a json file and log if it fails
if (!File::Delete(userFilePathTxt)) else if (!File::Rename(userFilePathTxt, userFilePath))
{ {
INFO_LOG(SLIPPI_ONLINE, "Failed to delete %s", userFilePathTxt.c_str()); WARN_LOG(SLIPPI_ONLINE, "Could not move file %s to %s", userFilePathTxt.c_str(),
userFilePath.c_str());
} }
} }
} }