Skip to content

Commit e158642

Browse files
committedDec 25, 2015
change project/backup file only after preparing the data
This avoids corrupted project files when crashing during the preparation of the XML data to write to the project file, e.g. due to a faulty plugin.
1 parent 7c3cf64 commit e158642

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed
 

‎src/core/qgsproject.cpp

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,53 +1002,12 @@ bool QgsProject::write()
10021002
{
10031003
clearError();
10041004

1005-
// Create backup file
1006-
if ( QFile::exists( fileName() ) )
1007-
{
1008-
QFile backupFile( fileName() + '~' );
1009-
bool ok = true;
1010-
ok &= backupFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
1011-
ok &= imp_->file.open( QIODevice::ReadOnly );
1012-
1013-
QByteArray ba;
1014-
while ( ok && !imp_->file.atEnd() )
1015-
{
1016-
ba = imp_->file.read( 10240 );
1017-
ok &= backupFile.write( ba ) == ba.size();
1018-
}
1019-
1020-
imp_->file.close();
1021-
backupFile.close();
1022-
1023-
if ( !ok )
1024-
{
1025-
setError( tr( "Unable to create backup file %1" ).arg( backupFile.fileName() ) );
1026-
return false;
1027-
}
1028-
1029-
QFileInfo fi( fileName() );
1030-
struct utimbuf tb = { fi.lastRead().toTime_t(), fi.lastModified().toTime_t() };
1031-
utime( backupFile.fileName().toUtf8().constData(), &tb );
1032-
}
1033-
10341005
// if we have problems creating or otherwise writing to the project file,
10351006
// let's find out up front before we go through all the hand-waving
10361007
// necessary to create all the Dom objects
1037-
if ( !imp_->file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
1038-
{
1039-
imp_->file.close(); // even though we got an error, let's make
1040-
// sure it's closed anyway
1041-
1042-
setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
1043-
return false;
1044-
}
1045-
10461008
QFileInfo myFileInfo( imp_->file );
1047-
if ( !myFileInfo.isWritable() )
1009+
if ( myFileInfo.exists() && !myFileInfo.isWritable() )
10481010
{
1049-
// even though we got an error, let's make
1050-
// sure it's closed anyway
1051-
imp_->file.close();
10521011
setError( tr( "%1 is not writable. Please adjust permissions (if possible) and try again." )
10531012
.arg( imp_->file.fileName() ) );
10541013
return false;
@@ -1149,6 +1108,44 @@ bool QgsProject::write()
11491108
// now wrap it up and ship it to the project file
11501109
doc->normalize(); // XXX I'm not entirely sure what this does
11511110

1111+
// Create backup file
1112+
if ( QFile::exists( fileName() ) )
1113+
{
1114+
QFile backupFile( fileName() + '~' );
1115+
bool ok = true;
1116+
ok &= backupFile.open( QIODevice::WriteOnly | QIODevice::Truncate );
1117+
ok &= imp_->file.open( QIODevice::ReadOnly );
1118+
1119+
QByteArray ba;
1120+
while ( ok && !imp_->file.atEnd() )
1121+
{
1122+
ba = imp_->file.read( 10240 );
1123+
ok &= backupFile.write( ba ) == ba.size();
1124+
}
1125+
1126+
imp_->file.close();
1127+
backupFile.close();
1128+
1129+
if ( !ok )
1130+
{
1131+
setError( tr( "Unable to create backup file %1" ).arg( backupFile.fileName() ) );
1132+
return false;
1133+
}
1134+
1135+
QFileInfo fi( fileName() );
1136+
struct utimbuf tb = { fi.lastRead().toTime_t(), fi.lastModified().toTime_t() };
1137+
utime( backupFile.fileName().toUtf8().constData(), &tb );
1138+
}
1139+
1140+
if ( !imp_->file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
1141+
{
1142+
imp_->file.close(); // even though we got an error, let's make
1143+
// sure it's closed anyway
1144+
1145+
setError( tr( "Unable to save to file %1" ).arg( imp_->file.fileName() ) );
1146+
return false;
1147+
}
1148+
11521149
QTemporaryFile tempFile;
11531150
bool ok = tempFile.open();
11541151
if ( ok )

0 commit comments

Comments
 (0)
Please sign in to comment.