Use "native paths" on POSIX platforms as well
authorJoão Abecasis <joao.abecasis@nokia.com>
Fri, 5 Aug 2011 08:49:44 +0000 (10:49 +0200)
committerQt by Nokia <qt-info@nokia.com>
Fri, 21 Oct 2011 20:20:34 +0000 (22:20 +0200)
And don't rely solely on "local8Bit" conversions.

QFile defines an API for overriding how encoding conversions are done
for filenames. In generating unique names, QTemporaryFile ignored that
API and hardcoded the use of local 8-bit, implicitly assuming that that
was appropriate.

With this change, we switch that assumption to one where user supplied
encoding function keeps the byte value of 'X' and '/', also assuming
that encoded 'X' takes up a single-byte (i.e., the byte sequence for
"XXXXXX" remains unchanged).

There was also, and there still is an assumption in name generation that
byte values for ASCII alpha-numeric characters are valid in the "native"
encoding.

In practice this change is compatible with UTF-8, Latin-1 and other
ISO/IEC 8859 encodings. At any rate, it's very likely that only UTF-8 is
relevant here.

Reviewed-by: Denis Dzyubenko
(cherry picked from commit 0de701d01cb221464eed773fd3751aff73fe4d60)

Change-Id: I9ee0fe8e3cad48694d5ec9a2bedd5412cfc0d172
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
src/corelib/io/qtemporaryfile.cpp

index b86dbf9..82d67ab 100644 (file)
@@ -366,14 +366,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
     if (!filePathIsTemplate)
         return QFSFileEngine::open(openMode);
 
-    const QString qfilename =
-#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
-        // Since the native encoding is out of our control, we need to process
-        // the path as UTF-16 before doing any conversions
-        d->fileEntry.filePath();
-#else
-        d->fileEntry.nativeFilePath();
-#endif
+    const QFileSystemEntry::NativePath qfilename = d->fileEntry.nativeFilePath();
 
     // Find placeholder string.
     uint phPos = qfilename.length();
@@ -382,7 +375,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
     while (phPos != 0) {
         --phPos;
 
-        if (qfilename[phPos] == QLatin1Char('X')) {
+        if (qfilename[phPos] == Latin1Char('X')) {
             ++phLength;
             continue;
         }
@@ -390,7 +383,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
         if (phLength >= 6
                 || qfilename[phPos] ==
 #if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
-                    QLatin1Char('/')
+                    '/'
 #else
                     QLatin1Char('\\')
 #endif
@@ -404,29 +397,12 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
 
     QFileSystemEntry::NativePath filename;
 
-#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
-    if (phLength < 6) {
-        filename = qfilename.toLocal8Bit();
-
-        phPos = filename.length() + 1; // Account for added dot in prefix
-        phLength = 6;
-        filename = filename % '.' % Placeholder(phLength);
-    } else {
-        QByteArray prefix, suffix;
-        prefix = qfilename.leftRef(phPos).toLocal8Bit();
-        suffix = qfilename.midRef(phPos + phLength).toLocal8Bit();
-
-        phPos = prefix.length();
-        filename = prefix % Placeholder(phLength) % suffix;
-    }
-#else
     if (phLength < 6) {
         phPos = qfilename.length() + 1; // Account for added dot in prefix
         phLength = 6;
         filename = qfilename % Latin1Char('.') % Placeholder(phLength);
     } else
         filename = qfilename;
-#endif
 
     QSystemError error;
 #if defined(Q_OS_WIN)