Skip to content
Permalink
Browse files

ogr provider: reopen the data source if a REPACK failed (fixes #7540)

  • Loading branch information
jef-n committed Jul 9, 2013
1 parent aca3dfa commit 78b04df91a1b85ee70ff5b326980f480b3069d47
Showing with 41 additions and 6 deletions.
  1. +41 −6 src/providers/ogr/qgsogrprovider.cpp
@@ -969,6 +969,7 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
{
returnvalue = false;
}

recalculateFeatureCount();

if ( returnvalue )
@@ -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;
}

@@ -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();

@@ -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() )

1 comment on commit 78b04df

@rouault

This comment has been minimized.

Copy link
Contributor

@rouault rouault commented on 78b04df Jul 9, 2013

I've added a few inline comments

Please sign in to comment.
You can’t perform that action at this time.