Qt 4.8
qfilesystemiterator_win.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 #if _WIN32_WINNT < 0x0500
43 #undef _WIN32_WINNT
44 #define _WIN32_WINNT 0x0500
45 #endif
46 
47 #include "qfilesystemiterator_p.h"
48 #include "qfilesystemengine_p.h"
49 #include "qplatformdefs.h"
50 
51 #include <QtCore/qt_windows.h>
52 
54 #ifndef QT_NO_FILESYSTEMITERATOR
55 
56 bool done = true;
57 
59  const QStringList &nameFilters, QDirIterator::IteratorFlags flags)
60  : nativePath(entry.nativeFilePath())
61  , dirPath(entry.filePath())
62  , findFileHandle(INVALID_HANDLE_VALUE)
63  , uncFallback(false)
64  , uncShareIndex(0)
65  , onlyDirs(false)
66 {
67  Q_UNUSED(nameFilters)
68  Q_UNUSED(flags)
69  if (nativePath.endsWith(QLatin1String(".lnk"))) {
70  QFileSystemMetaData metaData;
71  QFileSystemEntry link = QFileSystemEngine::getLinkTarget(entry, metaData);
72  nativePath = link.nativeFilePath();
73  }
74  if (!nativePath.endsWith(QLatin1Char('\\')))
75  nativePath.append(QLatin1Char('\\'));
76  nativePath.append(QLatin1Char('*'));
77  if (!dirPath.endsWith(QLatin1Char('/')))
78  dirPath.append(QLatin1Char('/'));
79  if ((filters & (QDir::Dirs|QDir::Drives)) && (!(filters & (QDir::Files))))
80  onlyDirs = true;
81 }
82 
84 {
85  if (findFileHandle != INVALID_HANDLE_VALUE)
86  FindClose(findFileHandle);
87 }
88 
90 {
91  bool haveData = false;
92  WIN32_FIND_DATA findData;
93 
94  if (findFileHandle == INVALID_HANDLE_VALUE && !uncFallback) {
95  haveData = true;
96  int infoLevel = 0 ; // FindExInfoStandard;
97  DWORD dwAdditionalFlags = 0;
98 #ifndef Q_OS_WINCE
100  dwAdditionalFlags = 2; // FIND_FIRST_EX_LARGE_FETCH
101  infoLevel = 1 ; // FindExInfoBasic;
102  }
103 #endif
104  int searchOps = 0; // FindExSearchNameMatch
105  if (onlyDirs)
106  searchOps = 1 ; // FindExSearchLimitToDirectories
107  findFileHandle = FindFirstFileEx((const wchar_t *)nativePath.utf16(), FINDEX_INFO_LEVELS(infoLevel), &findData,
108  FINDEX_SEARCH_OPS(searchOps), 0, dwAdditionalFlags);
109  if (findFileHandle == INVALID_HANDLE_VALUE) {
110  if (nativePath.startsWith(QLatin1String("\\\\?\\UNC\\"))) {
113  QLatin1String("\\\\") + parts.at(2), &uncShares)) {
114  if (uncShares.isEmpty())
115  return false; // No shares found in the server
116  else
117  uncFallback = true;
118  }
119  }
120  }
121  }
122  if (findFileHandle == INVALID_HANDLE_VALUE && !uncFallback)
123  return false;
124  // Retrieve the new file information.
125  if (!haveData) {
126  if (uncFallback) {
127  if (++uncShareIndex >= uncShares.count())
128  return false;
129  } else {
130  if (!FindNextFile(findFileHandle, &findData))
131  return false;
132  }
133  }
134  // Create the new file system entry & meta data.
135  if (uncFallback) {
137  metaData.fillFromFileAttribute(FILE_ATTRIBUTE_DIRECTORY);
138  return true;
139  } else {
140  QString fileName = QString::fromWCharArray(findData.cFileName);
141  fileEntry = QFileSystemEntry(dirPath + fileName);
142  metaData = QFileSystemMetaData();
143  if (!fileName.endsWith(QLatin1String(".lnk"))) {
144  metaData.fillFromFindData(findData, true);
145  }
146  return true;
147  }
148  return false;
149 }
150 
151 #endif // QT_NO_FILESYSTEMITERATOR
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
void fillFromFileAttribute(DWORD fileAttribute, bool isDriveRoot=false)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QFileSystemEntry::NativePath nativePath
static WinVersion windowsVersion()
Returns the version of the Windows operating system on which the application is run (Windows only)...
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
void fillFromFindData(WIN32_FIND_DATA &findData, bool setLinkType=false, bool isDriveRoot=false)
static bool uncListSharesOnServer(const QString &server, QStringList *list)
QFileSystemEntry::NativePath dirPath
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
NativePath nativeFilePath() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static QFileSystemEntry getLinkTarget(const QFileSystemEntry &link, QFileSystemMetaData &data)
bool advance(QFileSystemEntry &fileEntry, QFileSystemMetaData &metaData)
static const char *const filters[3]
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)
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
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
static QString fileName(const QString &fileUrl)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290