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

@@ -637,38 +637,35 @@ FilePathInfo FileUtils::filePathInfoFromTriple(const QString &infos)
return {size, flags, dt};
}
/*!
Copies the directory specified by \a srcFilePath recursively to \a tgtFilePath. \a tgtFilePath will contain
the target directory, which will be created. Example usage:
\code
QString error;
bool ok = Utils::FileUtils::copyRecursively("/foo/bar", "/foo/baz", &error);
if (!ok)
qDebug() << error;
\endcode
This will copy the contents of /foo/bar into to the baz directory under /foo, which will be created in the process.
\note The \a error parameter is optional.
Returns whether the operation succeeded.
*/
bool FileUtils::copyRecursively(const FilePath &srcFilePath, const FilePath &tgtFilePath, QString *error)
bool FileUtils::copyRecursively(
const FilePath &srcFilePath,
const FilePath &tgtFilePath,
QString *error,
std::function<bool(const FilePath &, const FilePath &, QString *)> copyHelper)
{
return copyRecursively(
srcFilePath, tgtFilePath, error, [](const FilePath &src, const FilePath &dest, QString *error) {
if (!src.copyFile(dest)) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils",
"Could not copy file \"%1\" to \"%2\".")
.arg(src.toUserOutput(), dest.toUserOutput());
}
return false;
if (srcFilePath.isDir()) {
if (!tgtFilePath.ensureWritableDir()) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils",
"Failed to create directory \"%1\".")
.arg(tgtFilePath.toUserOutput());
}
return true;
});
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;
}
/*!