From de6ba0405dc6612c47a68d06a8a77a671d770497 Mon Sep 17 00:00:00 2001 From: sln Date: Mon, 5 Dec 2011 07:49:31 +0000 Subject: [PATCH] Session backup and Study saving --- src/LightApp/LightApp_Driver.cxx | 56 ++++++++++++++++---------- src/LightApp/LightApp_Driver.h | 1 + src/SUIT/SUIT_Session.cxx | 69 ++++++++++++++++++++++++++------ src/SUIT/SUIT_Session.h | 6 ++- 4 files changed, 97 insertions(+), 35 deletions(-) diff --git a/src/LightApp/LightApp_Driver.cxx b/src/LightApp/LightApp_Driver.cxx index 08b791f66..1690c49a1 100644 --- a/src/LightApp/LightApp_Driver.cxx +++ b/src/LightApp/LightApp_Driver.cxx @@ -31,6 +31,8 @@ #ifdef WIN32 #include +#include +#include #endif /*! Constructor.*/ @@ -44,7 +46,8 @@ LightApp_Driver::LightApp_Driver() myFileSizes( 0 ), myFileNameSizes( 0 ), myCurrOFile( 0 ), - myNbFilles( 0 ) + myNbFilles( 0 ), + myBloc( 0 ) { } @@ -232,6 +235,12 @@ void LightApp_Driver::SetListOfFiles( const char* theModuleName, const ListOfFil { std::string aName (theModuleName); myMap[aName] = theListOfFiles; + + if ( myBloc ) + fclose( myBloc ); + + QString fName = Qtx::addSlash( theListOfFiles.front().c_str() ) + "used_by_salome"; + myBloc = fopen( fName.toLatin1().constData(), "w" ); } /*! @@ -285,8 +294,15 @@ void LightApp_Driver::RemoveTemporaryFiles( const char* theModuleName, const boo ListOfFiles aFiles = myMap[aModuleName]; // aFiles must contain temporary directory name in its first item // and names of files (relatively the temporary directory) in the others - RemoveFiles( aFiles, IsDirDeleted ); + if ( myBloc ) + { + fclose( myBloc ); + QString fName = Qtx::addSlash( aFiles.front().c_str() ) + "used_by_salome"; + QFile::remove( fName ); + } + + RemoveFiles( aFiles, IsDirDeleted ); } /*! @@ -340,29 +356,23 @@ std::string LightApp_Driver::GetTmpDir() aTmpDir = TCollection_AsciiString("/tmp/"); #endif } + + // create temporary folder + QString pref = Qtx::addSlash( aTmpDir.ToCString() ) + + SUIT_Session::session()->getSavePrefix(); + int i = 0; + QString tmpDir; + do + { + tmpDir = pref + QString( "%1" ).arg( ++i ); + } + while ( QFileInfo( tmpDir ).exists() ); - srand((unsigned int)time(NULL)); - int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory - TCollection_AsciiString aSubDir(aRND); - if(aSubDir.Length() <= 1) aSubDir = TCollection_AsciiString("123409876"); - - aTmpDir += aSubDir; //Get RND sub directory - -#ifdef WIN32 - if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\'; -#else - if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/'; -#endif + aTmpDir = Qtx::addSlash(tmpDir).toLatin1().constData(); OSD_Path aPath(aTmpDir); OSD_Directory aDir(aPath); - for(aRND = 0; aDir.Exists(); aRND++) { - aTmpDir.Insert((aTmpDir.Length() - 1), TCollection_AsciiString(aRND)); //Build a unique directory name - aPath = OSD_Path(aTmpDir); - aDir = OSD_Directory(aPath); - } - #ifdef WIN32 // Workaround for OSD_Protection bug on Windows OSD_Protection aProtection(OSD_RWXD, OSD_RWXD, OSD_RWXD, OSD_RWXD); @@ -370,9 +380,13 @@ std::string LightApp_Driver::GetTmpDir() OSD_Protection aProtection(OSD_RX, OSD_RWXD, OSD_RX, OSD_RX); #endif aDir.Build(aProtection); - myTmpDir = aTmpDir.ToCString(); + if ( myBloc ) + fclose( myBloc ); + QString blocName = Qtx::addSlash(tmpDir) + "used_by_salome"; + myBloc = fopen( blocName.toLatin1().constData(), "w" ); + return aTmpDir.ToCString(); } diff --git a/src/LightApp/LightApp_Driver.h b/src/LightApp/LightApp_Driver.h index 5021551ce..58499f022 100644 --- a/src/LightApp/LightApp_Driver.h +++ b/src/LightApp/LightApp_Driver.h @@ -86,6 +86,7 @@ protected: typedef std::map MapOfListOfFiles; MapOfListOfFiles myMap; std::string myTmpDir; + FILE* myBloc; private: bool myIsTemp; diff --git a/src/SUIT/SUIT_Session.cxx b/src/SUIT/SUIT_Session.cxx index cf1b0e931..b85562b85 100755 --- a/src/SUIT/SUIT_Session.cxx +++ b/src/SUIT/SUIT_Session.cxx @@ -182,7 +182,7 @@ SUIT_Application* SUIT_Session::startApplication( const QString& name, int /*arg //Remove backup folders if it exists QDir tmpDir( QDir::tempPath() ); - QString pref = getBPrefix(); + QString pref = getBackupPrefix(); QFileInfoList aFolders = tmpDir.entryInfoList( QStringList() << QString( pref + "*" ), QDir::Dirs ); @@ -198,7 +198,7 @@ SUIT_Application* SUIT_Session::startApplication( const QString& name, int /*arg myBTimer = 0; } - createBTimer(); + createBackupTimer(); } return app; @@ -411,9 +411,8 @@ void SUIT_Session::onApplicationActivated( SUIT_Application* app ) /*! Gets prefix to be used for creating backup copies */ -QString SUIT_Session::getBPrefix() const +QString SUIT_Session::getBackupPrefix() const { - //qint64 pId = QApplication::applicationPid(); #ifdef WNT QString usr( getenv( "USERNAME" ) ); #else @@ -428,7 +427,32 @@ QString SUIT_Session::getBPrefix() const else anAppName = "SALOME"; - QString pref = anAppName + "_" + usr; + QString pref = anAppName + "_backup_" + usr + "_"; + + return pref; +} + +/*! + Gets prefix for study saving; this method is used by storage + driver and the session to clear useless files after crash. +*/ +QString SUIT_Session::getSavePrefix() const +{ +#ifdef WNT + QString usr( getenv( "USERNAME" ) ); +#else + QString usr( getenv( "USER" ) ); +#endif + + // Create folder + QString anAppName; + SUIT_Application* app = activeApplication(); + if ( app ) + anAppName = app->applicationName(); + else + anAppName = "SALOME"; + + QString pref = anAppName + "_study_" + usr + "_"; return pref; } @@ -466,7 +490,7 @@ void SUIT_Session::setBackupTime( const double val ) const /*! Creates timer to be used for backup */ -void SUIT_Session::createBTimer() +void SUIT_Session::createBackupTimer() { if ( myBTimer ) return; @@ -482,20 +506,21 @@ void SUIT_Session::createBTimer() if ( mSec > 0 ) myBTimer->start( mSec ); - QString pref = QDir::convertSeparators( QDir::tempPath() + "/" + getBPrefix() ); + QString pref = QDir::convertSeparators( QDir::tempPath() + "/" + getBackupPrefix() ); myBFolder = pref; int i = 0; - while( QFileInfo( myBFolder ).exists() ) + do { - myBFolder = pref + QString( "_%1" ).arg( ++i ); + myBFolder = pref + QString( "%1" ).arg( ++i ); } + while( QFileInfo( myBFolder ).exists() ); if ( !QDir().mkdir( myBFolder ) ) myBFolder = ""; if ( !myBFolder.isEmpty() ) { - QString used = Qtx::addSlash( myBFolder ) + "used"; + QString used = Qtx::addSlash( myBFolder ) + "used_by_salome"; myBFile = fopen( used.toLatin1().constData(), "w" ); } } @@ -541,7 +566,7 @@ void SUIT_Session::onBTimer() */ void SUIT_Session::restoreBackup() { - QString pref = getBPrefix(); + QString pref = getBackupPrefix(); // checks whether temp folder contains old backups QDir tmpDir( QDir::tempPath() ); @@ -566,7 +591,7 @@ void SUIT_Session::restoreBackup() const QString& stdRoot = Qtx::addSlash( QDir::tempPath() ) + *sessIter; // checks whether folder is not currently used - QString testFile = Qtx::addSlash( stdRoot ) + "used"; + QString testFile = Qtx::addSlash( stdRoot ) + "used_by_salome"; QFileInfo fi( testFile ); if ( fi.exists() && !QFile( testFile ).remove() ) continue; @@ -642,5 +667,25 @@ void SUIT_Session::restoreBackup() { Qtx::rmDir( *it ); } + + // remove all unused temporary folders + QString savePref = getSavePrefix(); + + filt.clear(); + filt.append( savePref + "*" ); + tmpDir.setNameFilters( filt ); + + QStringList tmpFolders = tmpDir.entryList( QDir::Dirs ); + for ( it = tmpFolders.begin(); it != tmpFolders.end(); ++it ) + { + // iterate through tmp folders + const QString& currF = Qtx::addSlash( QDir::tempPath() ) + *it; + QString blocName = Qtx::addSlash( currF ) + "used_by_salome"; + if ( QFileInfo( blocName ).exists() && QFile::remove( blocName ) ) + { + // unused non-removed folder + Qtx::rmDir( currF ); + } + } } diff --git a/src/SUIT/SUIT_Session.h b/src/SUIT/SUIT_Session.h index 70147565f..bc9928593 100755 --- a/src/SUIT/SUIT_Session.h +++ b/src/SUIT/SUIT_Session.h @@ -81,6 +81,9 @@ public: double backupTime() const; void setBackupTime( const double val ) const; + QString getBackupPrefix() const; + QString getSavePrefix() const; + signals: void applicationClosed( SUIT_Application* ); @@ -93,8 +96,7 @@ private slots: void onBTimer(); private: - QString getBPrefix() const; - void createBTimer(); + void createBackupTimer(); void restoreBackup(); private: -- 2.39.2