QTemporaryDir: Remove directories on failure
authorEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
Tue, 20 Jan 2015 08:34:42 +0000 (09:34 +0100)
committerJani Heikkinen <jani.heikkinen@theqtcompany.com>
Thu, 22 Jan 2015 07:31:54 +0000 (08:31 +0100)
When creating a temporary directory but failing to set its permissions,
we need to remove the directory we created to avoid leaving 256 empty,
unused directories in the destination folder.

This happens on Android if you try creating a QTemporaryDir in the
download path on the sdcard.

Task-number: QTBUG-43352
Change-Id: Ic88fb7572f1abd65e5c7d8882b59c95f4b22ed72
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/io/qtemporarydir.cpp
tests/auto/corelib/io/qtemporarydir/tst_qtemporarydir.cpp

index 5e0def74eebb3ae39cee667fc6a0f0fd9a5d94ab..5f0c50018381fd838383c9442c6508dcc4137817 100644 (file)
@@ -128,8 +128,11 @@ static char *q_mkdtemp(char *templateName)
                                               QFile::ReadOwner |
                                               QFile::WriteOwner |
                                               QFile::ExeOwner, error);
-            if (error.error() != 0)
+            if (error.error() != 0) {
+                if (!QFileSystemEngine::removeDirectory(fileSystemEntry, false))
+                    qWarning() << "Unable to remove unused directory" << templateNameStr;
                 continue;
+            }
             return templateName;
         }
     }
index a68a1185b81348ba2dae2ffc0ed25163edbfdceb..e909b90a128ec984f3220836e754b8e8118fbdfa 100644 (file)
@@ -70,6 +70,8 @@ private slots:
     void QTBUG_4796_data();
     void QTBUG_4796();
 
+    void QTBUG43352_failedSetPermissions();
+
 public:
 };
 
@@ -419,5 +421,17 @@ void tst_QTemporaryDir::QTBUG_4796() // unicode support
     cleaner.reset();
 }
 
+void tst_QTemporaryDir::QTBUG43352_failedSetPermissions()
+{
+    QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + QStringLiteral("/");
+    int count = QDir(path).entryList().size();
+
+    {
+        QTemporaryDir dir(path);
+    }
+
+    QCOMPARE(QDir(path).entryList().size(), count);
+}
+
 QTEST_MAIN(tst_QTemporaryDir)
 #include "tst_qtemporarydir.moc"