diff options
Diffstat (limited to 'src/corelib/io/qfilesystemiterator_unix.cpp')
-rw-r--r-- | src/corelib/io/qfilesystemiterator_unix.cpp | 113 |
1 files changed, 45 insertions, 68 deletions
diff --git a/src/corelib/io/qfilesystemiterator_unix.cpp b/src/corelib/io/qfilesystemiterator_unix.cpp index cf01eeff15..a1130728ef 100644 --- a/src/corelib/io/qfilesystemiterator_unix.cpp +++ b/src/corelib/io/qfilesystemiterator_unix.cpp @@ -1,49 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qplatformdefs.h" #include "qfilesystemiterator_p.h" -#include <private/qstringconverter_p.h> - #ifndef QT_NO_FILESYSTEMITERATOR +#include <qvarlengtharray.h> + #include <memory> #include <stdlib.h> @@ -51,53 +15,66 @@ QT_BEGIN_NAMESPACE -static bool checkNameDecodable(const char *d_name, qsizetype len) -{ - // This function is called in a loop from advance() below, but the loop is - // usually run only once. - - return QUtf8::isValidUtf8(QByteArrayView(d_name, len)).isValidUtf8; -} - -QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters filters, - const QStringList &nameFilters, QDirIterator::IteratorFlags flags) - : nativePath(entry.nativeFilePath()) - , dir(nullptr) - , dirEntry(nullptr) - , lastError(0) +/* + Native filesystem iterator, which uses ::opendir()/readdir()/dirent from the system + libraries to iterate over the directory represented by \a entry. +*/ +QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters filters) + : dirPath(entry.filePath()), + toUtf16(QStringDecoder::Utf8) { Q_UNUSED(filters); - Q_UNUSED(nameFilters); - Q_UNUSED(flags); - if ((dir = QT_OPENDIR(nativePath.constData())) == nullptr) { + dir.reset(QT_OPENDIR(entry.nativeFilePath().constData())); + if (!dir) { lastError = errno; } else { - if (!nativePath.endsWith('/')) - nativePath.append('/'); + if (!dirPath.endsWith(u'/')) + dirPath.append(u'/'); } } -QFileSystemIterator::~QFileSystemIterator() -{ - if (dir) - QT_CLOSEDIR(dir); -} +QFileSystemIterator::~QFileSystemIterator() = default; bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaData &metaData) { + auto asFileEntry = [this](QStringView name) { +#ifdef Q_OS_DARWIN + // must match QFile::decodeName + QString normalized = name.toString().normalized(QString::NormalizationForm_C); + name = normalized; +#endif + return QFileSystemEntry(dirPath + name, QFileSystemEntry::FromInternalPath()); + }; if (!dir) return false; for (;;) { - dirEntry = QT_READDIR(dir); + // From readdir man page: + // If the end of the directory stream is reached, NULL is returned and errno is + // not changed. If an error occurs, NULL is returned and errno is set to indicate + // the error. To distinguish end of stream from an error, set errno to zero before + // calling readdir() and then check the value of errno if NULL is returned. + errno = 0; + dirEntry = QT_READDIR(dir.get()); if (dirEntry) { - qsizetype len = strlen(dirEntry->d_name); - if (checkNameDecodable(dirEntry->d_name, len)) { - fileEntry = QFileSystemEntry(nativePath + QByteArray(dirEntry->d_name, len), QFileSystemEntry::FromNativePath()); + // POSIX allows readdir() to return a file name in struct dirent that + // extends past the end of the d_name array (it's a char[1] array on QNX, for + // example). Therefore, we *must* call strlen() on it to get the actual length + // of the file name. See: + // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html#tag_13_07_05 + QByteArrayView name(dirEntry->d_name, strlen(dirEntry->d_name)); + // name.size() is sufficient here, see QUtf8::convertToUnicode() for details + QVarLengthArray<char16_t> buffer(name.size()); + auto *end = toUtf16.appendToBuffer(buffer.data(), name); + buffer.resize(end - buffer.constData()); + if (!toUtf16.hasError()) { + fileEntry = asFileEntry(buffer); metaData.fillFromDirEnt(*dirEntry); return true; + } else { + errno = EILSEQ; // Invalid or incomplete multibyte or wide character } } else { break; |