summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qdir.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/io/qdir.cpp')
-rw-r--r--src/corelib/io/qdir.cpp623
1 files changed, 324 insertions, 299 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp
index 09315f9afe..9291201d88 100644
--- a/src/corelib/io/qdir.cpp
+++ b/src/corelib/io/qdir.cpp
@@ -9,7 +9,7 @@
#ifndef QT_NO_DEBUG_STREAM
#include "qdebug.h"
#endif
-#include "qdiriterator.h"
+#include "qdirlisting.h"
#include "qdatetime.h"
#include "qstring.h"
#if QT_CONFIG(regularexpression)
@@ -21,9 +21,10 @@
#include "qfilesystemengine_p.h"
#include <qstringbuilder.h>
-#ifdef QT_BUILD_CORE_LIB
-# include "qresource.h"
-# include "private/qcoreglobaldata_p.h"
+#ifndef QT_BOOTSTRAPPED
+# include <qcollator.h>
+# include "qreadwritelock.h"
+# include "qmutex.h"
#endif
#include <algorithm>
@@ -57,13 +58,13 @@ enum {
};
// Return the length of the root part of an absolute path, for use by cleanPath(), cd().
-static int rootLength(const QString &name, bool allowUncPaths)
+static qsizetype rootLength(QStringView name, bool allowUncPaths)
{
- const int len = name.length();
+ const qsizetype len = name.size();
// starts with double slash
if (allowUncPaths && name.startsWith("//"_L1)) {
// Server name '//server/path' is part of the prefix.
- const int nextSlash = name.indexOf(u'/', 2);
+ const qsizetype nextSlash = name.indexOf(u'/', 2);
return nextSlash >= 0 ? nextSlash + 1 : len;
}
#if defined(Q_OS_WIN)
@@ -78,46 +79,44 @@ static int rootLength(const QString &name, bool allowUncPaths)
}
//************* QDirPrivate
-QDirPrivate::QDirPrivate(const QString &path, const QStringList &nameFilters_, QDir::SortFlags sort_, QDir::Filters filters_)
- : QSharedData()
- , fileListsInitialized(false)
- , nameFilters(nameFilters_)
- , sort(sort_)
- , filters(filters_)
+QDirPrivate::QDirPrivate(const QString &path, const QStringList &nameFilters_,
+ QDir::SortFlags sort_, QDir::Filters filters_)
+ : QSharedData(), nameFilters(nameFilters_), sort(sort_), filters(filters_)
{
setPath(path.isEmpty() ? QString::fromLatin1(".") : path);
- bool empty = nameFilters.isEmpty();
- if (!empty) {
- empty = true;
- for (int i = 0; i < nameFilters.size(); ++i) {
- if (!nameFilters.at(i).isEmpty()) {
- empty = false;
- break;
- }
- }
- }
+ auto isEmpty = [](const auto &e) { return e.isEmpty(); };
+ const bool empty = std::all_of(nameFilters.cbegin(), nameFilters.cend(), isEmpty);
if (empty)
nameFilters = QStringList(QString::fromLatin1("*"));
}
QDirPrivate::QDirPrivate(const QDirPrivate &copy)
- : QSharedData(copy)
- , fileListsInitialized(false)
- , nameFilters(copy.nameFilters)
- , sort(copy.sort)
- , filters(copy.filters)
- , dirEntry(copy.dirEntry)
- , metaData(copy.metaData)
-{
+ : QSharedData(copy),
+ // mutex is not copied
+ nameFilters(copy.nameFilters),
+ sort(copy.sort),
+ filters(copy.filters),
+ // fileEngine is not copied
+ dirEntry(copy.dirEntry)
+{
+ QMutexLocker locker(&copy.fileCache.mutex);
+ fileCache.fileListsInitialized = copy.fileCache.fileListsInitialized.load();
+ fileCache.files = copy.fileCache.files;
+ fileCache.fileInfos = copy.fileCache.fileInfos;
+ fileCache.absoluteDirEntry = copy.fileCache.absoluteDirEntry;
+ fileCache.metaData = copy.fileCache.metaData;
}
bool QDirPrivate::exists() const
{
if (!fileEngine) {
- QFileSystemEngine::fillMetaData(dirEntry, metaData,
- QFileSystemMetaData::ExistsAttribute | QFileSystemMetaData::DirectoryType); // always stat
- return metaData.exists() && metaData.isDirectory();
+ QMutexLocker locker(&fileCache.mutex);
+ QFileSystemEngine::fillMetaData(
+ dirEntry, fileCache.metaData,
+ QFileSystemMetaData::ExistsAttribute
+ | QFileSystemMetaData::DirectoryType); // always stat
+ return fileCache.metaData.exists() && fileCache.metaData.isDirectory();
}
const QAbstractFileEngine::FileFlags info =
fileEngine->fileFlags(QAbstractFileEngine::DirectoryType
@@ -132,7 +131,7 @@ bool QDirPrivate::exists() const
inline QChar QDirPrivate::getFilterSepChar(const QString &nameFilter)
{
QChar sep(u';');
- int i = nameFilter.indexOf(sep, 0);
+ qsizetype i = nameFilter.indexOf(sep, 0);
if (i == -1 && nameFilter.indexOf(u' ', 0) != -1)
sep = QChar(u' ');
return sep;
@@ -153,63 +152,96 @@ inline void QDirPrivate::setPath(const QString &path)
{
QString p = QDir::fromNativeSeparators(path);
if (p.endsWith(u'/')
- && p.length() > 1
+ && p.size() > 1
#if defined(Q_OS_WIN)
&& (!(p.length() == 3 && p.at(1).unicode() == ':' && p.at(0).isLetter()))
#endif
) {
- p.truncate(p.length() - 1);
+ p.truncate(p.size() - 1);
}
-
dirEntry = QFileSystemEntry(p, QFileSystemEntry::FromInternalPath());
- metaData.clear();
- initFileEngine();
- clearFileLists();
- absoluteDirEntry = QFileSystemEntry();
+ clearCache(IncludingMetaData);
+ fileCache.absoluteDirEntry = QFileSystemEntry();
}
-inline void QDirPrivate::clearFileLists()
+inline QString QDirPrivate::resolveAbsoluteEntry() const
{
- fileListsInitialized = false;
- files.clear();
- fileInfos.clear();
-}
+ QMutexLocker locker(&fileCache.mutex);
+ if (!fileCache.absoluteDirEntry.isEmpty())
+ return fileCache.absoluteDirEntry.filePath();
-inline void QDirPrivate::resolveAbsoluteEntry() const
-{
- if (!absoluteDirEntry.isEmpty() || dirEntry.isEmpty())
- return;
+ if (dirEntry.isEmpty())
+ return dirEntry.filePath();
QString absoluteName;
if (!fileEngine) {
if (!dirEntry.isRelative() && dirEntry.isClean()) {
- absoluteDirEntry = dirEntry;
- return;
+ fileCache.absoluteDirEntry = dirEntry;
+ return dirEntry.filePath();
}
absoluteName = QFileSystemEngine::absoluteName(dirEntry).filePath();
} else {
absoluteName = fileEngine->fileName(QAbstractFileEngine::AbsoluteName);
}
-
- absoluteDirEntry = QFileSystemEntry(QDir::cleanPath(absoluteName), QFileSystemEntry::FromInternalPath());
+ auto absoluteFileSystemEntry =
+ QFileSystemEntry(QDir::cleanPath(absoluteName), QFileSystemEntry::FromInternalPath());
+ fileCache.absoluteDirEntry = absoluteFileSystemEntry;
+ return absoluteFileSystemEntry.filePath();
}
/* For sorting */
struct QDirSortItem
{
+ QDirSortItem() = default;
+ QDirSortItem(const QFileInfo &fi, QDir::SortFlags sort)
+ : item(fi)
+ {
+ // A dir e.g. "dirA.bar" doesn't have actually have an extension/suffix, when
+ // sorting by type such "suffix" should be ignored but that would complicate
+ // the code and uses can change the behavior by setting DirsFirst/DirsLast
+ if (sort.testAnyFlag(QDir::Type))
+ suffix_cache = item.suffix();
+ }
+
mutable QString filename_cache;
- mutable QString suffix_cache;
+ QString suffix_cache;
QFileInfo item;
};
-
class QDirSortItemComparator
{
QDir::SortFlags qt_cmp_si_sort_flags;
+
+#ifndef QT_BOOTSTRAPPED
+ QCollator *collator = nullptr;
+#endif
public:
- QDirSortItemComparator(QDir::SortFlags flags) : qt_cmp_si_sort_flags(flags) {}
+#ifndef QT_BOOTSTRAPPED
+ QDirSortItemComparator(QDir::SortFlags flags, QCollator *coll = nullptr)
+ : qt_cmp_si_sort_flags(flags), collator(coll)
+ {
+ Q_ASSERT(!qt_cmp_si_sort_flags.testAnyFlag(QDir::LocaleAware) || collator);
+
+ if (collator && qt_cmp_si_sort_flags.testAnyFlag(QDir::IgnoreCase))
+ collator->setCaseSensitivity(Qt::CaseInsensitive);
+ }
+#else
+ QDirSortItemComparator(QDir::SortFlags flags)
+ : qt_cmp_si_sort_flags(flags)
+ {
+ }
+#endif
bool operator()(const QDirSortItem &, const QDirSortItem &) const;
+
+ int compareStrings(const QString &a, const QString &b, Qt::CaseSensitivity cs) const
+ {
+#ifndef QT_BOOTSTRAPPED
+ if (collator)
+ return collator->compare(a, b);
+#endif
+ return a.compare(b, cs);
+ }
};
bool QDirSortItemComparator::operator()(const QDirSortItem &n1, const QDirSortItem &n2) const
@@ -222,43 +254,25 @@ bool QDirSortItemComparator::operator()(const QDirSortItem &n1, const QDirSortIt
if ((qt_cmp_si_sort_flags & QDir::DirsLast) && (f1->item.isDir() != f2->item.isDir()))
return !f1->item.isDir();
+ const bool ic = qt_cmp_si_sort_flags.testAnyFlag(QDir::IgnoreCase);
+ const auto qtcase = ic ? Qt::CaseInsensitive : Qt::CaseSensitive;
+
qint64 r = 0;
int sortBy = ((qt_cmp_si_sort_flags & QDir::SortByMask)
| (qt_cmp_si_sort_flags & QDir::Type)).toInt();
switch (sortBy) {
case QDir::Time: {
- QDateTime firstModified = f1->item.lastModified();
- QDateTime secondModified = f2->item.lastModified();
-
- // QDateTime by default will do all sorts of conversions on these to
- // find timezones, which is incredibly expensive. As we aren't
- // presenting these to the user, we don't care (at all) about the
- // local timezone, so force them to UTC to avoid that conversion.
- firstModified.setTimeSpec(Qt::UTC);
- secondModified.setTimeSpec(Qt::UTC);
-
+ const QDateTime firstModified = f1->item.lastModified(QTimeZone::UTC);
+ const QDateTime secondModified = f2->item.lastModified(QTimeZone::UTC);
r = firstModified.msecsTo(secondModified);
break;
}
case QDir::Size:
r = f2->item.size() - f1->item.size();
break;
- case QDir::Type:
- {
- bool ic = qt_cmp_si_sort_flags.testAnyFlag(QDir::IgnoreCase);
-
- if (f1->suffix_cache.isNull())
- f1->suffix_cache = ic ? f1->item.suffix().toLower()
- : f1->item.suffix();
- if (f2->suffix_cache.isNull())
- f2->suffix_cache = ic ? f2->item.suffix().toLower()
- : f2->item.suffix();
-
- r = qt_cmp_si_sort_flags & QDir::LocaleAware
- ? f1->suffix_cache.localeAwareCompare(f2->suffix_cache)
- : f1->suffix_cache.compare(f2->suffix_cache);
- }
+ case QDir::Type:
+ r = compareStrings(f1->suffix_cache, f2->suffix_cache, qtcase);
break;
default:
;
@@ -266,69 +280,89 @@ bool QDirSortItemComparator::operator()(const QDirSortItem &n1, const QDirSortIt
if (r == 0 && sortBy != QDir::Unsorted) {
// Still not sorted - sort by name
- bool ic = qt_cmp_si_sort_flags.testAnyFlag(QDir::IgnoreCase);
if (f1->filename_cache.isNull())
- f1->filename_cache = ic ? f1->item.fileName().toLower()
- : f1->item.fileName();
+ f1->filename_cache = f1->item.fileName();
if (f2->filename_cache.isNull())
- f2->filename_cache = ic ? f2->item.fileName().toLower()
- : f2->item.fileName();
+ f2->filename_cache = f2->item.fileName();
- r = qt_cmp_si_sort_flags & QDir::LocaleAware
- ? f1->filename_cache.localeAwareCompare(f2->filename_cache)
- : f1->filename_cache.compare(f2->filename_cache);
+ r = compareStrings(f1->filename_cache, f2->filename_cache, qtcase);
}
if (qt_cmp_si_sort_flags & QDir::Reversed)
return r > 0;
return r < 0;
}
-inline void QDirPrivate::sortFileList(QDir::SortFlags sort, QFileInfoList &l,
+inline void QDirPrivate::sortFileList(QDir::SortFlags sort, const QFileInfoList &l,
QStringList *names, QFileInfoList *infos)
{
- // names and infos are always empty lists or 0 here
- int n = l.size();
- if (n > 0) {
- if (n == 1 || (sort & QDir::SortByMask) == QDir::Unsorted) {
- if (infos)
- *infos = l;
- if (names) {
- for (int i = 0; i < n; ++i)
- names->append(l.at(i).fileName());
- }
+ Q_ASSERT(names || infos);
+ Q_ASSERT(!infos || infos->isEmpty());
+ Q_ASSERT(!names || names->isEmpty());
+
+ const qsizetype n = l.size();
+ if (n == 0)
+ return;
+
+ if (n == 1 || (sort & QDir::SortByMask) == QDir::Unsorted) {
+ if (infos)
+ *infos = l;
+
+ if (names) {
+ for (const QFileInfo &fi : l)
+ names->append(fi.fileName());
+ }
+ } else {
+ QScopedArrayPointer<QDirSortItem> si(new QDirSortItem[n]);
+ for (qsizetype i = 0; i < n; ++i)
+ si[i] = QDirSortItem{l.at(i), sort};
+
+#ifndef QT_BOOTSTRAPPED
+ if (sort.testAnyFlag(QDir::LocaleAware)) {
+ QCollator coll;
+ std::sort(si.data(), si.data() + n, QDirSortItemComparator(sort, &coll));
} else {
- QScopedArrayPointer<QDirSortItem> si(new QDirSortItem[n]);
- for (int i = 0; i < n; ++i)
- si[i].item = l.at(i);
std::sort(si.data(), si.data() + n, QDirSortItemComparator(sort));
- // put them back in the list(s)
- if (infos) {
- for (int i = 0; i < n; ++i)
- infos->append(si[i].item);
- }
+ }
+#else
+ std::sort(si.data(), si.data() + n, QDirSortItemComparator(sort));
+#endif // QT_BOOTSTRAPPED
+
+ // put them back in the list(s)
+ for (qsizetype i = 0; i < n; ++i) {
+ auto &fileInfo = si[i].item;
+ if (infos)
+ infos->append(fileInfo);
if (names) {
- for (int i = 0; i < n; ++i)
- names->append(si[i].item.fileName());
+ const bool cached = !si[i].filename_cache.isNull();
+ names->append(cached ? si[i].filename_cache : fileInfo.fileName());
}
}
}
}
+
inline void QDirPrivate::initFileLists(const QDir &dir) const
{
- if (!fileListsInitialized) {
+ QMutexLocker locker(&fileCache.mutex);
+ if (!fileCache.fileListsInitialized) {
QFileInfoList l;
- QDirIterator it(dir);
- while (it.hasNext())
- l.append(it.nextFileInfo());
- sortFileList(sort, l, &files, &fileInfos);
- fileListsInitialized = true;
+ for (const auto &dirEntry : QDirListing(dir))
+ l.emplace_back(dirEntry.fileInfo());
+
+ sortFileList(sort, l, &fileCache.files, &fileCache.fileInfos);
+ fileCache.fileListsInitialized = true;
}
}
-inline void QDirPrivate::initFileEngine()
+inline void QDirPrivate::clearCache(MetaDataClearing mode)
{
- fileEngine.reset(QFileSystemEngine::resolveEntryAndCreateLegacyEngine(dirEntry, metaData));
+ QMutexLocker locker(&fileCache.mutex);
+ if (mode == IncludingMetaData)
+ fileCache.metaData.clear();
+ fileCache.fileListsInitialized = false;
+ fileCache.files.clear();
+ fileCache.fileInfos.clear();
+ fileEngine = QFileSystemEngine::createLegacyEngine(dirEntry, fileCache.metaData);
}
/*!
@@ -340,6 +374,7 @@ inline void QDirPrivate::initFileEngine()
\ingroup shared
\reentrant
+ \compares equality
A QDir is used to manipulate path names, access information
regarding paths and files, and manipulate the underlying file
@@ -495,8 +530,8 @@ inline void QDirPrivate::initFileEngine()
\snippet code/src_corelib_io_qdir.cpp 4
- (We could also use the static convenience function
- QFile::exists().)
+ (We could also use one of the static convenience functions
+ QFileInfo::exists() or QFile::exists().)
Traversing directories and reading a file:
@@ -507,7 +542,12 @@ inline void QDirPrivate::initFileEngine()
\snippet qdir-listfiles/main.cpp 0
- \sa QFileInfo, QFile, QFileDialog, QCoreApplication::applicationDirPath(), {Find Files Example}
+ \section1 Platform Specific Issues
+
+ \include android-content-uri-limitations.qdocinc
+
+ \sa QFileInfo, QFile, QFileDialog, QCoreApplication::applicationDirPath(),
+ {Fetch More Example}
*/
/*!
@@ -611,7 +651,7 @@ void QDir::setPath(const QString &path)
*/
QString QDir::path() const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
return d->dirEntry.filePath();
}
@@ -625,9 +665,11 @@ QString QDir::path() const
*/
QString QDir::absolutePath() const
{
- const QDirPrivate* d = d_ptr.constData();
- d->resolveAbsoluteEntry();
- return d->absoluteDirEntry.filePath();
+ Q_D(const QDir);
+ if (!d->fileEngine)
+ return d->resolveAbsoluteEntry();
+
+ return d->fileEngine->fileName(QAbstractFileEngine::AbsoluteName);
}
/*!
@@ -648,9 +690,11 @@ QString QDir::absolutePath() const
*/
QString QDir::canonicalPath() const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (!d->fileEngine) {
- QFileSystemEntry answer = QFileSystemEngine::canonicalName(d->dirEntry, d->metaData);
+ QMutexLocker locker(&d->fileCache.mutex);
+ QFileSystemEntry answer =
+ QFileSystemEngine::canonicalName(d->dirEntry, d->fileCache.metaData);
return answer.filePath();
}
return d->fileEngine->fileName(QAbstractFileEngine::CanonicalName);
@@ -669,29 +713,31 @@ QString QDir::canonicalPath() const
*/
QString QDir::dirName() const
{
- const QDirPrivate* d = d_ptr.constData();
- return d->dirEntry.fileName();
+ Q_D(const QDir);
+ if (!d_ptr->fileEngine)
+ return d->dirEntry.fileName();
+ return d->fileEngine->fileName(QAbstractFileEngine::BaseName);
}
#ifdef Q_OS_WIN
-static int drivePrefixLength(const QString &path)
+static qsizetype drivePrefixLength(QStringView path)
{
// Used to extract path's drive for use as prefix for an "absolute except for drive" path
- const int size = path.length();
- int drive = 2; // length of drive prefix
+ const qsizetype size = path.size();
+ qsizetype drive = 2; // length of drive prefix
if (size > 1 && path.at(1).unicode() == ':') {
if (Q_UNLIKELY(!path.at(0).isLetter()))
return 0;
} else if (path.startsWith("//"_L1)) {
// UNC path; use its //server/share part as "drive" - it's as sane a
// thing as we can do.
- for (int i = 2; i-- > 0; ) { // Scan two "path fragments":
+ for (int i = 0 ; i < 2 ; ++i) { // Scan two "path fragments":
while (drive < size && path.at(drive).unicode() == '/')
drive++;
if (drive >= size) {
qWarning("Base directory starts with neither a drive nor a UNC share: %s",
- qUtf8Printable(QDir::toNativeSeparators(path)));
+ qUtf8Printable(QDir::toNativeSeparators(path.toString())));
return 0;
}
while (drive < size && path.at(drive).unicode() != '/')
@@ -733,7 +779,7 @@ QString QDir::filePath(const QString &fileName) const
if (treatAsAbsolute(fileName))
return fileName;
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
QString ret = d->dirEntry.filePath();
if (fileName.isEmpty())
return ret;
@@ -741,7 +787,7 @@ QString QDir::filePath(const QString &fileName) const
#ifdef Q_OS_WIN
if (fileName.startsWith(u'/') || fileName.startsWith(u'\\')) {
// Handle the "absolute except for drive" case (i.e. \blah not c:\blah):
- const int drive = drivePrefixLength(ret);
+ const qsizetype drive = drivePrefixLength(ret);
return drive > 0 ? QStringView{ret}.left(drive) % fileName : fileName;
}
#endif // Q_OS_WIN
@@ -764,16 +810,15 @@ QString QDir::absoluteFilePath(const QString &fileName) const
if (treatAsAbsolute(fileName))
return fileName;
- const QDirPrivate* d = d_ptr.constData();
- d->resolveAbsoluteEntry();
- const QString absoluteDirPath = d->absoluteDirEntry.filePath();
+ Q_D(const QDir);
+ QString absoluteDirPath = d->resolveAbsoluteEntry();
if (fileName.isEmpty())
return absoluteDirPath;
#ifdef Q_OS_WIN
// Handle the "absolute except for drive" case (i.e. \blah not c:\blah):
if (fileName.startsWith(u'/') || fileName.startsWith(u'\\')) {
// Combine absoluteDirPath's drive with fileName
- const int drive = drivePrefixLength(absoluteDirPath);
+ const qsizetype drive = drivePrefixLength(absoluteDirPath);
if (Q_LIKELY(drive))
return QStringView{absoluteDirPath}.left(drive) % fileName;
@@ -883,7 +928,7 @@ QString QDir::relativeFilePath(const QString &fileName) const
QString QDir::toNativeSeparators(const QString &pathName)
{
#if defined(Q_OS_WIN)
- int i = pathName.indexOf(u'/');
+ qsizetype i = pathName.indexOf(u'/');
if (i != -1) {
QString n(pathName);
@@ -988,6 +1033,9 @@ bool QDir::cd(const QString &dirName)
otherwise returns \c false. Note that the logical cdUp() operation is
not performed if the new directory does not exist.
+ \note On Android, this is not supported for content URIs. For more information,
+ see \l {Android: DocumentFile.getParentFile()}{DocumentFile.getParentFile()}.
+
\sa cd(), isReadable(), exists(), path()
*/
bool QDir::cdUp()
@@ -1000,7 +1048,7 @@ bool QDir::cdUp()
*/
QStringList QDir::nameFilters() const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
return d->nameFilters;
}
@@ -1021,14 +1069,22 @@ QStringList QDir::nameFilters() const
*/
void QDir::setNameFilters(const QStringList &nameFilters)
{
- QDirPrivate* d = d_ptr.data();
- d->initFileEngine();
- d->clearFileLists();
-
+ Q_D(QDir);
+ d->clearCache(QDirPrivate::KeepMetaData);
d->nameFilters = nameFilters;
}
-#ifdef QT_BUILD_CORE_LIB
+#ifndef QT_BOOTSTRAPPED
+
+namespace {
+struct DirSearchPaths {
+ mutable QReadWriteLock mutex;
+ QHash<QString, QStringList> paths;
+};
+}
+
+Q_GLOBAL_STATIC(DirSearchPaths, dirSearchPaths)
+
/*!
\since 4.3
@@ -1051,24 +1107,24 @@ void QDir::setNameFilters(const QStringList &nameFilters)
*/
void QDir::setSearchPaths(const QString &prefix, const QStringList &searchPaths)
{
- if (prefix.length() < 2) {
+ if (prefix.size() < 2) {
qWarning("QDir::setSearchPaths: Prefix must be longer than 1 character");
return;
}
- for (int i = 0; i < prefix.length(); ++i) {
- if (!prefix.at(i).isLetterOrNumber()) {
+ for (QChar ch : prefix) {
+ if (!ch.isLetterOrNumber()) {
qWarning("QDir::setSearchPaths: Prefix can only contain letters or numbers");
return;
}
}
- QWriteLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock);
- QHash<QString, QStringList> &paths = QCoreGlobalData::instance()->dirSearchPaths;
+ DirSearchPaths &conf = *dirSearchPaths;
+ const QWriteLocker lock(&conf.mutex);
if (searchPaths.isEmpty()) {
- paths.remove(prefix);
+ conf.paths.remove(prefix);
} else {
- paths.insert(prefix, searchPaths);
+ conf.paths.insert(prefix, searchPaths);
}
}
@@ -1084,8 +1140,9 @@ void QDir::addSearchPath(const QString &prefix, const QString &path)
if (path.isEmpty())
return;
- QWriteLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock);
- QCoreGlobalData::instance()->dirSearchPaths[prefix] += path;
+ DirSearchPaths &conf = *dirSearchPaths;
+ const QWriteLocker lock(&conf.mutex);
+ conf.paths[prefix] += path;
}
/*!
@@ -1097,18 +1154,22 @@ void QDir::addSearchPath(const QString &prefix, const QString &path)
*/
QStringList QDir::searchPaths(const QString &prefix)
{
- QReadLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock);
- return QCoreGlobalData::instance()->dirSearchPaths.value(prefix);
+ if (!dirSearchPaths.exists())
+ return QStringList();
+
+ const DirSearchPaths &conf = *dirSearchPaths;
+ const QReadLocker lock(&conf.mutex);
+ return conf.paths.value(prefix);
}
-#endif // QT_BUILD_CORE_LIB
+#endif // QT_BOOTSTRAPPED
/*!
Returns the value set by setFilter()
*/
QDir::Filters QDir::filter() const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
return d->filters;
}
@@ -1187,10 +1248,8 @@ QDir::Filters QDir::filter() const
*/
void QDir::setFilter(Filters filters)
{
- QDirPrivate* d = d_ptr.data();
- d->initFileEngine();
- d->clearFileLists();
-
+ Q_D(QDir);
+ d->clearCache(QDirPrivate::KeepMetaData);
d->filters = filters;
}
@@ -1201,7 +1260,7 @@ void QDir::setFilter(Filters filters)
*/
QDir::SortFlags QDir::sorting() const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
return d->sort;
}
@@ -1234,6 +1293,7 @@ QDir::SortFlags QDir::sorting() const
after the directories, again in reverse order.
*/
+#ifndef QT_BOOTSTRAPPED
/*!
Sets the sort order used by entryList() and entryInfoList().
@@ -1244,10 +1304,8 @@ QDir::SortFlags QDir::sorting() const
*/
void QDir::setSorting(SortFlags sort)
{
- QDirPrivate* d = d_ptr.data();
- d->initFileEngine();
- d->clearFileLists();
-
+ Q_D(QDir);
+ d->clearCache(QDirPrivate::KeepMetaData);
d->sort = sort;
}
@@ -1256,13 +1314,16 @@ void QDir::setSorting(SortFlags sort)
Equivalent to entryList().count().
+ \note In Qt versions prior to 6.5, this function returned \c{uint}, not
+ \c{qsizetype}.
+
\sa operator[](), entryList()
*/
-uint QDir::count() const
+qsizetype QDir::count(QT6_IMPL_NEW_OVERLOAD) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
d->initFileLists(*this);
- return d->files.count();
+ return d->fileCache.files.size();
}
/*!
@@ -1270,13 +1331,15 @@ uint QDir::count() const
names. Equivalent to entryList().at(index).
\a pos must be a valid index position in the list (i.e., 0 <= pos < count()).
+ \note In Qt versions prior to 6.5, \a pos was an \c{int}, not \c{qsizetype}.
+
\sa count(), entryList()
*/
-QString QDir::operator[](int pos) const
+QString QDir::operator[](qsizetype pos) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
d->initFileLists(*this);
- return d->files[pos];
+ return d->fileCache.files[pos];
}
/*!
@@ -1300,7 +1363,7 @@ QString QDir::operator[](int pos) const
*/
QStringList QDir::entryList(Filters filters, SortFlags sort) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
return entryList(d->nameFilters, filters, sort);
}
@@ -1323,7 +1386,7 @@ QStringList QDir::entryList(Filters filters, SortFlags sort) const
*/
QFileInfoList QDir::entryInfoList(Filters filters, SortFlags sort) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
return entryInfoList(d->nameFilters, filters, sort);
}
@@ -1346,24 +1409,34 @@ QFileInfoList QDir::entryInfoList(Filters filters, SortFlags sort) const
QStringList QDir::entryList(const QStringList &nameFilters, Filters filters,
SortFlags sort) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (filters == NoFilter)
filters = d->filters;
if (sort == NoSort)
sort = d->sort;
+ const bool needsSorting = (sort & QDir::SortByMask) != QDir::Unsorted;
+
if (filters == d->filters && sort == d->sort && nameFilters == d->nameFilters) {
- d->initFileLists(*this);
- return d->files;
+ // Don't fill a QFileInfo cache if we just need names
+ if (needsSorting || d->fileCache.fileListsInitialized) {
+ d->initFileLists(*this);
+ return d->fileCache.files;
+ }
}
- QFileInfoList l;
- QDirIterator it(d->dirEntry.filePath(), nameFilters, filters);
- while (it.hasNext())
- l.append(it.nextFileInfo());
+ QDirListing dirList(d->dirEntry.filePath(), nameFilters, filters);
QStringList ret;
- d->sortFileList(sort, l, &ret, nullptr);
+ if (needsSorting) {
+ QFileInfoList l;
+ for (const auto &dirEntry : dirList)
+ l.emplace_back(dirEntry.fileInfo());
+ d->sortFileList(sort, l, &ret, nullptr);
+ } else {
+ for (const auto &dirEntry : dirList)
+ ret.emplace_back(dirEntry.fileName());
+ }
return ret;
}
@@ -1386,7 +1459,7 @@ QStringList QDir::entryList(const QStringList &nameFilters, Filters filters,
QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, Filters filters,
SortFlags sort) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (filters == NoFilter)
filters = d->filters;
@@ -1395,17 +1468,17 @@ QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, Filters filter
if (filters == d->filters && sort == d->sort && nameFilters == d->nameFilters) {
d->initFileLists(*this);
- return d->fileInfos;
+ return d->fileCache.fileInfos;
}
QFileInfoList l;
- QDirIterator it(d->dirEntry.filePath(), nameFilters, filters);
- while (it.hasNext())
- l.append(it.nextFileInfo());
+ for (const auto &dirEntry : QDirListing(d->dirEntry.filePath(), nameFilters, filters))
+ l.emplace_back(dirEntry.fileInfo());
QFileInfoList ret;
d->sortFileList(sort, l, nullptr, &ret);
return ret;
}
+#endif // !QT_BOOTSTRAPPED
/*!
Creates a sub-directory called \a dirName.
@@ -1429,7 +1502,7 @@ QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, Filters filter
*/
bool QDir::mkdir(const QString &dirName, QFile::Permissions permissions) const
{
- const QDirPrivate *d = d_ptr.constData();
+ Q_D(const QDir);
if (dirName.isEmpty()) {
qWarning("QDir::mkdir: Empty or null file name");
@@ -1451,7 +1524,7 @@ bool QDir::mkdir(const QString &dirName, QFile::Permissions permissions) const
*/
bool QDir::mkdir(const QString &dirName) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (dirName.isEmpty()) {
qWarning("QDir::mkdir: Empty or null file name");
@@ -1475,7 +1548,7 @@ bool QDir::mkdir(const QString &dirName) const
*/
bool QDir::rmdir(const QString &dirName) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (dirName.isEmpty()) {
qWarning("QDir::rmdir: Empty or null file name");
@@ -1503,7 +1576,7 @@ bool QDir::rmdir(const QString &dirName) const
*/
bool QDir::mkpath(const QString &dirPath) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (dirPath.isEmpty()) {
qWarning("QDir::mkpath: Empty or null file name");
@@ -1529,7 +1602,7 @@ bool QDir::mkpath(const QString &dirPath) const
*/
bool QDir::rmpath(const QString &dirPath) const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (dirPath.isEmpty()) {
qWarning("QDir::rmpath: Empty or null file name");
@@ -1542,6 +1615,7 @@ bool QDir::rmpath(const QString &dirPath) const
return d->fileEngine->rmdir(fn, true);
}
+#ifndef QT_BOOTSTRAPPED
/*!
\since 5.0
Removes the directory, including all its contents.
@@ -1570,12 +1644,11 @@ bool QDir::removeRecursively()
bool success = true;
const QString dirPath = path();
// not empty -- we must empty it first
- QDirIterator di(dirPath, QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
- while (di.hasNext()) {
- const QFileInfo fi = di.nextFileInfo();
- const QString &filePath = di.filePath();
+ constexpr auto dirFilters = QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot;
+ for (const auto &dirEntry : QDirListing(dirPath, dirFilters)) {
+ const QString &filePath = dirEntry.filePath();
bool ok;
- if (fi.isDir() && !fi.isSymLink()) {
+ if (dirEntry.isDir() && !dirEntry.isSymLink()) {
ok = QDir(filePath).removeRecursively(); // recursive
} else {
ok = QFile::remove(filePath);
@@ -1595,6 +1668,7 @@ bool QDir::removeRecursively()
return success;
}
+#endif // !QT_BOOTSTRAPPED
/*!
Returns \c true if the directory is readable \e and we can open files
@@ -1607,13 +1681,15 @@ bool QDir::removeRecursively()
*/
bool QDir::isReadable() const
{
- const QDirPrivate* d = d_ptr.constData();
+ Q_D(const QDir);
if (!d->fileEngine) {
- if (!d->metaData.hasFlags(QFileSystemMetaData::UserReadPermission))
- QFileSystemEngine::fillMetaData(d->dirEntry, d->metaData, QFileSystemMetaData::UserReadPermission);
-
- return d->metaData.permissions().testAnyFlag(QFile::ReadUser);
+ QMutexLocker locker(&d->fileCache.mutex);
+ if (!d->fileCache.metaData.hasFlags(QFileSystemMetaData::UserReadPermission)) {
+ QFileSystemEngine::fillMetaData(d->dirEntry, d->fileCache.metaData,
+ QFileSystemMetaData::UserReadPermission);
+ }
+ return d->fileCache.metaData.permissions().testAnyFlag(QFile::ReadUser);
}
const QAbstractFileEngine::FileFlags info =
@@ -1710,7 +1786,7 @@ bool QDir::isRelative() const
*/
bool QDir::makeAbsolute()
{
- const QDirPrivate *d = d_ptr.constData();
+ Q_D(const QDir);
std::unique_ptr<QDirPrivate> dir;
if (!!d->fileEngine) {
QString absolutePath = d->fileEngine->fileName(QAbstractFileEngine::AbsoluteName);
@@ -1720,16 +1796,18 @@ bool QDir::makeAbsolute()
dir.reset(new QDirPrivate(*d_ptr.constData()));
dir->setPath(absolutePath);
} else { // native FS
- d->resolveAbsoluteEntry();
+ QString absoluteFilePath = d->resolveAbsoluteEntry();
dir.reset(new QDirPrivate(*d_ptr.constData()));
- dir->setPath(d->absoluteDirEntry.filePath());
+ dir->setPath(absoluteFilePath);
}
d_ptr = dir.release(); // actually detach
return true;
}
/*!
- Returns \c true if directory \a dir and this directory have the same
+ \fn bool QDir::operator==(const QDir &lhs, const QDir &rhs)
+
+ Returns \c true if directory \a lhs and directory \a rhs have the same
path and their sort and filter settings are the same; otherwise
returns \c false.
@@ -1737,10 +1815,10 @@ bool QDir::makeAbsolute()
\snippet code/src_corelib_io_qdir.cpp 10
*/
-bool QDir::operator==(const QDir &dir) const
+bool comparesEqual(const QDir &lhs, const QDir &rhs)
{
- const QDirPrivate *d = d_ptr.constData();
- const QDirPrivate *other = dir.d_ptr.constData();
+ const QDirPrivate *d = lhs.d_ptr.constData();
+ const QDirPrivate *other = rhs.d_ptr.constData();
if (d == other)
return true;
@@ -1764,18 +1842,18 @@ bool QDir::operator==(const QDir &dir) const
if (d->dirEntry.filePath() == other->dirEntry.filePath())
return true;
- if (exists()) {
- if (!dir.exists())
+ if (lhs.exists()) {
+ if (!rhs.exists())
return false; //can't be equal if only one exists
// Both exist, fallback to expensive canonical path computation
- return canonicalPath().compare(dir.canonicalPath(), sensitive) == 0;
+ return lhs.canonicalPath().compare(rhs.canonicalPath(), sensitive) == 0;
} else {
- if (dir.exists())
+ if (rhs.exists())
return false; //can't be equal if only one exists
// Neither exists, compare absolute paths rather than canonical (which would be empty strings)
- d->resolveAbsoluteEntry();
- other->resolveAbsoluteEntry();
- return d->absoluteDirEntry.filePath().compare(other->absoluteDirEntry.filePath(), sensitive) == 0;
+ QString thisFilePath = d->resolveAbsoluteEntry();
+ QString otherFilePath = other->resolveAbsoluteEntry();
+ return thisFilePath.compare(otherFilePath, sensitive) == 0;
}
}
return false;
@@ -1800,11 +1878,10 @@ QDir &QDir::operator=(const QDir &dir)
*/
/*!
- \fn bool QDir::operator!=(const QDir &dir) const
+ \fn bool QDir::operator!=(const QDir &lhs, const QDir &rhs)
- Returns \c true if directory \a dir and this directory have different
- paths or different sort or filter settings; otherwise returns
- false.
+ Returns \c true if directory \a lhs and directory \a rhs have different
+ paths or different sort or filter settings; otherwise returns \c false.
Example:
@@ -1871,9 +1948,10 @@ bool QDir::exists(const QString &name) const
qWarning("QDir::exists: Empty or null file name");
return false;
}
- return QFile::exists(filePath(name));
+ return QFileInfo::exists(filePath(name));
}
+#ifndef QT_BOOTSTRAPPED
/*!
Returns whether the directory is empty.
@@ -1889,16 +1967,18 @@ bool QDir::exists(const QString &name) const
*/
bool QDir::isEmpty(Filters filters) const
{
- const auto d = d_ptr.constData();
- QDirIterator it(d->dirEntry.filePath(), d->nameFilters, filters);
- return !it.hasNext();
+ Q_D(const QDir);
+ QDirListing dirList(d->dirEntry.filePath(), d->nameFilters, filters);
+ return dirList.cbegin() == dirList.cend();
}
+#endif // !QT_BOOTSTRAPPED
/*!
Returns a list of the root directories on this system.
On Windows this returns a list of QFileInfo objects containing "C:/",
- "D:/", etc. On other operating systems, it returns a list containing
+ "D:/", etc. This does not return drives with ejectable media that are empty.
+ On other operating systems, it returns a list containing
just one root directory (i.e. "/").
\sa root(), rootPath()
@@ -2127,7 +2207,7 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza
{
const bool allowUncPaths = flags.testAnyFlag(QDirPrivate::AllowUncPaths);
const bool isRemote = flags.testAnyFlag(QDirPrivate::RemotePath);
- const int len = name.length();
+ const qsizetype len = name.size();
if (ok)
*ok = false;
@@ -2135,15 +2215,15 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza
if (len == 0)
return name;
- int i = len - 1;
+ qsizetype i = len - 1;
QVarLengthArray<char16_t> outVector(len);
- int used = len;
+ qsizetype used = len;
char16_t *out = outVector.data();
- const ushort *p = reinterpret_cast<const ushort *>(name.data());
- const ushort *prefix = p;
- int up = 0;
+ const char16_t *p = reinterpret_cast<const char16_t *>(name.data());
+ const char16_t *prefix = p;
+ qsizetype up = 0;
- const int prefixLength = rootLength(name, allowUncPaths);
+ const qsizetype prefixLength = rootLength(name, allowUncPaths);
p += prefixLength;
i -= prefixLength;
@@ -2154,10 +2234,10 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza
--i;
}
- auto isDot = [](const ushort *p, int i) {
+ auto isDot = [](const char16_t *p, qsizetype i) {
return i > 1 && p[i - 1] == '.' && p[i - 2] == '/';
};
- auto isDotDot = [](const ushort *p, int i) {
+ auto isDotDot = [](const char16_t *p, qsizetype i) {
return i > 2 && p[i - 1] == '.' && p[i - 2] == '.' && p[i - 3] == '/';
};
@@ -2260,7 +2340,7 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza
// string only consists of a prefix followed by one or more slashes. Just skip the slash.
++used;
}
- for (int i = prefixLength - 1; i >= 0; --i)
+ for (qsizetype i = prefixLength - 1; i >= 0; --i)
out[--used] = prefix[i];
} else {
if (isEmpty) {
@@ -2278,7 +2358,7 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza
// If path was not modified return the original value
if (used == 0)
return name;
- return QString::fromUtf16(out + used, len - used);
+ return QStringView(out + used, len - used).toString();
}
static QString qt_cleanPath(const QString &path, bool *ok)
@@ -2292,7 +2372,7 @@ static QString qt_cleanPath(const QString &path, bool *ok)
QString ret = qt_normalizePathSegments(name, OSSupportsUncPaths ? QDirPrivate::AllowUncPaths : QDirPrivate::DefaultNormalization, ok);
// Strip away last slash except for root directories
- if (ret.length() > 1 && ret.endsWith(u'/')) {
+ if (ret.size() > 1 && ret.endsWith(u'/')) {
#if defined (Q_OS_WIN)
if (!(ret.length() == 3 && ret.at(1) == u':'))
#endif
@@ -2338,10 +2418,8 @@ bool QDir::isRelativePath(const QString &path)
*/
void QDir::refresh() const
{
- QDirPrivate *d = const_cast<QDir*>(this)->d_ptr.data();
- d->metaData.clear();
- d->initFileEngine();
- d->clearFileLists();
+ QDirPrivate *d = const_cast<QDir *>(this)->d_func();
+ d->clearCache(QDirPrivate::IncludingMetaData);
}
/*!
@@ -2364,59 +2442,6 @@ QStringList QDir::nameFiltersFromString(const QString &nameFilter)
return QDirPrivate::splitFilters(nameFilter);
}
-/*!
- \macro void Q_INIT_RESOURCE(name)
- \relates QDir
-
- Initializes the resources specified by the \c .qrc file with the
- specified base \a name. Normally, when resources are built as part
- of the application, the resources are loaded automatically at
- startup. The Q_INIT_RESOURCE() macro is necessary on some platforms
- for resources stored in a static library.
-
- For example, if your application's resources are listed in a file
- called \c myapp.qrc, you can ensure that the resources are
- initialized at startup by adding this line to your \c main()
- function:
-
- \snippet code/src_corelib_io_qdir.cpp 13
-
- If the file name contains characters that cannot be part of a valid C++ function name
- (such as '-'), they have to be replaced by the underscore character ('_').
-
- \note This macro cannot be used in a namespace. It should be called from
- main(). If that is not possible, the following workaround can be used
- to init the resource \c myapp from the function \c{MyNamespace::myFunction}:
-
- \snippet code/src_corelib_io_qdir.cpp 14
-
- \sa Q_CLEANUP_RESOURCE(), {The Qt Resource System}
-*/
-
-/*!
- \since 4.1
- \macro void Q_CLEANUP_RESOURCE(name)
- \relates QDir
-
- Unloads the resources specified by the \c .qrc file with the base
- name \a name.
-
- Normally, Qt resources are unloaded automatically when the
- application terminates, but if the resources are located in a
- plugin that is being unloaded, call Q_CLEANUP_RESOURCE() to force
- removal of your resources.
-
- \note This macro cannot be used in a namespace. Please see the
- Q_INIT_RESOURCE documentation for a workaround.
-
- Example:
-
- \snippet code/src_corelib_io_qdir.cpp 15
-
- \sa Q_INIT_RESOURCE(), {The Qt Resource System}
-*/
-
-
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, QDir::Filters filters)
{