From: kan Date: Fri, 16 Dec 2011 17:49:55 +0000 (+0000) Subject: Coding seems to be done for 0817. Check driver:344 I am not sure about lock. X-Git-Tag: CTH_V17a~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=d964bd3cc1cfa1ec60e3bd17a361918a9b39c108;p=modules%2Fgui.git Coding seems to be done for 0817. Check driver:344 I am not sure about lock. --- diff --git a/src/LightApp/LightApp_Driver.cxx b/src/LightApp/LightApp_Driver.cxx index 32eaade9b..82f6f501c 100644 --- a/src/LightApp/LightApp_Driver.cxx +++ b/src/LightApp/LightApp_Driver.cxx @@ -20,6 +20,11 @@ #ifdef WIN32 #include +#else +#include +#include +#include +#include #endif #include @@ -47,7 +52,8 @@ LightApp_Driver::LightApp_Driver() myFileNameSizes( 0 ), myCurrOFile( 0 ), myNbFilles( 0 ), - myBloc( 0 ) + myBloc( 0 ), + myBlocFcntl( 0 ) { } @@ -241,8 +247,44 @@ void LightApp_Driver::SetListOfFiles( const char* theModuleName, const ListOfFil QString fName = Qtx::addSlash( theListOfFiles.front().c_str() ) + "used_by_salome"; myBloc = fopen( fName.toLatin1().constData(), "w" ); + +#ifndef WIN32 + fName += ".fcntl"; + myBlocFcntl = fopen( fName.toLatin1().constData(), "w" ); +#endif + lockFcntl( QString() ); + } +/* + * Lock theLF or myBlockFcntl if empty + * returns 0 on success + */ +int LightApp_Driver::lockFcntl( QString theLF ) +{ +#ifdef WIN32 + return 0; +#else + if ( theLF.isEmpty() && !myBlocFcntl ) + return -2; + + FILE* aFD; + if ( theLF.isEmpty() ) + aFD = myBlocFcntl; + else + aFD = fopen( theLF.toLatin1().constData(), "w" ); + + struct flock fLock; + fLock.l_type = F_WRLCK; + fLock.l_whence = SEEK_SET; + fLock.l_len = 0; + fLock.l_start = 0; + return fcntl( fileno( aFD ), F_SETLK, &fLock ); +#endif + +} + + /*! Remove files. First item in is a directory with slash at the end. Other items are names of files. If is true, @@ -387,6 +429,16 @@ std::string LightApp_Driver::GetTmpDir() QString blocName = Qtx::addSlash(tmpDir) + "used_by_salome"; myBloc = fopen( blocName.toLatin1().constData(), "w" ); +#ifndef WIN32 + blocName += ".fcntl"; + if ( myBlocFcntl ) + fclose( myBlocFcntl ); + myBlocFcntl = fopen( blocName.toLatin1().constData(), "w" ); +#endif + lockFcntl( QString() ); + + + return aTmpDir.ToCString(); } diff --git a/src/LightApp/LightApp_Driver.h b/src/LightApp/LightApp_Driver.h index 58499f022..1928bf950 100644 --- a/src/LightApp/LightApp_Driver.h +++ b/src/LightApp/LightApp_Driver.h @@ -31,6 +31,8 @@ #pragma warning( disable:4251 ) #endif +class QString; + /*!Description : Driver can save to file and read from file list of files for light modules*/ class LIGHTAPP_EXPORT LightApp_Driver @@ -81,12 +83,17 @@ protected: void SetIsTemporary( bool theFlag ) { myIsTemp = theFlag; } bool IsTemporary() const { return myIsTemp; } + + int lockFcntl( QString theLF ); protected: typedef std::map MapOfListOfFiles; MapOfListOfFiles myMap; std::string myTmpDir; FILE* myBloc; +#ifndef WIN32 + FILE* myBlocFcntl; +#endif private: bool myIsTemp; diff --git a/src/SUIT/SUIT_Session.cxx b/src/SUIT/SUIT_Session.cxx index 7ebb712f6..e3bf5f503 100755 --- a/src/SUIT/SUIT_Session.cxx +++ b/src/SUIT/SUIT_Session.cxx @@ -22,6 +22,11 @@ #include #else #include + +#include +#include +#include +#include #endif #include "SUIT_Study.h" @@ -48,7 +53,8 @@ SUIT_Session::SUIT_Session() myExitStatus( NORMAL ), myExitFlags ( 0 ), myBTimer( 0 ), - myBFile( 0 ) + myBFile( 0 ), + myBFileFcntl( 0 ) { SUIT_ASSERT( !mySession ) @@ -525,9 +531,43 @@ void SUIT_Session::createBackupTimer() { QString used = Qtx::addSlash( myBFolder ) + "used_by_salome"; myBFile = fopen( used.toLatin1().constData(), "w" ); + +#ifndef WIN32 + used += ".fcntl"; + myBFileFcntl = fopen( used.toLatin1().constData(), "w" ); +#endif + lockFcntl( QString() ); } } +/* + * Lock theLF or myBFileFcntl if empty + * returns 0 on success + */ +int SUIT_Session::lockFcntl( QString theLF ) +{ +#ifdef WIN32 + return 0; +#else + if ( theLF.isEmpty() && !myBFileFcntl ) + return -2; + + FILE* aFD; + if ( theLF.isEmpty() ) + aFD = myBFileFcntl; + else + aFD = fopen( theLF.toLatin1().constData(), "w" ); + + struct flock fLock; + fLock.l_type = F_WRLCK; + fLock.l_whence = SEEK_SET; + fLock.l_len = 0; + fLock.l_start = 0; + return fcntl( fileno( aFD ), F_SETLK, &fLock ); +#endif + +} + /*! Slot, called when backup interval is out, iterates through all opened applications, creates 'folderName' directories for them and calls app->backup( folderName ); @@ -596,9 +636,18 @@ void SUIT_Session::restoreBackup() // checks whether folder is not currently used QString testFile = Qtx::addSlash( stdRoot ) + "used_by_salome"; QFileInfo fi( testFile ); - if ( fi.exists() && !QFile( testFile ).remove() ) - continue; - + if ( fi.exists() ) + { +#ifdef WIN32 + if ( !QFile( testFile ).remove() ) + continue; +#else + testFile += ".fcntl"; + if ( lockFcntl( testFile ) ) + continue; +#endif + } + toRemove.append( stdRoot ); QDir sessDir( stdRoot ); @@ -694,7 +743,17 @@ void SUIT_Session::removeTmpFiles() // 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 ) ) + + + bool locked; +#ifdef WIN32 + locked = !QFile( blocName ).remove(); +#else + QString testFile = blocName + ".fcntl"; + locked = lockFcntl( testFile ); +#endif + + if ( QFileInfo( blocName ).exists() && !locked ) { // unused non-removed folder Qtx::rmDir( currF ); diff --git a/src/SUIT/SUIT_Session.h b/src/SUIT/SUIT_Session.h index f062a6dba..c0df6792c 100755 --- a/src/SUIT/SUIT_Session.h +++ b/src/SUIT/SUIT_Session.h @@ -99,6 +99,7 @@ private: void createBackupTimer(); void restoreBackup(); void removeTmpFiles(); + int lockFcntl( QString theLF ); private: typedef QList AppList; @@ -124,6 +125,9 @@ private: QString myBFolder; QTimer* myBTimer; FILE* myBFile; +#ifndef WIN32 + FILE* myBFileFcntl; +#endif }; #endif