Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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"
6
#
include
"qfilesystemiterator_p.h"
7
8
#
ifndef
QT_NO_FILESYSTEMITERATOR
9
10
#
include
<
qvarlengtharray
.
h
>
11
12
#
include
<
private
/
qstringconverter_p
.
h
>
13
14
#
include
<
stdlib
.
h
>
15
#
include
<
errno
.
h
>
16
17
QT_BEGIN_NAMESPACE
18
19
/*
20
Native filesystem iterator, which uses ::opendir()/readdir()/dirent from the system
21
libraries to iterate over the directory represented by \a entry.
22
*/
23
QFileSystemIterator::QFileSystemIterator(
const
QFileSystemEntry &entry)
24
: dirPath(entry.filePath())
25
{
26
dir.reset(QT_OPENDIR(entry.nativeFilePath().constData()));
27
if
(!dir) {
28
lastError = errno;
29
}
else
{
30
if
(!dirPath.endsWith(u'/'))
31
dirPath.append(u'/');
32
}
33
}
34
35
QFileSystemIterator::QFileSystemIterator(
const
QFileSystemEntry &entry, QDirListing::IteratorFlags)
36
: QFileSystemIterator(entry)
37
{}
38
39
QFileSystemIterator::QFileSystemIterator(
const
QFileSystemEntry &entry, QDir::Filters)
40
: QFileSystemIterator(entry)
41
{
42
}
43
44
QFileSystemIterator::~QFileSystemIterator() =
default
;
45
46
QFileSystemNativeId QFileSystemIterator::nativeId()
const
47
{
48
if
(!dir)
49
return
{};
50
#
if
QT_CONFIG
(
dirfd
)
51
// We can use dirfd() to fstat() the open directory - no race condition.
52
return
QFileSystemEngine::nativeId(dirfd(dir.get()));
53
#
else
54
// We must stat() the name - possible race condition, but already present
55
// in QDirListing because we use full file names.
56
return
QFileSystemEngine::nativeId(
57
QFileSystemEntry(dirPath, QFileSystemEntry::FromInternalPath()));
58
#
endif
59
}
60
61
std::optional<QDirEntryInfo> QFileSystemIterator::advance()
62
{
63
auto
asFileEntry = [
this
](QStringView name) {
64
#
ifdef
Q_OS_DARWIN
65
// must match QFile::decodeName
66
QString normalized = name.toString().normalized(QString::NormalizationForm_C);
67
name = normalized;
68
#
endif
69
return
QFileSystemEntry(dirPath + name, QFileSystemEntry::FromInternalPath());
70
};
71
if
(!dir)
72
return
std::nullopt;
73
74
for
(;;) {
75
// From readdir man page:
76
// If the end of the directory stream is reached, NULL is returned and errno is
77
// not changed. If an error occurs, NULL is returned and errno is set to indicate
78
// the error. To distinguish end of stream from an error, set errno to zero before
79
// calling readdir() and then check the value of errno if NULL is returned.
80
errno = 0;
81
dirEntry = QT_READDIR(dir.get());
82
83
if
(dirEntry) {
84
// POSIX allows readdir() to return a file name in struct dirent that
85
// extends past the end of the d_name array (it's a char[1] array on QNX, for
86
// example). Therefore, we *must* call strlen() on it to get the actual length
87
// of the file name. See:
88
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html#tag_13_07_05
89
QByteArrayView name(dirEntry->d_name, strlen(dirEntry->d_name));
90
// name.size() is sufficient here, see QUtf8::convertToUnicode() for details
91
QVarLengthArray<
char16_t
> buffer(name.size());
92
QStringDecoder toUtf16(QStringDecoder::Utf8);
93
auto
*end = toUtf16.appendToBuffer(buffer.data(), name);
94
buffer.resize(end - buffer.constData());
95
if
(!toUtf16.hasError()) {
96
QFileSystemMetaData metaData;
97
metaData.fillFromDirEnt(*dirEntry);
98
return
QDirEntryInfo{asFileEntry(buffer), std::move(metaData)};
99
}
else
{
100
errno = EILSEQ;
// Invalid or incomplete multibyte or wide character
101
}
102
}
else
{
103
break
;
104
}
105
}
106
107
lastError = errno;
108
return
std::nullopt;
109
}
110
111
QT_END_NAMESPACE
112
113
#
endif
// QT_NO_FILESYSTEMITERATOR
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtbase
src
corelib
io
qfilesystemiterator_unix.cpp
Generated on
for Qt by
1.18.0