@@ -1002,53 +1002,12 @@ bool QgsProject::write()
1002
1002
{
1003
1003
clearError ();
1004
1004
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
-
1034
1005
// if we have problems creating or otherwise writing to the project file,
1035
1006
// let's find out up front before we go through all the hand-waving
1036
1007
// 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
-
1046
1008
QFileInfo myFileInfo ( imp_->file );
1047
- if ( !myFileInfo.isWritable () )
1009
+ if ( myFileInfo. exists () && !myFileInfo.isWritable () )
1048
1010
{
1049
- // even though we got an error, let's make
1050
- // sure it's closed anyway
1051
- imp_->file .close ();
1052
1011
setError ( tr ( " %1 is not writable. Please adjust permissions (if possible) and try again." )
1053
1012
.arg ( imp_->file .fileName () ) );
1054
1013
return false ;
@@ -1149,6 +1108,44 @@ bool QgsProject::write()
1149
1108
// now wrap it up and ship it to the project file
1150
1109
doc->normalize (); // XXX I'm not entirely sure what this does
1151
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
+
1152
1149
QTemporaryFile tempFile;
1153
1150
bool ok = tempFile.open ();
1154
1151
if ( ok )
0 commit comments