Skip to content

Commit

Permalink
ogr provider: reopen the data source if a REPACK failed (fixes #7540)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jul 9, 2013
1 parent aca3dfa commit 78b04df
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -969,6 +969,7 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
{
returnvalue = false;
}

recalculateFeatureCount();

if ( returnvalue )
Expand Down Expand Up @@ -1139,7 +1140,10 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr
}
}

OGR_L_SyncToDisk( ogrLayer );
if ( OGR_L_SyncToDisk( ogrLayer ) != OGRERR_NONE )
{
pushError( tr( "OGR error syncing to disk: %1" ).arg( CPLGetLastErrorMsg() ) );
}
return true;
}

Expand Down Expand Up @@ -1254,11 +1258,39 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )
returnvalue = false;
}

QString layerName = FROM8( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );
// run REPACK on shape files
if ( ogrDriverName == "ESRI Shapefile" )
{
QString layerName = FROM8( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );

QString sql = QString( "REPACK %1" ).arg( layerName ); // don't quote the layer name as it works with spaces in the name and won't work if the name is quoted
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).constData(), NULL, NULL );
QString sql = QString( "REPACK %1" ).arg( layerName ); // don't quote the layer name as it works with spaces in the name and won't work if the name is quoted
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).constData(), NULL, NULL );

if ( mFilePath.endsWith( ".shp", Qt::CaseInsensitive ) || mFilePath.endsWith( ".dbf", Qt::CaseInsensitive ) )
{
QString packedDbf( mFilePath.left( mFilePath.size() - 4 ) + "_packed.dbf" );
if ( QFile::exists( packedDbf ) )
{
QgsMessageLog::logMessage( tr( "Possible corruption after REPACK detected. %1 still exists. This may point to a permission or locking problem of the original DBF." ).arg( packedDbf ), tr( "OGR" ), QgsMessageLog::CRITICAL );

OGR_DS_Destroy( ogrDataSource );

ogrDataSource = OGROpen( TO8F( mFilePath ), true, NULL );

This comment has been minimized.

Copy link
@rouault

rouault Jul 9, 2013

Contributor

ogrDataSource should be tested against NULL just in case...


if ( mLayerName.isNull() )
{
ogrOrigLayer = OGR_DS_GetLayer( ogrDataSource, mLayerIndex );
}
else
{
ogrOrigLayer = OGR_DS_GetLayerByName( ogrDataSource, TO8( mLayerName ) );
}

ogrLayer = ogrOrigLayer;

This comment has been minimized.

Copy link
@rouault

rouault Jul 9, 2013

Contributor

same here on ogrLayer

}
}
}

recalculateFeatureCount();

Expand Down Expand Up @@ -2254,7 +2286,10 @@ QString QgsOgrProvider::quotedIdentifier( QString field )

bool QgsOgrProvider::syncToDisc()
{
OGR_L_SyncToDisk( ogrLayer );
if ( OGR_L_SyncToDisk( ogrLayer ) != OGRERR_NONE )
{
pushError( tr( "OGR error syncing to disk: %1" ).arg( CPLGetLastErrorMsg() ) );
}

//for shapefiles: is there already a spatial index?
if ( !mFilePath.isEmpty() )
Expand Down

1 comment on commit 78b04df

@rouault
Copy link
Contributor

@rouault rouault commented on 78b04df Jul 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a few inline comments

Please sign in to comment.