Skip to content

Commit 78b04df

Browse files
committed
ogr provider: reopen the data source if a REPACK failed (fixes #7540)
1 parent aca3dfa commit 78b04df

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

src/providers/ogr/qgsogrprovider.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ bool QgsOgrProvider::addFeatures( QgsFeatureList & flist )
969969
{
970970
returnvalue = false;
971971
}
972+
972973
recalculateFeatureCount();
973974

974975
if ( returnvalue )
@@ -1139,7 +1140,10 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap & attr
11391140
}
11401141
}
11411142

1142-
OGR_L_SyncToDisk( ogrLayer );
1143+
if ( OGR_L_SyncToDisk( ogrLayer ) != OGRERR_NONE )
1144+
{
1145+
pushError( tr( "OGR error syncing to disk: %1" ).arg( CPLGetLastErrorMsg() ) );
1146+
}
11431147
return true;
11441148
}
11451149

@@ -1254,11 +1258,39 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )
12541258
returnvalue = false;
12551259
}
12561260

1257-
QString layerName = FROM8( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );
1261+
// run REPACK on shape files
1262+
if ( ogrDriverName == "ESRI Shapefile" )
1263+
{
1264+
QString layerName = FROM8( OGR_FD_GetName( OGR_L_GetLayerDefn( ogrOrigLayer ) ) );
12581265

1259-
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
1260-
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
1261-
OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).constData(), NULL, NULL );
1266+
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
1267+
QgsDebugMsg( QString( "SQL: %1" ).arg( sql ) );
1268+
OGR_DS_ExecuteSQL( ogrDataSource, mEncoding->fromUnicode( sql ).constData(), NULL, NULL );
1269+
1270+
if ( mFilePath.endsWith( ".shp", Qt::CaseInsensitive ) || mFilePath.endsWith( ".dbf", Qt::CaseInsensitive ) )
1271+
{
1272+
QString packedDbf( mFilePath.left( mFilePath.size() - 4 ) + "_packed.dbf" );
1273+
if ( QFile::exists( packedDbf ) )
1274+
{
1275+
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 );
1276+
1277+
OGR_DS_Destroy( ogrDataSource );
1278+
1279+
ogrDataSource = OGROpen( TO8F( mFilePath ), true, NULL );
1280+
1281+
if ( mLayerName.isNull() )
1282+
{
1283+
ogrOrigLayer = OGR_DS_GetLayer( ogrDataSource, mLayerIndex );
1284+
}
1285+
else
1286+
{
1287+
ogrOrigLayer = OGR_DS_GetLayerByName( ogrDataSource, TO8( mLayerName ) );
1288+
}
1289+
1290+
ogrLayer = ogrOrigLayer;
1291+
}
1292+
}
1293+
}
12621294

12631295
recalculateFeatureCount();
12641296

@@ -2254,7 +2286,10 @@ QString QgsOgrProvider::quotedIdentifier( QString field )
22542286

22552287
bool QgsOgrProvider::syncToDisc()
22562288
{
2257-
OGR_L_SyncToDisk( ogrLayer );
2289+
if ( OGR_L_SyncToDisk( ogrLayer ) != OGRERR_NONE )
2290+
{
2291+
pushError( tr( "OGR error syncing to disk: %1" ).arg( CPLGetLastErrorMsg() ) );
2292+
}
22582293

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

0 commit comments

Comments
 (0)