Utils::FileUtils: Use Utils::FileName for file paths instead of QString.

Change-Id: I9ee4c0760820e9299e238c116936ce8ef140c727
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
Reviewed-by: Daniel Teske <daniel.teske@nokia.com>
Reviewed-by: Christian Kandeler <christian.kandeler@nokia.com>
This commit is contained in:
Christian Kandeler
2012-09-05 15:52:53 +02:00
parent b21f7ca7e5
commit 6e09fa8c70
10 changed files with 101 additions and 98 deletions

View File

@@ -138,7 +138,7 @@ bool BuildableHelperLibrary::copyFiles(const QString &sourcePath,
QString *errorMessage) QString *errorMessage)
{ {
// try remove the directory // try remove the directory
if (!FileUtils::removeRecursively(targetDirectory, errorMessage)) if (!FileUtils::removeRecursively(FileName::fromString(targetDirectory), errorMessage))
return false; return false;
if (!QDir().mkpath(targetDirectory)) { if (!QDir().mkpath(targetDirectory)) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary", "The target directory %1 could not be created.").arg(targetDirectory); *errorMessage = QCoreApplication::translate("ProjectExplorer::DebuggingHelperLibrary", "The target directory %1 could not be created.").arg(targetDirectory);

View File

@@ -59,14 +59,14 @@ namespace Utils {
\return Whether the operation succeeded. \return Whether the operation succeeded.
*/ */
bool FileUtils::removeRecursively(const QString &filePath, QString *error) bool FileUtils::removeRecursively(const FileName &filePath, QString *error)
{ {
QFileInfo fileInfo(filePath); QFileInfo fileInfo = filePath.toFileInfo();
if (!fileInfo.exists() && !fileInfo.isSymLink()) if (!fileInfo.exists() && !fileInfo.isSymLink())
return true; return true;
QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser); QFile::setPermissions(filePath.toString(), fileInfo.permissions() | QFile::WriteUser);
if (fileInfo.isDir()) { if (fileInfo.isDir()) {
QDir dir(filePath); QDir dir(filePath.toString());
dir = dir.canonicalPath(); dir = dir.canonicalPath();
if (dir.isRoot()) { if (dir.isRoot()) {
if (error) { if (error) {
@@ -86,21 +86,21 @@ bool FileUtils::removeRecursively(const QString &filePath, QString *error)
QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden
| QDir::System | QDir::Dirs | QDir::NoDotAndDotDot); | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QString &fileName, fileNames) { foreach (const QString &fileName, fileNames) {
if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error)) if (!removeRecursively(FileName(filePath).appendPath(fileName), error))
return false; return false;
} }
if (!QDir::root().rmdir(dir.path())) { if (!QDir::root().rmdir(dir.path())) {
if (error) { if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove directory '%1'.") *error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove directory '%1'.")
.arg(QDir::toNativeSeparators(filePath)); .arg(filePath.toUserOutput());
} }
return false; return false;
} }
} else { } else {
if (!QFile::remove(filePath)) { if (!QFile::remove(filePath.toString())) {
if (error) { if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove file '%1'.") *error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove file '%1'.")
.arg(QDir::toNativeSeparators(filePath)); .arg(filePath.toUserOutput());
} }
return false; return false;
} }
@@ -125,36 +125,36 @@ bool FileUtils::removeRecursively(const QString &filePath, QString *error)
\return Whether the operation succeeded. \return Whether the operation succeeded.
*/ */
bool FileUtils::copyRecursively(const QString &srcFilePath, bool FileUtils::copyRecursively(const FileName &srcFilePath, const FileName &tgtFilePath,
const QString &tgtFilePath, QString *error) QString *error)
{ {
QFileInfo srcFileInfo(srcFilePath); QFileInfo srcFileInfo = srcFilePath.toFileInfo();
if (srcFileInfo.isDir()) { if (srcFileInfo.isDir()) {
QDir targetDir(tgtFilePath); QDir targetDir(tgtFilePath.toString());
targetDir.cdUp(); targetDir.cdUp();
if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) { if (!targetDir.mkdir(tgtFilePath.toFileInfo().fileName())) {
if (error) { if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to create directory '%1'.") *error = QCoreApplication::translate("Utils::FileUtils", "Failed to create directory '%1'.")
.arg(QDir::toNativeSeparators(tgtFilePath)); .arg(tgtFilePath.toUserOutput());
return false; return false;
} }
} }
QDir sourceDir(srcFilePath); QDir sourceDir(srcFilePath.toString());
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System); QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot
| QDir::Hidden | QDir::System);
foreach (const QString &fileName, fileNames) { foreach (const QString &fileName, fileNames) {
const QString newSrcFilePath FileName newSrcFilePath = srcFilePath;
= srcFilePath + QLatin1Char('/') + fileName; newSrcFilePath.appendPath(fileName);
const QString newTgtFilePath FileName newTgtFilePath = tgtFilePath;
= tgtFilePath + QLatin1Char('/') + fileName; newTgtFilePath.appendPath(fileName);
if (!copyRecursively(newSrcFilePath, newTgtFilePath, error)) if (!copyRecursively(newSrcFilePath, newTgtFilePath, error))
return false; return false;
} }
} else { } else {
if (!QFile::copy(srcFilePath, tgtFilePath)) { if (!QFile::copy(srcFilePath.toString(), tgtFilePath.toString())) {
if (error) { if (error) {
*error = QCoreApplication::translate("Utils::FileUtils", "Could not copy file '%1' to '%2'.") *error = QCoreApplication::translate("Utils::FileUtils", "Could not copy file '%1' to '%2'.")
.arg(QDir::toNativeSeparators(srcFilePath), .arg(srcFilePath.toUserOutput(), tgtFilePath.toUserOutput());
QDir::toNativeSeparators(tgtFilePath));
} }
return false; return false;
} }
@@ -169,19 +169,16 @@ bool FileUtils::copyRecursively(const QString &srcFilePath,
\return Whether at least one file in \a filePath has a newer date than \a timeStamp. \return Whether at least one file in \a filePath has a newer date than \a timeStamp.
*/ */
bool FileUtils::isFileNewerThan(const QString &filePath, bool FileUtils::isFileNewerThan(const FileName &filePath, const QDateTime &timeStamp)
const QDateTime &timeStamp)
{ {
QFileInfo fileInfo(filePath); QFileInfo fileInfo = filePath.toFileInfo();
if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp) if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp)
return true; return true;
if (fileInfo.isDir()) { if (fileInfo.isDir()) {
const QStringList dirContents = QDir(filePath) const QStringList dirContents = QDir(filePath.toString())
.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot); .entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QString &curFileName, dirContents) { foreach (const QString &curFileName, dirContents) {
const QString curFilePath if (isFileNewerThan(FileName(filePath).appendPath(curFileName), timeStamp))
= filePath + QLatin1Char('/') + curFileName;
if (isFileNewerThan(curFilePath, timeStamp))
return true; return true;
} }
} }
@@ -197,15 +194,15 @@ bool FileUtils::isFileNewerThan(const QString &filePath,
return Symlink target file path. return Symlink target file path.
*/ */
QString FileUtils::resolveSymlinks(const QString &path) FileName FileUtils::resolveSymlinks(const FileName &path)
{ {
QFileInfo f(path); QFileInfo f = path.toFileInfo();
int links = 16; int links = 16;
while (links-- && f.isSymLink()) while (links-- && f.isSymLink())
f.setFile(f.symLinkTarget()); f.setFile(f.symLinkTarget());
if (links <= 0) if (links <= 0)
return QString(); return FileName();
return f.filePath(); return FileName::fromString(f.filePath());
} }
QByteArray FileReader::fetchQrc(const QString &fileName) QByteArray FileReader::fetchQrc(const QString &fileName)

View File

@@ -50,14 +50,52 @@ QT_END_NAMESPACE
namespace Utils { namespace Utils {
class QTCREATOR_UTILS_EXPORT FileName : private QString
{
public:
FileName();
explicit FileName(const QFileInfo &info);
QFileInfo toFileInfo() const;
static FileName fromString(const QString &filename);
static FileName fromUserInput(const QString &filename);
QString toString() const;
QString toUserOutput() const;
FileName parentDir() const;
bool operator==(const FileName &other) const;
bool operator!=(const FileName &other) const;
bool operator<(const FileName &other) const;
bool operator<=(const FileName &other) const;
bool operator>(const FileName &other) const;
bool operator>=(const FileName &other) const;
bool isChildOf(const FileName &s) const;
bool isChildOf(const QDir &dir) const;
bool endsWith(const QString &s) const;
Utils::FileName relativeChildPath(const FileName &parent) const;
Utils::FileName &appendPath(const QString &s);
Utils::FileName &append(const QString &str);
Utils::FileName &append(QChar str);
using QString::size;
using QString::count;
using QString::length;
using QString::isEmpty;
using QString::isNull;
using QString::clear;
private:
FileName(const QString &string);
};
class QTCREATOR_UTILS_EXPORT FileUtils { class QTCREATOR_UTILS_EXPORT FileUtils {
public: public:
static bool removeRecursively(const QString &filePath, QString *error = 0); static bool removeRecursively(const FileName &filePath, QString *error = 0);
static bool copyRecursively(const QString &srcFilePath, static bool copyRecursively(const FileName &srcFilePath, const FileName &tgtFilePath,
const QString &tgtFilePath, QString *error = 0); QString *error = 0);
static bool isFileNewerThan(const QString &filePath, static bool isFileNewerThan(const FileName &filePath, const QDateTime &timeStamp);
const QDateTime &timeStamp); static FileName resolveSymlinks(const FileName &path);
static QString resolveSymlinks(const QString &path);
}; };
class QTCREATOR_UTILS_EXPORT FileReader class QTCREATOR_UTILS_EXPORT FileReader
@@ -139,45 +177,6 @@ private:
bool m_autoRemove; bool m_autoRemove;
}; };
class QTCREATOR_UTILS_EXPORT FileName : private QString
{
public:
FileName();
explicit FileName(const QFileInfo &info);
QFileInfo toFileInfo() const;
static FileName fromString(const QString &filename);
static FileName fromUserInput(const QString &filename);
QString toString() const;
QString toUserOutput() const;
FileName parentDir() const;
bool operator==(const FileName &other) const;
bool operator!=(const FileName &other) const;
bool operator<(const FileName &other) const;
bool operator<=(const FileName &other) const;
bool operator>(const FileName &other) const;
bool operator>=(const FileName &other) const;
bool isChildOf(const FileName &s) const;
bool isChildOf(const QDir &dir) const;
bool endsWith(const QString &s) const;
Utils::FileName relativeChildPath(const FileName &parent) const;
Utils::FileName &appendPath(const QString &s);
Utils::FileName &append(const QString &str);
Utils::FileName &append(QChar str);
using QString::size;
using QString::count;
using QString::length;
using QString::isEmpty;
using QString::isNull;
using QString::clear;
private:
FileName(const QString &string);
};
} // namespace Utils } // namespace Utils
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE

View File

@@ -100,7 +100,8 @@ bool SaveFile::commit()
return false; return false;
} }
QString finalFileName = Utils::FileUtils::resolveSymlinks(m_finalFileName); QString finalFileName
= FileUtils::resolveSymlinks(FileName::fromString(m_finalFileName)).toString();
QString bakname = finalFileName + QLatin1Char('~'); QString bakname = finalFileName + QLatin1Char('~');
QFile::remove(bakname); // Kill old backup QFile::remove(bakname); // Kill old backup
QFile::rename(finalFileName, bakname); // Backup current file QFile::rename(finalFileName, bakname); // Backup current file

View File

@@ -636,7 +636,7 @@ DebianManager::ActionStatus DebianManager::createTemplate(Qt4ProjectManager::Qt4
if (!QFile::rename(location.appendPath(QLatin1String("debian")).toString(), debianDir.toString())) { if (!QFile::rename(location.appendPath(QLatin1String("debian")).toString(), debianDir.toString())) {
raiseError(tr("Unable to move new debian directory to '%1'.").arg(debianDir.toUserOutput())); raiseError(tr("Unable to move new debian directory to '%1'.").arg(debianDir.toUserOutput()));
Utils::FileUtils::removeRecursively(location.toString(), &error); Utils::FileUtils::removeRecursively(location, &error);
return ActionFailed; return ActionFailed;
} }

View File

@@ -299,9 +299,9 @@ void MaemoCopyToSysrootStep::run(QFutureInterface<bool> &fi)
+ deployable.remoteDirectory() + sep + localFileInfo.fileName(); + deployable.remoteDirectory() + sep + localFileInfo.fileName();
sysrootDir.mkpath(deployable.remoteDirectory().mid(1)); sysrootDir.mkpath(deployable.remoteDirectory().mid(1));
QString errorMsg; QString errorMsg;
Utils::FileUtils::removeRecursively(targetFilePath, &errorMsg); Utils::FileUtils::removeRecursively(Utils::FileName::fromString(targetFilePath), &errorMsg);
if (!Utils::FileUtils::copyRecursively(deployable.localFilePath().toString(), if (!Utils::FileUtils::copyRecursively(deployable.localFilePath(),
targetFilePath, &errorMsg)) { Utils::FileName::fromString(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

@@ -372,7 +372,7 @@ bool MaemoDebianPackageCreationStep::copyDebianFiles(bool inSourceBuild)
return false; return false;
} }
QString error; QString error;
if (!FileUtils::removeRecursively(debianDirPath, &error)) { if (!FileUtils::removeRecursively(FileName::fromString(debianDirPath), &error)) {
raiseError(tr("Packaging failed: Could not remove directory '%1': %2") raiseError(tr("Packaging failed: Could not remove directory '%1': %2")
.arg(debianDirPath, error)); .arg(debianDirPath, error));
return false; return false;

View File

@@ -58,6 +58,7 @@ using namespace Core;
using namespace Qt4ProjectManager; using namespace Qt4ProjectManager;
using namespace RemoteLinux; using namespace RemoteLinux;
using namespace QSsh; using namespace QSsh;
using namespace Utils;
namespace Madde { namespace Madde {
namespace Internal { namespace Internal {
@@ -127,7 +128,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 (!Utils::FileUtils::removeRecursively(tmpDirContainer(), &error)) { if (!FileUtils::removeRecursively(FileName::fromString(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;
@@ -201,7 +202,7 @@ bool MaemoPublisherFremantleFree::copyRecursively(const QString &srcFilePath,
} }
} else { } else {
if (tgtFilePath == m_tmpProjectDir + QLatin1String("/debian/rules")) { if (tgtFilePath == m_tmpProjectDir + QLatin1String("/debian/rules")) {
Utils::FileReader reader; FileReader reader;
if (!reader.fetch(srcFilePath)) { if (!reader.fetch(srcFilePath)) {
emit progressReport(reader.errorString(), ErrorOutput); emit progressReport(reader.errorString(), ErrorOutput);
return false; return false;
@@ -211,7 +212,7 @@ bool MaemoPublisherFremantleFree::copyRecursively(const QString &srcFilePath,
rulesContents.replace("# Add here commands to configure the package.", rulesContents.replace("# Add here commands to configure the package.",
"qmake " + QFileInfo(m_project->document()->fileName()).fileName().toLocal8Bit()); "qmake " + QFileInfo(m_project->document()->fileName()).fileName().toLocal8Bit());
MaemoDebianPackageCreationStep::ensureShlibdeps(rulesContents); MaemoDebianPackageCreationStep::ensureShlibdeps(rulesContents);
Utils::FileSaver saver(tgtFilePath); FileSaver saver(tgtFilePath);
saver.write(rulesContents); saver.write(rulesContents);
if (!saver.finalize()) { if (!saver.finalize()) {
emit progressReport(saver.errorString(), ErrorOutput); emit progressReport(saver.errorString(), ErrorOutput);
@@ -243,7 +244,7 @@ bool MaemoPublisherFremantleFree::fixNewlines()
const QStringList &fileNames = debianDir.entryList(QDir::Files); const QStringList &fileNames = debianDir.entryList(QDir::Files);
foreach (const QString &fileName, fileNames) { foreach (const QString &fileName, fileNames) {
QString filePath = debianDir.filePath(fileName); QString filePath = debianDir.filePath(fileName);
Utils::FileReader reader; FileReader reader;
if (!reader.fetch(filePath)) if (!reader.fetch(filePath))
return false; return false;
QByteArray contents = reader.data(); QByteArray contents = reader.data();
@@ -251,7 +252,7 @@ bool MaemoPublisherFremantleFree::fixNewlines()
if (!contents.contains(crlf)) if (!contents.contains(crlf))
continue; continue;
contents.replace(crlf, "\n"); contents.replace(crlf, "\n");
Utils::FileSaver saver(filePath); FileSaver saver(filePath);
saver.write(contents); saver.write(contents);
if (!saver.finalize()) if (!saver.finalize())
return false; return false;
@@ -365,7 +366,7 @@ void MaemoPublisherFremantleFree::runDpkgBuildPackage()
} }
foreach (const QString &filePath, d.filesToExclude()) { foreach (const QString &filePath, d.filesToExclude()) {
QString error; QString error;
if (!Utils::FileUtils::removeRecursively(filePath, &error)) { if (!FileUtils::removeRecursively(FileName::fromString(filePath), &error)) {
finishWithFailure(error, finishWithFailure(error,
tr("Publishing failed: Could not create package.")); tr("Publishing failed: Could not create package."));
} }

View File

@@ -72,6 +72,8 @@
#include <QDeclarativeContext> #include <QDeclarativeContext>
#include <QDesktopServices> #include <QDesktopServices>
using namespace Utils;
namespace QtSupport { namespace QtSupport {
namespace Internal { namespace Internal {
@@ -337,9 +339,9 @@ QString ExamplesWelcomePage::copyToAlternativeLocation(const QFileInfo& proFileI
.arg(nativeProjectDir)); .arg(nativeProjectDir));
lay->addWidget(descrLbl, 0, 0, 1, 2); lay->addWidget(descrLbl, 0, 0, 1, 2);
QLabel *txt = new QLabel(tr("&Location:")); QLabel *txt = new QLabel(tr("&Location:"));
Utils::PathChooser *chooser = new Utils::PathChooser; PathChooser *chooser = new PathChooser;
txt->setBuddy(chooser); txt->setBuddy(chooser);
chooser->setExpectedKind(Utils::PathChooser::ExistingDirectory); chooser->setExpectedKind(PathChooser::ExistingDirectory);
QSettings *settings = Core::ICore::settings(); QSettings *settings = Core::ICore::settings();
chooser->setPath(settings->value(QString::fromLatin1(C_FALLBACK_ROOT), chooser->setPath(settings->value(QString::fromLatin1(C_FALLBACK_ROOT),
Core::DocumentManager::projectsDirectory()).toString()); Core::DocumentManager::projectsDirectory()).toString());
@@ -368,15 +370,18 @@ QString ExamplesWelcomePage::copyToAlternativeLocation(const QFileInfo& proFileI
} else { } else {
QString error; QString error;
QString targetDir = destBaseDir + QLatin1Char('/') + exampleDirName; QString targetDir = destBaseDir + QLatin1Char('/') + exampleDirName;
if (Utils::FileUtils::copyRecursively(projectDir, targetDir, &error)) { if (FileUtils::copyRecursively(FileName::fromString(projectDir),
FileName::fromString(targetDir), &error)) {
// set vars to new location // set vars to new location
const QStringList::Iterator end = filesToOpen.end(); const QStringList::Iterator end = filesToOpen.end();
for (QStringList::Iterator it = filesToOpen.begin(); it != end; ++it) for (QStringList::Iterator it = filesToOpen.begin(); it != end; ++it)
it->replace(projectDir, targetDir); it->replace(projectDir, targetDir);
foreach (const QString &dependency, dependencies) { foreach (const QString &dependency, dependencies) {
QString dirName = QDir(dependency).dirName(); FileName targetFile = FileName::fromString(targetDir);
if (!Utils::FileUtils::copyRecursively(dependency, targetDir + QDir::separator()+ dirName, &error)) { targetFile.appendPath(QDir(dependency).dirName());
if (!FileUtils::copyRecursively(FileName::fromString(dependency), targetFile,
&error)) {
QMessageBox::warning(Core::ICore::mainWindow(), tr("Cannot Copy Project"), error); QMessageBox::warning(Core::ICore::mainWindow(), tr("Cannot Copy Project"), error);
// do not fail, just warn; // do not fail, just warn;
} }

View File

@@ -137,7 +137,7 @@ bool AbstractPackagingStep::isPackagingNeeded() const
const DeploymentData &dd = target()->deploymentData(); const DeploymentData &dd = target()->deploymentData();
for (int i = 0; i < dd.fileCount(); ++i) { for (int i = 0; i < dd.fileCount(); ++i) {
if (Utils::FileUtils::isFileNewerThan(dd.fileAt(i).localFilePath().toString(), if (Utils::FileUtils::isFileNewerThan(dd.fileAt(i).localFilePath(),
packageInfo.lastModified())) { packageInfo.lastModified())) {
return true; return true;
} }