comparison base/TempWriteFile.cpp @ 1365:3382d914e110

Merge from branch 3.0-integration
author Chris Cannam
date Fri, 13 Jan 2017 10:29:44 +0000
parents 1c9bbbb6116a
children 48e9f538e6e9
comparison
equal deleted inserted replaced
1272:6a7ea3bd0e10 1365:3382d914e110
25 { 25 {
26 QTemporaryFile temp(m_target + "."); 26 QTemporaryFile temp(m_target + ".");
27 temp.setAutoRemove(false); 27 temp.setAutoRemove(false);
28 temp.open(); // creates the file and opens it atomically 28 temp.open(); // creates the file and opens it atomically
29 if (temp.error()) { 29 if (temp.error()) {
30 cerr << "TempWriteFile: Failed to create temporary file in directory of " << m_target << ": " << temp.errorString() << endl; 30 SVCERR << "TempWriteFile: Failed to create temporary file in directory of " << m_target << ": " << temp.errorString() << endl;
31 throw FileOperationFailed(temp.fileName(), "creation"); 31 throw FileOperationFailed(temp.fileName(), "creation");
32 } 32 }
33 33
34 m_temp = temp.fileName(); 34 m_temp = temp.fileName();
35 temp.close(); // does not remove the file 35 temp.close(); // does not remove the file
52 void 52 void
53 TempWriteFile::moveToTarget() 53 TempWriteFile::moveToTarget()
54 { 54 {
55 if (m_temp == "") return; 55 if (m_temp == "") return;
56 56
57 QDir dir(QFileInfo(m_temp).dir()); 57 QFile tempFile(m_temp);
58 // According to http://doc.trolltech.com/4.4/qdir.html#rename 58 QFile targetFile(m_target);
59 // some systems fail, if renaming over an existing file. 59
60 // Therefore, delete first the existing file. 60 if (targetFile.exists()) {
61 if (dir.exists(m_target)) dir.remove(m_target); 61 if (!targetFile.remove()) {
62 if (!dir.rename(m_temp, m_target)) { 62 SVCERR << "TempWriteFile: WARNING: Failed to remove existing target file " << m_target << " prior to moving temporary file " << m_temp << " to it" << endl;
63 cerr << "TempWriteFile: Failed to rename temporary file " << m_temp << " to target " << m_target << endl; 63 }
64 }
65
66 if (!tempFile.rename(m_target)) {
67 SVCERR << "TempWriteFile: Failed to rename temporary file " << m_temp << " to target " << m_target << endl;
64 throw FileOperationFailed(m_temp, "rename"); 68 throw FileOperationFailed(m_temp, "rename");
65 } 69 }
66 70
67 m_temp = ""; 71 m_temp = "";
68 } 72 }