Clean temp files when starting STK instead of exiting

This commit is contained in:
Benau 2020-02-03 16:52:45 +08:00
parent 5683ff7494
commit d147291e2a
4 changed files with 48 additions and 61 deletions

View File

@ -19,7 +19,6 @@
#ifdef ANDROID
#include "addons/addons_manager.hpp"
#include "io/file_manager.hpp"
#endif
#include "audio/music_manager.hpp"
#include "audio/sfx_manager.hpp"
@ -191,10 +190,6 @@ bool EventHandler::OnEvent (const SEvent &event)
PlayerManager::get()->save();
if (addons_manager->hasDownloadedIcons())
addons_manager->saveInstalled();
// Clean temp files manually as destructor of file manager
// won't be called at all in mobile if user just press home
// button
file_manager->cleanTempFiles();
}
}
else if (cmd == APP_CMD_RESUME || cmd == APP_CMD_GAINED_FOCUS)

View File

@ -408,6 +408,53 @@ void FileManager::init()
addAssetsSearchPath();
m_cert_bundle_location = m_file_system->getAbsolutePath(
getAsset("cacert.pem").c_str()).c_str();
// Clean up left-over files in addons/tmp that are older than 24h
// ==============================================================
// (The 24h delay is useful when debugging a problem with a zip file)
// We do when starting STK because for mobile STK destructor of file
// manager may never be called if only home button is pressed
std::set<std::string> allfiles;
std::string tmp=getAddonsFile("tmp");
listFiles(allfiles, tmp);
for(std::set<std::string>::iterator i=allfiles.begin();
i!=allfiles.end(); i++)
{
if((*i)=="." || (*i)=="..") continue;
// For now there should be only zip files or .part files
// (not fully downloaded files) in tmp. Warn about any
// other files.
std::string full_path=tmp+"/"+*i;
if(StringUtils::getExtension(*i)!="zip" &&
StringUtils::getExtension(*i)!="part" )
{
Log::warn("[FileManager]", "Unexpected tmp file '%s' found.",
full_path.c_str());
continue;
}
if(isDirectory(full_path))
{
// Gee, a .zip file which is a directory - stay away from it
Log::warn("[FileManager]", "'%s' is a directory and will not be deleted.",
full_path.c_str());
continue;
}
struct stat mystat;
FileUtils::statU8Path(full_path, &mystat);
StkTime::TimeType current = StkTime::getTimeSinceEpoch();
if(current - mystat.st_ctime <24*3600)
{
if(UserConfigParams::logAddons())
Log::verbose("[FileManager]", "'%s' is less than 24h old "
"and will not be deleted.",
full_path.c_str());
continue;
}
if(UserConfigParams::logAddons())
Log::verbose("[FileManager]", "Deleting tmp file'%s'.",full_path.c_str());
removeFile(full_path);
} // for i in all files in tmp
} // init
//-----------------------------------------------------------------------------
@ -454,7 +501,6 @@ void FileManager::reinitAfterDownloadAssets()
//-----------------------------------------------------------------------------
FileManager::~FileManager()
{
cleanTempFiles();
// Clean up rest of file manager
// =============================
popMusicSearchPath();
@ -465,55 +511,6 @@ FileManager::~FileManager()
m_file_system = NULL;
} // ~FileManager
// ----------------------------------------------------------------------------
void FileManager::cleanTempFiles()
{
// Clean up left-over files in addons/tmp that are older than 24h
// ==============================================================
// (The 24h delay is useful when debugging a problem with a zip file)
std::set<std::string> allfiles;
std::string tmp=getAddonsFile("tmp");
listFiles(allfiles, tmp);
for(std::set<std::string>::iterator i=allfiles.begin();
i!=allfiles.end(); i++)
{
if((*i)=="." || (*i)=="..") continue;
// For now there should be only zip files or .part files
// (not fully downloaded files) in tmp. Warn about any
// other files.
std::string full_path=tmp+"/"+*i;
if(StringUtils::getExtension(*i)!="zip" &&
StringUtils::getExtension(*i)!="part" )
{
Log::warn("[FileManager]", "Unexpected tmp file '%s' found.",
full_path.c_str());
continue;
}
if(isDirectory(full_path))
{
// Gee, a .zip file which is a directory - stay away from it
Log::warn("[FileManager]", "'%s' is a directory and will not be deleted.",
full_path.c_str());
continue;
}
struct stat mystat;
FileUtils::statU8Path(full_path, &mystat);
StkTime::TimeType current = StkTime::getTimeSinceEpoch();
if(current - mystat.st_ctime <24*3600)
{
if(UserConfigParams::logAddons())
Log::verbose("[FileManager]", "'%s' is less than 24h old "
"and will not be deleted.",
full_path.c_str());
continue;
}
if(UserConfigParams::logAddons())
Log::verbose("[FileManager]", "Deleting tmp file'%s'.",full_path.c_str());
removeFile(full_path);
} // for i in all files in tmp
} // cleanTempFiles
// ----------------------------------------------------------------------------
/** Returns true if the specified file exists.
*/

View File

@ -245,8 +245,7 @@ public:
// ------------------------------------------------------------------------
const std::string& getCertBundleLocation() const
{ return m_cert_bundle_location; }
// ------------------------------------------------------------------------
void cleanTempFiles();
}; // FileManager
extern FileManager* file_manager;

View File

@ -137,10 +137,6 @@ float MainLoop::getLimitedDt()
PlayerManager::get()->save();
if (addons_manager->hasDownloadedIcons())
addons_manager->saveInstalled();
// Clean temp files manually as destructor of file manager
// won't be called at all in mobile if user just press home
// button
file_manager->cleanTempFiles();
}
dev->run();
win_active = dev->isWindowActive();