diff options
Diffstat (limited to 'src/corelib/io/qdir.cpp')
-rw-r--r-- | src/corelib/io/qdir.cpp | 116 |
1 files changed, 88 insertions, 28 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index fb3a2f0aea..671913e92f 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -74,7 +74,7 @@ static QString driveSpec(const QString &path) if (path.size() < 2) return QString(); char c = path.at(0).toLatin1(); - if (c < 'a' && c > 'z' && c < 'A' && c > 'Z') + if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) return QString(); if (path.at(1).toLatin1() != ':') return QString(); @@ -748,6 +748,21 @@ static int drivePrefixLength(const QString &path) } #endif // Q_OS_WIN +static bool treatAsAbsolute(const QString &path) +{ + // ### Qt 6: be consistent about absolute paths + + // QFileInfo will use the right FS-engine for virtual file-systems + // (e.g. resource paths). Unfortunately, for real file-systems, it relies + // on QFileSystemEntry's isRelative(), which is flawed on MS-Win, ignoring + // its (correct) isAbsolute(). So only use that isAbsolute() unless there's + // a colon in the path. + // FIXME: relies on virtual file-systems having colons in their prefixes. + // The case of an MS-absolute C:/... path happens to work either way. + return (path.contains(QLatin1Char(':')) && QFileInfo(path).isAbsolute()) + || QFileSystemEntry(path).isAbsolute(); +} + /*! Returns the path name of a file in the directory. Does \e not check if the file actually exists in the directory; but see @@ -759,13 +774,10 @@ static int drivePrefixLength(const QString &path) */ QString QDir::filePath(const QString &fileName) const { - const QDirPrivate* d = d_ptr.constData(); - // Mistrust our own isAbsolutePath() for real files; Q_OS_WIN needs a drive. - if (fileName.startsWith(QLatin1Char(':')) // i.e. resource path - ? isAbsolutePath(fileName) : QFileSystemEntry(fileName).isAbsolute()) { + if (treatAsAbsolute(fileName)) return fileName; - } + const QDirPrivate* d = d_ptr.constData(); QString ret = d->dirEntry.filePath(); if (fileName.isEmpty()) return ret; @@ -793,13 +805,10 @@ QString QDir::filePath(const QString &fileName) const */ QString QDir::absoluteFilePath(const QString &fileName) const { - const QDirPrivate* d = d_ptr.constData(); - // Mistrust our own isAbsolutePath() for real files; Q_OS_WIN needs a drive. - if (fileName.startsWith(QLatin1Char(':')) // i.e. resource path - ? isAbsolutePath(fileName) : QFileSystemEntry(fileName).isAbsolute()) { + if (treatAsAbsolute(fileName)) return fileName; - } + const QDirPrivate* d = d_ptr.constData(); d->resolveAbsoluteEntry(); const QString absoluteDirPath = d->absoluteDirEntry.filePath(); if (fileName.isEmpty()) @@ -1040,7 +1049,8 @@ QStringList QDir::nameFilters() const list of filters specified by \a nameFilters. Each name filter is a wildcard (globbing) filter that understands - \c{*} and \c{?} wildcards. (See \l{Wildcard matching}.) + \c{*} and \c{?} wildcards. See \l{QRegularExpression#Wildcard matching} + {QRegularExpression Wildcard Matching}. For example, the following code sets three name filters on a QDir to ensure that only files with extensions typically used for C++ @@ -1059,6 +1069,7 @@ void QDir::setNameFilters(const QStringList &nameFilters) d->nameFilters = nameFilters; } +#if QT_DEPRECATED_SINCE(5, 13) /*! \obsolete @@ -1073,11 +1084,15 @@ void QDir::setNameFilters(const QStringList &nameFilters) void QDir::addResourceSearchPath(const QString &path) { #ifdef QT_BUILD_CORE_LIB +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QResource::addSearchPath(path); +QT_WARNING_POP #else Q_UNUSED(path) #endif } +#endif #ifdef QT_BUILD_CORE_LIB /*! @@ -1805,6 +1820,7 @@ QDir &QDir::operator=(const QDir &dir) return *this; } +#if QT_DEPRECATED_SINCE(5, 13) /*! \overload \obsolete @@ -1818,6 +1834,7 @@ QDir &QDir::operator=(const QString &path) d_ptr->setPath(path); return *this; } +#endif /*! \fn void QDir::swap(QDir &other) @@ -2120,16 +2137,15 @@ QString QDir::rootPath() patterns in the list of \a filters; otherwise returns \c false. The matching is case insensitive. - \sa {Wildcard matching}, QRegularExpression::wildcardToRegularExpression(), - entryList(), entryInfoList() + \sa {QRegularExpression#Wildcard matching}{QRegularExpression Wildcard Matching}, + entryList(), entryInfoList() */ bool QDir::match(const QStringList &filters, const QString &fileName) { for (QStringList::ConstIterator sit = filters.constBegin(); sit != filters.constEnd(); ++sit) { - QString wildcard = QRegularExpression::wildcardToRegularExpression(*sit); // Insensitive exact match // (see Notes for QRegExp Users in QRegularExpression's documentation) - QRegularExpression rx(QRegularExpression::anchoredPattern(wildcard), + QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(*sit), QRegularExpression::CaseInsensitiveOption); if (rx.match(fileName).hasMatch()) return true; @@ -2143,8 +2159,8 @@ bool QDir::match(const QStringList &filters, const QString &fileName) contain multiple patterns separated by spaces or semicolons. The matching is case insensitive. - \sa {Wildcard matching}, QRegularExpression::wildcardToRegularExpression, - entryList(), entryInfoList() + \sa {QRegularExpression#Wildcard matching}{QRegularExpression Wildcard Matching}, + entryList(), entryInfoList() */ bool QDir::match(const QString &filter, const QString &fileName) { @@ -2160,9 +2176,10 @@ bool QDir::match(const QString &filter, const QString &fileName) This method is shared with QUrl, so it doesn't deal with QDir::separator(), nor does it remove the trailing slash, if any. */ -Q_AUTOTEST_EXPORT QString qt_normalizePathSegments(const QString &name, bool allowUncPaths, - bool *ok = nullptr) +QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormalizations flags, bool *ok) { + const bool allowUncPaths = QDirPrivate::AllowUncPaths & flags; + const bool isRemote = QDirPrivate::RemotePath & flags; const int len = name.length(); if (ok) @@ -2184,14 +2201,30 @@ Q_AUTOTEST_EXPORT QString qt_normalizePathSegments(const QString &name, bool all i -= prefixLength; // replicate trailing slash (i > 0 checks for emptiness of input string p) - if (i > 0 && p[i] == '/') { + // except for remote paths because there can be /../ or /./ ending + if (i > 0 && p[i] == '/' && !isRemote) { out[--used] = '/'; --i; } + auto isDot = [](const ushort *p, int i) { + return i > 1 && p[i - 1] == '.' && p[i - 2] == '/'; + }; + auto isDotDot = [](const ushort *p, int i) { + return i > 2 && p[i - 1] == '.' && p[i - 2] == '.' && p[i - 3] == '/'; + }; + while (i >= 0) { - // remove trailing slashes + // copy trailing slashes for remote urls if (p[i] == '/') { + if (isRemote && !up) { + if (isDot(p, i)) { + i -= 2; + continue; + } + out[--used] = p[i]; + } + --i; continue; } @@ -2203,10 +2236,17 @@ Q_AUTOTEST_EXPORT QString qt_normalizePathSegments(const QString &name, bool all } // detect up dir - if (i >= 1 && p[i] == '.' && p[i-1] == '.' - && (i == 1 || (i >= 2 && p[i-2] == '/'))) { + if (i >= 1 && p[i] == '.' && p[i-1] == '.' && (i < 2 || p[i - 2] == '/')) { ++up; - i -= 2; + i -= i >= 2 ? 3 : 2; + + if (isRemote) { + // moving up should consider empty path segments too (/path//../ -> /path/) + while (i > 0 && up && p[i] == '/') { + --up; + --i; + } + } continue; } @@ -2216,7 +2256,27 @@ Q_AUTOTEST_EXPORT QString qt_normalizePathSegments(const QString &name, bool all // skip or copy while (i >= 0) { - if (p[i] == '/') { // do not copy slashes + if (p[i] == '/') { + // copy all slashes as is for remote urls if they are not part of /./ or /../ + if (isRemote && !up) { + while (i > 0 && p[i] == '/' && !isDotDot(p, i)) { + + if (isDot(p, i)) { + i -= 2; + continue; + } + + out[--used] = p[i]; + --i; + } + + // in case of /./, jump over + if (isDot(p, i)) + i -= 2; + + break; + } + --i; break; } @@ -2237,7 +2297,7 @@ Q_AUTOTEST_EXPORT QString qt_normalizePathSegments(const QString &name, bool all *ok = prefixLength == 0 || up == 0; // add remaining '..' - while (up) { + while (up && !isRemote) { if (used != len && out[used] != '/') // is not empty and there isn't already a '/' out[--used] = '/'; out[--used] = '.'; @@ -2283,7 +2343,7 @@ static QString qt_cleanPath(const QString &path, bool *ok) if (dir_separator != QLatin1Char('/')) name.replace(dir_separator, QLatin1Char('/')); - QString ret = qt_normalizePathSegments(name, OSSupportsUncPaths, 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(QLatin1Char('/'))) { |