#ifdef WIN32
#include <time.h>
+#include <Qtx.h>
+#include <SUIT_Session.h>
#endif
/*! Constructor.*/
myFileSizes( 0 ),
myFileNameSizes( 0 ),
myCurrOFile( 0 ),
- myNbFilles( 0 )
+ myNbFilles( 0 ),
+ myBloc( 0 )
{
}
{
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" );
}
/*!
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 );
}
/*!
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);
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();
}
//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 );
myBTimer = 0;
}
- createBTimer();
+ createBackupTimer();
}
return 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
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;
}
/*!
Creates timer to be used for backup
*/
-void SUIT_Session::createBTimer()
+void SUIT_Session::createBackupTimer()
{
if ( myBTimer )
return;
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" );
}
}
*/
void SUIT_Session::restoreBackup()
{
- QString pref = getBPrefix();
+ QString pref = getBackupPrefix();
// checks whether temp folder contains old backups
QDir tmpDir( QDir::tempPath() );
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;
{
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 );
+ }
+ }
}