aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/tools
diff options
context:
space:
mode:
authorJake Petroules <jake.petroules@qt.io>2016-07-14 10:17:22 -0700
committerJake Petroules <jake.petroules@qt.io>2016-08-22 16:42:15 +0000
commitc5873fb3ea6c109eda30cedc8ea39700c75b88d7 (patch)
tree4b9c5d607f2b720592073f41cf9e133e1b869b18 /src/lib/corelib/tools
parent323a52542f4e1a670f838082e1d88f0fdbf155da (diff)
Introduce FileSaver
This utility class is a wrapper around QSaveFile which *only* writes its output to disk if the written file contents would differ from the existing file contents. This is intended for use by IDE generators which will "write out" generated project files on every build without causing unnecessary reloads due to externally changed project files. Change-Id: Id7abe6d2d98a9199047d090496c359c40c0319cf Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src/lib/corelib/tools')
-rw-r--r--src/lib/corelib/tools/filesaver.cpp83
-rw-r--r--src/lib/corelib/tools/filesaver.h65
-rw-r--r--src/lib/corelib/tools/tools.pri2
3 files changed, 150 insertions, 0 deletions
diff --git a/src/lib/corelib/tools/filesaver.cpp b/src/lib/corelib/tools/filesaver.cpp
new file mode 100644
index 000000000..b7ba69143
--- /dev/null
+++ b/src/lib/corelib/tools/filesaver.cpp
@@ -0,0 +1,83 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing
+**
+** This file is part of Qbs.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms and
+** conditions see http://www.qt.io/terms-conditions. For further information
+** use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#include "filesaver.h"
+
+#include <QFile>
+#include <QSaveFile>
+
+namespace qbs {
+namespace Internal {
+
+FileSaver::FileSaver(const QString &filePath, bool overwriteIfUnchanged)
+ : m_filePath(filePath), m_overwriteIfUnchanged(overwriteIfUnchanged)
+{
+}
+
+QIODevice *FileSaver::device()
+{
+ return m_memoryDevice.data();
+}
+
+bool FileSaver::open()
+{
+ if (!m_overwriteIfUnchanged) {
+ QFile file(m_filePath);
+ if (file.open(QIODevice::ReadOnly))
+ m_oldFileContents = file.readAll();
+ else
+ m_oldFileContents.clear();
+ }
+
+ m_newFileContents.clear();
+ m_memoryDevice.reset(new QBuffer(&m_newFileContents));
+ return m_memoryDevice->open(QIODevice::WriteOnly);
+}
+
+bool FileSaver::commit()
+{
+ if (!m_overwriteIfUnchanged && m_oldFileContents == m_newFileContents)
+ return true; // no need to write unchanged data
+
+ QSaveFile saveFile(m_filePath);
+ if (!saveFile.open(QIODevice::WriteOnly))
+ return false;
+
+ saveFile.write(m_newFileContents);
+ return saveFile.commit();
+}
+
+qint64 FileSaver::write(const QByteArray &data)
+{
+ return device()->write(data);
+}
+
+} // namespace Internal
+} // namespace qbs
diff --git a/src/lib/corelib/tools/filesaver.h b/src/lib/corelib/tools/filesaver.h
new file mode 100644
index 000000000..ae69a83d6
--- /dev/null
+++ b/src/lib/corelib/tools/filesaver.h
@@ -0,0 +1,65 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing
+**
+** This file is part of Qbs.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms and
+** conditions see http://www.qt.io/terms-conditions. For further information
+** use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, The Qt Company gives you certain additional
+** rights. These rights are described in The Qt Company LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#ifndef FILESAVER_H
+#define FILESAVER_H
+
+#include <QBuffer>
+#include <QByteArray>
+#include <QScopedPointer>
+#include <QString>
+
+namespace qbs {
+namespace Internal {
+
+/*!
+ * QSaveFile wrapper which doesn't update the target file if the contents are unchanged.
+ */
+class FileSaver {
+public:
+ FileSaver(const QString &filePath, bool overwriteIfUnchanged = false);
+
+ QIODevice *device();
+ bool open();
+ bool commit();
+ qint64 write(const QByteArray &data);
+
+private:
+ QByteArray m_newFileContents;
+ QByteArray m_oldFileContents;
+ QScopedPointer<QBuffer> m_memoryDevice;
+ const QString m_filePath;
+ const bool m_overwriteIfUnchanged;
+};
+
+} // namespace Internal
+} // namespace qbs
+
+#endif // FILESAVER_H
diff --git a/src/lib/corelib/tools/tools.pri b/src/lib/corelib/tools/tools.pri
index 585985406..7bcf60516 100644
--- a/src/lib/corelib/tools/tools.pri
+++ b/src/lib/corelib/tools/tools.pri
@@ -10,6 +10,7 @@ HEADERS += \
$$PWD/error.h \
$$PWD/executablefinder.h \
$$PWD/fileinfo.h \
+ $$PWD/filesaver.h \
$$PWD/filetime.h \
$$PWD/generateoptions.h \
$$PWD/id.h \
@@ -54,6 +55,7 @@ SOURCES += \
$$PWD/error.cpp \
$$PWD/executablefinder.cpp \
$$PWD/fileinfo.cpp \
+ $$PWD/filesaver.cpp \
$$PWD/generateoptions.cpp \
$$PWD/id.cpp \
$$PWD/jsliterals.cpp \