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"
|
|
|
|
|
|
2021-07-16 11:16:45 +02:00
|
|
|
#include "filepath.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
|
|
|
|
2021-07-16 15:24:26 +02:00
|
|
|
#ifdef QT_WIDGETS_LIB
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
#endif
|
|
|
|
|
|
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
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
2014-09-04 20:43:09 +02:00
|
|
|
class QDataStream;
|
2011-03-30 12:06:05 +02:00
|
|
|
class QTextStream;
|
2014-09-04 20:43:09 +02:00
|
|
|
class QWidget;
|
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
|
|
|
|
|
|
|
|
|
|
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-07-23 12:06:38 +02:00
|
|
|
std::function<qint64(const FilePath &)> fileSize;
|
2021-04-27 12:05:13 +02:00
|
|
|
};
|
|
|
|
|
|
2021-08-09 08:53:40 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT FileUtils
|
|
|
|
|
{
|
2011-07-05 12:59:14 +02:00
|
|
|
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,
|
2021-08-09 08:53:40 +02:00
|
|
|
const std::function<void(FilePath)> &postOperation = {});
|
|
|
|
|
bool operator()(const FilePath &src, const FilePath &dest, QString *error);
|
2021-07-14 16:49:42 +02:00
|
|
|
QList<FilePath> files() const;
|
2020-06-11 15:41:26 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QWidget *m_parent;
|
2021-08-09 08:53:40 +02:00
|
|
|
FilePaths m_files;
|
|
|
|
|
std::function<void(FilePath)> 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);
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
|
static void setDeviceFileHooks(const DeviceFileHooks &hooks);
|
2021-07-16 15:24:26 +02:00
|
|
|
|
|
|
|
|
#ifdef QT_WIDGETS_LIB
|
|
|
|
|
static void setDialogParentGetter(const std::function<QWidget *()> &getter);
|
|
|
|
|
|
|
|
|
|
static FilePath getOpenFilePath(const QString &caption = {},
|
|
|
|
|
const FilePath &dir = {},
|
|
|
|
|
const QString &filter = {},
|
|
|
|
|
QString *selectedFilter = nullptr,
|
|
|
|
|
QFileDialog::Options options = {});
|
|
|
|
|
|
|
|
|
|
static FilePath getSaveFilePath(const QString &caption = {},
|
|
|
|
|
const FilePath &dir = {},
|
|
|
|
|
const QString &filter = {},
|
|
|
|
|
QString *selectedFilter = nullptr,
|
|
|
|
|
QFileDialog::Options options = {});
|
|
|
|
|
|
|
|
|
|
static FilePath getExistingDirectory(const QString &caption = {},
|
|
|
|
|
const FilePath &dir = {},
|
|
|
|
|
QFileDialog::Options options = QFileDialog::ShowDirsOnly);
|
|
|
|
|
|
|
|
|
|
static FilePaths getOpenFilePaths(const QString &caption = {},
|
|
|
|
|
const FilePath &dir = {},
|
|
|
|
|
const QString &filter = {},
|
|
|
|
|
QString *selectedFilter = nullptr,
|
|
|
|
|
QFileDialog::Options options = {});
|
|
|
|
|
#endif
|
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)
|
|
|
|
|
{
|
2021-08-09 08:53:40 +02:00
|
|
|
if (srcFilePath.isDir()) {
|
2020-07-07 10:11:53 +02:00
|
|
|
if (!tgtFilePath.exists()) {
|
2021-08-09 08:53:40 +02:00
|
|
|
if (!tgtFilePath.parentDir().ensureWritableDir()) {
|
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 {
|
2021-08-09 08:53:40 +02:00
|
|
|
if (!copyHelper(srcFilePath, tgtFilePath, error))
|
2020-07-07 10:11:53 +02:00
|
|
|
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:
|
2021-08-09 08:09:39 +02:00
|
|
|
bool m_isSafe = false;
|
2011-03-30 12:06:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2021-07-16 11:16:45 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QTextStream &operator<<(QTextStream &s, const FilePath &fn);
|
|
|
|
|
|
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
|
|
|
|
|
|