Details
Description
On Windows, QFile::rename() fails if names differ only in case. The Windows filesystem is not case sensitive for manipulating files, but the case is stored on the filesystem.
Therefore, renaming the file qtsoftware.txt to QtSoftware.txt is usually possible on Windows but it fails with Qt.
Here is a test case to reproduce the issue:
#include <QtTest/QtTest> #include <QtCore/qfile.h> #include <stdio.h> #include <unistd.h> class Test : public QObject\{ Q_OBJECT private: void createFile() \{ QFile file("qtsoftware.txt"); file.open(QIODevice::WriteOnly); file.close(); } void cleanFiles() \{ unlink("QtSoftware.txt"); unlink("qtsoftware.txt"); } private slots: void testRenameChangeFilename() \{ cleanFiles(); createFile(); QFile file("qtsoftware.txt"); // rename QCOMPARE(file.rename("QtSoftware.txt"), true); cleanFiles(); } }; QTEST_MAIN(Test) #include "test.moc"
Here is a patch for the issue, if the name differs only by the case, it uses the fileEngine to rename the file. A better patch should check if the filesystem is case sensitive instead of checking if it is windows (the filesystem of mac can be case sensitive or not depending on the configuration for example):
diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index dde159f..353fa11 100644 — a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -671,6 +671,15 @@ QFile::rename(const QString &newName) qWarning("QFile::rename: Empty or null file name"); return false; } +#if defined(Q_OS_WIN) || defined(Q_OS_WINCE) + QString currentName = fileName(); + if (newName.compare(currentName, Qt::CaseInsensitive) == 0 && currentName != newName) \{ + if (fileEngine()->rename(newName)) \{ + unsetError(); + return true; + } + } +#endif if (QFile(newName).exists()) \{ // ### Race condition. If a file is moved in after this, it /will/ be // overwritten. On Unix, the proper solution is to use hardlinks:
Attachments
Issue Links
- is required for
-
QTCREATORBUG-2434 Cannot change letter case while renaming a file under windows
- Closed
- relates to
-
QTBUG-28246 QFileSystemEngine::isCaseSensitive() is hardcoded to return false on Windows and true on Unix
- Open
-
QTCREATORBUG-5076 File name cannot be changed in Qt Creator.
- Closed
-
QTCREATORBUG-13981 [REG 3.3 -> 3.4] Renaming file doesn't work correctly when changing case only
- Closed
- replaces
-
QTCREATORBUG-9197 Renaming file by changing letter case fails on Mac
- Closed
-
QTBUG-2086 QDir::rename() does not change folder names to uppercase
- Closed
For Gerrit Dashboard: QTBUG-3570 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
40848,6 | Rename files that differ only in case. | master | qt/qtbase | Status: ABANDONED | -1 | 0 |
41133,2 | Improve auto-test of QFile. | master | qt/qtbase | Status: MERGED | +2 | 0 |
41535,8 | Fix renaming of files that differ only in case. | dev | qt/qtbase | Status: ABANDONED | 0 | 0 |
44093,1 | WIP: Fix renaming of files that differ only in case - v3. | stable | qt/qtbase | Status: ABANDONED | 0 | 0 |