Skip to content

Commit

Permalink
Fixing potential infinite loop when rescanning file too early and
Browse files Browse the repository at this point in the history
removing unnecessary multiple rescans as file is progressively updated
  • Loading branch information
ccrook committed Aug 12, 2013
1 parent 4ba9c47 commit 6b21b86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -77,6 +77,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
, mUseSubsetIndex( false )
, mMaxInvalidLines( 50 )
, mShowInvalidLines( true )
, mRescanRequired( false )
, mCrs()
, mWkbType( QGis::WKBNoGeometry )
, mGeometryType( QGis::UnknownGeometry )
Expand Down Expand Up @@ -319,6 +320,7 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )

mLayerValid = false;
mValid = false;
mRescanRequired = false;

clearInvalidLines();

Expand Down Expand Up @@ -675,6 +677,7 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )

void QgsDelimitedTextProvider::rescanFile()
{
mRescanRequired = false;
resetIndexes();

bool buildSpatialIndex = mSpatialIndex != 0;
Expand Down Expand Up @@ -717,6 +720,7 @@ void QgsDelimitedTextProvider::rescanFile()
reportErrors( messages, false );
QgsDebugMsg( "Delimited text source invalid on rescan - missing geometry fields" );
mValid = false;
return;
}

// Reset the field columns
Expand Down Expand Up @@ -864,8 +868,9 @@ void QgsDelimitedTextProvider::resetStream()

QgsFeatureIterator QgsDelimitedTextProvider::getFeatures( const QgsFeatureRequest& request )
{
// If the file has become invalid, check that it is still invalid.
if ( mLayerValid && ! mValid ) rescanFile();
// If the file has become invalid, rescan to check that it is still invalid.
//
if ( (mLayerValid && ! mValid) || mRescanRequired ) rescanFile();

return QgsFeatureIterator( new QgsDelimitedTextFeatureIterator( this, request ) );
}
Expand Down Expand Up @@ -1050,9 +1055,13 @@ void QgsDelimitedTextProvider::setUriParameter( QString parameter, QString value

void QgsDelimitedTextProvider::onFileUpdated()
{
QStringList messages;
messages.append( tr( "The file has been updated by another application - reloading" ) );
reportErrors( messages, false );
if( ! mRescanRequired )
{
QStringList messages;
messages.append( tr( "The file has been updated by another application - reloading" ) );
reportErrors( messages, false );
mRescanRequired = true;
}

while ( !mActiveIterators.empty() )
{
Expand All @@ -1061,7 +1070,6 @@ void QgsDelimitedTextProvider::onFileUpdated()
it->close();
}

rescanFile();
}

bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature, QgsDelimitedTextFile *file, QgsDelimitedTextFeatureIterator *iterator )
Expand All @@ -1079,9 +1087,12 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature, QgsDelimitedTex
first = false;

// before we do anything else, assume that there's something wrong with
// the feature
// the feature. If the provider is not currently valid, then cannot return
// feature.

feature.setValid( false );
if( ! mValid ) break;

QgsDelimitedTextFile::Status status = file->nextRecord( tokens );
if ( status == QgsDelimitedTextFile::RecordEOF ) break;
if ( status != QgsDelimitedTextFile::RecordOk ) continue;
Expand Down Expand Up @@ -1250,6 +1261,7 @@ void QgsDelimitedTextProvider::fetchAttribute( QgsFeature& feature, int fieldIdx
// Return the extent of the layer
QgsRectangle QgsDelimitedTextProvider::extent()
{
if( mRescanRequired ) rescanFile();
return mExtent;
}

Expand All @@ -1266,6 +1278,7 @@ QGis::WkbType QgsDelimitedTextProvider::geometryType() const
*/
long QgsDelimitedTextProvider::featureCount() const
{
if( mRescanRequired ) const_cast<QgsDelimitedTextProvider *>(this)->rescanFile();
return mNumberFeatures;
}

Expand Down
3 changes: 3 additions & 0 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.h
Expand Up @@ -296,6 +296,9 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
//! Only want to show the invalid lines once to the user
bool mShowInvalidLines;

//! Record file updates, flags rescan required
bool mRescanRequired;

struct wkbPoint
{
unsigned char byteOrder;
Expand Down

0 comments on commit 6b21b86

Please sign in to comment.