Utils: Use FilePath in copy helpers

Change-Id: I81b367a5851c0fbcdf45c63c5536c206845a8337
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2021-08-09 08:53:40 +02:00
parent c179ce867c
commit 2ab2d96753
5 changed files with 33 additions and 40 deletions

View File

@@ -144,13 +144,12 @@ static bool removeRecursivelyLocal(const FilePath &filePath, QString *error)
bool FileUtils::copyRecursively(const FilePath &srcFilePath, const FilePath &tgtFilePath, QString *error)
{
return copyRecursively(
srcFilePath, tgtFilePath, error, [](const QFileInfo &src, const QFileInfo &dest, QString *error) {
if (!QFile::copy(src.filePath(), dest.filePath())) {
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(FilePath::fromFileInfo(src).toUserOutput(),
FilePath::fromFileInfo(dest).toUserOutput());
.arg(src.toUserOutput(), dest.toUserOutput());
}
return false;
}

View File

@@ -283,14 +283,13 @@ TempFileSaver::~TempFileSaver()
}
#ifdef QT_GUI_LIB
FileUtils::CopyAskingForOverwrite::CopyAskingForOverwrite(
QWidget *dialogParent, const std::function<void(QFileInfo)> &postOperation)
FileUtils::CopyAskingForOverwrite::CopyAskingForOverwrite(QWidget *dialogParent, const std::function<void (FilePath)> &postOperation)
: m_parent(dialogParent)
, m_postOperation(postOperation)
{}
bool FileUtils::CopyAskingForOverwrite::operator()(const QFileInfo &src,
const QFileInfo &dest,
bool FileUtils::CopyAskingForOverwrite::operator()(const FilePath &src,
const FilePath &dest,
QString *error)
{
bool copyFile = true;
@@ -302,7 +301,7 @@ bool FileUtils::CopyAskingForOverwrite::operator()(const QFileInfo &src,
m_parent,
QCoreApplication::translate("Utils::FileUtils", "Overwrite File?"),
QCoreApplication::translate("Utils::FileUtils", "Overwrite existing file \"%1\"?")
.arg(FilePath::fromFileInfo(dest).toUserOutput()),
.arg(dest.toUserOutput()),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll
| QMessageBox::Cancel);
if (res == QMessageBox::Cancel) {
@@ -316,18 +315,16 @@ bool FileUtils::CopyAskingForOverwrite::operator()(const QFileInfo &src,
m_overwriteAll = true;
}
if (copyFile)
QFile::remove(dest.filePath());
dest.removeFile();
}
}
if (copyFile) {
if (!dest.absoluteDir().exists())
dest.absoluteDir().mkpath(dest.absolutePath());
if (!QFile::copy(src.filePath(), dest.filePath())) {
dest.parentDir().ensureWritableDir();
if (!src.copyFile(dest)) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils",
"Could not copy file \"%1\" to \"%2\".")
.arg(FilePath::fromFileInfo(src).toUserOutput(),
FilePath::fromFileInfo(dest).toUserOutput());
.arg(src.toUserOutput(), dest.toUserOutput());
}
return false;
}
@@ -340,7 +337,7 @@ bool FileUtils::CopyAskingForOverwrite::operator()(const QFileInfo &src,
FilePaths FileUtils::CopyAskingForOverwrite::files() const
{
return transform(m_files, &FilePath::fromString);
return m_files;
}
#endif // QT_GUI_LIB

View File

@@ -89,21 +89,22 @@ public:
std::function<qint64(const FilePath &)> fileSize;
};
class QTCREATOR_UTILS_EXPORT FileUtils {
class QTCREATOR_UTILS_EXPORT FileUtils
{
public:
#ifdef QT_GUI_LIB
class QTCREATOR_UTILS_EXPORT CopyAskingForOverwrite
{
public:
CopyAskingForOverwrite(QWidget *dialogParent,
const std::function<void(QFileInfo)> &postOperation = {});
bool operator()(const QFileInfo &src, const QFileInfo &dest, QString *error);
const std::function<void(FilePath)> &postOperation = {});
bool operator()(const FilePath &src, const FilePath &dest, QString *error);
QList<FilePath> files() const;
private:
QWidget *m_parent;
QStringList m_files;
std::function<void(QFileInfo)> m_postOperation;
FilePaths m_files;
std::function<void(FilePath)> m_postOperation;
bool m_overwriteAll = false;
bool m_skipAll = false;
};
@@ -167,11 +168,9 @@ bool FileUtils::copyRecursively(const FilePath &srcFilePath,
QString *error,
T &&copyHelper)
{
const QFileInfo srcFileInfo = srcFilePath.toFileInfo();
if (srcFileInfo.isDir()) {
if (srcFilePath.isDir()) {
if (!tgtFilePath.exists()) {
const QDir targetDir(tgtFilePath.parentDir().toString());
if (!targetDir.mkpath(tgtFilePath.fileName())) {
if (!tgtFilePath.parentDir().ensureWritableDir()) {
if (error) {
*error = QCoreApplication::translate("Utils::FileUtils",
"Failed to create directory \"%1\".")
@@ -190,7 +189,7 @@ bool FileUtils::copyRecursively(const FilePath &srcFilePath,
return false;
}
} else {
if (!copyHelper(srcFileInfo, tgtFilePath.toFileInfo(), error))
if (!copyHelper(srcFilePath, tgtFilePath, error))
return false;
}
return true;

View File

@@ -420,16 +420,16 @@ private:
Data *m_data = nullptr;
};
static std::function<void(QFileInfo)> postCopyOperation()
static std::function<void(FilePath)> postCopyOperation()
{
return [](const QFileInfo &fi) {
return [](const FilePath &filePath) {
if (!HostOsInfo::isMacHost())
return;
// On macOS, downloaded files get a quarantine flag, remove it, otherwise it is a hassle
// to get it loaded as a plugin in Qt Creator.
QtcProcess xattr;
xattr.setTimeoutS(1);
xattr.setCommand({"/usr/bin/xattr", {"-d", "com.apple.quarantine", fi.absoluteFilePath()}});
xattr.setCommand({"/usr/bin/xattr", {"-d", "com.apple.quarantine", filePath.absoluteFilePath().toString()}});
xattr.runBlocking();
};
}
@@ -437,7 +437,7 @@ static std::function<void(QFileInfo)> postCopyOperation()
static bool copyPluginFile(const FilePath &src, const FilePath &dest)
{
const FilePath destFile = dest.pathAppended(src.fileName());
if (QFile::exists(destFile.toString())) {
if (destFile.exists()) {
QMessageBox box(QMessageBox::Question,
PluginInstallWizard::tr("Overwrite File"),
PluginInstallWizard::tr("The file \"%1\" exists. Overwrite?")
@@ -450,17 +450,17 @@ static bool copyPluginFile(const FilePath &src, const FilePath &dest)
box.exec();
if (box.clickedButton() != acceptButton)
return false;
QFile::remove(destFile.toString());
destFile.removeFile();
}
QDir(dest.toString()).mkpath(".");
if (!QFile::copy(src.toString(), destFile.toString())) {
dest.parentDir().ensureWritableDir();
if (!src.copyFile(destFile)) {
QMessageBox::warning(ICore::dialogParent(),
PluginInstallWizard::tr("Failed to Write File"),
PluginInstallWizard::tr("Failed to write file \"%1\".")
.arg(destFile.toUserOutput()));
return false;
}
postCopyOperation()(destFile.toFileInfo());
postCopyOperation()(destFile);
return true;
}

View File

@@ -322,20 +322,18 @@ static bool copyRecursively(const QString &sourceDirPath,
const QString &targetDirPath,
QString *error)
{
auto copyHelper = [](QFileInfo sourceInfo, QFileInfo targetInfo, QString *error) -> bool {
const QString sourcePath = sourceInfo.absoluteFilePath();
const QString targetPath = targetInfo.absoluteFilePath();
if (!QFile::copy(sourcePath, targetPath)) {
auto copyHelper = [](const FilePath &sourcePath, const FilePath &targetPath, QString *error) -> bool {
if (!sourcePath.copyFile(targetPath)) {
if (error) {
*error = QString::fromLatin1("copyRecursively() failed: \"%1\" to \"%2\".")
.arg(sourcePath, targetPath);
.arg(sourcePath.toUserOutput(), targetPath.toUserOutput());
}
return false;
}
// Copied files from Qt resources are read-only. Make them writable
// so that their parent directory can be removed without warnings.
QFile file(targetPath);
QFile file(targetPath.toString());
return file.setPermissions(file.permissions() | QFile::WriteUser);
};