Utils: Add FilePath::copyRecursively

Change-Id: I0cb07158906a5e163ea35670f46f3b4fd9ec40b8
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marcus Tillmanns
2023-01-20 11:30:21 +01:00
parent d57dd8462e
commit 7cb74e325f
9 changed files with 158 additions and 87 deletions

View File

@@ -61,14 +61,12 @@ public:
};
#endif // QT_GUI_LIB
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 &&copyHelper);
static bool copyRecursively(
const FilePath &srcFilePath,
const FilePath &tgtFilePath,
QString *error,
std::function<bool(const FilePath &, const FilePath &, QString *)> helper);
static bool copyIfDifferent(const FilePath &srcFilePath,
const FilePath &tgtFilePath);
static QString fileSystemFriendlyName(const QString &name);
@@ -126,39 +124,6 @@ public:
};
template<typename T>
bool FileUtils::copyRecursively(const FilePath &srcFilePath,
const FilePath &tgtFilePath,
QString *error,
T &&copyHelper)
{
if (srcFilePath.isDir()) {
if (!tgtFilePath.exists()) {
if (!tgtFilePath.ensureWritableDir()) {
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(srcFilePath, tgtFilePath, error))
return false;
}
return true;
}
// for actually finding out if e.g. directories are writable on Windows
#ifdef Q_OS_WIN