| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtWidgets 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 "qfileinfogatherer_p.h" |
| 41 | #include <qdebug.h> |
| 42 | #include <qdiriterator.h> |
| 43 | #include <private/qfileinfo_p.h> |
| 44 | #ifndef Q_OS_WIN |
| 45 | # include <unistd.h> |
| 46 | # include <sys/types.h> |
| 47 | #endif |
| 48 | #if defined(Q_OS_VXWORKS) |
| 49 | # include "qplatformdefs.h" |
| 50 | #endif |
| 51 | |
| 52 | QT_BEGIN_NAMESPACE |
| 53 | |
| 54 | #ifdef QT_BUILD_INTERNAL |
| 55 | static QBasicAtomicInt fetchedRoot = Q_BASIC_ATOMIC_INITIALIZER(false); |
| 56 | Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot() |
| 57 | { |
| 58 | fetchedRoot.storeRelaxed(false); |
| 59 | } |
| 60 | |
| 61 | Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot() |
| 62 | { |
| 63 | return fetchedRoot.loadRelaxed(); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | static QString translateDriveName(const QFileInfo &drive) |
| 68 | { |
| 69 | QString driveName = drive.absoluteFilePath(); |
| 70 | #ifdef Q_OS_WIN |
| 71 | if (driveName.startsWith(QLatin1Char('/'))) // UNC host |
| 72 | return drive.fileName(); |
| 73 | if (driveName.endsWith(QLatin1Char('/'))) |
| 74 | driveName.chop(1); |
| 75 | #endif // Q_OS_WIN |
| 76 | return driveName; |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | Creates thread |
| 81 | */ |
| 82 | QFileInfoGatherer::QFileInfoGatherer(QObject *parent) |
| 83 | : QThread(parent) |
| 84 | , m_iconProvider(&defaultProvider) |
| 85 | { |
| 86 | start(LowPriority); |
| 87 | } |
| 88 | |
| 89 | /*! |
| 90 | Destroys thread |
| 91 | */ |
| 92 | QFileInfoGatherer::~QFileInfoGatherer() |
| 93 | { |
| 94 | abort.storeRelaxed(true); |
| 95 | QMutexLocker locker(&mutex); |
| 96 | condition.wakeAll(); |
| 97 | locker.unlock(); |
| 98 | wait(); |
| 99 | } |
| 100 | |
| 101 | void QFileInfoGatherer::setResolveSymlinks(bool enable) |
| 102 | { |
| 103 | Q_UNUSED(enable); |
| 104 | #ifdef Q_OS_WIN |
| 105 | m_resolveSymlinks = enable; |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | void QFileInfoGatherer::driveAdded() |
| 110 | { |
| 111 | fetchExtendedInformation(QString(), QStringList()); |
| 112 | } |
| 113 | |
| 114 | void QFileInfoGatherer::driveRemoved() |
| 115 | { |
| 116 | QStringList drives; |
| 117 | const QFileInfoList driveInfoList = QDir::drives(); |
| 118 | for (const QFileInfo &fi : driveInfoList) |
| 119 | drives.append(translateDriveName(fi)); |
| 120 | newListOfFiles(QString(), drives); |
| 121 | } |
| 122 | |
| 123 | bool QFileInfoGatherer::resolveSymlinks() const |
| 124 | { |
| 125 | #ifdef Q_OS_WIN |
| 126 | return m_resolveSymlinks; |
| 127 | #else |
| 128 | return false; |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | void QFileInfoGatherer::setIconProvider(QAbstractFileIconProvider *provider) |
| 133 | { |
| 134 | m_iconProvider = provider; |
| 135 | } |
| 136 | |
| 137 | QAbstractFileIconProvider *QFileInfoGatherer::iconProvider() const |
| 138 | { |
| 139 | return m_iconProvider; |
| 140 | } |
| 141 | |
| 142 | /*! |
| 143 | Fetch extended information for all \a files in \a path |
| 144 | |
| 145 | \sa updateFile(), update(), resolvedName() |
| 146 | */ |
| 147 | void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files) |
| 148 | { |
| 149 | QMutexLocker locker(&mutex); |
| 150 | // See if we already have this dir/file in our queue |
| 151 | int loc = this->path.lastIndexOf(path); |
| 152 | while (loc > 0) { |
| 153 | if (this->files.at(loc) == files) { |
| 154 | return; |
| 155 | } |
| 156 | loc = this->path.lastIndexOf(path, loc - 1); |
| 157 | } |
| 158 | this->path.push(path); |
| 159 | this->files.push(files); |
| 160 | condition.wakeAll(); |
| 161 | |
| 162 | #if QT_CONFIG(filesystemwatcher) |
| 163 | if (files.isEmpty() |
| 164 | && !path.isEmpty() |
| 165 | && !path.startsWith(QLatin1String("//" )) /*don't watch UNC path*/) { |
| 166 | if (!watchedDirectories().contains(path)) |
| 167 | watchPaths(QStringList(path)); |
| 168 | } |
| 169 | #endif |
| 170 | } |
| 171 | |
| 172 | /*! |
| 173 | Fetch extended information for all \a filePath |
| 174 | |
| 175 | \sa fetchExtendedInformation() |
| 176 | */ |
| 177 | void QFileInfoGatherer::updateFile(const QString &filePath) |
| 178 | { |
| 179 | QString dir = filePath.mid(0, filePath.lastIndexOf(QLatin1Char('/'))); |
| 180 | QString fileName = filePath.mid(dir.length() + 1); |
| 181 | fetchExtendedInformation(dir, QStringList(fileName)); |
| 182 | } |
| 183 | |
| 184 | QStringList QFileInfoGatherer::watchedFiles() const |
| 185 | { |
| 186 | #if QT_CONFIG(filesystemwatcher) |
| 187 | if (m_watcher) |
| 188 | return m_watcher->files(); |
| 189 | #endif |
| 190 | return {}; |
| 191 | } |
| 192 | |
| 193 | QStringList QFileInfoGatherer::watchedDirectories() const |
| 194 | { |
| 195 | #if QT_CONFIG(filesystemwatcher) |
| 196 | if (m_watcher) |
| 197 | return m_watcher->directories(); |
| 198 | #endif |
| 199 | return {}; |
| 200 | } |
| 201 | |
| 202 | void QFileInfoGatherer::createWatcher() |
| 203 | { |
| 204 | #if QT_CONFIG(filesystemwatcher) |
| 205 | m_watcher = new QFileSystemWatcher(this); |
| 206 | connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &QFileInfoGatherer::list); |
| 207 | connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &QFileInfoGatherer::updateFile); |
| 208 | # if defined(Q_OS_WIN) |
| 209 | const QVariant listener = m_watcher->property("_q_driveListener" ); |
| 210 | if (listener.canConvert<QObject *>()) { |
| 211 | if (QObject *driveListener = listener.value<QObject *>()) { |
| 212 | connect(driveListener, SIGNAL(driveAdded()), this, SLOT(driveAdded())); |
| 213 | connect(driveListener, SIGNAL(driveRemoved()), this, SLOT(driveRemoved())); |
| 214 | } |
| 215 | } |
| 216 | # endif // Q_OS_WIN |
| 217 | #endif |
| 218 | } |
| 219 | |
| 220 | void QFileInfoGatherer::watchPaths(const QStringList &paths) |
| 221 | { |
| 222 | #if QT_CONFIG(filesystemwatcher) |
| 223 | if (m_watching) { |
| 224 | if (m_watcher == nullptr) |
| 225 | createWatcher(); |
| 226 | m_watcher->addPaths(paths); |
| 227 | } |
| 228 | #else |
| 229 | Q_UNUSED(paths); |
| 230 | #endif |
| 231 | } |
| 232 | |
| 233 | void QFileInfoGatherer::unwatchPaths(const QStringList &paths) |
| 234 | { |
| 235 | #if QT_CONFIG(filesystemwatcher) |
| 236 | if (m_watcher && !paths.isEmpty()) |
| 237 | m_watcher->removePaths(paths); |
| 238 | #else |
| 239 | Q_UNUSED(paths); |
| 240 | #endif |
| 241 | } |
| 242 | |
| 243 | bool QFileInfoGatherer::isWatching() const |
| 244 | { |
| 245 | bool result = false; |
| 246 | #if QT_CONFIG(filesystemwatcher) |
| 247 | QMutexLocker locker(&mutex); |
| 248 | result = m_watching; |
| 249 | #endif |
| 250 | return result; |
| 251 | } |
| 252 | |
| 253 | void QFileInfoGatherer::setWatching(bool v) |
| 254 | { |
| 255 | #if QT_CONFIG(filesystemwatcher) |
| 256 | QMutexLocker locker(&mutex); |
| 257 | if (v != m_watching) { |
| 258 | if (!v) { |
| 259 | delete m_watcher; |
| 260 | m_watcher = nullptr; |
| 261 | } |
| 262 | m_watching = v; |
| 263 | } |
| 264 | #else |
| 265 | Q_UNUSED(v); |
| 266 | #endif |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | List all files in \a directoryPath |
| 271 | |
| 272 | \sa listed() |
| 273 | */ |
| 274 | void QFileInfoGatherer::clear() |
| 275 | { |
| 276 | #if QT_CONFIG(filesystemwatcher) |
| 277 | QMutexLocker locker(&mutex); |
| 278 | unwatchPaths(watchedFiles()); |
| 279 | unwatchPaths(watchedDirectories()); |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | Remove a \a path from the watcher |
| 285 | |
| 286 | \sa listed() |
| 287 | */ |
| 288 | void QFileInfoGatherer::removePath(const QString &path) |
| 289 | { |
| 290 | #if QT_CONFIG(filesystemwatcher) |
| 291 | QMutexLocker locker(&mutex); |
| 292 | unwatchPaths(QStringList(path)); |
| 293 | #else |
| 294 | Q_UNUSED(path); |
| 295 | #endif |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | List all files in \a directoryPath |
| 300 | |
| 301 | \sa listed() |
| 302 | */ |
| 303 | void QFileInfoGatherer::list(const QString &directoryPath) |
| 304 | { |
| 305 | fetchExtendedInformation(directoryPath, QStringList()); |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | Until aborted wait to fetch a directory or files |
| 310 | */ |
| 311 | void QFileInfoGatherer::run() |
| 312 | { |
| 313 | forever { |
| 314 | QMutexLocker locker(&mutex); |
| 315 | while (!abort.loadRelaxed() && path.isEmpty()) |
| 316 | condition.wait(&mutex); |
| 317 | if (abort.loadRelaxed()) |
| 318 | return; |
| 319 | const QString thisPath = qAsConst(path).front(); |
| 320 | path.pop_front(); |
| 321 | const QStringList thisList = qAsConst(files).front(); |
| 322 | files.pop_front(); |
| 323 | locker.unlock(); |
| 324 | |
| 325 | getFileInfos(thisPath, thisList); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const |
| 330 | { |
| 331 | QExtendedInformation info(fileInfo); |
| 332 | info.icon = m_iconProvider->icon(fileInfo); |
| 333 | info.displayType = m_iconProvider->type(fileInfo); |
| 334 | #if QT_CONFIG(filesystemwatcher) |
| 335 | // ### Not ready to listen all modifications by default |
| 336 | static const bool watchFiles = qEnvironmentVariableIsSet("QT_FILESYSTEMMODEL_WATCH_FILES" ); |
| 337 | if (watchFiles) { |
| 338 | if (!fileInfo.exists() && !fileInfo.isSymLink()) { |
| 339 | const_cast<QFileInfoGatherer *>(this)-> |
| 340 | unwatchPaths(QStringList(fileInfo.absoluteFilePath())); |
| 341 | } else { |
| 342 | const QString path = fileInfo.absoluteFilePath(); |
| 343 | if (!path.isEmpty() && fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() |
| 344 | && !watchedFiles().contains(path)) { |
| 345 | const_cast<QFileInfoGatherer *>(this)->watchPaths(QStringList(path)); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | #endif // filesystemwatcher |
| 350 | |
| 351 | #ifdef Q_OS_WIN |
| 352 | if (m_resolveSymlinks && info.isSymLink(/* ignoreNtfsSymLinks = */ true)) { |
| 353 | QFileInfo resolvedInfo(fileInfo.symLinkTarget()); |
| 354 | resolvedInfo = resolvedInfo.canonicalFilePath(); |
| 355 | if (resolvedInfo.exists()) { |
| 356 | emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName()); |
| 357 | } |
| 358 | } |
| 359 | #endif |
| 360 | return info; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | Get specific file info's, batch the files so update when we have 100 |
| 365 | items and every 200ms after that |
| 366 | */ |
| 367 | void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files) |
| 368 | { |
| 369 | // List drives |
| 370 | if (path.isEmpty()) { |
| 371 | #ifdef QT_BUILD_INTERNAL |
| 372 | fetchedRoot.storeRelaxed(true); |
| 373 | #endif |
| 374 | QFileInfoList infoList; |
| 375 | if (files.isEmpty()) { |
| 376 | infoList = QDir::drives(); |
| 377 | } else { |
| 378 | infoList.reserve(files.count()); |
| 379 | for (const auto &file : files) |
| 380 | infoList << QFileInfo(file); |
| 381 | } |
| 382 | QList<QPair<QString, QFileInfo>> updatedFiles; |
| 383 | updatedFiles.reserve(infoList.count()); |
| 384 | for (int i = infoList.count() - 1; i >= 0; --i) { |
| 385 | QFileInfo driveInfo = infoList.at(i); |
| 386 | driveInfo.stat(); |
| 387 | QString driveName = translateDriveName(driveInfo); |
| 388 | updatedFiles.append(QPair<QString,QFileInfo>(driveName, driveInfo)); |
| 389 | } |
| 390 | emit updates(path, updatedFiles); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | QElapsedTimer base; |
| 395 | base.start(); |
| 396 | QFileInfo fileInfo; |
| 397 | bool firstTime = true; |
| 398 | QList<QPair<QString, QFileInfo>> updatedFiles; |
| 399 | QStringList filesToCheck = files; |
| 400 | |
| 401 | QStringList allFiles; |
| 402 | if (files.isEmpty()) { |
| 403 | QDirIterator dirIt(path, QDir::AllEntries | QDir::System | QDir::Hidden); |
| 404 | while (!abort.loadRelaxed() && dirIt.hasNext()) { |
| 405 | dirIt.next(); |
| 406 | fileInfo = dirIt.fileInfo(); |
| 407 | fileInfo.stat(); |
| 408 | allFiles.append(fileInfo.fileName()); |
| 409 | fetch(fileInfo, base, firstTime, updatedFiles, path); |
| 410 | } |
| 411 | } |
| 412 | if (!allFiles.isEmpty()) |
| 413 | emit newListOfFiles(path, allFiles); |
| 414 | |
| 415 | QStringList::const_iterator filesIt = filesToCheck.constBegin(); |
| 416 | while (!abort.loadRelaxed() && filesIt != filesToCheck.constEnd()) { |
| 417 | fileInfo.setFile(path + QDir::separator() + *filesIt); |
| 418 | ++filesIt; |
| 419 | fileInfo.stat(); |
| 420 | fetch(fileInfo, base, firstTime, updatedFiles, path); |
| 421 | } |
| 422 | if (!updatedFiles.isEmpty()) |
| 423 | emit updates(path, updatedFiles); |
| 424 | emit directoryLoaded(path); |
| 425 | } |
| 426 | |
| 427 | void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QElapsedTimer &base, bool &firstTime, |
| 428 | QList<QPair<QString, QFileInfo>> &updatedFiles, const QString &path) |
| 429 | { |
| 430 | updatedFiles.append(QPair<QString, QFileInfo>(fileInfo.fileName(), fileInfo)); |
| 431 | QElapsedTimer current; |
| 432 | current.start(); |
| 433 | if ((firstTime && updatedFiles.count() > 100) || base.msecsTo(current) > 1000) { |
| 434 | emit updates(path, updatedFiles); |
| 435 | updatedFiles.clear(); |
| 436 | base = current; |
| 437 | firstTime = false; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | QT_END_NAMESPACE |
| 442 | |
| 443 | #include "moc_qfileinfogatherer_p.cpp" |
| 444 | |