@ -45,7 +45,7 @@
// TODO: http://www.spinnaker.de/linux/nfs-locking.html
// TODO: http://www.spinnaker.de/linux/nfs-locking.html
// TODO: Make regression test
// TODO: Make regression test
class KLockFile: : K LockFilePrivate {
class TDELockFile: : TDE LockFilePrivate {
public :
public :
TQString file ;
TQString file ;
int staleTime ;
int staleTime ;
@ -62,9 +62,9 @@ public:
// 30 seconds
// 30 seconds
KLockFile: : K LockFile( const TQString & file )
TDELockFile: : TDE LockFile( const TQString & file )
{
{
d = new K LockFilePrivate( ) ;
d = new TDE LockFilePrivate( ) ;
d - > file = file ;
d - > file = file ;
d - > staleTime = 30 ;
d - > staleTime = 30 ;
d - > isLocked = false ;
d - > isLocked = false ;
@ -72,21 +72,21 @@ KLockFile::KLockFile(const TQString &file)
d - > linkCountSupport = true ;
d - > linkCountSupport = true ;
}
}
KLockFile: : ~ K LockFile( )
TDELockFile: : ~ TDE LockFile( )
{
{
unlock ( ) ;
unlock ( ) ;
delete d ;
delete d ;
}
}
int
int
K LockFile: : staleTime ( ) const
TDE LockFile: : staleTime ( ) const
{
{
return d - > staleTime ;
return d - > staleTime ;
}
}
void
void
K LockFile: : setStaleTime ( int _staleTime )
TDE LockFile: : setStaleTime ( int _staleTime )
{
{
d - > staleTime = _staleTime ;
d - > staleTime = _staleTime ;
}
}
@ -109,17 +109,17 @@ static bool testLinkCountSupport(const TQCString &fileName)
return ( ( result = = 0 ) & & ( st_buf . st_nlink = = 2 ) ) ;
return ( ( result = = 0 ) & & ( st_buf . st_nlink = = 2 ) ) ;
}
}
static K LockFile: : LockResult lockFile ( const TQString & lockFile , KDE_struct_stat & st_buf , bool & linkCountSupport )
static TDE LockFile: : LockResult lockFile ( const TQString & lockFile , KDE_struct_stat & st_buf , bool & linkCountSupport )
{
{
TQCString lockFileName = TQFile : : encodeName ( lockFile ) ;
TQCString lockFileName = TQFile : : encodeName ( lockFile ) ;
int result = KDE_lstat ( lockFileName , & st_buf ) ;
int result = KDE_lstat ( lockFileName , & st_buf ) ;
if ( result = = 0 )
if ( result = = 0 )
return K LockFile: : LockFail ;
return TDE LockFile: : LockFail ;
KTempFile uniqueFile ( lockFile , TQString : : null , 0644 ) ;
KTempFile uniqueFile ( lockFile , TQString : : null , 0644 ) ;
uniqueFile . setAutoDelete ( true ) ;
uniqueFile . setAutoDelete ( true ) ;
if ( uniqueFile . status ( ) ! = 0 )
if ( uniqueFile . status ( ) ! = 0 )
return K LockFile: : LockError ;
return TDE LockFile: : LockError ;
char hostname [ 256 ] ;
char hostname [ 256 ] ;
hostname [ 0 ] = 0 ;
hostname [ 0 ] = 0 ;
@ -138,23 +138,23 @@ static KLockFile::LockResult lockFile(const TQString &lockFile, KDE_struct_stat
// Create lock file
// Create lock file
result = : : link ( uniqueName , lockFileName ) ;
result = : : link ( uniqueName , lockFileName ) ;
if ( result ! = 0 )
if ( result ! = 0 )
return K LockFile: : LockError ;
return TDE LockFile: : LockError ;
if ( ! linkCountSupport )
if ( ! linkCountSupport )
return K LockFile: : LockOK ;
return TDE LockFile: : LockOK ;
# else
# else
//TODO for win32
//TODO for win32
return K LockFile: : LockOK ;
return TDE LockFile: : LockOK ;
# endif
# endif
KDE_struct_stat st_buf2 ;
KDE_struct_stat st_buf2 ;
result = KDE_lstat ( uniqueName , & st_buf2 ) ;
result = KDE_lstat ( uniqueName , & st_buf2 ) ;
if ( result ! = 0 )
if ( result ! = 0 )
return K LockFile: : LockError ;
return TDE LockFile: : LockError ;
result = KDE_lstat ( lockFileName , & st_buf ) ;
result = KDE_lstat ( lockFileName , & st_buf ) ;
if ( result ! = 0 )
if ( result ! = 0 )
return K LockFile: : LockError ;
return TDE LockFile: : LockError ;
if ( ! statResultIsEqual ( st_buf , st_buf2 ) | | S_ISLNK ( st_buf . st_mode ) | | S_ISLNK ( st_buf2 . st_mode ) )
if ( ! statResultIsEqual ( st_buf , st_buf2 ) | | S_ISLNK ( st_buf . st_mode ) | | S_ISLNK ( st_buf2 . st_mode ) )
{
{
@ -163,15 +163,15 @@ static KLockFile::LockResult lockFile(const TQString &lockFile, KDE_struct_stat
{
{
linkCountSupport = testLinkCountSupport ( uniqueName ) ;
linkCountSupport = testLinkCountSupport ( uniqueName ) ;
if ( ! linkCountSupport )
if ( ! linkCountSupport )
return K LockFile: : LockOK ; // Link count support is missing... assume everything is OK.
return TDE LockFile: : LockOK ; // Link count support is missing... assume everything is OK.
}
}
return K LockFile: : LockFail ;
return TDE LockFile: : LockFail ;
}
}
return K LockFile: : LockOK ;
return TDE LockFile: : LockOK ;
}
}
static K LockFile: : LockResult deleteStaleLock ( const TQString & lockFile , KDE_struct_stat & st_buf , bool & linkCountSupport )
static TDE LockFile: : LockResult deleteStaleLock ( const TQString & lockFile , KDE_struct_stat & st_buf , bool & linkCountSupport )
{
{
// This is dangerous, we could be deleting a new lock instead of
// This is dangerous, we could be deleting a new lock instead of
// the old stale one, let's be very careful
// the old stale one, let's be very careful
@ -179,7 +179,7 @@ static KLockFile::LockResult deleteStaleLock(const TQString &lockFile, KDE_struc
// Create temp file
// Create temp file
KTempFile ktmpFile ( lockFile ) ;
KTempFile ktmpFile ( lockFile ) ;
if ( ktmpFile . status ( ) ! = 0 )
if ( ktmpFile . status ( ) ! = 0 )
return K LockFile: : LockError ;
return TDE LockFile: : LockError ;
TQCString lckFile = TQFile : : encodeName ( lockFile ) ;
TQCString lckFile = TQFile : : encodeName ( lockFile ) ;
TQCString tmpFile = TQFile : : encodeName ( ktmpFile . name ( ) ) ;
TQCString tmpFile = TQFile : : encodeName ( ktmpFile . name ( ) ) ;
@ -189,10 +189,10 @@ static KLockFile::LockResult deleteStaleLock(const TQString &lockFile, KDE_struc
# ifdef Q_OS_UNIX
# ifdef Q_OS_UNIX
// link to lock file
// link to lock file
if ( : : link ( lckFile , tmpFile ) ! = 0 )
if ( : : link ( lckFile , tmpFile ) ! = 0 )
return K LockFile: : LockFail ; // Try again later
return TDE LockFile: : LockFail ; // Try again later
# else
# else
//TODO for win32
//TODO for win32
return K LockFile: : LockOK ;
return TDE LockFile: : LockOK ;
# endif
# endif
// check if link count increased with exactly one
// check if link count increased with exactly one
@ -209,7 +209,7 @@ static KLockFile::LockResult deleteStaleLock(const TQString &lockFile, KDE_struc
tqWarning ( " WARNING: deleting stale lockfile %s " , lckFile . data ( ) ) ;
tqWarning ( " WARNING: deleting stale lockfile %s " , lckFile . data ( ) ) ;
: : unlink ( lckFile ) ;
: : unlink ( lckFile ) ;
: : unlink ( tmpFile ) ;
: : unlink ( tmpFile ) ;
return K LockFile: : LockOK ;
return TDE LockFile: : LockOK ;
}
}
}
}
@ -227,34 +227,34 @@ static KLockFile::LockResult deleteStaleLock(const TQString &lockFile, KDE_struc
tqWarning ( " WARNING: deleting stale lockfile %s " , lckFile . data ( ) ) ;
tqWarning ( " WARNING: deleting stale lockfile %s " , lckFile . data ( ) ) ;
: : unlink ( lckFile ) ;
: : unlink ( lckFile ) ;
: : unlink ( tmpFile ) ;
: : unlink ( tmpFile ) ;
return K LockFile: : LockOK ;
return TDE LockFile: : LockOK ;
}
}
// Failed to delete stale lock file
// Failed to delete stale lock file
tqWarning ( " WARNING: Problem deleting stale lockfile %s " , lckFile . data ( ) ) ;
tqWarning ( " WARNING: Problem deleting stale lockfile %s " , lckFile . data ( ) ) ;
: : unlink ( tmpFile ) ;
: : unlink ( tmpFile ) ;
return K LockFile: : LockFail ;
return TDE LockFile: : LockFail ;
}
}
KLockFile: : LockResult K LockFile: : lock ( int options )
TDELockFile: : LockResult TDE LockFile: : lock ( int options )
{
{
if ( d - > isLocked )
if ( d - > isLocked )
return K LockFile: : LockOK ;
return TDE LockFile: : LockOK ;
K LockFile: : LockResult result ;
TDE LockFile: : LockResult result ;
int hardErrors = 5 ;
int hardErrors = 5 ;
int n = 5 ;
int n = 5 ;
while ( true )
while ( true )
{
{
KDE_struct_stat st_buf ;
KDE_struct_stat st_buf ;
result = lockFile ( d - > file , st_buf , d - > linkCountSupport ) ;
result = lockFile ( d - > file , st_buf , d - > linkCountSupport ) ;
if ( result = = K LockFile: : LockOK )
if ( result = = TDE LockFile: : LockOK )
{
{
d - > staleTimer = TQTime ( ) ;
d - > staleTimer = TQTime ( ) ;
break ;
break ;
}
}
else if ( result = = K LockFile: : LockError )
else if ( result = = TDE LockFile: : LockError )
{
{
d - > staleTimer = TQTime ( ) ;
d - > staleTimer = TQTime ( ) ;
if ( - - hardErrors = = 0 )
if ( - - hardErrors = = 0 )
@ -262,7 +262,7 @@ KLockFile::LockResult KLockFile::lock(int options)
break ;
break ;
}
}
}
}
else // K LockFile::Fail
else // TDE LockFile::Fail
{
{
if ( ! d - > staleTimer . isNull ( ) & & ! statResultIsEqual ( d - > statBuf , st_buf ) )
if ( ! d - > staleTimer . isNull ( ) & & ! statResultIsEqual ( d - > statBuf , st_buf ) )
d - > staleTimer = TQTime ( ) ;
d - > staleTimer = TQTime ( ) ;
@ -292,17 +292,17 @@ KLockFile::LockResult KLockFile::lock(int options)
if ( isStale )
if ( isStale )
{
{
if ( ( options & LockForce ) = = 0 )
if ( ( options & LockForce ) = = 0 )
return K LockFile: : LockStale ;
return TDE LockFile: : LockStale ;
result = deleteStaleLock ( d - > file , d - > statBuf , d - > linkCountSupport ) ;
result = deleteStaleLock ( d - > file , d - > statBuf , d - > linkCountSupport ) ;
if ( result = = K LockFile: : LockOK )
if ( result = = TDE LockFile: : LockOK )
{
{
// Lock deletion successful
// Lock deletion successful
d - > staleTimer = TQTime ( ) ;
d - > staleTimer = TQTime ( ) ;
continue ; // Now try to get the new lock
continue ; // Now try to get the new lock
}
}
else if ( result ! = K LockFile: : LockFail )
else if ( result ! = TDE LockFile: : LockFail )
{
{
return result ;
return result ;
}
}
@ -351,12 +351,12 @@ KLockFile::LockResult KLockFile::lock(int options)
return result ;
return result ;
}
}
bool K LockFile: : isLocked ( ) const
bool TDE LockFile: : isLocked ( ) const
{
{
return d - > isLocked ;
return d - > isLocked ;
}
}
void K LockFile: : unlock ( )
void TDE LockFile: : unlock ( )
{
{
if ( d - > isLocked )
if ( d - > isLocked )
{
{
@ -365,7 +365,7 @@ void KLockFile::unlock()
}
}
}
}
bool K LockFile: : getLockInfo ( int & pid , TQString & hostname , TQString & appname )
bool TDE LockFile: : getLockInfo ( int & pid , TQString & hostname , TQString & appname )
{
{
if ( d - > pid = = - 1 )
if ( d - > pid = = - 1 )
return false ;
return false ;