diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index 871e5ebb528..b4eb010a4ae 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -38,12 +38,152 @@ #include #include #include +#include #include #include #include namespace Utils { +/*! \class Utils::FileUtils + + \brief File- and directory-related convenience functions. + + File- and directory-related convenience functions. +*/ + +/*! + \fn Utils::FileUtils::removeRecursively(const QString &filePath, QString *error) + + Removes the directory \a filePath and its subdirectories recursively. + + \note The \a error parameter is optional. + + \return Whether the operation succeeded. +*/ + +/*! + \fn Utils::FileUtils::copyRecursively(const QString &srcFilePath, const QString &tgtFilePath, QString *error) + + 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; + book 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. + + \return Whether the operation succeeded. +*/ + +/*! + \fn Utils::FileUtils::isFileNewerThan(const QString &filePath, const QDateTime &timeStamp) + + If \a filePath is a directory, the function will recursively check all files and return + true if one of them is newer than \a timeStamp. If \a filePath is a single file, true will + be returned if the file is newer than \timeStamp. + + \return Whether at least one file in \a filePath has a newer date than \a timeStamp. +*/ + +bool FileUtils::removeRecursively(const QString &filePath, QString *error) +{ + QFileInfo fileInfo(filePath); + if (!fileInfo.exists()) + return true; + QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser); + if (fileInfo.isDir()) { + QDir dir(filePath); + QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden + | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot); + foreach (const QString &fileName, fileNames) { + if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error)) + return false; + } + dir.cdUp(); + if (!dir.rmdir(fileInfo.fileName())) { + if (error) { + *error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove directory '%1'.") + .arg(QDir::toNativeSeparators(filePath)); + } + return false; + } + } else { + if (!QFile::remove(filePath)) { + if (error) { + *error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove file '%1'.") + .arg(QDir::toNativeSeparators(filePath)); + } + return false; + } + } + return true; +} + +bool FileUtils::copyRecursively(const QString &srcFilePath, + const QString &tgtFilePath, QString *error) +{ + QFileInfo srcFileInfo(srcFilePath); + if (srcFileInfo.isDir()) { + QDir targetDir(tgtFilePath); + targetDir.cdUp(); + if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) { + if (error) { + *error = QCoreApplication::translate("Utils::FileUtils", "Failed to create directory '%1'.") + .arg(QDir::toNativeSeparators(tgtFilePath)); + return false; + } + } + QDir sourceDir(srcFilePath); + QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System); + foreach (const QString &fileName, fileNames) { + const QString newSrcFilePath + = srcFilePath + QLatin1Char('/') + fileName; + const QString newTgtFilePath + = tgtFilePath + QLatin1Char('/') + fileName; + if (!copyRecursively(newSrcFilePath, newTgtFilePath, error)) + return false; + } + } else { + if (!QFile::copy(srcFilePath, tgtFilePath)) { + if (error) { + *error = QCoreApplication::translate("Utils::FileUtils", "Could not copy file '%1' to '%2'.") + .arg(QDir::toNativeSeparators(srcFilePath), + QDir::toNativeSeparators(tgtFilePath)); + } + return false; + } + } + return true; +} + +bool FileUtils::isFileNewerThan(const QString &filePath, + const QDateTime &timeStamp) +{ + QFileInfo fileInfo(filePath); + if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp) + return true; + if (fileInfo.isDir()) { + const QStringList dirContents = QDir(filePath) + .entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); + foreach (const QString &curFileName, dirContents) { + const QString curFilePath + = filePath + QLatin1Char('/') + curFileName; + if (isFileNewerThan(curFilePath, timeStamp)) + return true; + } + } + return false; +} + + + QByteArray FileReader::fetchQrc(const QString &fileName) { QTC_ASSERT(fileName.startsWith(QLatin1Char(':')), return QByteArray()) diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index 21f5eee3f2b..d6b4e57ef26 100644 --- a/src/libs/utils/fileutils.h +++ b/src/libs/utils/fileutils.h @@ -45,10 +45,20 @@ class QTemporaryFile; class QWidget; class QTextStream; class QDataStream; +class QDateTime; QT_END_NAMESPACE namespace Utils { +class QTCREATOR_UTILS_EXPORT FileUtils { +public: + static bool removeRecursively(const QString &filePath, QString *error = 0); + static bool copyRecursively(const QString &srcFilePath, + const QString &tgtFilePath, QString *error = 0); + static bool isFileNewerThan(const QString &filePath, + const QDateTime &timeStamp); +}; + class QTCREATOR_UTILS_EXPORT FileReader { Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic! diff --git a/src/plugins/remotelinux/maemoglobal.cpp b/src/plugins/remotelinux/maemoglobal.cpp index 6d22c179ee3..e022aafd0d2 100644 --- a/src/plugins/remotelinux/maemoglobal.cpp +++ b/src/plugins/remotelinux/maemoglobal.cpp @@ -286,93 +286,6 @@ QString MaemoGlobal::architecture(const QString &qmakePath) return arch; } -bool MaemoGlobal::removeRecursively(const QString &filePath, QString &error) -{ - error.clear(); - QFileInfo fileInfo(filePath); - if (!fileInfo.exists()) - return true; - QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser); - if (fileInfo.isDir()) { - QDir dir(filePath); - QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden - | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot); - foreach (const QString &fileName, fileNames) { - if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error)) - return false; - } - dir.cdUp(); - if (!dir.rmdir(fileInfo.fileName())) { - error = tr("Failed to remove directory '%1'.") - .arg(QDir::toNativeSeparators(filePath)); - return false; - } - } else { - if (!QFile::remove(filePath)) { - error = tr("Failed to remove file '%1'.") - .arg(QDir::toNativeSeparators(filePath)); - return false; - } - } - return true; -} - -bool MaemoGlobal::copyRecursively(const QString &srcFilePath, - const QString &tgtFilePath, QString *error) -{ - QFileInfo srcFileInfo(srcFilePath); - if (srcFileInfo.isDir()) { - QDir targetDir(tgtFilePath); - targetDir.cdUp(); - if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) { - if (error) { - *error = tr("Failed to create directory '%1'.") - .arg(QDir::toNativeSeparators(tgtFilePath)); - return false; - } - } - QDir sourceDir(srcFilePath); - QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); - foreach (const QString &fileName, fileNames) { - const QString newSrcFilePath - = srcFilePath + QLatin1Char('/') + fileName; - const QString newTgtFilePath - = tgtFilePath + QLatin1Char('/') + fileName; - if (!copyRecursively(newSrcFilePath, newTgtFilePath)) - return false; - } - } else { - if (!QFile::copy(srcFilePath, tgtFilePath)) { - if (error) { - *error = tr("Could not copy file '%1' to '%2'.") - .arg(QDir::toNativeSeparators(srcFilePath), - QDir::toNativeSeparators(tgtFilePath)); - } - return false; - } - } - return true; -} - -bool MaemoGlobal::isFileNewerThan(const QString &filePath, - const QDateTime &timeStamp) -{ - QFileInfo fileInfo(filePath); - if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp) - return true; - if (fileInfo.isDir()) { - const QStringList dirContents = QDir(filePath) - .entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); - foreach (const QString &curFileName, dirContents) { - const QString curFilePath - = filePath + QLatin1Char('/') + curFileName; - if (isFileNewerThan(curFilePath, timeStamp)) - return true; - } - } - return false; -} - void MaemoGlobal::addMaddeEnvironment(Utils::Environment &env, const QString &qmakePath) { Utils::Environment maddeEnv; diff --git a/src/plugins/remotelinux/maemoglobal.h b/src/plugins/remotelinux/maemoglobal.h index c185acce5d3..f9751c65056 100644 --- a/src/plugins/remotelinux/maemoglobal.h +++ b/src/plugins/remotelinux/maemoglobal.h @@ -137,13 +137,6 @@ public: static PackagingSystem packagingSystem(const QString &osType); - static bool removeRecursively(const QString &filePath, QString &error); - static bool copyRecursively(const QString &srcFilePath, - const QString &tgtFilePath, QString *error = 0); - - static bool isFileNewerThan(const QString &filePath, - const QDateTime &timeStamp); - template static T *earlierBuildStep(const ProjectExplorer::DeployConfiguration *dc, const ProjectExplorer::BuildStep *laterBuildStep) { diff --git a/src/plugins/remotelinux/maemoinstalltosysrootstep.cpp b/src/plugins/remotelinux/maemoinstalltosysrootstep.cpp index c4111de49d7..ed10d87c9af 100644 --- a/src/plugins/remotelinux/maemoinstalltosysrootstep.cpp +++ b/src/plugins/remotelinux/maemoinstalltosysrootstep.cpp @@ -38,6 +38,8 @@ #include "maemoqtversion.h" #include "qt4maemodeployconfiguration.h" +#include + #include #include #include @@ -323,8 +325,8 @@ void MaemoCopyToSysrootStep::run(QFutureInterface &fi) + deployable.remoteDir + sep + localFileInfo.fileName(); sysrootDir.mkpath(deployable.remoteDir.mid(1)); QString errorMsg; - MaemoGlobal::removeRecursively(targetFilePath, errorMsg); - if (!MaemoGlobal::copyRecursively(deployable.localFilePath, + Utils::FileUtils::removeRecursively(targetFilePath, &errorMsg); + if (!Utils::FileUtils::copyRecursively(deployable.localFilePath, targetFilePath, &errorMsg)) { emit addOutput(tr("Sysroot installation failed: %1\n" " Continuing anyway.").arg(errorMsg), ErrorMessageOutput); diff --git a/src/plugins/remotelinux/maemopackagecreationstep.cpp b/src/plugins/remotelinux/maemopackagecreationstep.cpp index af2518898ae..044b9b71f66 100644 --- a/src/plugins/remotelinux/maemopackagecreationstep.cpp +++ b/src/plugins/remotelinux/maemopackagecreationstep.cpp @@ -205,7 +205,7 @@ bool AbstractMaemoPackageCreationStep::packagingNeeded() const const int deployableCount = deploymentInfo->deployableCount(); for (int i = 0; i < deployableCount; ++i) { - if (MaemoGlobal::isFileNewerThan(deploymentInfo->deployableAt(i).localFilePath, + if (Utils::FileUtils::isFileNewerThan(deploymentInfo->deployableAt(i).localFilePath, packageInfo.lastModified())) return true; } @@ -427,7 +427,7 @@ bool MaemoDebianPackageCreationStep::copyDebianFiles(bool inSourceBuild) return false; } QString error; - if (!MaemoGlobal::removeRecursively(debianDirPath, error)) { + if (!Utils::FileUtils::removeRecursively(debianDirPath, &error)) { raiseError(tr("Packaging failed."), tr("Could not remove directory '%1': %2").arg(debianDirPath, error)); return false; diff --git a/src/plugins/remotelinux/maemopublisherfremantlefree.cpp b/src/plugins/remotelinux/maemopublisherfremantlefree.cpp index 36f6f28bd50..45ab8417238 100644 --- a/src/plugins/remotelinux/maemopublisherfremantlefree.cpp +++ b/src/plugins/remotelinux/maemopublisherfremantlefree.cpp @@ -127,7 +127,7 @@ void MaemoPublisherFremantleFree::createPackage() if (QFileInfo(tmpDirContainer()).exists()) { emit progressReport(tr("Removing left-over temporary directory ...")); QString error; - if (!MaemoGlobal::removeRecursively(tmpDirContainer(), error)) { + if (!Utils::FileUtils::removeRecursively(tmpDirContainer(), &error)) { finishWithFailure(tr("Error removing temporary directory: %1").arg(error), tr("Publishing failed: Could not create source package.")); return; @@ -360,7 +360,7 @@ void MaemoPublisherFremantleFree::runDpkgBuildPackage() } foreach (const QString &filePath, d.filesToExclude()) { QString error; - if (!MaemoGlobal::removeRecursively(filePath, error)) { + if (!Utils::FileUtils::removeRecursively(filePath, &error)) { finishWithFailure(error, tr("Publishing failed: Could not create package.")); } diff --git a/src/plugins/remotelinux/qt4maemotarget.cpp b/src/plugins/remotelinux/qt4maemotarget.cpp index 1a8ed325bc8..92a675c7e85 100644 --- a/src/plugins/remotelinux/qt4maemotarget.cpp +++ b/src/plugins/remotelinux/qt4maemotarget.cpp @@ -311,7 +311,7 @@ void AbstractQt4MaemoTarget::handleTargetToBeRemoved(ProjectExplorer::Target *ta const QStringList otherContents = QDir(packagingPath).entryList(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot); if (otherContents.isEmpty()) { - if (!MaemoGlobal::removeRecursively(packagingPath, error)) + if (!Utils::FileUtils::removeRecursively(packagingPath, &error)) qDebug("%s", qPrintable(error)); } } @@ -684,7 +684,7 @@ bool AbstractDebBasedQt4MaemoTarget::targetCanBeRemoved() const void AbstractDebBasedQt4MaemoTarget::removeTarget() { QString error; - if (!MaemoGlobal::removeRecursively(debianDirPath(), error)) + if (!Utils::FileUtils::removeRecursively(debianDirPath(), &error)) qDebug("%s", qPrintable(error)); } @@ -705,7 +705,7 @@ AbstractQt4MaemoTarget::ActionStatus AbstractDebBasedQt4MaemoTarget::createSpeci projectDir.path() + QLatin1Char('/') + PackagingDirName); const QString dhMakeDebianDir = projectDir.path() + QLatin1Char('/') + PackagingDirName + QLatin1String("/debian"); - MaemoGlobal::removeRecursively(dhMakeDebianDir, error); + Utils::FileUtils::removeRecursively(dhMakeDebianDir, &error); const QStringList dh_makeArgs = QStringList() << QLatin1String("dh_make") << QLatin1String("-s") << QLatin1String("-n") << QLatin1String("-p") << (defaultPackageFileName() + QLatin1Char('_') @@ -733,7 +733,7 @@ AbstractQt4MaemoTarget::ActionStatus AbstractDebBasedQt4MaemoTarget::createSpeci if (!QFile::rename(dhMakeDebianDir, debianDirPath())) { raiseError(tr("Unable to move new debian directory to '%1'.") .arg(QDir::toNativeSeparators(debianDirPath()))); - MaemoGlobal::removeRecursively(dhMakeDebianDir, error); + Utils::FileUtils::removeRecursively(dhMakeDebianDir, &error); return ActionFailed; }