1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite 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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QDebug>
42 #include <QDirIterator>
43 #include <QString>
44 
45 #ifdef Q_OS_WIN
46 #   include <windows.h>
47 #else
48 #   include <sys/stat.h>
49 #   include <sys/types.h>
50 #   include <dirent.h>
51 #   include <errno.h>
52 #   include <string.h>
53 #endif
54 
55 #include <qtest.h>
56 
57 #include "qfilesystemiterator.h"
58 
59 class tst_qdiriterator : public QObject
60 {
61     Q_OBJECT
62 private slots:
63     void posix();
posix_data()64     void posix_data() { data(); }
65     void diriterator();
diriterator_data()66     void diriterator_data() { data(); }
67     void fsiterator();
fsiterator_data()68     void fsiterator_data() { data(); }
69     void data();
70 };
71 
72 
data()73 void tst_qdiriterator::data()
74 {
75 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
76     QByteArray qtdir = qPrintable(QCoreApplication::applicationDirPath());
77     qtdir += "/depot";
78 #else
79 #if defined(Q_OS_WIN)
80     const char *qtdir = "C:\\depot\\qt\\main";
81 #else
82     const char *qtdir = ::getenv("QTDIR");
83 #endif
84     if (!qtdir) {
85         fprintf(stderr, "QTDIR not set\n");
86         exit(1);
87     }
88 #endif
89 
90     QTest::addColumn<QByteArray>("dirpath");
91     QByteArray ba = QByteArray(qtdir) + "/src/corelib";
92     QByteArray ba1 = ba + "/io";
93     QTest::newRow(ba) << ba;
94     //QTest::newRow(ba1) << ba1;
95 }
96 
97 #ifdef Q_OS_WIN
posix_helper(const wchar_t * dirpath)98 static int posix_helper(const wchar_t *dirpath)
99 {
100     int count = 0;
101     HANDLE hSearch;
102     WIN32_FIND_DATA fd;
103 
104     const size_t origDirPathLength = wcslen(dirpath);
105 
106     wchar_t appendedPath[MAX_PATH];
107     wcscpy(appendedPath, dirpath);
108     wcscat(appendedPath, L"\\*");
109     hSearch = FindFirstFile(appendedPath, &fd);
110     appendedPath[origDirPathLength] = 0;
111 
112     if (hSearch == INVALID_HANDLE_VALUE) {
113         qWarning("FindFirstFile failed");
114         return count;
115     }
116 
117     do {
118         if (!(fd.cFileName[0] == L'.' && fd.cFileName[1] == 0) &&
119             !(fd.cFileName[0] == L'.' && fd.cFileName[1] == L'.' && fd.cFileName[2] == 0))
120         {
121             if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
122                 wcscat(appendedPath, L"\\");
123                 wcscat(appendedPath, fd.cFileName);
124                 count += posix_helper(appendedPath);
125                 appendedPath[origDirPathLength] = 0;
126             }
127             else {
128                 ++count;
129             }
130         }
131     } while (FindNextFile(hSearch, &fd));
132     FindClose(hSearch);
133 
134     return count;
135 }
136 
137 #else
138 
posix_helper(const char * dirpath)139 static int posix_helper(const char *dirpath)
140 {
141     //qDebug() << "DIR" << dirpath;
142     DIR *dir = ::opendir(dirpath);
143     if (!dir)
144         return 0;
145 
146     dirent *entry = 0;
147 
148     int count = 0;
149     while ((entry = ::readdir(dir))) {
150         if (qstrcmp(entry->d_name, ".") == 0)
151             continue;
152         if (qstrcmp(entry->d_name, "..") == 0)
153             continue;
154         ++count;
155         QByteArray ba = dirpath;
156         ba += '/';
157         ba += entry->d_name;
158         struct stat st;
159         lstat(ba.constData(), &st);
160         if (S_ISDIR(st.st_mode))
161             count += posix_helper(ba.constData());
162     }
163 
164     ::closedir(dir);
165     return count;
166 }
167 #endif
168 
169 
posix()170 void tst_qdiriterator::posix()
171 {
172     QFETCH(QByteArray, dirpath);
173 
174     int count = 0;
175     QString path(dirpath);
176     QBENCHMARK {
177 #ifdef Q_OS_WIN
178         wchar_t wPath[MAX_PATH];
179         path.toWCharArray(wPath);
180         count = posix_helper(wPath);
181 #else
182         count = posix_helper(dirpath.constData());
183 #endif
184     }
185     qDebug() << count;
186 }
187 
diriterator()188 void tst_qdiriterator::diriterator()
189 {
190     QFETCH(QByteArray, dirpath);
191 
192     int count = 0;
193 
194     QBENCHMARK {
195         int c = 0;
196 
197         QDirIterator dir(dirpath,
198             //QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot,
199             //QDir::AllEntries | QDir::Hidden,
200             QDir::Files,
201             QDirIterator::Subdirectories);
202 
203         while (dir.hasNext()) {
204             dir.next();
205             //printf("%s\n", qPrintable(dir.fileName()));
206             0 && printf("%d %s\n",
207                 dir.fileInfo().isDir(),
208                 //qPrintable(dir.fileInfo().absoluteFilePath()),
209                 //qPrintable(dir.path()),
210                 qPrintable(dir.filePath()));
211             ++c;
212         }
213         count = c;
214     }
215     qDebug() << count;
216 }
217 
fsiterator()218 void tst_qdiriterator::fsiterator()
219 {
220     QFETCH(QByteArray, dirpath);
221 
222     int count = 0;
223     int dump = 0;
224 
225     QBENCHMARK {
226         int c = 0;
227 
228         dump && printf("\n\n\n\n");
229         QFileSystemIterator dir(dirpath,
230             //QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot,
231             //QDir::AllEntries | QDir::Hidden,
232             //QDir::Files | QDir::NoDotAndDotDot,
233             QDir::Files,
234             QFileSystemIterator::Subdirectories);
235 
236         for (; !dir.atEnd(); dir.next()) {
237             dump && printf("%d %s\n",
238                 dir.fileInfo().isDir(),
239                 //qPrintable(dir.fileInfo().absoluteFilePath()),
240                 //qPrintable(dir.path()),
241                 qPrintable(dir.filePath())
242             );
243             ++c;
244         }
245         count = c;
246     }
247     qDebug() << count;
248 }
249 
250 QTEST_MAIN(tst_qdiriterator)
251 
252 #include "main.moc"
253