1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qfilesystemiterator_p.h"
41 #include "qfilesystemengine_p.h"
42 #include "qoperatingsystemversion.h"
43 #include "qplatformdefs.h"
44 #include "qvector.h"
45 
46 #include <QtCore/qt_windows.h>
47 
48 QT_BEGIN_NAMESPACE
49 
50 bool done = true;
51 
QFileSystemIterator(const QFileSystemEntry & entry,QDir::Filters filters,const QStringList & nameFilters,QDirIterator::IteratorFlags flags)52 QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters filters,
53                                          const QStringList &nameFilters, QDirIterator::IteratorFlags flags)
54     : nativePath(entry.nativeFilePath())
55     , dirPath(entry.filePath())
56     , findFileHandle(INVALID_HANDLE_VALUE)
57     , uncFallback(false)
58     , uncShareIndex(0)
59     , onlyDirs(false)
60 {
61     Q_UNUSED(nameFilters)
62     Q_UNUSED(flags)
63     if (nativePath.endsWith(QLatin1String(".lnk"))) {
64         QFileSystemMetaData metaData;
65         QFileSystemEntry link = QFileSystemEngine::getLinkTarget(entry, metaData);
66         nativePath = link.nativeFilePath();
67     }
68     if (!nativePath.endsWith(QLatin1Char('\\')))
69         nativePath.append(QLatin1Char('\\'));
70     nativePath.append(QLatin1Char('*'));
71     // In MSVC2015+ case we prepend //?/ for longer file-name support
72     if (!dirPath.endsWith(QLatin1Char('/')))
73         dirPath.append(QLatin1Char('/'));
74     if ((filters & (QDir::Dirs|QDir::Drives)) && (!(filters & (QDir::Files))))
75         onlyDirs = true;
76 }
77 
~QFileSystemIterator()78 QFileSystemIterator::~QFileSystemIterator()
79 {
80    if (findFileHandle != INVALID_HANDLE_VALUE)
81         FindClose(findFileHandle);
82 }
83 
advance(QFileSystemEntry & fileEntry,QFileSystemMetaData & metaData)84 bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaData &metaData)
85 {
86     bool haveData = false;
87     WIN32_FIND_DATA findData;
88 
89     if (findFileHandle == INVALID_HANDLE_VALUE && !uncFallback) {
90         haveData = true;
91         int infoLevel = 0 ;         // FindExInfoStandard;
92         DWORD dwAdditionalFlags  = 0;
93         if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows7) {
94             dwAdditionalFlags = 2;  // FIND_FIRST_EX_LARGE_FETCH
95             infoLevel = 1 ;         // FindExInfoBasic;
96         }
97         int searchOps =  0;         // FindExSearchNameMatch
98         if (onlyDirs)
99             searchOps = 1 ;         // FindExSearchLimitToDirectories
100         findFileHandle = FindFirstFileEx((const wchar_t *)nativePath.utf16(), FINDEX_INFO_LEVELS(infoLevel), &findData,
101                                          FINDEX_SEARCH_OPS(searchOps), 0, dwAdditionalFlags);
102         if (findFileHandle == INVALID_HANDLE_VALUE) {
103             if (nativePath.startsWith(QLatin1String("\\\\?\\UNC\\"))) {
104                 const QVector<QStringRef> parts = nativePath.splitRef(QLatin1Char('\\'), Qt::SkipEmptyParts);
105                 if (parts.count() == 4 && QFileSystemEngine::uncListSharesOnServer(
106                         QLatin1String("\\\\") + parts.at(2), &uncShares)) {
107                     if (uncShares.isEmpty())
108                         return false; // No shares found in the server
109                     uncFallback = true;
110                 }
111             }
112         }
113     }
114     if (findFileHandle == INVALID_HANDLE_VALUE && !uncFallback)
115         return false;
116     // Retrieve the new file information.
117     if (!haveData) {
118         if (uncFallback) {
119             if (++uncShareIndex >= uncShares.count())
120                 return false;
121         } else {
122             if (!FindNextFile(findFileHandle, &findData))
123                 return false;
124         }
125     }
126     // Create the new file system entry & meta data.
127     if (uncFallback) {
128         fileEntry = QFileSystemEntry(dirPath + uncShares.at(uncShareIndex));
129         metaData.fillFromFileAttribute(FILE_ATTRIBUTE_DIRECTORY);
130         return true;
131     } else {
132         QString fileName = QString::fromWCharArray(findData.cFileName);
133         fileEntry = QFileSystemEntry(dirPath + fileName);
134         metaData = QFileSystemMetaData();
135         if (!fileName.endsWith(QLatin1String(".lnk"))) {
136             metaData.fillFromFindData(findData, true);
137         }
138         return true;
139     }
140     return false;
141 }
142 
143 QT_END_NAMESPACE
144