From f39872b24ed2db4c3ee5c6c7f881a3c0013183c7 Mon Sep 17 00:00:00 2001 From: Jonathan Young Date: Sat, 17 May 2014 14:26:26 +1000 Subject: [PATCH] Fixed file transaction handling when the destination file doesn't exist. --- filecopy.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/filecopy.cpp b/filecopy.cpp index 314575e..7925a59 100644 --- a/filecopy.cpp +++ b/filecopy.cpp @@ -59,19 +59,35 @@ void FileCopyWorker::copy() goto cleanup; } - const QString tempDestFilename = FileUtils::uniqueFilename(files.at(i).dest); - QFile destinationFile(tempDestFilename); + // Write to the destination file if it doesn't exist, otherwise write to a uniquely named file. + const bool destExists = QFile::exists(files.at(i).dest); + QString destFilename; + + if (destExists) + { + destFilename = FileUtils::uniqueFilename(files.at(i).dest); + } + else + { + destFilename = files.at(i).dest; + } + + QFile destinationFile(destFilename); if (!destinationFile.open(QIODevice::WriteOnly)) { - emit errorMessage(QString("'%1': %2").arg(tempDestFilename).arg(destinationFile.errorString())); + emit errorMessage(QString("'%1': %2").arg(destFilename).arg(destinationFile.errorString())); goto cleanup; } - FileOperation fo; - fo.source = tempDestFilename; - fo.dest = files.at(i).dest; - renameOperations.append(fo); + // Don't add a rename operation if the destination file doesn't exist - not trying to overwrite, don't need to rename anything to complete the transaction. + if (destExists) + { + FileOperation fo; + fo.source = destFilename; + fo.dest = files.at(i).dest; + renameOperations.append(fo); + } emit fileChanged(QFileInfo(sourceFile.fileName()).fileName()); const qint64 totalBytes = sourceFile.size();