OSDN Git Service

replace in-place-rewriting of files with a read/modify/write cycle
authorOswald Buddenhagen <oswald.buddenhagen@nokia.com>
Wed, 30 Mar 2011 13:18:52 +0000 (15:18 +0200)
committerOswald Buddenhagen <oswald.buddenhagen@nokia.com>
Mon, 18 Apr 2011 12:10:14 +0000 (14:10 +0200)
easier to handle errors that way

src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.cpp
src/plugins/qt4projectmanager/qt-maemo/maemopackagecreationstep.h
src/plugins/qt4projectmanager/qt-maemo/maemopublisherfremantlefree.cpp
src/plugins/qt4projectmanager/qt-maemo/qt4maemotarget.cpp

index 6e3370b..77d7156 100644 (file)
@@ -44,6 +44,7 @@
 #include <qt4project.h>
 #include <qt4target.h>
 #include <utils/environment.h>
+#include <utils/fileutils.h>
 
 #include <QtCore/QDateTime>
 #include <QtCore/QProcess>
@@ -440,15 +441,15 @@ bool MaemoDebianPackageCreationStep::copyDebianFiles(bool inSourceBuild)
                 = templatesDirPath + QLatin1Char('/') + fileName;
         const QString destFile
                 = debianDirPath + QLatin1Char('/') + fileName;
-        if (!QFile::copy(srcFile, destFile)) {
+        if (fileName == QLatin1String("rules")) {
+            if (!adaptRulesFile(srcFile, destFile))
+                return false;
+        } else if (!QFile::copy(srcFile, destFile)) {
             raiseError(tr("Could not copy file '%1' to '%2'")
                        .arg(QDir::toNativeSeparators(srcFile),
                             QDir::toNativeSeparators(destFile)));
             return false;
         }
-
-        if (fileName == QLatin1String("rules"))
-            adaptRulesFile(destFile);
     }
 
     QFile magicFile(magicFilePath);
@@ -484,22 +485,22 @@ void MaemoDebianPackageCreationStep::ensureShlibdeps(QByteArray &rulesContent)
     rulesContent = contentAsString.toLocal8Bit();
 }
 
-void MaemoDebianPackageCreationStep::adaptRulesFile(const QString &rulesFilePath)
+bool MaemoDebianPackageCreationStep::adaptRulesFile(
+    const QString &templatePath, const QString &rulesFilePath)
 {
-    QFile rulesFile(rulesFilePath);
-    rulesFile.setPermissions(rulesFile.permissions() | QFile::ExeUser);
-    if (!rulesFile.open(QIODevice::ReadWrite)) {
-        qWarning("Cannot open rules file for Maemo6 icon path adaptation.");
-        return;
+    Utils::FileReader reader;
+    if (!reader.fetch(templatePath)) {
+        raiseError(reader.errorString());
+        return false;
     }
-    QByteArray content = rulesFile.readAll();
+    QByteArray content = reader.data();
     const int makeInstallLine = content.indexOf("\t$(MAKE) INSTALL_ROOT");
     if (makeInstallLine == -1)
-        return;
+        return true;
     const int makeInstallEol = content.indexOf('\n', makeInstallLine);
     if (makeInstallEol == -1)
-        return;
-    QString desktopFileDir = QFileInfo(rulesFile).dir().path()
+        return true;
+    QString desktopFileDir = QFileInfo(rulesFilePath).path()
         + QLatin1Char('/') + maemoTarget()->packageName()
         + QLatin1String("/usr/share/applications/");
     const Qt4BuildConfiguration * const bc = qt4BuildConfiguration();
@@ -539,8 +540,15 @@ void MaemoDebianPackageCreationStep::adaptRulesFile(const QString &rulesFilePath
     if (!(bc->qmakeBuildConfiguration() & QtVersion::DebugBuild))
         ensureShlibdeps(content);
 
-    rulesFile.resize(0);
-    rulesFile.write(content);
+    Utils::FileSaver saver(rulesFilePath);
+    saver.write(content);
+    if (!saver.finalize()) {
+        raiseError(saver.errorString());
+        return false;
+    }
+    QFile rulesFile(rulesFilePath);
+    rulesFile.setPermissions(rulesFile.permissions() | QFile::ExeUser);
+    return true;
 }
 
 void MaemoDebianPackageCreationStep::addWorkaroundForHarmattanBug(QByteArray &rulesFileContent,
index d630a23..f4fce3e 100644 (file)
@@ -137,7 +137,7 @@ private:
         int &insertPos, const MaemoDeployableListModel *model,
         const QString &desktopFileDir);
     void checkProjectName();
-    void adaptRulesFile(const QString &rulesFilePath);
+    bool adaptRulesFile(const QString &templatePath, const QString &rulesFilePath);
 
     static const QString CreatePackageId;
 };
index 20e10aa..ea83e5d 100644 (file)
@@ -43,6 +43,7 @@
 #include <projectexplorer/target.h>
 #include <qt4projectmanager/qmakestep.h>
 #include <qt4projectmanager/qt4buildconfiguration.h>
+#include <utils/fileutils.h>
 
 #include <QtCore/QCoreApplication>
 #include <QtCore/QDir>
@@ -203,28 +204,32 @@ bool MaemoPublisherFremantleFree::copyRecursively(const QString &srcFilePath,
                 return false;
         }
     } else {
-        if (!QFile::copy(srcFilePath, tgtFilePath)) {
-            emit progressReport(tr("Could not copy file '%1' to '%2'.")
-                .arg(QDir::toNativeSeparators(srcFilePath),
-                     QDir::toNativeSeparators(tgtFilePath)));
-            return false;
-        }
-        QCoreApplication::processEvents();
-
         if (tgtFilePath == m_tmpProjectDir + QLatin1String("/debian/rules")) {
-            QFile rulesFile(tgtFilePath);
-            if (!rulesFile.open(QIODevice::ReadWrite)) {
-                emit progressReport(tr("Error: Cannot open file '%1'.")
-                    .arg(QDir::toNativeSeparators(tgtFilePath)));
+            Utils::FileReader reader;
+            if (!reader.fetch(srcFilePath)) {
+                emit progressReport(reader.errorString(), ErrorOutput);
                 return false;
             }
-            QByteArray rulesContents = rulesFile.readAll();
+            QByteArray rulesContents = reader.data();
             rulesContents.replace("$(MAKE) clean", "# $(MAKE) clean");
             rulesContents.replace("# Add here commands to configure the package.",
                 "qmake " + QFileInfo(m_project->file()->fileName()).fileName().toLocal8Bit());
             MaemoDebianPackageCreationStep::ensureShlibdeps(rulesContents);
-            rulesFile.resize(0);
-            rulesFile.write(rulesContents);
+            Utils::FileSaver saver(tgtFilePath);
+            saver.write(rulesContents);
+            if (!saver.finalize()) {
+                emit progressReport(saver.errorString(), ErrorOutput);
+                return false;
+            }
+        } else {
+            QFile srcFile(srcFilePath);
+            if (!srcFile.copy(tgtFilePath)) {
+                emit progressReport(tr("Could not copy file '%1' to '%2': %3.")
+                    .arg(QDir::toNativeSeparators(srcFilePath),
+                         QDir::toNativeSeparators(tgtFilePath),
+                         srcFile.errorString()));
+                return false;
+            }
         }
     }
     return true;
@@ -235,16 +240,19 @@ bool MaemoPublisherFremantleFree::fixNewlines()
     QDir debianDir(m_tmpProjectDir + QLatin1String("/debian"));
     const QStringList &fileNames = debianDir.entryList(QDir::Files);
     foreach (const QString &fileName, fileNames) {
-        QFile file(debianDir.filePath(fileName));
-        if (!file.open(QIODevice::ReadWrite))
+        QString filePath = debianDir.filePath(fileName);
+        Utils::FileReader reader;
+        if (!reader.fetch(filePath))
             return false;
-        QByteArray contents = file.readAll();
+        QByteArray contents = reader.data();
         const QByteArray crlf("\r\n");
         if (!contents.contains(crlf))
             continue;
         contents.replace(crlf, "\n");
-        file.resize(0);
-        file.write(contents);
+        Utils::FileSaver saver(filePath);
+        saver.write(contents);
+        if (!saver.finalize())
+            return false;
     }
     return true;
 }
@@ -529,27 +537,25 @@ bool MaemoPublisherFremantleFree::updateDesktopFiles(QString *error) const
         if (desktopFilePath.isEmpty())
             continue;
         desktopFilePath.replace(model->projectDir(), m_tmpProjectDir);
-        QFile desktopFile(desktopFilePath);
         const QString executableFilePath = model->remoteExecutableFilePath();
         if (executableFilePath.isEmpty()) {
             qDebug("%s: Skipping subproject %s with missing deployment information.",
                 Q_FUNC_INFO, qPrintable(model->proFilePath()));
             continue;
         }
-        if (!desktopFile.exists() || !desktopFile.open(QIODevice::ReadWrite)) {
+        Utils::FileReader reader;
+        if (!reader.fetch(desktopFilePath, error)) {
             success = false;
-            if (error) {
-                *error = tr("Failed to adapt desktop file '%1'.")
-                    .arg(desktopFilePath);
-            }
             continue;
         }
-        QByteArray desktopFileContents = desktopFile.readAll();
+        QByteArray desktopFileContents = reader.data();
         bool fileNeedsUpdate = addOrReplaceDesktopFileValue(desktopFileContents,
             "Exec", executableFilePath.toUtf8());
         if (fileNeedsUpdate) {
-            desktopFile.resize(0);
-            desktopFile.write(desktopFileContents);
+            Utils::FileSaver saver(desktopFilePath);
+            saver.write(desktopFileContents);
+            if (!saver.finalize(error))
+                success = false;
         }
     }
     return success;
index e00af55..554ffdd 100644 (file)
@@ -50,6 +50,7 @@
 #include <utils/fileutils.h>
 
 #include <utils/filesystemwatcher.h>
+#include <utils/fileutils.h>
 
 #include <QtGui/QApplication>
 #include <QtGui/QMainWindow>
@@ -411,27 +412,16 @@ bool AbstractDebBasedQt4MaemoTarget::setProjectVersionInternal(const QString &ve
     QString *error)
 {
     const QString filePath = changeLogFilePath();
-    MaemoGlobal::FileUpdate update(filePath);
-    QSharedPointer<QFile> changeLog
-        = openFile(filePath, QIODevice::ReadWrite, error);
-    if (!changeLog)
+    Utils::FileReader reader;
+    if (!reader.fetch(filePath, error))
         return false;
-
-    QString content = QString::fromUtf8(changeLog->readAll());
+    QString content = QString::fromUtf8(reader.data());
     content.replace(QRegExp(QLatin1String("\\([a-zA-Z0-9_\\.]+\\)")),
         QLatin1Char('(') + version + QLatin1Char(')'));
-    changeLog->resize(0);
-    changeLog->write(content.toUtf8());
-    changeLog->close();
-    if (changeLog->error() != QFile::NoError) {
-        if (error) {
-            *error = tr("Error writing Debian changelog file '%1': %2")
-                .arg(QDir::toNativeSeparators(changeLog->fileName()),
-                     changeLog->errorString());
-        }
-        return false;
-    }
-    return true;
+    MaemoGlobal::FileUpdate update(filePath);
+    Utils::FileSaver saver(filePath);
+    saver.write(content.toUtf8());
+    return saver.finalize(error);
 }
 
 QIcon AbstractDebBasedQt4MaemoTarget::packageManagerIcon(QString *error) const
@@ -452,10 +442,8 @@ bool AbstractDebBasedQt4MaemoTarget::setPackageManagerIconInternal(const QString
     QString *error)
 {
     const QString filePath = controlFilePath();
-    MaemoGlobal::FileUpdate update(filePath);
-    const QSharedPointer<QFile> controlFile
-        = openFile(filePath, QIODevice::ReadWrite, error);
-    if (!controlFile)
+    Utils::FileReader reader;
+    if (!reader.fetch(filePath, error))
         return false;
     const QPixmap pixmap(iconFilePath);
     if (pixmap.isNull()) {
@@ -475,7 +463,7 @@ bool AbstractDebBasedQt4MaemoTarget::setPackageManagerIconInternal(const QString
     }
     buffer.close();
     iconAsBase64 = iconAsBase64.toBase64();
-    QByteArray contents = controlFile->readAll();
+    QByteArray contents = reader.data();
     const QByteArray iconFieldNameWithColon = IconFieldName + ':';
     const int iconFieldPos = contents.startsWith(iconFieldNameWithColon)
         ? 0 : contents.indexOf('\n' + iconFieldNameWithColon);
@@ -498,17 +486,10 @@ bool AbstractDebBasedQt4MaemoTarget::setPackageManagerIconInternal(const QString
         contents.replace(oldIconStartPos, nextEolPos - oldIconStartPos,
             ' ' + iconAsBase64);
     }
-    controlFile->resize(0);
-    controlFile->write(contents);
-    if (controlFile->error() != QFile::NoError) {
-        if (error) {
-            *error = tr("Error writing file '%1': %2")
-                .arg(QDir::toNativeSeparators(controlFile->fileName()),
-                    controlFile->errorString());
-        }
-        return false;
-    }
-    return true;
+    MaemoGlobal::FileUpdate update(filePath);
+    Utils::FileSaver saver(filePath);
+    saver.write(contents);
+    return saver.finalize(error);
 }
 
 QString AbstractDebBasedQt4MaemoTarget::packageName() const
@@ -525,32 +506,26 @@ bool AbstractDebBasedQt4MaemoTarget::setPackageNameInternal(const QString &packa
     if (!setControlFieldValue("Source", packageName.toUtf8()))
         return false;
 
-    QSharedPointer<QFile> changelogFile
-        = openFile(changeLogFilePath(), QIODevice::ReadWrite, 0);
-    if (!changelogFile)
+    Utils::FileReader reader;
+    if (!reader.fetch(changeLogFilePath()))
         return false;
-    QString changelogContents = QString::fromUtf8(changelogFile->readAll());
+    QString changelogContents = QString::fromUtf8(reader.data());
     QRegExp pattern(QLatin1String("[^\\s]+( \\(\\d\\.\\d\\.\\d\\))"));
     changelogContents.replace(pattern, packageName + QLatin1String("\\1"));
-    if (!changelogFile->resize(0))
+    Utils::FileSaver saver(changeLogFilePath());
+    saver.write(changelogContents.toUtf8());
+    if (!saver.finalize())
         return false;
-    changelogFile->write(changelogContents.toUtf8());
 
-    QSharedPointer<QFile> rulesFile
-        = openFile(rulesFilePath(), QIODevice::ReadWrite, 0);
-    if (!rulesFile)
+    if (!reader.fetch(rulesFilePath()))
         return false;
-    QByteArray rulesContents = rulesFile->readAll();
+    QByteArray rulesContents = reader.data();
     const QString oldString = QLatin1String("debian/") + oldPackageName;
     const QString newString = QLatin1String("debian/") + packageName;
     rulesContents.replace(oldString.toUtf8(), newString.toUtf8());
-    rulesFile->resize(0);
-    rulesFile->write(rulesContents);
-    if (rulesFile->error() != QFile::NoError
-            || changelogFile->error() != QFile::NoError) {
-        return false;
-    }
-    return true;
+    Utils::FileSaver rulesSaver(changeLogFilePath());
+    rulesSaver.write(rulesContents);
+    return rulesSaver.finalize();
 }
 
 QString AbstractDebBasedQt4MaemoTarget::packageManagerName() const
@@ -627,10 +602,10 @@ QByteArray AbstractDebBasedQt4MaemoTarget::controlFileFieldValue(const QString &
     bool multiLine) const
 {
     QByteArray value;
-    QFile controlFile(controlFilePath());
-    if (!controlFile.open(QIODevice::ReadOnly))
+    Utils::FileReader reader;
+    if (!reader.fetch(controlFilePath()))
         return value;
-    const QByteArray &contents = controlFile.readAll();
+    const QByteArray &contents = reader.data();
     const int keyPos = contents.indexOf(key.toUtf8() + ':');
     if (keyPos == -1)
         return value;
@@ -664,14 +639,15 @@ QByteArray AbstractDebBasedQt4MaemoTarget::controlFileFieldValue(const QString &
 bool AbstractDebBasedQt4MaemoTarget::setControlFieldValue(const QByteArray &fieldName,
     const QByteArray &fieldValue)
 {
-    QFile controlFile(controlFilePath());
-    MaemoGlobal::FileUpdate update(controlFile.fileName());
-    if (!controlFile.open(QIODevice::ReadWrite))
+    Utils::FileReader reader;
+    if (!reader.fetch(controlFilePath()))
         return false;
-    QByteArray contents = controlFile.readAll();
+    QByteArray contents = reader.data();
     if (adaptControlFileField(contents, fieldName, fieldValue)) {
-        controlFile.resize(0);
-        controlFile.write(contents);
+        MaemoGlobal::FileUpdate update(controlFilePath());
+        Utils::FileSaver saver(changeLogFilePath());
+        saver.write(contents);
+        return saver.finalize();
     }
     return true;
 }
@@ -784,13 +760,12 @@ AbstractQt4MaemoTarget::ActionStatus AbstractDebBasedQt4MaemoTarget::createSpeci
 
 bool AbstractDebBasedQt4MaemoTarget::adaptRulesFile()
 {
-    QFile rulesFile(rulesFilePath());
-    if (!rulesFile.open(QIODevice::ReadWrite)) {
-        raiseError(tr("Packaging Error: Cannot open file '%1'.")
-                   .arg(QDir::toNativeSeparators(rulesFilePath())));
+    Utils::FileReader reader;
+    if (!reader.fetch(rulesFilePath())) {
+        raiseError(reader.errorString());
         return false;
     }
-    QByteArray rulesContents = rulesFile.readAll();
+    QByteArray rulesContents = reader.data();
     const QByteArray comment("# Uncomment this line for use without Qt Creator");
     rulesContents.replace("DESTDIR", "INSTALL_ROOT");
     rulesContents.replace("dh_shlibdeps", "# dh_shlibdeps " + comment);
@@ -802,12 +777,10 @@ bool AbstractDebBasedQt4MaemoTarget::adaptRulesFile()
     // because dpkg-genchanges doesn't know about it (and can't be told).
     // rulesContents.replace("dh_builddeb", "dh_builddeb --destdir=.");
 
-    rulesFile.resize(0);
-    rulesFile.write(rulesContents);
-    rulesFile.close();
-    if (rulesFile.error() != QFile::NoError) {
-        raiseError(tr("Packaging Error: Cannot write file '%1'.")
-                   .arg(QDir::toNativeSeparators(rulesFilePath())));
+    Utils::FileSaver saver(rulesFilePath());
+    saver.write(rulesContents);
+    if (!saver.finalize()) {
+        raiseError(saver.errorString());
         return false;
     }
     return true;
@@ -815,14 +788,12 @@ bool AbstractDebBasedQt4MaemoTarget::adaptRulesFile()
 
 bool AbstractDebBasedQt4MaemoTarget::adaptControlFile()
 {
-    QFile controlFile(controlFilePath());
-    if (!controlFile.open(QIODevice::ReadWrite)) {
-        raiseError(tr("Packaging Error: Cannot open file '%1'.")
-                   .arg(QDir::toNativeSeparators(controlFilePath())));
+    Utils::FileReader reader;
+    if (!reader.fetch(controlFilePath())) {
+        raiseError(reader.errorString());
         return false;
     }
-
-    QByteArray controlContents = controlFile.readAll();
+    QByteArray controlContents = reader.data();
 
     adaptControlFileField(controlContents, "Section", defaultSection());
     adaptControlFileField(controlContents, "Priority", "optional");
@@ -843,12 +814,10 @@ bool AbstractDebBasedQt4MaemoTarget::adaptControlFile()
     }
 
     addAdditionalControlFileFields(controlContents);
-    controlFile.resize(0);
-    controlFile.write(controlContents);
-    controlFile.close();
-    if (controlFile.error() != QFile::NoError) {
-        raiseError(tr("Packaging Error: Cannot write file '%1'.")
-                   .arg(QDir::toNativeSeparators(controlFilePath())));
+    Utils::FileSaver saver(controlFilePath());
+    saver.write(controlContents);
+    if (!saver.finalize()) {
+        raiseError(saver.errorString());
         return false;
     }
     return true;
@@ -1038,11 +1007,10 @@ bool AbstractRpmBasedQt4MaemoTarget::initAdditionalPackagingSettingsFromOtherTar
 QByteArray AbstractRpmBasedQt4MaemoTarget::getValueForTag(const QByteArray &tag,
     QString *error) const
 {
-    QSharedPointer<QFile> specFile
-        = openFile(specFilePath(), QIODevice::ReadOnly, error);
-    if (!specFile)
+    Utils::FileReader reader;
+    if (!reader.fetch(specFilePath(), error))
         return QByteArray();
-    const QByteArray &content = specFile->readAll();
+    const QByteArray &content = reader.data();
     const QByteArray completeTag = tag.toLower() + ':';
     int index = content.toLower().indexOf(completeTag);
     if (index == -1)
@@ -1057,14 +1025,14 @@ QByteArray AbstractRpmBasedQt4MaemoTarget::getValueForTag(const QByteArray &tag,
 bool AbstractRpmBasedQt4MaemoTarget::setValueForTag(const QByteArray &tag,
     const QByteArray &value, QString *error)
 {
-    QSharedPointer<QFile> specFile
-        = openFile(specFilePath(), QIODevice::ReadWrite, error);
-    if (!specFile)
+    Utils::FileReader reader;
+    if (!reader.fetch(specFilePath(), error))
         return false;
-    QByteArray content = specFile->readAll();
+    QByteArray content = reader.data();
     if (adaptTagValue(content, tag, value, false)) {
-        specFile->resize(0);
-        specFile->write(content);
+        Utils::FileSaver saver(specFilePath());
+        saver.write(content);
+        return saver.finalize(error);
     }
     return true;
 }