[Widelands-cvs] SF.net SVN: widelands: [2331] trunk/src
Status: Beta
Brought to you by:
sirver
From: <kn...@us...> - 2007-06-27 15:58:41
|
Revision: 2331 http://svn.sourceforge.net/widelands/?rev=2331&view=rev Author: knutux Date: 2007-06-27 08:58:39 -0700 (Wed, 27 Jun 2007) Log Message: ----------- - Emergency save - automatic game saving if exception occurs (should help diagnosing crashes in some cases) - also made autosave more reliable (old save is not completely removed until autosave completes). Modified Paths: -------------- trunk/src/filesystem/disk_filesystem.cc trunk/src/filesystem/disk_filesystem.h trunk/src/filesystem/filesystem.h trunk/src/filesystem/layered_filesystem.cc trunk/src/filesystem/layered_filesystem.h trunk/src/filesystem/zip_filesystem.cc trunk/src/filesystem/zip_filesystem.h trunk/src/main.cc trunk/src/save_handler.cc trunk/src/wlapplication.cc trunk/src/wlapplication.h Modified: trunk/src/filesystem/disk_filesystem.cc =================================================================== --- trunk/src/filesystem/disk_filesystem.cc 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/filesystem/disk_filesystem.cc 2007-06-27 15:58:39 UTC (rev 2331) @@ -404,3 +404,12 @@ if (c != 1) throw wexception("Write to %s (%s) failed", fname.c_str(), fullname.c_str()); } + +// rename a file or directory +void RealFSImpl::Rename +(const std::string & old_name, const std::string & new_name) +{ + const std::string fullname1 = FS_CanonicalizeName(old_name); + const std::string fullname2 = FS_CanonicalizeName(new_name); + rename(fullname1.c_str(), fullname2.c_str()); +} Modified: trunk/src/filesystem/disk_filesystem.h =================================================================== --- trunk/src/filesystem/disk_filesystem.h 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/filesystem/disk_filesystem.h 2007-06-27 15:58:39 UTC (rev 2331) @@ -47,6 +47,7 @@ virtual FileSystem* CreateSubFileSystem(const std::string dirname, const Type); virtual void Unlink(const std::string file); + virtual void Rename(const std::string&, const std::string&); virtual void listSubdirs() const; Modified: trunk/src/filesystem/filesystem.h =================================================================== --- trunk/src/filesystem/filesystem.h 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/filesystem/filesystem.h 2007-06-27 15:58:39 UTC (rev 2331) @@ -61,6 +61,7 @@ virtual FileSystem* CreateSubFileSystem( const std::string dirname, const Type ) = 0; virtual void Unlink( const std::string ) = 0; + virtual void Rename(const std::string&, const std::string&) = 0; static FileSystem *Create(const std::string root) throw(FileType_error, FileNotFound_error, FileAccessDenied_error); Modified: trunk/src/filesystem/layered_filesystem.cc =================================================================== --- trunk/src/filesystem/layered_filesystem.cc 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/filesystem/layered_filesystem.cc 2007-06-27 15:58:39 UTC (rev 2331) @@ -269,3 +269,22 @@ return; } } + +void LayeredFileSystem::Rename +(const std::string & old_name, const std::string & new_name) +{ + if( !FileExists( old_name )) + return; + + for(FileSystem_rit it = m_filesystems.rbegin(); + it != m_filesystems.rend(); it++) + { + if (!(*it)->IsWritable()) + continue; + if (!(*it)->FileExists(old_name)) + continue; + + (*it)->Rename(old_name, new_name); + return; + } +} Modified: trunk/src/filesystem/layered_filesystem.h =================================================================== --- trunk/src/filesystem/layered_filesystem.h 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/filesystem/layered_filesystem.h 2007-06-27 15:58:39 UTC (rev 2331) @@ -65,6 +65,7 @@ virtual FileSystem* CreateSubFileSystem(const std::string dirname, const Type); virtual void Unlink(const std::string file); + virtual void Rename(const std::string&, const std::string&); virtual void listSubdirs() const; Modified: trunk/src/filesystem/zip_filesystem.cc =================================================================== --- trunk/src/filesystem/zip_filesystem.cc 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/filesystem/zip_filesystem.cc 2007-06-27 15:58:39 UTC (rev 2331) @@ -397,3 +397,9 @@ m_state = STATE_UNZIPPPING; } + +void ZipFilesystem::Rename +(const std::string & old_name, const std::string & new_name) +{ + assert (false and "rename inside zip FS is not implemented yet"); +} Modified: trunk/src/filesystem/zip_filesystem.h =================================================================== --- trunk/src/filesystem/zip_filesystem.h 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/filesystem/zip_filesystem.h 2007-06-27 15:58:39 UTC (rev 2331) @@ -48,6 +48,7 @@ virtual FileSystem* CreateSubFileSystem(const std::string dirname, const Type ); virtual void Unlink(const std::string filename) __attribute__ ((noreturn)); + virtual void Rename(const std::string&, const std::string&); public: static FileSystem *CreateFromDirectory(const std::string directory); Modified: trunk/src/main.cc =================================================================== --- trunk/src/main.cc 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/main.cc 2007-06-27 15:58:39 UTC (rev 2331) @@ -61,11 +61,11 @@ "The exception said: "<<e.what()<<endl<<endl<< "This should not happen. Please file a bug report."<<endl<<endl<< flush; + g_app->emergency_save(e.what()); delete g_app; return 1; } -#if 0 catch(std::exception &e) { cerr<<endl<< "Caught exception (of type '"<<typeid(e).name()<< @@ -73,6 +73,7 @@ "The exception said: "<<e.what()<<endl<<endl<< "This should not happen. Please file a bug report."<<endl<<endl<< flush; + g_app->emergency_save(e.what()); delete g_app; return 1; @@ -82,9 +83,9 @@ "Caught unknown exception in outermost handler!"<<endl<<endl<< "This should not happen. Please file a bug report."<<endl<<endl<< flush; + g_app->emergency_save("Unhandled exception"); delete g_app; return 1; } -#endif } Modified: trunk/src/save_handler.cc =================================================================== --- trunk/src/save_handler.cc 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/save_handler.cc 2007-06-27 15:58:39 UTC (rev 2331) @@ -42,14 +42,32 @@ // save the game std::string complete_filename = create_file_name (get_base_dir(), "WL_autosave"); + std::string backup_filename; // always overwrite a file if(g_fs->FileExists(complete_filename)) { - g_fs->Unlink(complete_filename); + backup_filename = create_file_name (get_base_dir(), "WL_autosave2"); + if(g_fs->FileExists(backup_filename)) { + g_fs->Unlink(backup_filename); + } + g_fs->Rename(complete_filename, backup_filename); } - if (!save_game(g, complete_filename)) + if (!save_game(g, complete_filename)) { log("Autosave: ERROR\n"); + + // if backup file was created, move it back + if (backup_filename.length() > 0) { + if(g_fs->FileExists(complete_filename)) { + g_fs->Unlink(complete_filename); + } + g_fs->Rename(backup_filename, complete_filename); + } + } else { + // if backup file was created, time to remove it + if (backup_filename.length() > 0 && g_fs->FileExists(backup_filename)) + g_fs->Unlink(backup_filename); + } log("Autosave: save took %d ms\n", m_lastSaveTime - realtime); } Modified: trunk/src/wlapplication.cc =================================================================== --- trunk/src/wlapplication.cc 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/wlapplication.cc 2007-06-27 15:58:39 UTC (rev 2331) @@ -1296,3 +1296,31 @@ } } + +/** +* Try to save if possible + */ +void WLApplication::emergency_save(const std::string & message) { + if (NULL == m_game) + return; + + try { + time_t t; + time(&t); + SaveHandler * save_handler = m_game->get_save_handler(); + char * current_time = ctime(&t); + // remove trailing newline character + std::string time_string (current_time, strlen(current_time)-1); + int pos = std::string::npos; + // ':' is not a valid file name character under Windows + while ((pos = time_string.find (':')) != std::string::npos) { + time_string[pos] = '.'; + } + + std::string filename = save_handler->create_file_name + (save_handler->get_base_dir(), time_string); + save_handler->save_game(m_game, filename); + } catch (...) { + // just ignore + } +} Modified: trunk/src/wlapplication.h =================================================================== --- trunk/src/wlapplication.h 2007-06-27 14:56:36 UTC (rev 2330) +++ trunk/src/wlapplication.h 2007-06-27 15:58:39 UTC (rev 2331) @@ -211,6 +211,8 @@ static void show_usage(); + void emergency_save(const std::string & message); + protected: WLApplication(const int argc, const char **argv); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |