1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qfilesystemengine_p.h"
#include <QtCore/qdir.h>
#include <QtCore/qset.h>
#include <QtCore/qstringbuilder.h>
#include <QtCore/private/qabstractfileengine_p.h>
#ifdef QT_BUILD_CORE_LIB
#include <QtCore/private/qresource_p.h>
#endif
QT_BEGIN_NAMESPACE
/*!
\internal
Returns the canonicalized form of \a path (i.e., with all symlinks
resolved, and all redundant path elements removed.
*/
QString QFileSystemEngine::slowCanonicalized(const QString &path)
{
if (path.isEmpty())
return path;
QFileInfo fi;
const QChar slash(QLatin1Char('/'));
QString tmpPath = path;
int separatorPos = 0;
QSet<QString> nonSymlinks;
QSet<QString> known;
known.insert(path);
do {
#ifdef Q_OS_WIN
if (separatorPos == 0) {
if (tmpPath.size() >= 2 && tmpPath.at(0) == slash && tmpPath.at(1) == slash) {
// UNC, skip past the first two elements
separatorPos = tmpPath.indexOf(slash, 2);
} else if (tmpPath.size() >= 3 && tmpPath.at(1) == QLatin1Char(':') && tmpPath.at(2) == slash) {
// volume root, skip since it can not be a symlink
separatorPos = 2;
}
}
if (separatorPos != -1)
#endif
separatorPos = tmpPath.indexOf(slash, separatorPos + 1);
QString prefix = separatorPos == -1 ? tmpPath : tmpPath.left(separatorPos);
if (
#ifdef Q_OS_SYMBIAN
// Symbian doesn't support directory symlinks, so do not check for link unless we
// are handling the last path element. This not only slightly improves performance,
// but also saves us from lot of unnecessary platform security check failures
// when dealing with files under *:/private directories.
separatorPos == -1 &&
#endif
!nonSymlinks.contains(prefix)) {
fi.setFile(prefix);
if (fi.isSymLink()) {
QString target = fi.symLinkTarget();
if(QFileInfo(target).isRelative())
target = fi.absolutePath() + slash + target;
if (separatorPos != -1) {
if (fi.isDir() && !target.endsWith(slash))
target.append(slash);
target.append(tmpPath.mid(separatorPos));
}
tmpPath = QDir::cleanPath(target);
separatorPos = 0;
if (known.contains(tmpPath))
return QString();
known.insert(tmpPath);
} else {
nonSymlinks.insert(prefix);
}
}
} while (separatorPos != -1);
return QDir::cleanPath(tmpPath);
}
static inline bool _q_checkEntry(QFileSystemEntry &entry, QFileSystemMetaData &data, bool resolvingEntry)
{
if (resolvingEntry) {
if (!QFileSystemEngine::fillMetaData(entry, data, QFileSystemMetaData::ExistsAttribute)
|| !data.exists()) {
data.clear();
return false;
}
}
return true;
}
static inline bool _q_checkEntry(QAbstractFileEngine *&engine, bool resolvingEntry)
{
if (resolvingEntry) {
if (!(engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::ExistsFlag)) {
delete engine;
engine = 0;
return false;
}
}
return true;
}
static bool _q_resolveEntryAndCreateLegacyEngine_recursive(QFileSystemEntry &entry, QFileSystemMetaData &data,
QAbstractFileEngine *&engine, bool resolvingEntry = false)
{
QString const &filePath = entry.filePath();
if ((engine = qt_custom_file_engine_handler_create(filePath)))
return _q_checkEntry(engine, resolvingEntry);
#if defined(QT_BUILD_CORE_LIB)
for (int prefixSeparator = 0; prefixSeparator < filePath.size(); ++prefixSeparator) {
QChar const ch = filePath[prefixSeparator];
if (ch == QLatin1Char('/'))
break;
if (ch == QLatin1Char(':')) {
if (prefixSeparator == 0) {
engine = new QResourceFileEngine(filePath);
return _q_checkEntry(engine, resolvingEntry);
}
if (prefixSeparator == 1)
break;
const QStringList &paths = QDir::searchPaths(filePath.left(prefixSeparator));
for (int i = 0; i < paths.count(); i++) {
entry = QFileSystemEntry(QDir::cleanPath(paths.at(i) % QLatin1Char('/') % filePath.mid(prefixSeparator + 1)));
// Recurse!
if (_q_resolveEntryAndCreateLegacyEngine_recursive(entry, data, engine, true))
return true;
}
// entry may have been clobbered at this point.
return false;
}
// There's no need to fully validate the prefix here. Consulting the
// unicode tables could be expensive and validation is already
// performed in QDir::setSearchPaths.
//
// if (!ch.isLetterOrNumber())
// break;
}
#endif // defined(QT_BUILD_CORE_LIB)
return _q_checkEntry(entry, data, resolvingEntry);
}
/*!
\internal
Resolves the \a entry (see QDir::searchPaths) and returns an engine for
it, but never a QFSFileEngine.
Returns a file engine that can be used to access the entry. Returns 0 if
QFileSystemEngine API should be used to query and interact with the file
system object.
*/
QAbstractFileEngine *QFileSystemEngine::resolveEntryAndCreateLegacyEngine(
QFileSystemEntry &entry, QFileSystemMetaData &data) {
QFileSystemEntry copy = entry;
QAbstractFileEngine *engine = 0;
if (_q_resolveEntryAndCreateLegacyEngine_recursive(copy, data, engine))
// Reset entry to resolved copy.
entry = copy;
else
data.clear();
return engine;
}
//these unix functions are in this file, because they are shared by symbian port
//for open C file handles.
#ifdef Q_OS_UNIX
//static
bool QFileSystemEngine::fillMetaData(int fd, QFileSystemMetaData &data)
{
data.entryFlags &= ~QFileSystemMetaData::PosixStatFlags;
data.knownFlagsMask |= QFileSystemMetaData::PosixStatFlags;
QT_STATBUF statBuffer;
if (QT_FSTAT(fd, &statBuffer) == 0) {
data.fillFromStatBuf(statBuffer);
return true;
}
return false;
}
#if defined(Q_OS_QNX)
static void fillStat64fromStat32(struct stat64 *statBuf64, const struct stat &statBuf32)
{
statBuf64->st_mode = statBuf32.st_mode;
statBuf64->st_size = statBuf32.st_size;
statBuf64->st_ctime = statBuf32.st_ctime;
statBuf64->st_mtime = statBuf32.st_mtime;
statBuf64->st_atime = statBuf32.st_atime;
statBuf64->st_uid = statBuf32.st_uid;
statBuf64->st_gid = statBuf32.st_gid;
}
#endif
void QFileSystemMetaData::fillFromStatBuf(const QT_STATBUF &statBuffer)
{
// Permissions
if (statBuffer.st_mode & S_IRUSR)
entryFlags |= QFileSystemMetaData::OwnerReadPermission;
if (statBuffer.st_mode & S_IWUSR)
entryFlags |= QFileSystemMetaData::OwnerWritePermission;
if (statBuffer.st_mode & S_IXUSR)
entryFlags |= QFileSystemMetaData::OwnerExecutePermission;
if (statBuffer.st_mode & S_IRGRP)
entryFlags |= QFileSystemMetaData::GroupReadPermission;
if (statBuffer.st_mode & S_IWGRP)
entryFlags |= QFileSystemMetaData::GroupWritePermission;
if (statBuffer.st_mode & S_IXGRP)
entryFlags |= QFileSystemMetaData::GroupExecutePermission;
if (statBuffer.st_mode & S_IROTH)
entryFlags |= QFileSystemMetaData::OtherReadPermission;
if (statBuffer.st_mode & S_IWOTH)
entryFlags |= QFileSystemMetaData::OtherWritePermission;
if (statBuffer.st_mode & S_IXOTH)
entryFlags |= QFileSystemMetaData::OtherExecutePermission;
// Type
if ((statBuffer.st_mode & S_IFMT) == S_IFREG)
entryFlags |= QFileSystemMetaData::FileType;
else if ((statBuffer.st_mode & S_IFMT) == S_IFDIR)
entryFlags |= QFileSystemMetaData::DirectoryType;
else
entryFlags |= QFileSystemMetaData::SequentialType;
// Attributes
entryFlags |= QFileSystemMetaData::ExistsAttribute;
size_ = statBuffer.st_size;
#if !defined(QWS) && !defined(Q_WS_QPA) && defined(Q_OS_MAC) \
&& MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
if (statBuffer.st_flags & UF_HIDDEN) {
entryFlags |= QFileSystemMetaData::HiddenAttribute;
knownFlagsMask |= QFileSystemMetaData::HiddenAttribute;
}
#endif
// Times
#ifdef Q_OS_SYMBIAN
modificationTime_ = qt_symbian_time_t_To_TTime(statBuffer.st_mtime);
#else
creationTime_ = statBuffer.st_ctime ? statBuffer.st_ctime : statBuffer.st_mtime;
modificationTime_ = statBuffer.st_mtime;
accessTime_ = statBuffer.st_atime;
userId_ = statBuffer.st_uid;
groupId_ = statBuffer.st_gid;
#endif
}
void QFileSystemMetaData::fillFromDirEnt(const QT_DIRENT &entry)
{
#if defined(Q_OS_QNX)
entryFlags = 0;
knownFlagsMask = 0;
for (dirent_extra *extra = _DEXTRA_FIRST(&entry); _DEXTRA_VALID(extra, &entry);
extra = _DEXTRA_NEXT(extra)) {
if (extra->d_type == _DTYPE_STAT || extra->d_type == _DTYPE_LSTAT) {
const struct dirent_extra_stat * const extra_stat =
reinterpret_cast<struct dirent_extra_stat *>(extra);
// Remember whether this was a link or not, this saves an lstat() call later.
if (extra->d_type == _DTYPE_LSTAT) {
knownFlagsMask |= QFileSystemMetaData::LinkType;
if (S_ISLNK(extra_stat->d_stat.st_mode))
entryFlags |= QFileSystemMetaData::LinkType;
}
// For symlinks, the extra type _DTYPE_LSTAT doesn't work for filling out the meta data,
// as we need the stat() information there, not the lstat() information.
// In this case, don't use the extra information.
// Unfortunately, readdir() never seems to return extra info of type _DTYPE_STAT, so for
// symlinks, we always incur the cost of an extra stat() call later.
if (S_ISLNK(extra_stat->d_stat.st_mode) && extra->d_type == _DTYPE_LSTAT)
continue;
#if defined(QT_USE_XOPEN_LFS_EXTENSIONS) && defined(QT_LARGEFILE_SUPPORT)
// Even with large file support, d_stat is always of type struct stat, not struct stat64,
// so it needs to be converted
struct stat64 statBuf;
fillStat64fromStat32(&statBuf, extra_stat->d_stat);
fillFromStatBuf(statBuf);
#else
fillFromStatBuf(extra_stat->d_stat);
#endif
knownFlagsMask |= QFileSystemMetaData::PosixStatFlags;
if (!S_ISLNK(extra_stat->d_stat.st_mode)) {
knownFlagsMask |= QFileSystemMetaData::ExistsAttribute;
entryFlags |= QFileSystemMetaData::ExistsAttribute;
}
}
}
#elif defined(_DIRENT_HAVE_D_TYPE) || defined(Q_OS_BSD4) || defined(Q_OS_SYMBIAN)
// BSD4 includes Mac OS X
// ### This will clear all entry flags and knownFlagsMask
switch (entry.d_type)
{
case DT_DIR:
knownFlagsMask = QFileSystemMetaData::LinkType
| QFileSystemMetaData::FileType
| QFileSystemMetaData::DirectoryType
| QFileSystemMetaData::SequentialType
| QFileSystemMetaData::ExistsAttribute;
entryFlags = QFileSystemMetaData::DirectoryType
| QFileSystemMetaData::ExistsAttribute;
break;
case DT_BLK:
case DT_CHR:
case DT_FIFO:
case DT_SOCK:
// ### System attribute
knownFlagsMask = QFileSystemMetaData::LinkType
| QFileSystemMetaData::FileType
| QFileSystemMetaData::DirectoryType
| QFileSystemMetaData::BundleType
| QFileSystemMetaData::AliasType
| QFileSystemMetaData::SequentialType
| QFileSystemMetaData::ExistsAttribute;
entryFlags = QFileSystemMetaData::SequentialType
| QFileSystemMetaData::ExistsAttribute;
break;
case DT_LNK:
knownFlagsMask = QFileSystemMetaData::LinkType;
entryFlags = QFileSystemMetaData::LinkType;
break;
case DT_REG:
knownFlagsMask = QFileSystemMetaData::LinkType
| QFileSystemMetaData::FileType
| QFileSystemMetaData::DirectoryType
| QFileSystemMetaData::BundleType
| QFileSystemMetaData::SequentialType
| QFileSystemMetaData::ExistsAttribute;
entryFlags = QFileSystemMetaData::FileType
| QFileSystemMetaData::ExistsAttribute;
break;
case DT_UNKNOWN:
default:
clear();
}
#else
Q_UNUSED(entry)
#endif
}
#endif
//static
QString QFileSystemEngine::resolveUserName(const QFileSystemEntry &entry, QFileSystemMetaData &metaData)
{
#if defined (Q_OS_SYMBIAN)
Q_UNUSED(entry);
Q_UNUSED(metaData);
return QString();
#elif defined(Q_OS_WIN)
Q_UNUSED(metaData);
return QFileSystemEngine::owner(entry, QAbstractFileEngine::OwnerUser);
#else //(Q_OS_UNIX)
if (!metaData.hasFlags(QFileSystemMetaData::UserId))
QFileSystemEngine::fillMetaData(entry, metaData, QFileSystemMetaData::UserId);
return resolveUserName(metaData.userId());
#endif
}
//static
QString QFileSystemEngine::resolveGroupName(const QFileSystemEntry &entry, QFileSystemMetaData &metaData)
{
#if defined (Q_OS_SYMBIAN)
Q_UNUSED(entry);
Q_UNUSED(metaData);
return QString();
#elif defined(Q_OS_WIN)
Q_UNUSED(metaData);
return QFileSystemEngine::owner(entry, QAbstractFileEngine::OwnerGroup);
#else //(Q_OS_UNIX)
if (!metaData.hasFlags(QFileSystemMetaData::GroupId))
QFileSystemEngine::fillMetaData(entry, metaData, QFileSystemMetaData::GroupId);
return resolveGroupName(metaData.groupId());
#endif
}
QT_END_NAMESPACE
|