@@ -1002,42 +1002,12 @@ bool QgsProject::write()
1002
1002
{
1003
1003
clearError ();
1004
1004
1005
- // Create backup file
1006
- if ( QFile::exists ( fileName () ) )
1007
- {
1008
- QString backup = fileName () + ' ~' ;
1009
- if ( QFile::exists ( backup ) )
1010
- QFile::remove ( backup );
1011
-
1012
- if ( !QFile::copy ( fileName (), backup ) )
1013
- {
1014
- setError ( tr ( " Unable to create backup file %1" ).arg ( backup ) );
1015
- return false ;
1016
- }
1017
-
1018
- QFileInfo fi ( fileName () );
1019
- struct utimbuf tb = { fi.lastRead ().toTime_t (), fi.lastModified ().toTime_t () };
1020
- utime ( backup.toUtf8 ().constData (), &tb );
1021
- }
1022
-
1023
1005
// if we have problems creating or otherwise writing to the project file,
1024
1006
// let's find out up front before we go through all the hand-waving
1025
1007
// necessary to create all the Dom objects
1026
- if ( !imp_->file .open ( QIODevice::WriteOnly | QIODevice::Truncate ) )
1027
- {
1028
- imp_->file .close (); // even though we got an error, let's make
1029
- // sure it's closed anyway
1030
-
1031
- setError ( tr ( " Unable to save to file %1" ).arg ( imp_->file .fileName () ) );
1032
- return false ;
1033
- }
1034
-
1035
1008
QFileInfo myFileInfo ( imp_->file );
1036
- if ( !myFileInfo.isWritable () )
1009
+ if ( myFileInfo. exists () && !myFileInfo.isWritable () )
1037
1010
{
1038
- // even though we got an error, let's make
1039
- // sure it's closed anyway
1040
- imp_->file .close ();
1041
1011
setError ( tr ( " %1 is not writable. Please adjust permissions (if possible) and try again." )
1042
1012
.arg ( imp_->file .fileName () ) );
1043
1013
return false ;
@@ -1138,6 +1108,44 @@ bool QgsProject::write()
1138
1108
// now wrap it up and ship it to the project file
1139
1109
doc->normalize (); // XXX I'm not entirely sure what this does
1140
1110
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
+
1141
1149
QTemporaryFile tempFile;
1142
1150
bool ok = tempFile.open ();
1143
1151
if ( ok )
0 commit comments