Skip to content

Commit

Permalink
[GRASS] fixed running modules in mapsets not owned by user
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Oct 19, 2015
1 parent 6261a38 commit 483f3ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
28 changes: 9 additions & 19 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1922,35 +1922,25 @@ QProcess *QgsGrass::startModule( const QString& gisdbase, const QString& locati

QString error = tr( "Cannot start module" ) + "\n" + tr( "command: %1 %2" ).arg( module, arguments.join( " " ) );

// Modules must be run in a mapset owned by user, because each module calls G_gisinit()
// which checks if G_mapset() is owned by user.
QString ownedMapset = mapset;
if ( ownedMapset.isEmpty() )
QTextStream out( &gisrcFile );
out << "GISDBASE: " << gisdbase << "\n";
out << "LOCATION_NAME: " << location << "\n";
if ( mapset.isEmpty() )
{
Q_FOREACH ( const QString& ms, mapsets( gisdbase, location ) )
{
if ( isOwner( gisdbase, location, ms ) )
{
ownedMapset = ms;
}
}
out << "MAPSET: PERMANENT\n";
}
if ( ownedMapset.isEmpty() )
else
{
throw QgsGrass::Exception( error + "\n" + tr( "Cannot find a mapset owned by current user" ) );
out << "MAPSET: " << mapset << "\n";
}

QTextStream out( &gisrcFile );
out << "GISDBASE: " << gisdbase << "\n";
out << "LOCATION_NAME: " << location << "\n";
//out << "MAPSET: PERMANENT\n";
out << "MAPSET: " << ownedMapset << "\n";
out.flush();
QgsDebugMsg( gisrcFile.fileName() );
gisrcFile.close();
QStringList environment = QProcess::systemEnvironment();
environment.append( "GISRC=" + gisrcFile.fileName() );
environment.append( "GRASS_MESSAGE_FORMAT=gui" );
// Normaly modules must be run in a mapset owned by user, because each module calls G_gisinit()
// which checks if G_mapset() is owned by user. The check is disabled by GRASS_SKIP_MAPSET_OWNER_CHECK.
environment.append( "GRASS_SKIP_MAPSET_OWNER_CHECK=1" );

process->setEnvironment( environment );
Expand Down
16 changes: 3 additions & 13 deletions tests/src/providers/grass/testqgsgrassprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,6 @@ void TestQgsGrassProvider::info()
reportHeader( "TestQgsGrassProvider::info" );
bool ok = true;

// GRASS modules must be run in a mapset owned by user, the source code may have different user.
QString tmpGisdbase;
if ( !copyLocation( tmpGisdbase ) )
{
reportRow( "cannot copy location" );
GVERIFY( false );
return;
}

QgsRectangle expectedExtent( -5, -5, 5, 5 );
QMap<QString, QgsRasterBandStats> expectedStats;
QgsRasterBandStats es;
Expand All @@ -522,7 +513,7 @@ void TestQgsGrassProvider::info()
es = expectedStats.value( map );
// TODO: QgsGrass::info() may open dialog window on error which blocks tests
QString error;
QHash<QString, QString> info = QgsGrass::info( tmpGisdbase, mLocation, "test", map, QgsGrassObject::Raster, "stats",
QHash<QString, QString> info = QgsGrass::info( mGisdbase, mLocation, "test", map, QgsGrassObject::Raster, "stats",
expectedExtent, 10, 10, 5000, error );
if ( !error.isEmpty() )
{
Expand All @@ -541,7 +532,7 @@ void TestQgsGrassProvider::info()
compare( es.minimumValue, s.minimumValue, ok );
compare( es.maximumValue, s.maximumValue, ok );

QgsRectangle extent = QgsGrass::extent( tmpGisdbase, mLocation, "test", map, QgsGrassObject::Raster, error );
QgsRectangle extent = QgsGrass::extent( mGisdbase, mLocation, "test", map, QgsGrassObject::Raster, error );
reportRow( "expectedExtent: " + expectedExtent.toString() );
reportRow( "extent: " + extent.toString() );
if ( !error.isEmpty() )
Expand All @@ -561,7 +552,7 @@ void TestQgsGrassProvider::info()

reportRow( "expectedCrs: " + expectedCrs.toWkt() );
QString error;
QgsCoordinateReferenceSystem crs = QgsGrass::crs( tmpGisdbase, mLocation, error );
QgsCoordinateReferenceSystem crs = QgsGrass::crs( mGisdbase, mLocation, error );
if ( !error.isEmpty() )
{
ok = false;
Expand All @@ -583,7 +574,6 @@ void TestQgsGrassProvider::info()
}
}
}
removeRecursively( tmpGisdbase );
GVERIFY( ok );
}

Expand Down

0 comments on commit 483f3ee

Please sign in to comment.