Skip to content
Permalink
Browse files
Editor: Remove BAK files on Editor close
There are temporary files automatically created during the file saving. Their purpose is providing a safer mechanism of the file saving and avoiding possible damage of the file because of external factors like BSOD or a power surge. These files usually gets remove after the 4-second waiting, however, they won't be removed if Editor got closed before. This change makes these files being automatically removed when closing editor without the 4-second wait.
  • Loading branch information
Wohlstand committed Apr 3, 2022
1 parent 750f1f6 commit 5438d7313008b2ed24c24bdfdb0fd5caa9de5637
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
@@ -21,6 +21,7 @@
#include <QFileInfo>
#include <QDir>
#include <QTimer>
#include <QSet>

#ifndef __WIN32

@@ -74,7 +75,7 @@ FileKeeper::FileKeeper(const QString &path) :
if(!QFile::exists(path))
{
m_tempPath = m_origPath;
return; //Do nothing when file is not exists (aka, new-made)
return; // Do nothing when file is not exists (aka, new-made)
}

QFileInfo info = QFileInfo(path);
@@ -112,6 +113,8 @@ void FileKeeper::remove()
}
}

static QSet<QString> s_existingBakFiles;

void FileKeeper::restore()
{
if(m_tempPath == m_origPath)
@@ -121,6 +124,7 @@ void FileKeeper::restore()
{
#ifndef __WIN32
int fd = open(m_tempPath.toUtf8().data(), O_FSYNC | O_APPEND, 0660);

if(fd)
{
fsync(fd);
@@ -133,23 +137,28 @@ void FileKeeper::restore()
QFile::remove(m_backupPath);
QFile::rename(m_origPath, m_backupPath);
}

QFile::rename(m_tempPath, m_origPath);

#else
std::wstring srcPath = m_tempPath.toStdWString();
std::wstring dstPath = m_origPath.toStdWString();
std::wstring backupPath = m_backupPath.toStdWString();

if(QFile::exists(m_backupPath))
QFile::remove(m_backupPath);

LogDebug("Attempt to override " + m_origPath + " file with " + m_tempPath + " file and making a backup " + m_backupPath + " backup path...");
BOOL ret = ReplaceFileW(dstPath.c_str(),
srcPath.c_str(),
backupPath.c_str(),
0, nullptr, nullptr);

if(ret == FALSE)
{
DWORD reterr = GetLastError();
LogWarning("Failed to override " + m_origPath + " file with " + m_tempPath + "! ["+ errorToString() + "]");

if(reterr == ERROR_UNABLE_TO_MOVE_REPLACEMENT)
LogWarning("The replacement file could not be renamed.");
else if(reterr == ERROR_UNABLE_TO_MOVE_REPLACEMENT_2)
@@ -163,16 +172,32 @@ void FileKeeper::restore()
QFile::remove(m_backupPath);
QFile::rename(m_origPath, m_backupPath);
}

QFile::rename(m_tempPath, m_origPath);
}
#endif

QString backupPathToRemove = m_backupPath;
QTimer::singleShot(4000, Qt::CoarseTimer, [backupPathToRemove](){
s_existingBakFiles.insert(backupPathToRemove);

QTimer::singleShot(4000, Qt::CoarseTimer, [backupPathToRemove]()
{
if(QFile::exists(backupPathToRemove))
QFile::remove(backupPathToRemove);
s_existingBakFiles.remove(backupPathToRemove);
}); // automatically remove backup file after 4 seconds

m_tempPath.clear();
}
}

void FileKeeper::removeAllBaks()
{
for(const auto &i : s_existingBakFiles)
{
if(QFile::exists(i))
QFile::remove(i);
}

s_existingBakFiles.clear();
}
@@ -50,6 +50,11 @@ class FileKeeper
* \brief Restore file back
*/
void restore();

/**
* @brief Remove all bak files immediately (call this when normally quit the Editor)
*/
static void removeAllBaks();
};


@@ -39,6 +39,7 @@
#include <common_features/themes.h>
#include <common_features/crashhandler.h>
#include <common_features/main_window_ptr.h>
#include <common_features/file_keeper.h>
#include <SingleApplication/singleapplication.h>
#include <SingleApplication/editor_application.h>

@@ -88,6 +89,9 @@ static void pgeInitFreeImage()

static void pgeEditorQuit()
{
// Remove all BAK files immediately
FileKeeper::removeAllBaks();

if(initied_sdl)
{
#ifdef USE_SDL_MIXER
@@ -113,13 +117,16 @@ static void pgeEditorQuit()
LogDebugNC("Deleting MainWindow...");
delete MainWinConnect::pMainWin;
}

QApplication::quit();
QApplication::exit();

if(app)
{
LogDebugNC("Deleting Qt-Application...");
delete app;
}

if(appSingle)
{
LogDebugNC("Deleting Single-Application...");
@@ -23,6 +23,7 @@
- Fixed a dead freeze on configuration package loading fatal errors
- Added the compatibility mode and speedrunner's stopwatch settings for TheXTech tester
- Fixed an unexpected grid offset changes at NPC with custom graphics but no custom configs
- All BAK files will be removed when closing the Editor without the 4-second wait.

Editor 0.3.2
- Added support for an element (Block, BGO, NPC), section, and level-wide Extra Settings

0 comments on commit 5438d73

Please sign in to comment.