Qt 4.8
qfilesystemiterator_unix.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qplatformdefs.h"
43 #include "qfilesystemiterator_p.h"
44 
45 #ifndef QT_NO_FILESYSTEMITERATOR
46 
47 #include <stdlib.h>
48 #include <errno.h>
49 
51 
53  const QStringList &nameFilters, QDirIterator::IteratorFlags flags)
54  : nativePath(entry.nativeFilePath())
55  , dir(0)
56  , dirEntry(0)
57 #if defined(Q_OS_QNX) && defined(__EXT_QNX__READDIR_R)
58  , direntSize(0)
59 #endif
60  , lastError(0)
61 {
62  Q_UNUSED(filters)
63  Q_UNUSED(nameFilters)
64  Q_UNUSED(flags)
65 
66  if ((dir = QT_OPENDIR(nativePath.constData())) == 0) {
67  lastError = errno;
68  } else {
69 
70  if (!nativePath.endsWith('/'))
71  nativePath.append('/');
72 
73 #if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN)
74  // ### Race condition; we should use fpathconf and dirfd().
75  size_t maxPathName = ::pathconf(nativePath.constData(), _PC_NAME_MAX);
76  if (maxPathName == size_t(-1))
77  maxPathName = FILENAME_MAX;
78  maxPathName += sizeof(QT_DIRENT) + 1;
79 
80  QT_DIRENT *p = reinterpret_cast<QT_DIRENT*>(::malloc(maxPathName));
81  Q_CHECK_PTR(p);
82 
83  mt_file.reset(p);
84 #if defined(Q_OS_QNX) && defined(__EXT_QNX__READDIR_R)
85  direntSize = maxPathName;
86 
87  // Include extra stat information in the readdir() call (d_stat member of dirent_extra_stat).
88  // This is used in QFileSystemMetaData::fillFromDirEnt() to avoid extra stat() calls when iterating
89  // over directories
90  if (dircntl(dir, D_SETFLAG, D_FLAG_STAT) == -1)
91  lastError = errno;
92 #endif
93 #endif
94  }
95 }
96 
98 {
99  if (dir)
100  QT_CLOSEDIR(dir);
101 }
102 
104 {
105  if (!dir)
106  return false;
107 
108 #if defined(Q_OS_QNX) && defined(QT_EXT_QNX_READDIR_R)
109  lastError = QT_EXT_QNX_READDIR_R(dir, mt_file.data(), &dirEntry, direntSize);
110  if (lastError)
111  return false;
112 #elif defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN)
113  lastError = QT_READDIR_R(dir, mt_file.data(), &dirEntry);
114  if (lastError)
115  return false;
116 #else
117  // ### add local lock to prevent breaking reentrancy
118  dirEntry = QT_READDIR(dir);
119 #endif // _POSIX_THREAD_SAFE_FUNCTIONS
120 
121  if (dirEntry) {
122  fileEntry = QFileSystemEntry(nativePath + QByteArray(dirEntry->d_name), QFileSystemEntry::FromNativePath());
123  metaData.fillFromDirEnt(*dirEntry);
124  return true;
125  }
126 
127  lastError = errno;
128  return false;
129 }
130 
132 
133 #endif // QT_NO_FILESYSTEMITERATOR
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QFileSystemEntry::NativePath nativePath
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
bool advance(QFileSystemEntry &fileEntry, QFileSystemMetaData &metaData)
static const char *const filters[3]
#define Q_CHECK_PTR(p)
Definition: qglobal.h:1853
QString & append(QChar c)
Definition: qstring.cpp:1777
QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters filters, const QStringList &nameFilters, QDirIterator::IteratorFlags flags=QDirIterator::FollowSymlinks|QDirIterator::Subdirectories)
if(void) toggleToolbarShown
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:3796
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
void fillFromDirEnt(const QT_DIRENT &statBuffer)
int errno