comparison 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
comparison
equal deleted inserted replaced
1324:d4a28d1479a8 1527:710e6250a401
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
36 } 36 }
37 37
38 TempWriteFile::~TempWriteFile() 38 TempWriteFile::~TempWriteFile()
39 { 39 {
40 if (m_temp != "") { 40 if (m_temp != "") {
41 QDir dir(QFileInfo(m_temp).dir()); 41 QDir dir(QFileInfo(m_temp).dir());
42 dir.remove(m_temp); 42 dir.remove(m_temp);
43 } 43 }
44 } 44 }
45 45
46 QString 46 QString
47 TempWriteFile::getTemporaryFilename() 47 TempWriteFile::getTemporaryFilename()
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 throw FileOperationFailed(m_temp, "rename"); 64 }
65
66 if (!tempFile.rename(m_target)) {
67 SVCERR << "TempWriteFile: Failed to rename temporary file " << m_temp << " to target " << m_target << endl;
68 throw FileOperationFailed(m_temp, "rename");
65 } 69 }
66 70
67 m_temp = ""; 71 m_temp = "";
68 } 72 }
69 73