Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfilesystemiterator_unix.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qplatformdefs.h"
7
8#ifndef QT_NO_FILESYSTEMITERATOR
9
10#include <qvarlengtharray.h>
11
12#include <private/qstringconverter_p.h>
13
14#include <memory>
15
16#include <stdlib.h>
17#include <errno.h>
18
20
21/*
22 Native filesystem iterator, which uses ::opendir()/readdir()/dirent from the system
23 libraries to iterate over the directory represented by \a entry.
24*/
25QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry)
26 : dirPath(entry.filePath())
27{
28 dir.reset(QT_OPENDIR(entry.nativeFilePath().constData()));
29 if (!dir) {
30 lastError = errno;
31 } else {
32 if (!dirPath.endsWith(u'/'))
33 dirPath.append(u'/');
34 }
35}
36
37QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDirListing::IteratorFlags)
38 : QFileSystemIterator(entry)
39{}
40
41QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters)
42 : QFileSystemIterator(entry)
43{
44}
45
46QFileSystemIterator::~QFileSystemIterator() = default;
47
48std::optional<QDirEntryInfo> QFileSystemIterator::advance()
49{
50 auto asFileEntry = [this](QStringView name) {
51#ifdef Q_OS_DARWIN
52 // must match QFile::decodeName
53 QString normalized = name.toString().normalized(QString::NormalizationForm_C);
54 name = normalized;
55#endif
56 return QFileSystemEntry(dirPath + name, QFileSystemEntry::FromInternalPath());
57 };
58 if (!dir)
59 return std::nullopt;
60
61 for (;;) {
62 // From readdir man page:
63 // If the end of the directory stream is reached, NULL is returned and errno is
64 // not changed. If an error occurs, NULL is returned and errno is set to indicate
65 // the error. To distinguish end of stream from an error, set errno to zero before
66 // calling readdir() and then check the value of errno if NULL is returned.
67 errno = 0;
68 dirEntry = QT_READDIR(dir.get());
69
70 if (dirEntry) {
71 // POSIX allows readdir() to return a file name in struct dirent that
72 // extends past the end of the d_name array (it's a char[1] array on QNX, for
73 // example). Therefore, we *must* call strlen() on it to get the actual length
74 // of the file name. See:
75 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html#tag_13_07_05
76 QByteArrayView name(dirEntry->d_name, strlen(dirEntry->d_name));
77 // name.size() is sufficient here, see QUtf8::convertToUnicode() for details
78 QVarLengthArray<char16_t> buffer(name.size());
79 QStringDecoder toUtf16(QStringDecoder::Utf8);
80 auto *end = toUtf16.appendToBuffer(buffer.data(), name);
81 buffer.resize(end - buffer.constData());
82 if (!toUtf16.hasError()) {
83 QFileSystemMetaData metaData;
84 metaData.fillFromDirEnt(*dirEntry);
85 return QDirEntryInfo{asFileEntry(buffer), std::move(metaData)};
86 } else {
87 errno = EILSEQ; // Invalid or incomplete multibyte or wide character
88 }
89 } else {
90 break;
91 }
92 }
93
94 lastError = errno;
95 return std::nullopt;
96}
97
98QT_END_NAMESPACE
99
100#endif // QT_NO_FILESYSTEMITERATOR
Combined button and popup list for selecting options.