From d3dc0f21225845f404262ea563870044cbbbe53f Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 9 Jan 2013 09:11:17 +0100 Subject: Fix renaming of files that differ only in case. This currently fails on case-insensitive file systems since the check for existence then triggered and indicated "file already exists". Check on the file id (inode or file id) whether the target file is really a different file for a case-changing rename. Task-number: QTBUG-3570 Change-Id: I1b2d40850692e02142ee23d2c753428de00aedc6 Reviewed-by: Oswald Buddenhagen Reviewed-by: Thiago Macieira --- src/corelib/io/qfile.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/corelib/io/qfile.cpp') diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index d9f2c5c605..24f0eba1b7 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -48,6 +48,7 @@ #include "qfileinfo.h" #include "private/qiodevice_p.h" #include "private/qfile_p.h" +#include "private/qfilesystemengine_p.h" #include "private/qsystemerror_p.h" #if defined(QT_BUILD_CORE_LIB) # include "qcoreapplication.h" @@ -548,7 +549,19 @@ QFile::rename(const QString &newName) qWarning("QFile::rename: Empty or null file name"); return false; } - if (QFile(newName).exists()) { + if (d->fileName == newName) { + d->setError(QFile::RenameError, tr("Destination file is the same file.")); + return false; + } + if (!exists()) { + d->setError(QFile::RenameError, tr("Source file does not exist.")); + return false; + } + // If the file exists and it is a case-changing rename ("foo" -> "Foo"), + // compare Ids to make sure it really is a different file. + if (QFile::exists(newName) + && (d->fileName.compare(newName, Qt::CaseInsensitive) + || QFileSystemEngine::id(QFileSystemEntry(d->fileName)) != QFileSystemEngine::id(QFileSystemEntry(newName)))) { // ### Race condition. If a file is moved in after this, it /will/ be // overwritten. On Unix, the proper solution is to use hardlinks: // return ::link(old, new) && ::remove(old); -- cgit v1.2.3