summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/io/qdir.cpp41
-rw-r--r--src/corelib/io/qfileinfo.cpp8
-rw-r--r--tests/auto/qdir/tst_qdir.cpp99
-rw-r--r--tests/auto/qfileinfo/tst_qfileinfo.cpp48
4 files changed, 176 insertions, 20 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp
index b126c6d..6393509 100644
--- a/src/corelib/io/qdir.cpp
+++ b/src/corelib/io/qdir.cpp
@@ -136,6 +136,19 @@ public:
delete fileEngine;
}
+ bool exists() const
+ {
+ if (!fileEngine)
+ return false;
+ const QAbstractFileEngine::FileFlags info =
+ fileEngine->fileFlags(QAbstractFileEngine::DirectoryType
+ | QAbstractFileEngine::ExistsFlag
+ | QAbstractFileEngine::Refresh);
+ if (!(info & QAbstractFileEngine::DirectoryType))
+ return false;
+ return info & QAbstractFileEngine::ExistsFlag;
+ }
+
void initFileEngine();
void initFileLists() const;
@@ -891,11 +904,13 @@ bool QDir::cd(const QString &dirName)
}
}
- QDir dir(newPath);
- if (!dir.exists())
+ QScopedPointer<QDirPrivate> dir(new QDirPrivate(*d_ptr.constData()));
+ dir->setPath(newPath);
+
+ if (!dir->exists())
return false;
- *this = dir;
+ d_ptr = dir.take();
return true;
}
@@ -1495,17 +1510,7 @@ bool QDir::isReadable() const
*/
bool QDir::exists() const
{
- const QDirPrivate* d = d_ptr.constData();
-
- if (!d->fileEngine)
- return false;
- const QAbstractFileEngine::FileFlags info =
- d->fileEngine->fileFlags(QAbstractFileEngine::DirectoryType
- | QAbstractFileEngine::ExistsFlag
- | QAbstractFileEngine::Refresh);
- if (!(info & QAbstractFileEngine::DirectoryType))
- return false;
- return info & QAbstractFileEngine::ExistsFlag;
+ return d_ptr->exists();
}
/*!
@@ -1579,11 +1584,13 @@ bool QDir::makeAbsolute() // ### What do the return values signify?
if (QDir::isRelativePath(absolutePath))
return false;
- QDir dir(absolutePath);
- if (!(dir.d_ptr.constData()->fileEngine->fileFlags(QAbstractFileEngine::TypesMask) & QAbstractFileEngine::DirectoryType))
+ QScopedPointer<QDirPrivate> dir(new QDirPrivate(*d_ptr.constData()));
+ dir->setPath(absolutePath);
+
+ if (!(dir->fileEngine->fileFlags(QAbstractFileEngine::TypesMask) & QAbstractFileEngine::DirectoryType))
return false;
- *this = dir;
+ d_ptr = dir.take();
return true;
}
diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp
index 248b83d..7eca212 100644
--- a/src/corelib/io/qfileinfo.cpp
+++ b/src/corelib/io/qfileinfo.cpp
@@ -395,7 +395,9 @@ QFileInfo &QFileInfo::operator=(const QFileInfo &fileinfo)
*/
void QFileInfo::setFile(const QString &file)
{
+ bool caching = d_ptr.constData()->cache_enabled;
*this = QFileInfo(file);
+ d_ptr->cache_enabled = caching;
}
/*!
@@ -411,7 +413,7 @@ void QFileInfo::setFile(const QString &file)
*/
void QFileInfo::setFile(const QFile &file)
{
- *this = QFileInfo(file.fileName());
+ setFile(file.fileName());
}
/*!
@@ -427,7 +429,7 @@ void QFileInfo::setFile(const QFile &file)
*/
void QFileInfo::setFile(const QDir &dir, const QString &file)
{
- *this = QFileInfo(dir.filePath(file));
+ setFile(dir.filePath(file));
}
/*!
@@ -579,7 +581,7 @@ bool QFileInfo::makeAbsolute()
QString absFileName = d_ptr.constData()->getFileName(QAbstractFileEngine::AbsoluteName);
// QSharedDataPointer::operator->() will detach.
- *this = QFileInfo(absFileName);
+ setFile(absFileName);
return true;
}
diff --git a/tests/auto/qdir/tst_qdir.cpp b/tests/auto/qdir/tst_qdir.cpp
index c8c835f..fb83a5a 100644
--- a/tests/auto/qdir/tst_qdir.cpp
+++ b/tests/auto/qdir/tst_qdir.cpp
@@ -167,6 +167,8 @@ private slots:
void longFileName();
void updateFileLists();
+
+ void detachingOperations();
};
// Testing get/set functions
@@ -1541,6 +1543,103 @@ void tst_QDir::updateFileLists()
QCOMPARE(dir.entryList(), QStringList() << "sub-dir1" << "sub-dir2" << "file1.txt");
}
+void tst_QDir::detachingOperations()
+{
+ QString const defaultPath(".");
+ QStringList const defaultNameFilters = QStringList("*");
+ QDir::SortFlags const defaultSorting = QDir::Name | QDir::IgnoreCase;
+ QDir::Filters const defaultFilter = QDir::AllEntries;
+
+ QString const path1("..");
+ QString const path2("./foo");
+ QStringList const nameFilters = QStringList(QString("*.txt"));
+ QDir::SortFlags const sorting = QDir::Name | QDir::DirsLast | QDir::Reversed;
+ QDir::Filters const filter = QDir::Writable;
+
+ QDir dir1;
+
+ QCOMPARE(dir1.path(), defaultPath);
+ QCOMPARE(dir1.filter(), defaultFilter);
+ QCOMPARE(dir1.nameFilters(), defaultNameFilters);
+ QCOMPARE(dir1.sorting(), defaultSorting);
+
+ dir1.setPath(path1);
+ QCOMPARE(dir1.path(), path1);
+ QCOMPARE(dir1.filter(), defaultFilter);
+ QCOMPARE(dir1.nameFilters(), defaultNameFilters);
+ QCOMPARE(dir1.sorting(), defaultSorting);
+
+ dir1.setFilter(filter);
+ QCOMPARE(dir1.path(), path1);
+ QCOMPARE(dir1.filter(), filter);
+ QCOMPARE(dir1.nameFilters(), defaultNameFilters);
+ QCOMPARE(dir1.sorting(), defaultSorting);
+
+ dir1.setNameFilters(nameFilters);
+ QCOMPARE(dir1.path(), path1);
+ QCOMPARE(dir1.filter(), filter);
+ QCOMPARE(dir1.nameFilters(), nameFilters);
+ QCOMPARE(dir1.sorting(), defaultSorting);
+
+ dir1.setSorting(sorting);
+ QCOMPARE(dir1.path(), path1);
+ QCOMPARE(dir1.filter(), filter);
+ QCOMPARE(dir1.nameFilters(), nameFilters);
+ QCOMPARE(dir1.sorting(), sorting);
+
+ dir1.setPath(path2);
+ QCOMPARE(dir1.path(), path2);
+ QCOMPARE(dir1.filter(), filter);
+ QCOMPARE(dir1.nameFilters(), nameFilters);
+ QCOMPARE(dir1.sorting(), sorting);
+
+ {
+ QDir dir2(dir1);
+ QCOMPARE(dir2.path(), path2);
+ QCOMPARE(dir2.filter(), filter);
+ QCOMPARE(dir2.nameFilters(), nameFilters);
+ QCOMPARE(dir2.sorting(), sorting);
+ }
+
+ {
+ QDir dir2;
+ QCOMPARE(dir2.path(), defaultPath);
+ QCOMPARE(dir2.filter(), defaultFilter);
+ QCOMPARE(dir2.nameFilters(), defaultNameFilters);
+ QCOMPARE(dir2.sorting(), defaultSorting);
+
+ dir2 = dir1;
+ QCOMPARE(dir2.path(), path2);
+ QCOMPARE(dir2.filter(), filter);
+ QCOMPARE(dir2.nameFilters(), nameFilters);
+ QCOMPARE(dir2.sorting(), sorting);
+
+ dir2 = path1;
+ QCOMPARE(dir2.path(), path1);
+ QCOMPARE(dir2.filter(), filter);
+ QCOMPARE(dir2.nameFilters(), nameFilters);
+ QCOMPARE(dir2.sorting(), sorting);
+ }
+
+ dir1.refresh();
+ QCOMPARE(dir1.path(), path2);
+ QCOMPARE(dir1.filter(), filter);
+ QCOMPARE(dir1.nameFilters(), nameFilters);
+ QCOMPARE(dir1.sorting(), sorting);
+
+ QString const currentPath = QDir::currentPath();
+ QVERIFY(dir1.cd(currentPath));
+ QCOMPARE(dir1.path(), currentPath);
+ QCOMPARE(dir1.filter(), filter);
+ QCOMPARE(dir1.nameFilters(), nameFilters);
+ QCOMPARE(dir1.sorting(), sorting);
+
+ QVERIFY(dir1.cdUp());
+ QCOMPARE(dir1.filter(), filter);
+ QCOMPARE(dir1.nameFilters(), nameFilters);
+ QCOMPARE(dir1.sorting(), sorting);
+}
+
QTEST_MAIN(tst_QDir)
#include "tst_qdir.moc"
diff --git a/tests/auto/qfileinfo/tst_qfileinfo.cpp b/tests/auto/qfileinfo/tst_qfileinfo.cpp
index 540a1cd..208110a 100644
--- a/tests/auto/qfileinfo/tst_qfileinfo.cpp
+++ b/tests/auto/qfileinfo/tst_qfileinfo.cpp
@@ -172,6 +172,8 @@ private slots:
void equalOperator() const;
void equalOperatorWithDifferentSlashes() const;
void notEqualOperator() const;
+
+ void detachingOperations();
};
tst_QFileInfo::tst_QFileInfo()
@@ -1377,5 +1379,51 @@ void tst_QFileInfo::notEqualOperator() const
QVERIFY(QFileInfo() != QFileInfo());
}
+void tst_QFileInfo::detachingOperations()
+{
+ QFileInfo info1;
+ QVERIFY(info1.caching());
+ info1.setCaching(false);
+
+ {
+ QFileInfo info2 = info1;
+
+ QVERIFY(!info1.caching());
+ QVERIFY(!info2.caching());
+
+ info2.setCaching(true);
+ QVERIFY(info2.caching());
+
+ info1.setFile("foo");
+ QVERIFY(!info1.caching());
+ }
+
+ {
+ QFile file("foo");
+ info1.setFile(file);
+ QVERIFY(!info1.caching());
+ }
+
+ info1.setFile(QDir(), "foo");
+ QVERIFY(!info1.caching());
+
+ {
+ QFileInfo info3;
+ QVERIFY(info3.caching());
+
+ info3 = info1;
+ QVERIFY(!info3.caching());
+ }
+
+ info1.refresh();
+ QVERIFY(!info1.caching());
+
+ QVERIFY(info1.makeAbsolute());
+ QVERIFY(!info1.caching());
+
+ info1.detach();
+ QVERIFY(!info1.caching());
+}
+
QTEST_MAIN(tst_QFileInfo)
#include "tst_qfileinfo.moc"