| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:58:39 +01:00
										 |  |  | ** Copyright (C) 2016 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** This file is part of Qt Creator. | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** 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 | 
					
						
							| 
									
										
										
										
											2016-01-15 14:58:39 +01:00
										 |  |  | ** a written agreement between you and The Qt Company. For licensing terms | 
					
						
							|  |  |  | ** and conditions see https://www.qt.io/terms-conditions. For further
 | 
					
						
							|  |  |  | ** information use the contact form at https://www.qt.io/contact-us.
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:58:39 +01:00
										 |  |  | ** GNU General Public License Usage | 
					
						
							|  |  |  | ** Alternatively, this file may be used under the terms of the GNU | 
					
						
							|  |  |  | ** General Public License version 3 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
					
						
							|  |  |  | ** included in the packaging of this file. Please review the following | 
					
						
							|  |  |  | ** information to ensure the GNU General Public License requirements will | 
					
						
							|  |  |  | ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 07:55:01 +01:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "utils_global.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 14:57:30 +02:00
										 |  |  | #include "hostosinfo.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-15 10:42:41 +01:00
										 |  |  | #include <QCoreApplication>
 | 
					
						
							| 
									
										
										
										
											2020-07-07 10:11:53 +02:00
										 |  |  | #include <QDir>
 | 
					
						
							|  |  |  | #include <QFileInfo>
 | 
					
						
							| 
									
										
										
										
											2012-02-15 10:42:41 +01:00
										 |  |  | #include <QMetaType>
 | 
					
						
							| 
									
										
										
										
											2014-07-07 16:16:49 +02:00
										 |  |  | #include <QStringList>
 | 
					
						
							| 
									
										
										
										
											2019-05-09 16:35:25 +02:00
										 |  |  | #include <QUrl>
 | 
					
						
							| 
									
										
										
										
											2020-07-07 10:11:53 +02:00
										 |  |  | #include <QXmlStreamWriter> // Mac.
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 20:43:09 +02:00
										 |  |  | #include <functional>
 | 
					
						
							| 
									
										
										
										
											2017-06-29 15:04:01 +02:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2014-09-04 20:43:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 15:58:33 +02:00
										 |  |  | namespace Utils { | 
					
						
							|  |  |  | class Environment; | 
					
						
							|  |  |  | class FilePath; | 
					
						
							|  |  |  | } // Utils
 | 
					
						
							| 
									
										
										
										
											2014-02-13 11:49:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | QT_BEGIN_NAMESPACE | 
					
						
							| 
									
										
										
										
											2014-09-04 20:43:09 +02:00
										 |  |  | class QDataStream; | 
					
						
							|  |  |  | class QDateTime; | 
					
						
							|  |  |  | class QDir; | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | class QFile; | 
					
						
							| 
									
										
										
										
											2014-09-04 20:43:09 +02:00
										 |  |  | class QFileInfo; | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | class QTemporaryFile; | 
					
						
							|  |  |  | class QTextStream; | 
					
						
							| 
									
										
										
										
											2014-09-04 20:43:09 +02:00
										 |  |  | class QWidget; | 
					
						
							| 
									
										
										
										
											2014-02-13 11:49:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  | QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug dbg, const Utils::FilePath &c); | 
					
						
							| 
									
										
										
										
											2014-02-13 11:49:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-05 14:22:26 +02:00
										 |  |  | // for withNtfsPermissions
 | 
					
						
							| 
									
										
										
										
											2017-05-24 15:31:40 +02:00
										 |  |  | #ifdef Q_OS_WIN
 | 
					
						
							|  |  |  | extern Q_CORE_EXPORT int qt_ntfs_permission_lookup; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | QT_END_NAMESPACE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 10:46:25 +02:00
										 |  |  | // tst_fileutils becomes a friend of Utils::FilePath for testing private method
 | 
					
						
							|  |  |  | class tst_fileutils; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | namespace Utils { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  | class DeviceFileHooks | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     std::function<bool(const FilePath &)> isExecutableFile; | 
					
						
							|  |  |  |     std::function<bool(const FilePath &)> isReadableFile; | 
					
						
							|  |  |  |     std::function<bool(const FilePath &)> isReadableDir; | 
					
						
							|  |  |  |     std::function<bool(const FilePath &)> isWritableDir; | 
					
						
							| 
									
										
										
										
											2021-06-22 07:09:00 +02:00
										 |  |  |     std::function<bool(const FilePath &)> isWritableFile; | 
					
						
							| 
									
										
										
										
											2021-07-14 07:41:23 +02:00
										 |  |  |     std::function<bool(const FilePath &)> isFile; | 
					
						
							|  |  |  |     std::function<bool(const FilePath &)> isDir; | 
					
						
							| 
									
										
										
										
											2021-05-31 18:20:46 +02:00
										 |  |  |     std::function<bool(const FilePath &)> ensureWritableDir; | 
					
						
							| 
									
										
										
										
											2021-06-29 09:41:53 +02:00
										 |  |  |     std::function<bool(const FilePath &)> ensureExistingFile; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     std::function<bool(const FilePath &)> createDir; | 
					
						
							| 
									
										
										
										
											2021-06-01 09:40:17 +02:00
										 |  |  |     std::function<bool(const FilePath &)> exists; | 
					
						
							| 
									
										
										
										
											2021-06-14 08:33:44 +02:00
										 |  |  |     std::function<bool(const FilePath &)> removeFile; | 
					
						
							| 
									
										
										
										
											2021-06-28 15:26:44 +02:00
										 |  |  |     std::function<bool(const FilePath &)> removeRecursively; | 
					
						
							| 
									
										
										
										
											2021-06-14 08:33:44 +02:00
										 |  |  |     std::function<bool(const FilePath &, const FilePath &)> copyFile; | 
					
						
							| 
									
										
										
										
											2021-06-28 13:57:10 +02:00
										 |  |  |     std::function<bool(const FilePath &, const FilePath &)> renameFile; | 
					
						
							| 
									
										
										
										
											2021-07-06 15:56:30 +02:00
										 |  |  |     std::function<FilePath(const FilePath &, const QList<FilePath> &)> searchInPath; | 
					
						
							| 
									
										
										
										
											2021-06-29 15:57:41 +02:00
										 |  |  |     std::function<FilePath(const FilePath &)> symLinkTarget; | 
					
						
							| 
									
										
										
										
											2021-06-29 11:10:27 +02:00
										 |  |  |     std::function<QList<FilePath>(const FilePath &, const QStringList &, | 
					
						
							|  |  |  |                                   QDir::Filters, QDir::SortFlags)> dirEntries; | 
					
						
							| 
									
										
										
										
											2021-07-09 08:30:11 +02:00
										 |  |  |     std::function<QByteArray(const FilePath &, qint64, qint64)> fileContents; | 
					
						
							| 
									
										
										
										
											2021-06-15 18:29:51 +02:00
										 |  |  |     std::function<bool(const FilePath &, const QByteArray &)> writeFileContents; | 
					
						
							| 
									
										
										
										
											2021-06-14 08:33:44 +02:00
										 |  |  |     std::function<QDateTime(const FilePath &)> lastModified; | 
					
						
							| 
									
										
										
										
											2021-06-17 10:39:31 +02:00
										 |  |  |     std::function<QFile::Permissions(const FilePath &)> permissions; | 
					
						
							| 
									
										
										
										
											2021-06-29 16:57:16 +02:00
										 |  |  |     std::function<OsType(const FilePath &)> osType; | 
					
						
							| 
									
										
										
										
											2021-07-06 15:58:33 +02:00
										 |  |  |     std::function<Environment(const FilePath &)> environment; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  | class QTCREATOR_UTILS_EXPORT FilePath | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     FilePath(); | 
					
						
							| 
									
										
										
										
											2019-05-10 13:18:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     static FilePath fromString(const QString &filepath); | 
					
						
							|  |  |  |     static FilePath fromFileInfo(const QFileInfo &info); | 
					
						
							|  |  |  |     static FilePath fromStringWithExtension(const QString &filepath, const QString &defaultExtension); | 
					
						
							|  |  |  |     static FilePath fromUserInput(const QString &filepath); | 
					
						
							|  |  |  |     static FilePath fromUtf8(const char *filepath, int filepathSize = -1); | 
					
						
							|  |  |  |     static FilePath fromVariant(const QVariant &variant); | 
					
						
							| 
									
										
										
										
											2019-05-10 13:18:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-23 15:01:08 +02:00
										 |  |  |     QString toString() const; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     FilePath onDevice(const FilePath &deviceTemplate) const; | 
					
						
							| 
									
										
										
										
											2021-07-14 13:01:28 +02:00
										 |  |  |     FilePath withNewPath(const QString &newPath) const; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 13:18:37 +02:00
										 |  |  |     QFileInfo toFileInfo() const; | 
					
						
							| 
									
										
										
										
											2019-05-09 16:35:25 +02:00
										 |  |  |     QVariant toVariant() const; | 
					
						
							| 
									
										
										
										
											2021-04-22 16:15:26 +02:00
										 |  |  |     QDir toDir() const; | 
					
						
							| 
									
										
										
										
											2019-05-09 16:35:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  |     QString toUserOutput() const; | 
					
						
							| 
									
										
										
										
											2019-05-17 13:54:27 +02:00
										 |  |  |     QString shortNativePath() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 10:49:09 +02:00
										 |  |  |     QString fileName() const; | 
					
						
							|  |  |  |     QString fileNameWithPathComponents(int pathComponents) const; | 
					
						
							| 
									
										
										
										
											2021-06-04 07:59:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 12:53:40 +02:00
										 |  |  |     QString baseName() const; | 
					
						
							| 
									
										
										
										
											2021-06-04 07:59:00 +02:00
										 |  |  |     QString completeBaseName() const; | 
					
						
							| 
									
										
										
										
											2021-06-04 09:13:49 +02:00
										 |  |  |     QString suffix() const; | 
					
						
							|  |  |  |     QString completeSuffix() const; | 
					
						
							| 
									
										
										
										
											2021-05-18 18:03:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     QString scheme() const { return m_scheme; } | 
					
						
							|  |  |  |     void setScheme(const QString &scheme); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString host() const { return m_host; } | 
					
						
							|  |  |  |     void setHost(const QString &host); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QString path() const { return m_data; } | 
					
						
							|  |  |  |     void setPath(const QString &path) { m_data = path; } | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool needsDevice() const; | 
					
						
							| 
									
										
										
										
											2014-10-24 13:15:54 +02:00
										 |  |  |     bool exists() const; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool isWritablePath() const { return isWritableDir(); } // Remove.
 | 
					
						
							|  |  |  |     bool isWritableDir() const; | 
					
						
							| 
									
										
										
										
											2021-06-22 07:09:00 +02:00
										 |  |  |     bool isWritableFile() const; | 
					
						
							| 
									
										
										
										
											2021-05-31 18:20:46 +02:00
										 |  |  |     bool ensureWritableDir() const; | 
					
						
							| 
									
										
										
										
											2021-06-29 09:41:53 +02:00
										 |  |  |     bool ensureExistingFile() const; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     bool isExecutableFile() const; | 
					
						
							|  |  |  |     bool isReadableFile() const; | 
					
						
							|  |  |  |     bool isReadableDir() const; | 
					
						
							| 
									
										
										
										
											2021-07-01 11:26:59 +02:00
										 |  |  |     bool isRelativePath() const; | 
					
						
							|  |  |  |     bool isAbsolutePath() const { return !isRelativePath(); } | 
					
						
							| 
									
										
										
										
											2021-07-14 07:41:23 +02:00
										 |  |  |     bool isFile() const; | 
					
						
							|  |  |  |     bool isDir() const; | 
					
						
							| 
									
										
										
										
											2021-07-01 11:26:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     bool createDir() const; | 
					
						
							| 
									
										
										
										
											2021-06-29 11:10:27 +02:00
										 |  |  |     QList<FilePath> dirEntries(const QStringList &nameFilters, | 
					
						
							|  |  |  |                                QDir::Filters filters, | 
					
						
							|  |  |  |                                QDir::SortFlags sort = QDir::NoSort) const; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     QList<FilePath> dirEntries(QDir::Filters filters) const; | 
					
						
							| 
									
										
										
										
											2021-07-09 08:30:11 +02:00
										 |  |  |     QByteArray fileContents(qint64 maxSize = -1, qint64 offset = 0) const; | 
					
						
							| 
									
										
										
										
											2021-06-15 18:29:51 +02:00
										 |  |  |     bool writeFileContents(const QByteArray &data) const; | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     FilePath parentDir() const; | 
					
						
							| 
									
										
										
										
											2019-12-18 18:55:27 +01:00
										 |  |  |     FilePath absolutePath() const; | 
					
						
							| 
									
										
										
										
											2021-05-18 12:43:48 +02:00
										 |  |  |     FilePath absoluteFilePath() const; | 
					
						
							| 
									
										
										
										
											2021-06-09 17:05:50 +02:00
										 |  |  |     FilePath absoluteFilePath(const FilePath &tail) const; | 
					
						
							| 
									
										
										
										
											2021-04-06 10:46:25 +02:00
										 |  |  |     FilePath absoluteFromRelativePath(const FilePath &anchor) const; | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     bool operator==(const FilePath &other) const; | 
					
						
							|  |  |  |     bool operator!=(const FilePath &other) const; | 
					
						
							|  |  |  |     bool operator<(const FilePath &other) const; | 
					
						
							|  |  |  |     bool operator<=(const FilePath &other) const; | 
					
						
							|  |  |  |     bool operator>(const FilePath &other) const; | 
					
						
							|  |  |  |     bool operator>=(const FilePath &other) const; | 
					
						
							|  |  |  |     FilePath operator+(const QString &s) const; | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     bool isChildOf(const FilePath &s) const; | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  |     bool isChildOf(const QDir &dir) const; | 
					
						
							| 
									
										
										
										
											2019-10-21 15:06:28 +02:00
										 |  |  |     bool startsWith(const QString &s) const; | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  |     bool endsWith(const QString &s) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 13:36:48 +02:00
										 |  |  |     bool isNewerThan(const QDateTime &timeStamp) const; | 
					
						
							| 
									
										
										
										
											2021-06-14 08:33:44 +02:00
										 |  |  |     QDateTime lastModified() const; | 
					
						
							| 
									
										
										
										
											2021-06-17 10:39:31 +02:00
										 |  |  |     QFile::Permissions permissions() const; | 
					
						
							| 
									
										
										
										
											2021-07-01 11:31:30 +02:00
										 |  |  |     OsType osType() const; | 
					
						
							| 
									
										
										
										
											2021-06-14 08:33:44 +02:00
										 |  |  |     bool removeFile() const; | 
					
						
							| 
									
										
										
										
											2021-07-01 08:59:32 +02:00
										 |  |  |     bool removeRecursively(QString *error = nullptr) const; | 
					
						
							| 
									
										
										
										
											2021-06-14 08:33:44 +02:00
										 |  |  |     bool copyFile(const FilePath &target) const; | 
					
						
							| 
									
										
										
										
											2021-06-28 13:57:10 +02:00
										 |  |  |     bool renameFile(const FilePath &target) const; | 
					
						
							| 
									
										
										
										
											2019-05-17 13:36:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 09:28:57 +02:00
										 |  |  |     Qt::CaseSensitivity caseSensitivity() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     FilePath relativeChildPath(const FilePath &parent) const; | 
					
						
							| 
									
										
										
										
											2021-04-06 10:46:25 +02:00
										 |  |  |     FilePath relativePath(const FilePath &anchor) const; | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     FilePath pathAppended(const QString &str) const; | 
					
						
							|  |  |  |     FilePath stringAppended(const QString &str) const; | 
					
						
							| 
									
										
										
										
											2019-12-04 13:36:04 +01:00
										 |  |  |     FilePath resolvePath(const QString &fileName) const; | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     FilePath resolveSymlinkTarget() const; | 
					
						
							| 
									
										
										
										
											2021-06-10 08:07:32 +02:00
										 |  |  |     FilePath cleanPath() const; | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     FilePath canonicalPath() const; | 
					
						
							| 
									
										
										
										
											2021-06-29 15:57:41 +02:00
										 |  |  |     FilePath symLinkTarget() const; | 
					
						
							| 
									
										
										
										
											2021-07-02 08:53:59 +02:00
										 |  |  |     FilePath resolveSymlinks() const; | 
					
						
							| 
									
										
										
										
											2021-06-29 16:57:16 +02:00
										 |  |  |     FilePath withExecutableSuffix() const; | 
					
						
							| 
									
										
										
										
											2019-05-17 13:44:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-19 17:35:56 +01:00
										 |  |  |     FilePath operator/(const QString &str) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     void clear(); | 
					
						
							|  |  |  |     bool isEmpty() const; | 
					
						
							| 
									
										
										
										
											2019-05-10 12:23:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     uint hash(uint seed) const; | 
					
						
							| 
									
										
										
										
											2019-05-10 11:48:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     // NOTE: Most FilePath operations on FilePath created from URL currently
 | 
					
						
							|  |  |  |     // do not work. Among the working are .toVariant() and .toUrl().
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     static FilePath fromUrl(const QUrl &url); | 
					
						
							| 
									
										
										
										
											2019-05-09 16:35:25 +02:00
										 |  |  |     QUrl toUrl() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 12:05:13 +02:00
										 |  |  |     static void setDeviceFileHooks(const DeviceFileHooks &hooks); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 06:53:28 +02:00
										 |  |  |     FilePath searchOnDevice(const QList<FilePath> &dirs) const; | 
					
						
							| 
									
										
										
										
											2021-07-06 15:58:33 +02:00
										 |  |  |     Environment deviceEnvironment() const; | 
					
						
							| 
									
										
										
										
											2021-06-07 15:47:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-04-06 10:46:25 +02:00
										 |  |  |     friend class ::tst_fileutils; | 
					
						
							|  |  |  |     static QString calcRelativePath(const QString &absolutePath, const QString &absoluteAnchorPath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 18:03:10 +02:00
										 |  |  |     QString m_scheme; | 
					
						
							|  |  |  |     QString m_host; | 
					
						
							| 
									
										
										
										
											2019-05-10 12:23:27 +02:00
										 |  |  |     QString m_data; | 
					
						
							| 
									
										
										
										
											2012-09-05 15:52:53 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  | QTCREATOR_UTILS_EXPORT QTextStream &operator<<(QTextStream &s, const FilePath &fn); | 
					
						
							| 
									
										
										
										
											2014-09-22 16:20:51 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 14:07:53 +01:00
										 |  |  | using FilePaths = QList<FilePath>; | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-05 12:59:14 +02:00
										 |  |  | class QTCREATOR_UTILS_EXPORT FileUtils { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-06-11 15:41:26 +02:00
										 |  |  | #ifdef QT_GUI_LIB
 | 
					
						
							|  |  |  |     class QTCREATOR_UTILS_EXPORT CopyAskingForOverwrite | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     public: | 
					
						
							| 
									
										
										
										
											2021-02-12 16:06:24 +01:00
										 |  |  |         CopyAskingForOverwrite(QWidget *dialogParent, | 
					
						
							|  |  |  |                                const std::function<void(QFileInfo)> &postOperation = {}); | 
					
						
							| 
									
										
										
										
											2020-06-11 15:41:26 +02:00
										 |  |  |         bool operator()(const QFileInfo &src, const QFileInfo &dest, QString *error); | 
					
						
							|  |  |  |         QStringList files() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							|  |  |  |         QWidget *m_parent; | 
					
						
							|  |  |  |         QStringList m_files; | 
					
						
							| 
									
										
										
										
											2021-02-12 16:06:24 +01:00
										 |  |  |         std::function<void(QFileInfo)> m_postOperation; | 
					
						
							| 
									
										
										
										
											2020-06-11 15:41:26 +02:00
										 |  |  |         bool m_overwriteAll = false; | 
					
						
							|  |  |  |         bool m_skipAll = false; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | #endif // QT_GUI_LIB
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 10:11:53 +02:00
										 |  |  |     static bool copyRecursively(const FilePath &srcFilePath, | 
					
						
							|  |  |  |                                 const FilePath &tgtFilePath, | 
					
						
							|  |  |  |                                 QString *error = nullptr); | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							|  |  |  |     static bool copyRecursively(const FilePath &srcFilePath, | 
					
						
							|  |  |  |                                 const FilePath &tgtFilePath, | 
					
						
							|  |  |  |                                 QString *error, | 
					
						
							|  |  |  |                                 T &©Helper); | 
					
						
							| 
									
										
										
										
											2021-01-11 15:57:16 +01:00
										 |  |  |     static bool copyIfDifferent(const FilePath &srcFilePath, | 
					
						
							|  |  |  |                                 const FilePath &tgtFilePath); | 
					
						
							| 
									
										
										
										
											2013-08-16 17:10:45 +02:00
										 |  |  |     static QString fileSystemFriendlyName(const QString &name); | 
					
						
							| 
									
										
										
										
											2013-12-09 12:56:24 +01:00
										 |  |  |     static int indexOfQmakeUnfriendly(const QString &name, int startpos = 0); | 
					
						
							|  |  |  |     static QString qmakeFriendlyName(const QString &name); | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     static bool makeWritable(const FilePath &path); | 
					
						
							| 
									
										
										
										
											2013-10-23 22:07:46 +03:00
										 |  |  |     static QString normalizePathName(const QString &name); | 
					
						
							| 
									
										
										
										
											2014-04-11 17:45:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static bool isRelativePath(const QString &fileName); | 
					
						
							|  |  |  |     static bool isAbsolutePath(const QString &fileName) { return !isRelativePath(fileName); } | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     static FilePath commonPath(const FilePath &oldCommonPath, const FilePath &fileName); | 
					
						
							|  |  |  |     static QByteArray fileId(const FilePath &fileName); | 
					
						
							| 
									
										
										
										
											2021-06-08 13:51:34 +02:00
										 |  |  |     static FilePath homePath(); | 
					
						
							| 
									
										
										
										
											2021-06-11 14:33:58 +02:00
										 |  |  |     static bool renameFile(const FilePath &srcFilePath, const FilePath &tgtFilePath); | 
					
						
							| 
									
										
										
										
											2011-07-05 12:59:14 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 10:11:53 +02:00
										 |  |  | template<typename T> | 
					
						
							|  |  |  | bool FileUtils::copyRecursively(const FilePath &srcFilePath, | 
					
						
							|  |  |  |                                 const FilePath &tgtFilePath, | 
					
						
							|  |  |  |                                 QString *error, | 
					
						
							|  |  |  |                                 T &©Helper) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const QFileInfo srcFileInfo = srcFilePath.toFileInfo(); | 
					
						
							|  |  |  |     if (srcFileInfo.isDir()) { | 
					
						
							|  |  |  |         if (!tgtFilePath.exists()) { | 
					
						
							| 
									
										
										
										
											2021-02-12 15:36:07 +01:00
										 |  |  |             const QDir targetDir(tgtFilePath.parentDir().toString()); | 
					
						
							|  |  |  |             if (!targetDir.mkpath(tgtFilePath.fileName())) { | 
					
						
							| 
									
										
										
										
											2020-07-07 10:11:53 +02:00
										 |  |  |                 if (error) { | 
					
						
							|  |  |  |                     *error = QCoreApplication::translate("Utils::FileUtils", | 
					
						
							|  |  |  |                                                          "Failed to create directory \"%1\".") | 
					
						
							|  |  |  |                                  .arg(tgtFilePath.toUserOutput()); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const QDir sourceDir(srcFilePath.toString()); | 
					
						
							|  |  |  |         const QStringList fileNames = sourceDir.entryList( | 
					
						
							|  |  |  |             QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System); | 
					
						
							|  |  |  |         for (const QString &fileName : fileNames) { | 
					
						
							|  |  |  |             const FilePath newSrcFilePath = srcFilePath / fileName; | 
					
						
							|  |  |  |             const FilePath newTgtFilePath = tgtFilePath / fileName; | 
					
						
							|  |  |  |             if (!copyRecursively(newSrcFilePath, newTgtFilePath, error, copyHelper)) | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (!copyHelper(srcFileInfo, tgtFilePath.toFileInfo(), error)) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 15:31:40 +02:00
										 |  |  | // for actually finding out if e.g. directories are writable on Windows
 | 
					
						
							|  |  |  | #ifdef Q_OS_WIN
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							| 
									
										
										
										
											2018-06-05 14:22:26 +02:00
										 |  |  | T withNtfsPermissions(const std::function<T()> &task) | 
					
						
							| 
									
										
										
										
											2017-05-24 15:31:40 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     qt_ntfs_permission_lookup++; | 
					
						
							|  |  |  |     T result = task(); | 
					
						
							|  |  |  |     qt_ntfs_permission_lookup--; | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 15:37:39 +01:00
										 |  |  | template <> | 
					
						
							| 
									
										
										
										
											2018-06-05 14:22:26 +02:00
										 |  |  | QTCREATOR_UTILS_EXPORT void withNtfsPermissions(const std::function<void()> &task); | 
					
						
							| 
									
										
										
										
											2018-02-19 15:37:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 15:31:40 +02:00
										 |  |  | #else // Q_OS_WIN
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							| 
									
										
										
										
											2018-06-05 14:22:26 +02:00
										 |  |  | T withNtfsPermissions(const std::function<T()> &task) | 
					
						
							| 
									
										
										
										
											2017-05-24 15:31:40 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return task(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // Q_OS_WIN
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | class QTCREATOR_UTILS_EXPORT FileReader | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-14 12:22:14 +02:00
										 |  |  |     Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic!
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     static QByteArray fetchQrc(const QString &fileName); // Only for internal resources
 | 
					
						
							| 
									
										
										
										
											2021-05-18 09:47:07 +02:00
										 |  |  |     bool fetch(const FilePath &filePath, QIODevice::OpenMode mode = QIODevice::NotOpen); // QIODevice::ReadOnly is implicit
 | 
					
						
							|  |  |  |     bool fetch(const FilePath &filePath, QIODevice::OpenMode mode, QString *errorString); | 
					
						
							|  |  |  |     bool fetch(const FilePath &filePath, QString *errorString) | 
					
						
							|  |  |  |         { return fetch(filePath, QIODevice::NotOpen, errorString); } | 
					
						
							| 
									
										
										
										
											2017-08-18 14:32:39 +02:00
										 |  |  | #ifdef QT_GUI_LIB
 | 
					
						
							| 
									
										
										
										
											2021-05-18 09:47:07 +02:00
										 |  |  |     bool fetch(const FilePath &filePath, QIODevice::OpenMode mode, QWidget *parent); | 
					
						
							|  |  |  |     bool fetch(const FilePath &filePath, QWidget *parent) | 
					
						
							|  |  |  |         { return fetch(filePath, QIODevice::NotOpen, parent); } | 
					
						
							| 
									
										
										
										
											2017-08-18 14:32:39 +02:00
										 |  |  | #endif // QT_GUI_LIB
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  |     const QByteArray &data() const { return m_data; } | 
					
						
							|  |  |  |     const QString &errorString() const { return m_errorString; } | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     QByteArray m_data; | 
					
						
							|  |  |  |     QString m_errorString; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QTCREATOR_UTILS_EXPORT FileSaverBase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-14 12:22:14 +02:00
										 |  |  |     Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic!
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     FileSaverBase(); | 
					
						
							| 
									
										
										
										
											2017-07-03 12:02:26 +02:00
										 |  |  |     virtual ~FileSaverBase(); | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 09:47:07 +02:00
										 |  |  |     FilePath filePath() const { return m_filePath; } | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  |     bool hasError() const { return m_hasError; } | 
					
						
							|  |  |  |     QString errorString() const { return m_errorString; } | 
					
						
							|  |  |  |     virtual bool finalize(); | 
					
						
							|  |  |  |     bool finalize(QString *errStr); | 
					
						
							| 
									
										
										
										
											2017-08-18 14:32:39 +02:00
										 |  |  | #ifdef QT_GUI_LIB
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  |     bool finalize(QWidget *parent); | 
					
						
							| 
									
										
										
										
											2017-08-18 14:32:39 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool write(const char *data, int len); | 
					
						
							|  |  |  |     bool write(const QByteArray &bytes); | 
					
						
							|  |  |  |     bool setResult(QTextStream *stream); | 
					
						
							|  |  |  |     bool setResult(QDataStream *stream); | 
					
						
							|  |  |  |     bool setResult(QXmlStreamWriter *stream); | 
					
						
							|  |  |  |     bool setResult(bool ok); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 15:04:01 +02:00
										 |  |  |     QFile *file() { return m_file.get(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2017-06-29 15:04:01 +02:00
										 |  |  |     std::unique_ptr<QFile> m_file; | 
					
						
							| 
									
										
										
										
											2021-05-18 09:47:07 +02:00
										 |  |  |     FilePath m_filePath; | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  |     QString m_errorString; | 
					
						
							| 
									
										
										
										
											2018-07-23 10:45:40 +02:00
										 |  |  |     bool m_hasError = false; | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     Q_DISABLE_COPY(FileSaverBase) | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QTCREATOR_UTILS_EXPORT FileSaver : public FileSaverBase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-14 12:22:14 +02:00
										 |  |  |     Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic!
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2018-05-07 15:07:21 +02:00
										 |  |  |     // QIODevice::WriteOnly is implicit
 | 
					
						
							| 
									
										
										
										
											2021-05-18 09:47:07 +02:00
										 |  |  |     explicit FileSaver(const FilePath &filePath, QIODevice::OpenMode mode = QIODevice::NotOpen); | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 15:07:21 +02:00
										 |  |  |     bool finalize() override; | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  |     using FileSaverBase::finalize; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     bool m_isSafe; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QTCREATOR_UTILS_EXPORT TempFileSaver : public FileSaverBase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-14 12:22:14 +02:00
										 |  |  |     Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic!
 | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     explicit TempFileSaver(const QString &templ = QString()); | 
					
						
							| 
									
										
										
										
											2018-05-07 15:07:21 +02:00
										 |  |  |     ~TempFileSaver() override; | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void setAutoRemove(bool on) { m_autoRemove = on; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2018-07-23 10:45:40 +02:00
										 |  |  |     bool m_autoRemove = true; | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  | inline uint qHash(const Utils::FilePath &a, uint seed = 0) { return a.hash(seed); } | 
					
						
							| 
									
										
										
										
											2011-03-30 12:06:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 12:23:27 +02:00
										 |  |  | } // namespace Utils
 | 
					
						
							| 
									
										
										
										
											2011-11-08 18:06:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 14:57:30 +02:00
										 |  |  | namespace std { | 
					
						
							| 
									
										
										
										
											2019-12-18 10:52:17 +01:00
										 |  |  | template<> struct QTCREATOR_UTILS_EXPORT hash<Utils::FilePath> | 
					
						
							| 
									
										
										
										
											2017-06-13 14:57:30 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  |     using argument_type = Utils::FilePath; | 
					
						
							| 
									
										
										
										
											2017-06-13 14:57:30 +02:00
										 |  |  |     using result_type = size_t; | 
					
						
							| 
									
										
										
										
											2019-12-17 14:18:14 +01:00
										 |  |  |     result_type operator()(const argument_type &fn) const; | 
					
						
							| 
									
										
										
										
											2017-06-13 14:57:30 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | } // namespace std
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 13:49:26 +02:00
										 |  |  | Q_DECLARE_METATYPE(Utils::FilePath) |