Skip to content

Commit

Permalink
Make sure files are truncated before writing
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 14, 2016
1 parent b4d9f64 commit 06d2d79
Show file tree
Hide file tree
Showing 37 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/analysis/interpolation/qgsgridfilewriter.cpp
Expand Up @@ -48,7 +48,7 @@ int QgsGridFileWriter::writeFile( bool showProgressDialog )
{
QFile outputFile( mOutputFilePath );

if ( !outputFile.open( QFile::WriteOnly ) )
if ( !outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
return 1;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ int QgsGridFileWriter::writeFile( bool showProgressDialog )
QFileInfo fi( mOutputFilePath );
QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj";
QFile prjFile( fileName );
if ( !prjFile.open( QFile::WriteOnly ) )
if ( !prjFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/raster/qgsrelief.cpp
Expand Up @@ -574,7 +574,7 @@ bool QgsRelief::exportFrequencyDistributionToCsv( const QString& file )

//write out frequency values to csv file for debugging
QFile outFile( file );
if ( !outFile.open( QIODevice::WriteOnly ) )
if ( !outFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -2877,7 +2877,7 @@ void QgsComposer::exportCompositionAsSVG( QgsComposer::OutputMode mode )
QFileInfo fi( outputFileName );
QString currentFileName = i == 0 ? outputFileName : fi.absolutePath() + '/' + fi.baseName() + '_' + QString::number( i + 1 ) + '.' + fi.suffix();
QFile out( currentFileName );
bool openOk = out.open( QIODevice::WriteOnly | QIODevice::Text );
bool openOk = out.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate );
if ( !openOk )
{
QMessageBox::warning( this, tr( "SVG export error" ),
Expand Down Expand Up @@ -3099,7 +3099,7 @@ void QgsComposer::on_mActionSaveAsTemplate_triggered()
settings.setValue( "UI/lastComposerTemplateDir", saveFileInfo.absolutePath() );

QFile templateFile( saveFileName );
if ( !templateFile.open( QIODevice::WriteOnly ) )
if ( !templateFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
return;
}
Expand Down Expand Up @@ -4068,7 +4068,7 @@ void QgsComposer::createComposerView()
void QgsComposer::writeWorldFile( const QString& worldFileName, double a, double b, double c, double d, double e, double f ) const
{
QFile worldFile( worldFileName );
if ( !worldFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !worldFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/main.cpp
Expand Up @@ -1229,7 +1229,7 @@ int main( int argc, char *argv[] )
QFile dxfFile;
if ( dxfOutputFile == "-" )
{
if ( !dxfFile.open( stdout, QIODevice::WriteOnly ) )
if ( !dxfFile.open( stdout, QIODevice::WriteOnly | QIODevice::Truncate ) )
{
std::cerr << "could not open stdout" << std::endl;
return 2;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsbookmarks.cpp
Expand Up @@ -372,7 +372,7 @@ void QgsBookmarks::exportToXml()
}

QFile f( fileName );
if ( !f.open( QFile::WriteOnly ) )
if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
f.close();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -1281,7 +1281,7 @@ void QgsRasterLayerProperties::on_pbnExportTransparentPixelValues_clicked()
}

QFile myOutputFile( myFileName );
if ( myOutputFile.open( QFile::WriteOnly ) )
if ( myOutputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
QTextStream myOutputStream( &myOutputFile );
myOutputStream << "# " << tr( "QGIS Generated Transparent Pixel Value Export File" ) << '\n';
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgssubstitutionlistwidget.cpp
Expand Up @@ -107,7 +107,7 @@ void QgsSubstitutionListWidget::on_mButtonExport_clicked()
doc.appendChild( root );

QFile file( fileName );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
QMessageBox::warning( nullptr, tr( "Export substitutions" ),
tr( "Cannot write file %1:\n%2." ).arg( fileName, file.errorString() ),
Expand Down
2 changes: 1 addition & 1 deletion src/core/auth/qgsauthcertutils.cpp
Expand Up @@ -253,7 +253,7 @@ QString QgsAuthCertUtils::pemTextToTempFile( const QString &name, const QByteArr
QFile pemFile( QDir::tempPath() + QDir::separator() + name );
QString pemFilePath( pemFile.fileName() );

if ( pemFile.open( QIODevice::WriteOnly ) )
if ( pemFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
qint64 bytesWritten = pemFile.write( pemtext );
if ( bytesWritten == -1 )
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsapplication.cpp
Expand Up @@ -494,7 +494,7 @@ void QgsApplication::setUITheme( const QString &themeName )

if ( variableInfo.exists() && variablesfile.open( QIODevice::ReadOnly ) )
{
if ( !file.open( QIODevice::ReadOnly ) || !fileout.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !file.open( QIODevice::ReadOnly ) || !fileout.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsrenderchecker.cpp
Expand Up @@ -230,7 +230,7 @@ bool QgsRenderChecker::runTest( const QString& theTestName,
//create a world file to go with the image...

QFile wldFile( QDir::tempPath() + '/' + theTestName + "_result.wld" );
if ( wldFile.open( QIODevice::WriteOnly ) )
if ( wldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QgsRectangle r = mMapSettings.extent();

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsscaleutils.cpp
Expand Up @@ -34,7 +34,7 @@ bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &s
}

QFile file( fileName );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
errorMessage = QString( "Cannot write file %1:\n%2." ).arg( fileName, file.errorString() );
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorfilewriter.cpp
Expand Up @@ -363,7 +363,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
{
QString layerName = vectorFileName.left( vectorFileName.indexOf( ".shp", Qt::CaseInsensitive ) );
QFile prjFile( layerName + ".qpj" );
if ( prjFile.open( QIODevice::WriteOnly ) )
if ( prjFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream prjStream( &prjFile );
prjStream << srs.toWkt().toLocal8Bit().constData() << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgsstyle.cpp
Expand Up @@ -1388,7 +1388,7 @@ bool QgsStyle::exportXml( const QString& filename )

// save
QFile f( filename );
if ( !f.open( QFile::WriteOnly ) )
if ( !f.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
mErrorString = "Couldn't open file for writing: " + filename;
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssymbollayerutils.cpp
Expand Up @@ -2950,7 +2950,7 @@ QMimeData* QgsSymbolLayerUtils::colorListToMimeData( const QgsNamedColorList& co

bool QgsSymbolLayerUtils::saveColorsToGpl( QFile &file, const QString& paletteName, const QgsNamedColorList& colors )
{
if ( !file.open( QIODevice::ReadWrite ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsconfigureshortcutsdialog.cpp
Expand Up @@ -139,7 +139,7 @@ void QgsConfigureShortcutsDialog::saveShortcuts()
}

QFile file( fileName );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
QMessageBox::warning( this, tr( "Saving shortcuts" ),
tr( "Cannot write file %1:\n%2." )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmanageconnectionsdialog.cpp
Expand Up @@ -133,7 +133,7 @@ void QgsManageConnectionsDialog::doExportImport()
}

QFile file( mFileName );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
QMessageBox::warning( this, tr( "Saving connections" ),
tr( "Cannot write file %1:\n%2." )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmapcanvas.cpp
Expand Up @@ -782,7 +782,7 @@ void QgsMapCanvas::saveAsImage( const QString& theFileName, QPixmap * theQPixmap
QString myWorldFileName = myInfo.absolutePath() + '/' + myInfo.baseName() + '.'
+ outputSuffix.at( 0 ) + outputSuffix.at( myInfo.suffix().size() - 1 ) + 'w';
QFile myWorldFile( myWorldFileName );
if ( !myWorldFile.open( QIODevice::WriteOnly ) ) //don't use QIODevice::Text
if ( !myWorldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) ) //don't use QIODevice::Text
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgssearchquerybuilder.cpp
Expand Up @@ -379,7 +379,7 @@ void QgsSearchQueryBuilder::saveQuery()
}

QFile saveFile( saveFileName );
if ( !saveFile.open( QIODevice::WriteOnly ) )
if ( !saveFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QMessageBox::critical( nullptr, tr( "Error" ), tr( "Could not open file for writing" ) );
return;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/raster/qgsrastertransparencywidget.cpp
Expand Up @@ -251,7 +251,7 @@ void QgsRasterTransparencyWidget::on_pbnExportTransparentPixelValues_clicked()
}

QFile myOutputFile( myFileName );
if ( myOutputFile.open( QFile::WriteOnly ) )
if ( myOutputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
QTextStream myOutputStream( &myOutputFile );
myOutputStream << "# " << tr( "QGIS Generated Transparent Pixel Value Export File" ) << '\n';
Expand Down
2 changes: 1 addition & 1 deletion src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp
Expand Up @@ -670,7 +670,7 @@ void QgsSingleBandPseudoColorRendererWidget::on_mExportToFileButton_clicked()
}

QFile outputFile( fileName );
if ( outputFile.open( QFile::WriteOnly ) )
if ( outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
{
QTextStream outputStream( &outputFile );
outputStream << "# " << tr( "QGIS Generated Color Map Export File" ) << '\n';
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/georeferencer/qgsgeorefplugingui.cpp
Expand Up @@ -1280,7 +1280,7 @@ bool QgsGeorefPluginGui::loadGCPs( /*bool verbose*/ )
void QgsGeorefPluginGui::saveGCPs()
{
QFile pointFile( mGCPpointsFileName );
if ( pointFile.open( QIODevice::WriteOnly ) )
if ( pointFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream points( &pointFile );
points << "mapX,mapY,pixelX,pixelY,enable" << endl;
Expand Down Expand Up @@ -1422,7 +1422,7 @@ bool QgsGeorefPluginGui::writeWorldFile( const QgsPoint& origin, double pixelXSi
{
// write the world file
QFile file( mWorldFileName );
if ( !file.open( QIODevice::WriteOnly ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
mMessageBar->pushMessage( tr( "Error" ), tr( "Could not write to %1." ).arg( mWorldFileName ), QgsMessageBar::CRITICAL, messageTimeout() );
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/grass/qgsgrassmapcalc.cpp
Expand Up @@ -1052,7 +1052,7 @@ void QgsGrassMapcalc::save()
+ "/mapcalc/" + mFileName;

QFile out( path );
if ( !out.open( QIODevice::WriteOnly ) )
if ( !out.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QMessageBox::warning( this, tr( "Save mapcalc" ),
tr( "Cannot open mapcalc file" ) );
Expand Down
Expand Up @@ -249,7 +249,7 @@ void QgsRasterTerrainAnalysisDialog::on_mExportColorsButton_clicked()
}

QFile outputFile( file );
if ( !outputFile.open( QIODevice::WriteOnly ) )
if ( !outputFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/providers/gpx/qgsgpxprovider.cpp
Expand Up @@ -203,7 +203,7 @@ bool QgsGPXProvider::addFeatures( QgsFeatureList & flist )

// write back to file
QFile file( mFileName );
if ( !file.open( QIODevice::WriteOnly ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
return false;
QTextStream ostr( &file );
data->writeXml( ostr );
Expand Down Expand Up @@ -391,7 +391,7 @@ bool QgsGPXProvider::deleteFeatures( const QgsFeatureIds & id )

// write back to file
QFile file( mFileName );
if ( !file.open( QIODevice::WriteOnly ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
return false;
QTextStream ostr( &file );
data->writeXml( ostr );
Expand Down Expand Up @@ -441,7 +441,7 @@ bool QgsGPXProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_

// write back to file
QFile file( mFileName );
if ( !file.open( QIODevice::WriteOnly ) )
if ( !file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
return false;
QTextStream ostr( &file );
data->writeXml( ostr );
Expand Down
4 changes: 2 additions & 2 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -74,7 +74,7 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
char* pszOutWkt = nullptr;
OSRExportToWkt( hSRS, &pszOutWkt );
QFile prjFile( layerName + ".prj" );
if ( prjFile.open( QIODevice::WriteOnly ) )
if ( prjFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream prjStream( &prjFile );
prjStream << pszOutWkt << endl;
Expand All @@ -90,7 +90,7 @@ bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs )

// save qgis-specific .qpj file (maybe because of better wkt compatibility?)
QFile qpjFile( layerName + ".qpj" );
if ( qpjFile.open( QIODevice::WriteOnly ) )
if ( qpjFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream qpjStream( &qpjFile );
qpjStream << wkt.toLocal8Bit().data() << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -2663,7 +2663,7 @@ QGISEXTERN bool createEmptyDataSource( const QString &uri,
{
QString layerName = uri.left( uri.indexOf( ".shp", Qt::CaseInsensitive ) );
QFile prjFile( layerName + ".qpj" );
if ( prjFile.open( QIODevice::WriteOnly ) )
if ( prjFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream prjStream( &prjFile );
prjStream << myWkt.toLocal8Bit().data() << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wcs/qgswcscapabilities.cpp
Expand Up @@ -427,7 +427,7 @@ bool QgsWcsCapabilities::parseCapabilitiesDom( QByteArray const &xml, QgsWcsCapa
QgsDebugMsg( "Entered." );
#ifdef QGISDEBUG
QFile file( QDir::tempPath() + "/qgis-wcs-capabilities.xml" );
if ( file.open( QIODevice::WriteOnly ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
file.write( xml );
file.close();
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wfs/qgswfsfeatureiterator.cpp
Expand Up @@ -904,7 +904,7 @@ void QgsWFSFeatureIterator::featureReceivedSynchronous( QVector<QgsWFSFeatureGml
mWriterFilename = QDir( QgsWFSUtils::acquireCacheDirectory() ).filePath( QString( "iterator_%1_%2.bin" ).arg( thisStr ).arg( mCounter ) );
QgsDebugMsg( QString( "Transferring feature iterator cache to %1" ).arg( mWriterFilename ) );
mWriterFile = new QFile( mWriterFilename );
if ( !mWriterFile->open( QIODevice::WriteOnly ) )
if ( !mWriterFile->open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QgsDebugMsg( QString( "Cannot open %1 for writing" ).arg( mWriterFilename ) );
delete mWriterFile;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wfs/qgswfsshareddata.cpp
Expand Up @@ -354,7 +354,7 @@ bool QgsWFSSharedData::createCache()

// Copy the in-memory template Spatialite DB into the target DB
QFile dbFile( mCacheDbname );
if ( !dbFile.open( QIODevice::WriteOnly ) )
if ( !dbFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QgsMessageLog::logMessage( tr( "Cannot create temporary SpatiaLite cache" ), tr( "WFS" ) );
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wms/qgswmscapabilities.cpp
Expand Up @@ -249,7 +249,7 @@ bool QgsWmsCapabilities::parseCapabilitiesDom( QByteArray const &xml, QgsWmsCapa

#ifdef QGISDEBUG
QFile file( QDir::tempPath() + "/qgis-wmsprovider-capabilities.xml" );
if ( file.open( QIODevice::WriteOnly ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
file.write( xml );
file.close();
Expand Down
2 changes: 1 addition & 1 deletion src/providers/wms/qgswmsprovider.cpp
Expand Up @@ -3835,7 +3835,7 @@ void QgsWmsTiledImageDownloadHandler::tileReplyFinished()
.arg( reply->url().toString() ), tr( "WMS" ) );
#ifdef QGISDEBUG
QFile file( QDir::tempPath() + "/broken-image.png" );
if ( file.open( QIODevice::WriteOnly ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
file.write( text );
file.close();
Expand Down
2 changes: 1 addition & 1 deletion src/server/qgsmsutils.cpp
Expand Up @@ -70,7 +70,7 @@ QString QgsMSUtils::createTempFilePath()
int QgsMSUtils::createTextFile( const QString& filePath, const QString& text )
{
QFile file( filePath );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) )
{
QTextStream fileStream( &file );
fileStream << text;
Expand Down