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-10-05 15:42:14 +02:00
|
|
|
#include "filepathinfo.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>
|
2022-10-12 17:17:44 +02:00
|
|
|
#include <variant>
|
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 {
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
class DeviceFileAccess;
|
2021-07-16 11:16:45 +02:00
|
|
|
class Environment;
|
2021-08-03 15:12:24 +02:00
|
|
|
class EnvironmentChange;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
template <class ...Args> using Continuation = std::function<void(Args...)>;
|
|
|
|
|
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);
|
|
|
|
|
2022-10-05 15:42:14 +02:00
|
|
|
QStringList asFindArguments(const QString &path) const;
|
2022-10-05 17:11:09 +02:00
|
|
|
|
2022-01-21 12:22:54 +01:00
|
|
|
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();
|
|
|
|
|
2021-09-09 08:14:26 +02:00
|
|
|
QString toUserOutput() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
QString toString() const;
|
|
|
|
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;
|
2022-10-14 10:11:13 +02:00
|
|
|
bool isSymLink() const;
|
2022-09-16 11:14:59 +02:00
|
|
|
bool isRootPath() 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;
|
2022-10-10 17:11:51 +02:00
|
|
|
bool writeFileContents(const QByteArray &data, qint64 offset = 0) const;
|
2022-10-05 15:42:14 +02:00
|
|
|
FilePathInfo filePathInfo() const;
|
2021-07-16 11:16:45 +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;
|
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;
|
2022-10-18 14:38:29 +02:00
|
|
|
[[nodiscard]] FilePath relativePathFrom(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;
|
2022-10-05 15:42:14 +02:00
|
|
|
|
2022-10-11 13:58:26 +02:00
|
|
|
using IterateDirCallback
|
|
|
|
= std::variant<
|
|
|
|
std::function<bool(const FilePath &item)>,
|
|
|
|
std::function<bool(const FilePath &item, const FilePathInfo &info)>
|
|
|
|
>;
|
2022-10-05 15:42:14 +02:00
|
|
|
|
2022-10-11 13:58:26 +02:00
|
|
|
void iterateDirectory(
|
|
|
|
const IterateDirCallback &callBack,
|
|
|
|
const FileFilter &filter) const;
|
2022-10-05 15:42:14 +02:00
|
|
|
|
2022-10-11 13:58:26 +02:00
|
|
|
static void iterateDirectories(
|
|
|
|
const FilePaths &dirs,
|
|
|
|
const IterateDirCallback &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
|
|
|
|
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;
|
2022-10-10 17:11:51 +02:00
|
|
|
void asyncWriteFileContents(const Continuation<bool> &cont,
|
|
|
|
const QByteArray &data,
|
|
|
|
qint64 offset = 0) const;
|
2021-08-30 15:42:21 +02:00
|
|
|
|
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-09-16 10:26:41 +02:00
|
|
|
bool isSameDevice(const FilePath &other) const;
|
2022-10-13 11:11:29 +02:00
|
|
|
bool isSameFile(const FilePath &other) const;
|
2022-09-16 10:26:41 +02:00
|
|
|
|
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-09-16 11:14:59 +02:00
|
|
|
// Support for FSEngine. Do not use unless needed.
|
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);
|
|
|
|
|
2022-09-14 09:55:30 +02:00
|
|
|
[[nodiscard]] bool ensureReachable(const FilePath &other) const;
|
|
|
|
|
2022-07-14 15:04:44 +02:00
|
|
|
[[nodiscard]] QString toFSPathString() const;
|
|
|
|
|
|
|
|
[[nodiscard]] static int rootLength(const QStringView path); // Assumes no scheme and host
|
|
|
|
[[nodiscard]] static int schemeAndHostLength(const QStringView path);
|
2022-09-16 11:14:59 +02:00
|
|
|
|
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-10-10 17:32:56 +02:00
|
|
|
DeviceFileAccess *fileAccess() const;
|
2022-09-13 08:58:31 +02:00
|
|
|
|
2022-09-02 16:47:54 +02:00
|
|
|
[[nodiscard]] QString encodedHost() const;
|
2021-07-16 11:16:45 +02:00
|
|
|
|
2022-09-21 12:59:02 +02:00
|
|
|
QString m_data; // Concatenated m_path, m_scheme, m_host
|
|
|
|
unsigned int m_pathLen = 0;
|
|
|
|
unsigned short m_schemeLen = 0;
|
|
|
|
unsigned short m_hostLen = 0;
|
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-10-10 17:32:56 +02:00
|
|
|
std::function<DeviceFileAccess *(const FilePath &)> fileAccess;
|
2022-07-26 13:32:07 +02:00
|
|
|
std::function<QString(const FilePath &)> deviceDisplayName;
|
2022-09-14 09:55:30 +02:00
|
|
|
std::function<bool(const FilePath &, const FilePath &)> ensureReachable;
|
2022-10-10 17:32:56 +02:00
|
|
|
std::function<Environment(const FilePath &)> environment;
|
|
|
|
std::function<bool(const FilePath &left, const FilePath &right)> isSameDevice;
|
2022-07-26 13:32:07 +02:00
|
|
|
};
|
|
|
|
|
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
|