RemoteLinux: Move generic Dir/File functions into Utils.

- Make error parameter an optional pointer
  for consistency with DirUtils::copyRecursively()
- Add documentation

Change-Id: I6671142341dbdcf2c8ca1118c35ea05548920609
Reviewed-on: http://codereview.qt.nokia.com/1090
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Christian Kandeler <christian.kandeler@nokia.com>
This commit is contained in:
Daniel Molkentin
2011-07-05 12:59:14 +02:00
parent 1ea75f0cbf
commit 15732d1bd2
8 changed files with 162 additions and 104 deletions

View File

@@ -38,12 +38,152 @@
#include <QtCore/QDir> #include <QtCore/QDir>
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QTemporaryFile> #include <QtCore/QTemporaryFile>
#include <QtCore/QDateTime>
#include <QtCore/QDataStream> #include <QtCore/QDataStream>
#include <QtCore/QTextStream> #include <QtCore/QTextStream>
#include <QtGui/QMessageBox> #include <QtGui/QMessageBox>
namespace Utils { 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) QByteArray FileReader::fetchQrc(const QString &fileName)
{ {
QTC_ASSERT(fileName.startsWith(QLatin1Char(':')), return QByteArray()) QTC_ASSERT(fileName.startsWith(QLatin1Char(':')), return QByteArray())

View File

@@ -45,10 +45,20 @@ class QTemporaryFile;
class QWidget; class QWidget;
class QTextStream; class QTextStream;
class QDataStream; class QDataStream;
class QDateTime;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { 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 class QTCREATOR_UTILS_EXPORT FileReader
{ {
Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic! Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic!

View File

@@ -286,93 +286,6 @@ QString MaemoGlobal::architecture(const QString &qmakePath)
return arch; 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) void MaemoGlobal::addMaddeEnvironment(Utils::Environment &env, const QString &qmakePath)
{ {
Utils::Environment maddeEnv; Utils::Environment maddeEnv;

View File

@@ -137,13 +137,6 @@ public:
static PackagingSystem packagingSystem(const QString &osType); 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<class T> static T *earlierBuildStep(const ProjectExplorer::DeployConfiguration *dc, template<class T> static T *earlierBuildStep(const ProjectExplorer::DeployConfiguration *dc,
const ProjectExplorer::BuildStep *laterBuildStep) const ProjectExplorer::BuildStep *laterBuildStep)
{ {

View File

@@ -38,6 +38,8 @@
#include "maemoqtversion.h" #include "maemoqtversion.h"
#include "qt4maemodeployconfiguration.h" #include "qt4maemodeployconfiguration.h"
#include <utils/fileutils.h>
#include <qt4projectmanager/qt4buildconfiguration.h> #include <qt4projectmanager/qt4buildconfiguration.h>
#include <qt4projectmanager/qt4target.h> #include <qt4projectmanager/qt4target.h>
#include <qtsupport/baseqtversion.h> #include <qtsupport/baseqtversion.h>
@@ -323,8 +325,8 @@ void MaemoCopyToSysrootStep::run(QFutureInterface<bool> &fi)
+ deployable.remoteDir + sep + localFileInfo.fileName(); + deployable.remoteDir + sep + localFileInfo.fileName();
sysrootDir.mkpath(deployable.remoteDir.mid(1)); sysrootDir.mkpath(deployable.remoteDir.mid(1));
QString errorMsg; QString errorMsg;
MaemoGlobal::removeRecursively(targetFilePath, errorMsg); Utils::FileUtils::removeRecursively(targetFilePath, &errorMsg);
if (!MaemoGlobal::copyRecursively(deployable.localFilePath, if (!Utils::FileUtils::copyRecursively(deployable.localFilePath,
targetFilePath, &errorMsg)) { targetFilePath, &errorMsg)) {
emit addOutput(tr("Sysroot installation failed: %1\n" emit addOutput(tr("Sysroot installation failed: %1\n"
" Continuing anyway.").arg(errorMsg), ErrorMessageOutput); " Continuing anyway.").arg(errorMsg), ErrorMessageOutput);

View File

@@ -205,7 +205,7 @@ bool AbstractMaemoPackageCreationStep::packagingNeeded() const
const int deployableCount = deploymentInfo->deployableCount(); const int deployableCount = deploymentInfo->deployableCount();
for (int i = 0; i < deployableCount; ++i) { for (int i = 0; i < deployableCount; ++i) {
if (MaemoGlobal::isFileNewerThan(deploymentInfo->deployableAt(i).localFilePath, if (Utils::FileUtils::isFileNewerThan(deploymentInfo->deployableAt(i).localFilePath,
packageInfo.lastModified())) packageInfo.lastModified()))
return true; return true;
} }
@@ -427,7 +427,7 @@ bool MaemoDebianPackageCreationStep::copyDebianFiles(bool inSourceBuild)
return false; return false;
} }
QString error; QString error;
if (!MaemoGlobal::removeRecursively(debianDirPath, error)) { if (!Utils::FileUtils::removeRecursively(debianDirPath, &error)) {
raiseError(tr("Packaging failed."), raiseError(tr("Packaging failed."),
tr("Could not remove directory '%1': %2").arg(debianDirPath, error)); tr("Could not remove directory '%1': %2").arg(debianDirPath, error));
return false; return false;

View File

@@ -127,7 +127,7 @@ void MaemoPublisherFremantleFree::createPackage()
if (QFileInfo(tmpDirContainer()).exists()) { if (QFileInfo(tmpDirContainer()).exists()) {
emit progressReport(tr("Removing left-over temporary directory ...")); emit progressReport(tr("Removing left-over temporary directory ..."));
QString error; QString error;
if (!MaemoGlobal::removeRecursively(tmpDirContainer(), error)) { if (!Utils::FileUtils::removeRecursively(tmpDirContainer(), &error)) {
finishWithFailure(tr("Error removing temporary directory: %1").arg(error), finishWithFailure(tr("Error removing temporary directory: %1").arg(error),
tr("Publishing failed: Could not create source package.")); tr("Publishing failed: Could not create source package."));
return; return;
@@ -360,7 +360,7 @@ void MaemoPublisherFremantleFree::runDpkgBuildPackage()
} }
foreach (const QString &filePath, d.filesToExclude()) { foreach (const QString &filePath, d.filesToExclude()) {
QString error; QString error;
if (!MaemoGlobal::removeRecursively(filePath, error)) { if (!Utils::FileUtils::removeRecursively(filePath, &error)) {
finishWithFailure(error, finishWithFailure(error,
tr("Publishing failed: Could not create package.")); tr("Publishing failed: Could not create package."));
} }

View File

@@ -311,7 +311,7 @@ void AbstractQt4MaemoTarget::handleTargetToBeRemoved(ProjectExplorer::Target *ta
const QStringList otherContents = QDir(packagingPath).entryList(QDir::Dirs const QStringList otherContents = QDir(packagingPath).entryList(QDir::Dirs
| QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot); | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot);
if (otherContents.isEmpty()) { if (otherContents.isEmpty()) {
if (!MaemoGlobal::removeRecursively(packagingPath, error)) if (!Utils::FileUtils::removeRecursively(packagingPath, &error))
qDebug("%s", qPrintable(error)); qDebug("%s", qPrintable(error));
} }
} }
@@ -684,7 +684,7 @@ bool AbstractDebBasedQt4MaemoTarget::targetCanBeRemoved() const
void AbstractDebBasedQt4MaemoTarget::removeTarget() void AbstractDebBasedQt4MaemoTarget::removeTarget()
{ {
QString error; QString error;
if (!MaemoGlobal::removeRecursively(debianDirPath(), error)) if (!Utils::FileUtils::removeRecursively(debianDirPath(), &error))
qDebug("%s", qPrintable(error)); qDebug("%s", qPrintable(error));
} }
@@ -705,7 +705,7 @@ AbstractQt4MaemoTarget::ActionStatus AbstractDebBasedQt4MaemoTarget::createSpeci
projectDir.path() + QLatin1Char('/') + PackagingDirName); projectDir.path() + QLatin1Char('/') + PackagingDirName);
const QString dhMakeDebianDir = projectDir.path() + QLatin1Char('/') const QString dhMakeDebianDir = projectDir.path() + QLatin1Char('/')
+ PackagingDirName + QLatin1String("/debian"); + PackagingDirName + QLatin1String("/debian");
MaemoGlobal::removeRecursively(dhMakeDebianDir, error); Utils::FileUtils::removeRecursively(dhMakeDebianDir, &error);
const QStringList dh_makeArgs = QStringList() << QLatin1String("dh_make") const QStringList dh_makeArgs = QStringList() << QLatin1String("dh_make")
<< QLatin1String("-s") << QLatin1String("-n") << QLatin1String("-p") << QLatin1String("-s") << QLatin1String("-n") << QLatin1String("-p")
<< (defaultPackageFileName() + QLatin1Char('_') << (defaultPackageFileName() + QLatin1Char('_')
@@ -733,7 +733,7 @@ AbstractQt4MaemoTarget::ActionStatus AbstractDebBasedQt4MaemoTarget::createSpeci
if (!QFile::rename(dhMakeDebianDir, debianDirPath())) { if (!QFile::rename(dhMakeDebianDir, debianDirPath())) {
raiseError(tr("Unable to move new debian directory to '%1'.") raiseError(tr("Unable to move new debian directory to '%1'.")
.arg(QDir::toNativeSeparators(debianDirPath()))); .arg(QDir::toNativeSeparators(debianDirPath())));
MaemoGlobal::removeRecursively(dhMakeDebianDir, error); Utils::FileUtils::removeRecursively(dhMakeDebianDir, &error);
return ActionFailed; return ActionFailed;
} }