Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qplatformdefs.h"
6
7#ifndef QT_NO_FILESYSTEMITERATOR
8
9#include <qvarlengtharray.h>
10
11#include <memory>
12
13#include <stdlib.h>
14#include <errno.h>
15
17
18/*
19 Native filesystem iterator, which uses ::opendir()/readdir()/dirent from the system
20 libraries to iterate over the directory represented by \a entry.
21*/
23 : dirPath(entry.filePath()),
25{
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
38
40{
41 auto asFileEntry = [this](QStringView name) {
42#ifdef Q_OS_DARWIN
43 // must match QFile::decodeName
46#endif
48 };
49 if (!dir)
50 return false;
51
52 for (;;) {
53 // From readdir man page:
54 // If the end of the directory stream is reached, NULL is returned and errno is
55 // not changed. If an error occurs, NULL is returned and errno is set to indicate
56 // the error. To distinguish end of stream from an error, set errno to zero before
57 // calling readdir() and then check the value of errno if NULL is returned.
58 errno = 0;
59 dirEntry = QT_READDIR(dir.get());
60
61 if (dirEntry) {
62 // POSIX allows readdir() to return a file name in struct dirent that
63 // extends past the end of the d_name array (it's a char[1] array on QNX, for
64 // example). Therefore, we *must* call strlen() on it to get the actual length
65 // of the file name. See:
66 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html#tag_13_07_05
67 QByteArrayView name(dirEntry->d_name, strlen(dirEntry->d_name));
68 // name.size() is sufficient here, see QUtf8::convertToUnicode() for details
69 QVarLengthArray<char16_t> buffer(name.size());
70 auto *end = toUtf16.appendToBuffer(buffer.data(), name);
71 buffer.resize(end - buffer.constData());
72 if (!toUtf16.hasError()) {
73 fileEntry = asFileEntry(buffer);
74 metaData.fillFromDirEnt(*dirEntry);
75 return true;
76 } else {
77 errno = EILSEQ; // Invalid or incomplete multibyte or wide character
78 }
79 } else {
80 break;
81 }
82 }
83
84 lastError = errno;
85 return false;
86}
87
89
90#endif // QT_NO_FILESYSTEMITERATOR
bool advance(QFileSystemEntry &fileEntry, QFileSystemMetaData &metaData)
QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters filters)
bool hasError() const noexcept
Returns true if a conversion could not correctly convert a character.
\inmodule QtCore
QChar * appendToBuffer(QChar *out, QByteArrayView ba)
Decodes the sequence of bytes viewed by in and writes the decoded result into the buffer starting at ...
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5506
QString & append(QChar c)
Definition qstring.cpp:3252
@ NormalizationForm_C
Definition qstring.h:619
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version=QChar::Unicode_Unassigned) const
Returns the string in the given Unicode normalization mode, according to the given version of the Uni...
Definition qstring.cpp:8475
Combined button and popup list for selecting options.
GLuint GLuint end
GLenum GLuint buffer
GLuint name
GLuint entry
GLint GLenum GLboolean normalized
Definition qopenglext.h:752
#define Q_UNUSED(x)
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]