2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "utils_global.h"
|
|
|
|
|
2022-05-24 00:40:44 +02:00
|
|
|
#include "osspecificaspects.h"
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
#include <QDir>
|
2021-09-14 14:20:50 +02:00
|
|
|
#include <QDirIterator>
|
2021-07-16 11:16:45 +02:00
|
|
|
#include <QMetaType>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2022-09-09 13:48:08 +02:00
|
|
|
#include <optional>
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
class QDateTime;
|
|
|
|
class QDebug;
|
|
|
|
class QFileInfo;
|
|
|
|
class QUrl;
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
class tst_fileutils; // This becomes a friend of Utils::FilePath for testing private methods.
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
|
|
class Environment;
|
2021-08-03 15:12:24 +02:00
|
|
|
class EnvironmentChange;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-01-21 12:22:54 +01:00
|
|
|
class QTCREATOR_UTILS_EXPORT FileFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileFilter(const QStringList &nameFilters,
|
|
|
|
const QDir::Filters fileFilters = QDir::NoFilter,
|
|
|
|
const QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags);
|
|
|
|
|
|
|
|
const QStringList nameFilters;
|
|
|
|
const QDir::Filters fileFilters = QDir::NoFilter;
|
|
|
|
const QDirIterator::IteratorFlags iteratorFlags = QDirIterator::NoIteratorFlags;
|
|
|
|
};
|
|
|
|
|
2022-07-21 15:58:09 +02:00
|
|
|
using FilePaths = QList<class FilePath>;
|
|
|
|
|
2021-07-16 11:16:45 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT FilePath
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FilePath();
|
|
|
|
|
2022-08-04 16:35:31 +02:00
|
|
|
template <size_t N> FilePath(const char (&literal)[N]) { setFromString(literal); }
|
2021-08-10 16:19:02 +02:00
|
|
|
|
2022-08-04 16:35:31 +02:00
|
|
|
[[nodiscard]] static FilePath fromString(const QString &filepath);
|
2021-09-13 11:54:59 +02:00
|
|
|
[[nodiscard]] static FilePath fromStringWithExtension(const QString &filepath, const QString &defaultExtension);
|
|
|
|
[[nodiscard]] static FilePath fromUserInput(const QString &filepath);
|
|
|
|
[[nodiscard]] static FilePath fromUtf8(const char *filepath, int filepathSize = -1);
|
|
|
|
[[nodiscard]] static FilePath fromVariant(const QVariant &variant);
|
|
|
|
[[nodiscard]] static FilePath fromUrl(const QUrl &url);
|
2022-08-04 15:00:24 +02:00
|
|
|
[[nodiscard]] static FilePath fromParts(const QStringView scheme, const QStringView host, const QStringView path);
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-07-28 15:59:52 +02:00
|
|
|
[[nodiscard]] static FilePath currentWorkingPath();
|
|
|
|
[[nodiscard]] static FilePath rootPath();
|
|
|
|
|
2021-09-09 08:14:26 +02:00
|
|
|
QString toUserOutput() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
QString toString() const;
|
2022-05-31 11:16:44 +02:00
|
|
|
QString toFSPathString() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
QVariant toVariant() const;
|
2021-09-09 08:14:26 +02:00
|
|
|
QUrl toUrl() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-08-04 13:35:42 +02:00
|
|
|
QStringView scheme() const;
|
|
|
|
QStringView host() const;
|
|
|
|
QString path() const;
|
2022-08-04 15:00:24 +02:00
|
|
|
|
|
|
|
void setParts(const QStringView scheme, const QStringView host, const QStringView path);
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
QString fileName() const;
|
|
|
|
QString fileNameWithPathComponents(int pathComponents) const;
|
|
|
|
|
|
|
|
QString baseName() const;
|
|
|
|
QString completeBaseName() const;
|
|
|
|
QString suffix() const;
|
|
|
|
QString completeSuffix() const;
|
|
|
|
|
2021-09-13 11:54:59 +02:00
|
|
|
[[nodiscard]] FilePath pathAppended(const QString &str) const;
|
|
|
|
[[nodiscard]] FilePath stringAppended(const QString &str) const;
|
2021-09-09 08:14:26 +02:00
|
|
|
bool startsWith(const QString &s) const;
|
|
|
|
bool endsWith(const QString &s) const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
bool exists() const;
|
2021-09-09 08:14:26 +02:00
|
|
|
|
|
|
|
FilePath parentDir() const;
|
|
|
|
bool isChildOf(const FilePath &s) const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
bool isWritableDir() const;
|
|
|
|
bool isWritableFile() const;
|
|
|
|
bool ensureWritableDir() const;
|
|
|
|
bool ensureExistingFile() const;
|
|
|
|
bool isExecutableFile() const;
|
|
|
|
bool isReadableFile() const;
|
|
|
|
bool isReadableDir() const;
|
|
|
|
bool isRelativePath() const;
|
|
|
|
bool isAbsolutePath() const { return !isRelativePath(); }
|
|
|
|
bool isFile() const;
|
|
|
|
bool isDir() const;
|
2021-09-09 08:14:26 +02:00
|
|
|
bool isNewerThan(const QDateTime &timeStamp) const;
|
|
|
|
QDateTime lastModified() const;
|
|
|
|
QFile::Permissions permissions() const;
|
|
|
|
bool setPermissions(QFile::Permissions permissions) const;
|
|
|
|
OsType osType() const;
|
|
|
|
bool removeFile() const;
|
|
|
|
bool removeRecursively(QString *error = nullptr) const;
|
|
|
|
bool copyFile(const FilePath &target) const;
|
|
|
|
bool renameFile(const FilePath &target) const;
|
|
|
|
qint64 fileSize() const;
|
2022-01-04 15:46:55 +01:00
|
|
|
qint64 bytesAvailable() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
bool createDir() const;
|
2022-07-21 15:58:09 +02:00
|
|
|
FilePaths dirEntries(const FileFilter &filter, QDir::SortFlags sort = QDir::NoSort) const;
|
|
|
|
FilePaths dirEntries(QDir::Filters filters) const;
|
2022-09-09 13:48:08 +02:00
|
|
|
std::optional<QByteArray> fileContents(qint64 maxSize = -1, qint64 offset = 0) const;
|
2021-07-16 11:16:45 +02:00
|
|
|
bool writeFileContents(const QByteArray &data) 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;
|
|
|
|
bool operator>=(const FilePath &other) const;
|
2021-09-13 11:54:59 +02:00
|
|
|
[[nodiscard]] FilePath operator+(const QString &s) const;
|
|
|
|
[[nodiscard]] FilePath operator/(const QString &str) const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2021-09-09 08:14:26 +02:00
|
|
|
Qt::CaseSensitivity caseSensitivity() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2021-09-09 08:14:26 +02:00
|
|
|
void clear();
|
|
|
|
bool isEmpty() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-07-13 09:27:18 +02:00
|
|
|
size_t hash(uint seed) const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-07-28 15:59:52 +02:00
|
|
|
[[nodiscard]] FilePath absoluteFilePath() const;
|
|
|
|
[[nodiscard]] FilePath absolutePath() const;
|
2021-09-13 11:54:59 +02:00
|
|
|
[[nodiscard]] FilePath resolvePath(const FilePath &tail) const;
|
|
|
|
[[nodiscard]] FilePath resolvePath(const QString &tail) const;
|
2021-09-09 11:56:11 +02:00
|
|
|
[[nodiscard]] FilePath cleanPath() const;
|
2021-09-13 11:54:59 +02:00
|
|
|
[[nodiscard]] FilePath canonicalPath() const;
|
|
|
|
[[nodiscard]] FilePath symLinkTarget() const;
|
|
|
|
[[nodiscard]] FilePath resolveSymlinks() const;
|
|
|
|
[[nodiscard]] FilePath withExecutableSuffix() const;
|
|
|
|
[[nodiscard]] FilePath relativeChildPath(const FilePath &parent) const;
|
|
|
|
[[nodiscard]] FilePath relativePath(const FilePath &anchor) const;
|
2022-07-21 15:58:09 +02:00
|
|
|
[[nodiscard]] FilePath searchInDirectories(const FilePaths &dirs) const;
|
2021-09-13 11:54:59 +02:00
|
|
|
[[nodiscard]] Environment deviceEnvironment() const;
|
|
|
|
[[nodiscard]] FilePath onDevice(const FilePath &deviceTemplate) const;
|
|
|
|
[[nodiscard]] FilePath withNewPath(const QString &newPath) const;
|
2021-09-16 12:30:10 +02:00
|
|
|
void iterateDirectory(const std::function<bool(const FilePath &item)> &callBack,
|
2022-01-21 12:22:54 +01:00
|
|
|
const FileFilter &filter) const;
|
2022-07-21 15:58:09 +02:00
|
|
|
static void iterateDirectories(const FilePaths &dirs,
|
2022-07-01 15:43:51 +02:00
|
|
|
const std::function<bool(const FilePath &item)> &callBack,
|
|
|
|
const FileFilter &filter);
|
2021-09-09 08:14:26 +02:00
|
|
|
|
2021-09-30 09:34:05 +02:00
|
|
|
enum PathAmending { AppendToPath, PrependToPath };
|
2022-07-21 15:58:09 +02:00
|
|
|
[[nodiscard]] FilePath searchInPath(const FilePaths &additionalDirs = {},
|
2021-09-30 09:34:05 +02:00
|
|
|
PathAmending = AppendToPath) const;
|
|
|
|
|
2021-09-09 08:14:26 +02:00
|
|
|
// makes sure that capitalization of directories is canonical
|
|
|
|
// on Windows and macOS. This is rarely needed.
|
2021-09-13 11:54:59 +02:00
|
|
|
[[nodiscard]] FilePath normalizedPathName() const;
|
2021-09-09 08:14:26 +02:00
|
|
|
|
2022-06-02 17:05:46 +02:00
|
|
|
QString displayName(const QString &args = {}) const;
|
2021-11-10 16:19:25 +01:00
|
|
|
QString nativePath() const;
|
2021-09-09 08:14:26 +02:00
|
|
|
QString shortNativePath() const;
|
|
|
|
bool startsWithDriveLetter() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-07-21 15:58:09 +02:00
|
|
|
static QString formatFilePaths(const FilePaths &files, const QString &separator);
|
|
|
|
static void removeDuplicates(FilePaths &files);
|
|
|
|
static void sort(FilePaths &files);
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2021-08-30 15:42:21 +02:00
|
|
|
// Asynchronous interface
|
|
|
|
template <class ...Args> using Continuation = std::function<void(Args...)>;
|
|
|
|
void asyncCopyFile(const Continuation<bool> &cont, const FilePath &target) const;
|
2022-09-09 13:48:08 +02:00
|
|
|
void asyncFileContents(const Continuation<const std::optional<QByteArray> &> &cont,
|
|
|
|
qint64 maxSize = -1,
|
|
|
|
qint64 offset = 0) const;
|
2021-08-30 15:42:21 +02:00
|
|
|
void asyncWriteFileContents(const Continuation<bool> &cont, const QByteArray &data) const;
|
|
|
|
|
2022-02-24 16:00:58 +01:00
|
|
|
// Prefer not to use
|
|
|
|
// Using needsDevice() in "user" code is likely to result in code that
|
|
|
|
// makes a local/remote distinction which should be avoided in general.
|
|
|
|
// There are usually other means available. E.g. distinguishing based
|
|
|
|
// on FilePath::osType().
|
|
|
|
bool needsDevice() const;
|
|
|
|
|
2022-07-28 15:59:52 +02:00
|
|
|
[[nodiscard]] QFileInfo toFileInfo() const;
|
|
|
|
[[nodiscard]] static FilePath fromFileInfo(const QFileInfo &info);
|
2021-09-09 08:14:26 +02:00
|
|
|
|
2022-05-31 11:16:44 +02:00
|
|
|
enum class SpecialPathComponent {
|
|
|
|
RootName,
|
|
|
|
RootPath,
|
|
|
|
DeviceRootName,
|
|
|
|
DeviceRootPath,
|
|
|
|
};
|
|
|
|
|
|
|
|
[[nodiscard]] static QString specialPath(SpecialPathComponent component);
|
|
|
|
[[nodiscard]] static FilePath specialFilePath(SpecialPathComponent component);
|
|
|
|
|
2021-07-16 11:16:45 +02:00
|
|
|
private:
|
|
|
|
friend class ::tst_fileutils;
|
|
|
|
static QString calcRelativePath(const QString &absolutePath, const QString &absoluteAnchorPath);
|
2022-09-13 08:58:31 +02:00
|
|
|
void setPath(QStringView path);
|
2022-08-04 16:35:31 +02:00
|
|
|
void setFromString(const QString &filepath);
|
2022-09-13 08:58:31 +02:00
|
|
|
|
2021-10-27 15:12:21 +02:00
|
|
|
[[nodiscard]] QString mapToDevicePath() const;
|
2022-09-02 16:47:54 +02:00
|
|
|
[[nodiscard]] QString encodedHost() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
|
|
|
QString m_scheme;
|
2022-02-24 16:00:58 +01:00
|
|
|
QString m_host; // May contain raw slashes.
|
2022-08-11 16:32:19 +02:00
|
|
|
QString m_path; // Includes the root bits
|
2021-07-16 11:16:45 +02:00
|
|
|
};
|
|
|
|
|
2022-07-13 09:27:18 +02:00
|
|
|
inline size_t qHash(const Utils::FilePath &a, uint seed = 0)
|
2021-10-19 10:56:15 +02:00
|
|
|
{
|
|
|
|
return a.hash(seed);
|
|
|
|
}
|
|
|
|
|
2022-08-04 10:44:45 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT DeviceFileHooks
|
2022-07-26 13:32:07 +02:00
|
|
|
{
|
|
|
|
public:
|
2022-08-04 10:44:45 +02:00
|
|
|
static DeviceFileHooks &instance();
|
|
|
|
|
2022-07-26 13:32:07 +02:00
|
|
|
std::function<bool(const FilePath &)> isExecutableFile;
|
|
|
|
std::function<bool(const FilePath &)> isReadableFile;
|
|
|
|
std::function<bool(const FilePath &)> isReadableDir;
|
|
|
|
std::function<bool(const FilePath &)> isWritableDir;
|
|
|
|
std::function<bool(const FilePath &)> isWritableFile;
|
|
|
|
std::function<bool(const FilePath &)> isFile;
|
|
|
|
std::function<bool(const FilePath &)> isDir;
|
|
|
|
std::function<bool(const FilePath &)> ensureWritableDir;
|
|
|
|
std::function<bool(const FilePath &)> ensureExistingFile;
|
|
|
|
std::function<bool(const FilePath &)> createDir;
|
|
|
|
std::function<bool(const FilePath &)> exists;
|
|
|
|
std::function<bool(const FilePath &)> removeFile;
|
|
|
|
std::function<bool(const FilePath &)> removeRecursively;
|
|
|
|
std::function<bool(const FilePath &, const FilePath &)> copyFile;
|
|
|
|
std::function<bool(const FilePath &, const FilePath &)> renameFile;
|
|
|
|
std::function<FilePath(const FilePath &, const FilePaths &)> searchInPath;
|
|
|
|
std::function<FilePath(const FilePath &)> symLinkTarget;
|
|
|
|
std::function<QString(const FilePath &)> mapToDevicePath;
|
|
|
|
std::function<void(const FilePath &,
|
|
|
|
const std::function<bool(const FilePath &)> &, // Abort on 'false' return.
|
|
|
|
const FileFilter &)> iterateDirectory;
|
2022-09-09 14:25:36 +02:00
|
|
|
std::function<std::optional<QByteArray>(const FilePath &, qint64, qint64)> fileContents;
|
2022-07-26 13:32:07 +02:00
|
|
|
std::function<bool(const FilePath &, const QByteArray &)> writeFileContents;
|
|
|
|
std::function<QDateTime(const FilePath &)> lastModified;
|
|
|
|
std::function<QFile::Permissions(const FilePath &)> permissions;
|
|
|
|
std::function<bool(const FilePath &, QFile::Permissions)> setPermissions;
|
|
|
|
std::function<OsType(const FilePath &)> osType;
|
|
|
|
std::function<Environment(const FilePath &)> environment;
|
|
|
|
std::function<qint64(const FilePath &)> fileSize;
|
|
|
|
std::function<qint64(const FilePath &)> bytesAvailable;
|
|
|
|
std::function<QString(const FilePath &)> deviceDisplayName;
|
|
|
|
|
|
|
|
template <class ...Args> using Continuation = std::function<void(Args...)>;
|
|
|
|
std::function<void(const Continuation<bool> &, const FilePath &, const FilePath &)> asyncCopyFile;
|
2022-09-09 13:48:08 +02:00
|
|
|
std::function<void(
|
|
|
|
const Continuation<const std::optional<QByteArray> &> &, const FilePath &, qint64, qint64)>
|
|
|
|
asyncFileContents;
|
2022-07-26 13:32:07 +02:00
|
|
|
std::function<void(const Continuation<bool> &, const FilePath &, const QByteArray &)> asyncWriteFileContents;
|
|
|
|
};
|
|
|
|
|
2021-07-16 11:16:45 +02:00
|
|
|
} // namespace Utils
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug dbg, const Utils::FilePath &c);
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(Utils::FilePath)
|
2021-10-19 10:56:15 +02:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
struct QTCREATOR_UTILS_EXPORT hash<Utils::FilePath>
|
|
|
|
{
|
|
|
|
using argument_type = Utils::FilePath;
|
|
|
|
using result_type = size_t;
|
|
|
|
result_type operator()(const argument_type &fn) const;
|
|
|
|
};
|
2022-07-26 13:32:07 +02:00
|
|
|
|
2021-10-19 10:56:15 +02:00
|
|
|
} // namespace std
|