diff options
author | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2023-01-20 11:30:21 +0100 |
---|---|---|
committer | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2023-01-25 07:29:52 +0000 |
commit | 7cb74e325f3adf54972d5257ab328ebeccbaf523 (patch) | |
tree | ba847ad8bc878923050b222362ad8af19f9ebdfb /src/libs/utils/fileutils.cpp | |
parent | d57dd8462ec6a5054e76faf23f99eb893dfd15ea (diff) |
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>
Diffstat (limited to 'src/libs/utils/fileutils.cpp')
-rw-r--r-- | src/libs/utils/fileutils.cpp | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index e44102f4c7..44bf0bab20 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -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) -{ - 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; +bool FileUtils::copyRecursively( + const FilePath &srcFilePath, + const FilePath &tgtFilePath, + QString *error, + std::function<bool(const FilePath &, const FilePath &, QString *)> copyHelper) +{ + 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; } /*! |