diff base/TempWriteFile.cpp @ 1527:710e6250a401 zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:14 +0100
parents 48e9f538e6e9
children
line wrap: on
line diff
--- a/base/TempWriteFile.cpp	Mon Dec 12 15:18:52 2016 +0000
+++ b/base/TempWriteFile.cpp	Mon Sep 17 13:51:14 2018 +0100
@@ -27,8 +27,8 @@
     temp.setAutoRemove(false);
     temp.open(); // creates the file and opens it atomically
     if (temp.error()) {
-	cerr << "TempWriteFile: Failed to create temporary file in directory of " << m_target << ": " << temp.errorString() << endl;
-	throw FileOperationFailed(temp.fileName(), "creation");
+        SVCERR << "TempWriteFile: Failed to create temporary file in directory of " << m_target << ": " << temp.errorString() << endl;
+        throw FileOperationFailed(temp.fileName(), "creation");
     }
     
     m_temp = temp.fileName();
@@ -38,8 +38,8 @@
 TempWriteFile::~TempWriteFile()
 {
     if (m_temp != "") {
-	QDir dir(QFileInfo(m_temp).dir());
-	dir.remove(m_temp);
+        QDir dir(QFileInfo(m_temp).dir());
+        dir.remove(m_temp);
     }
 }
 
@@ -54,14 +54,18 @@
 {
     if (m_temp == "") return;
 
-    QDir dir(QFileInfo(m_temp).dir());
-    // According to  http://doc.trolltech.com/4.4/qdir.html#rename
-    // some systems fail, if renaming over an existing file.
-    // Therefore, delete first the existing file.
-    if (dir.exists(m_target)) dir.remove(m_target);
-    if (!dir.rename(m_temp, m_target)) {
-	cerr << "TempWriteFile: Failed to rename temporary file " << m_temp << " to target " << m_target << endl;
-	throw FileOperationFailed(m_temp, "rename");
+    QFile tempFile(m_temp);
+    QFile targetFile(m_target);
+    
+    if (targetFile.exists()) {
+        if (!targetFile.remove()) {
+            SVCERR << "TempWriteFile: WARNING: Failed to remove existing target file " << m_target << " prior to moving temporary file " << m_temp << " to it" << endl;
+        }
+    }
+    
+    if (!tempFile.rename(m_target)) {
+        SVCERR << "TempWriteFile: Failed to rename temporary file " << m_temp << " to target " << m_target << endl;
+        throw FileOperationFailed(m_temp, "rename");
     }
 
     m_temp = "";