| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** All rights reserved. |
| 5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
| 6 |
** |
| 7 |
** This file is part of the QtCore module of the Qt Toolkit. |
| 8 |
** |
| 9 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 10 |
** No Commercial Usage |
| 11 |
** This file contains pre-release code and may not be distributed. |
| 12 |
** You may use this file in accordance with the terms and conditions |
| 13 |
** contained in the Technology Preview License Agreement accompanying |
| 14 |
** this package. |
| 15 |
** |
| 16 |
** GNU Lesser General Public License Usage |
| 17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
| 18 |
** General Public License version 2.1 as published by the Free Software |
| 19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
| 20 |
** packaging of this file. Please review the following information to |
| 21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
| 22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 23 |
** |
| 24 |
** In addition, as a special exception, Nokia gives you certain additional |
| 25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 27 |
** |
| 28 |
** If you have questions regarding the use of this file, please contact |
| 29 |
** Nokia at qt-info@nokia.com. |
| 30 |
** |
| 31 |
** |
| 32 |
** |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 37 |
** |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
#include "qplatformdefs.h" |
| 43 |
#include "qfsfileengine_iterator_p.h" |
| 44 |
|
| 45 |
#include <QtCore/qvariant.h> |
| 46 |
|
| 47 |
#ifndef QT_NO_FSFILEENGINE |
| 48 |
|
| 49 |
QT_BEGIN_NAMESPACE |
| 50 |
|
| 51 |
class QFSFileEngineIteratorPlatformSpecificData |
| 52 |
{ |
| 53 |
public: |
| 54 |
inline QFSFileEngineIteratorPlatformSpecificData() |
| 55 |
: dir(0), dirEntry(0), done(false) |
| 56 |
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) |
| 57 |
, mt_file(0) |
| 58 |
#endif |
| 59 |
{ } |
| 60 |
|
| 61 |
DIR *dir; |
| 62 |
dirent *dirEntry; |
| 63 |
bool done; |
| 64 |
|
| 65 |
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) |
| 66 |
// for readdir_r |
| 67 |
dirent *mt_file; |
| 68 |
#endif |
| 69 |
}; |
| 70 |
|
| 71 |
void QFSFileEngineIterator::advance() |
| 72 |
{ |
| 73 |
currentEntry = platform->dirEntry ? QFile::decodeName(QByteArray(platform->dirEntry->d_name)) : QString(); |
| 74 |
|
| 75 |
if (!platform->dir) |
| 76 |
return; |
| 77 |
|
| 78 |
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) |
| 79 |
if (::readdir_r(platform->dir, platform->mt_file, &platform->dirEntry) != 0) |
| 80 |
platform->done = true; |
| 81 |
#else |
| 82 |
// ### add local lock to prevent breaking reentrancy |
| 83 |
platform->dirEntry = ::readdir(platform->dir); |
| 84 |
#endif // _POSIX_THREAD_SAFE_FUNCTIONS |
| 85 |
if (!platform->dirEntry) { |
| 86 |
::closedir(platform->dir); |
| 87 |
platform->dir = 0; |
| 88 |
platform->done = true; |
| 89 |
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) |
| 90 |
delete [] platform->mt_file; |
| 91 |
platform->mt_file = 0; |
| 92 |
#endif |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void QFSFileEngineIterator::newPlatformSpecifics() |
| 97 |
{ |
| 98 |
platform = new QFSFileEngineIteratorPlatformSpecificData; |
| 99 |
} |
| 100 |
|
| 101 |
void QFSFileEngineIterator::deletePlatformSpecifics() |
| 102 |
{ |
| 103 |
if (platform->dir) { |
| 104 |
::closedir(platform->dir); |
| 105 |
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) |
| 106 |
delete [] platform->mt_file; |
| 107 |
platform->mt_file = 0; |
| 108 |
#endif |
| 109 |
} |
| 110 |
delete platform; |
| 111 |
platform = 0; |
| 112 |
} |
| 113 |
|
| 114 |
bool QFSFileEngineIterator::hasNext() const |
| 115 |
{ |
| 116 |
if (!platform->done && !platform->dir) { |
| 117 |
QFSFileEngineIterator *that = const_cast<QFSFileEngineIterator *>(this); |
| 118 |
if ((that->platform->dir = ::opendir(QFile::encodeName(path()).data())) == 0) { |
| 119 |
that->platform->done = true; |
| 120 |
} else { |
| 121 |
// ### Race condition; we should use fpathconf and dirfd(). |
| 122 |
long maxPathName = ::pathconf(QFile::encodeName(path()).data(), _PC_NAME_MAX); |
| 123 |
if ((int) maxPathName == -1) |
| 124 |
maxPathName = FILENAME_MAX; |
| 125 |
maxPathName += sizeof(dirent) + 1; |
| 126 |
#if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_CYGWIN) |
| 127 |
if (that->platform->mt_file) |
| 128 |
delete [] that->platform->mt_file; |
| 129 |
that->platform->mt_file = (dirent *)new char[maxPathName]; |
| 130 |
#endif |
| 131 |
|
| 132 |
that->advance(); |
| 133 |
} |
| 134 |
} |
| 135 |
return !platform->done; |
| 136 |
} |
| 137 |
|
| 138 |
QT_END_NAMESPACE |
| 139 |
|
| 140 |
#endif // QT_NO_FSFILEENGINE |