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"
5#include "qfilesystemiterator_p.h"
6
7#include <private/qstringconverter_p.h>
8
9#ifndef QT_NO_FILESYSTEMITERATOR
10
11#include <memory>
12
13#include <stdlib.h>
14#include <errno.h>
15
16QT_BEGIN_NAMESPACE
17
18static bool checkNameDecodable(const char *d_name, qsizetype len)
19{
20 // This function is called in a loop from advance() below, but the loop is
21 // usually run only once.
22
23 return QUtf8::isValidUtf8(in: QByteArrayView(d_name, len)).isValidUtf8;
24}
25
26QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters filters,
27 const QStringList &nameFilters, QDirIterator::IteratorFlags flags)
28 : nativePath(entry.nativeFilePath())
29 , dir(nullptr)
30 , dirEntry(nullptr)
31 , lastError(0)
32{
33 Q_UNUSED(filters);
34 Q_UNUSED(nameFilters);
35 Q_UNUSED(flags);
36
37 if ((dir = QT_OPENDIR(name: nativePath.constData())) == nullptr) {
38 lastError = errno;
39 } else {
40 if (!nativePath.endsWith(c: '/'))
41 nativePath.append(c: '/');
42 }
43}
44
45QFileSystemIterator::~QFileSystemIterator()
46{
47 if (dir)
48 QT_CLOSEDIR(dirp: dir);
49}
50
51bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaData &metaData)
52{
53 if (!dir)
54 return false;
55
56 for (;;) {
57 dirEntry = QT_READDIR(dirp: dir);
58
59 if (dirEntry) {
60 qsizetype len = strlen(s: dirEntry->d_name);
61 if (checkNameDecodable(d_name: dirEntry->d_name, len)) {
62 fileEntry = QFileSystemEntry(nativePath + QByteArray(dirEntry->d_name, len), QFileSystemEntry::FromNativePath());
63 metaData.fillFromDirEnt(statBuffer: *dirEntry);
64 return true;
65 }
66 } else {
67 break;
68 }
69 }
70
71 lastError = errno;
72 return false;
73}
74
75QT_END_NAMESPACE
76
77#endif // QT_NO_FILESYSTEMITERATOR
78

source code of qtbase/src/corelib/io/qfilesystemiterator_unix.cpp