Commit 15732d1b authored by Daniel Molkentin's avatar Daniel Molkentin

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/1090Reviewed-by: default avatarQt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarChristian Kandeler <christian.kandeler@nokia.com>
parent 1ea75f0c
......@@ -38,12 +38,152 @@
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QTemporaryFile>
#include <QtCore/QDateTime>
#include <QtCore/QDataStream>
#include <QtCore/QTextStream>
#include <QtGui/QMessageBox>
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())
......
......@@ -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!
......
......@@ -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;
......
......@@ -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<class T> static T *earlierBuildStep(const ProjectExplorer::DeployConfiguration *dc,
const ProjectExplorer::BuildStep *laterBuildStep)
{
......
......@@ -38,6 +38,8 @@
#include "maemoqtversion.h"
#include "qt4maemodeployconfiguration.h"
#include <utils/fileutils.h>
#include <qt4projectmanager/qt4buildconfiguration.h>
#include <qt4projectmanager/qt4target.h>
#include <qtsupport/baseqtversion.h>
......@@ -323,8 +325,8 @@ void MaemoCopyToSysrootStep::run(QFutureInterface<bool> &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);
......
......@@ -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;
......
......@@ -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."));
}
......
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment