diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-12-17 22:07:30 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2024-02-03 00:12:00 +0200 |
commit | 0516d48ae9538d7c51e270f1b90d601d437dc060 (patch) | |
tree | 6c65dbcbcf5fdc04da53485a3c0ff9a5cda23a44 /src/corelib/io/qfilesystemiterator_unix.cpp | |
parent | 3758829162ec38c1b010a0ee4ac836da6180bb55 (diff) |
QFileSystemIterator: set errno to 0 before calling readdir()
So that errno can be used to distinguish between "reached end of dir
stream" state and an error.
Set errno to something more useful (EILSEQ) when we fail to decode an
entry's name, requested by Thiago in code review.
Change-Id: I8091144d25e5e5aa875cf40eaf6ee13c9e409ee7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qfilesystemiterator_unix.cpp')
-rw-r--r-- | src/corelib/io/qfilesystemiterator_unix.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/corelib/io/qfilesystemiterator_unix.cpp b/src/corelib/io/qfilesystemiterator_unix.cpp index 1f059f175d..96ded99a8b 100644 --- a/src/corelib/io/qfilesystemiterator_unix.cpp +++ b/src/corelib/io/qfilesystemiterator_unix.cpp @@ -23,6 +23,10 @@ static bool checkNameDecodable(const char *d_name, qsizetype len) return QUtf8::isValidUtf8(QByteArrayView(d_name, len)).isValidUtf8; } +/* + 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, const QStringList &nameFilters, QDirIterator::IteratorFlags flags) : nativePath(entry.nativeFilePath()) @@ -48,6 +52,12 @@ bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaDa return false; for (;;) { + // 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) { @@ -56,6 +66,8 @@ bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaDa fileEntry = QFileSystemEntry(nativePath + QByteArray(dirEntry->d_name, len), QFileSystemEntry::FromNativePath()); metaData.fillFromDirEnt(*dirEntry); return true; + } else { + errno = EILSEQ; // Invalid or incomplete multibyte or wide character } } else { break; |