summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/binaryformat.cpp
diff options
context:
space:
mode:
authorkh1 <karsten.heimrich@digia.com>2014-06-30 14:56:00 +0200
committerKai Koehne <kai.koehne@digia.com>2014-07-02 14:07:23 +0200
commitb876a60eff6141a3c443f54b9bef096bbe7ff60b (patch)
treea41917b1ef4fbd64a64e1cd7c5ff86c16ae72e28 /src/libs/installer/binaryformat.cpp
parent71868e96bfd29368b55ef1111328ac9b9f91e1a4 (diff)
Revert change from QIODevice to QFileDevice.
Follow up on 924ebcdbc9b9e590a5f0905941e2d05ac34d4be2 (Part 2). Remove some unused methods. Cleanup some unused code. Add some documentation what the Archive class does and is used for. Add copy method and switch back the class to inherit from QIODevice as also the Archive class does not work as QFileDevice descent. Change-Id: Ia20f02c5aa53ba977fdc1d14785031b0cfbce386 Reviewed-by: Kai Koehne <kai.koehne@digia.com>
Diffstat (limited to 'src/libs/installer/binaryformat.cpp')
-rw-r--r--src/libs/installer/binaryformat.cpp187
1 files changed, 58 insertions, 129 deletions
diff --git a/src/libs/installer/binaryformat.cpp b/src/libs/installer/binaryformat.cpp
index 517883b63..8ff870256 100644
--- a/src/libs/installer/binaryformat.cpp
+++ b/src/libs/installer/binaryformat.cpp
@@ -44,7 +44,6 @@
#include "errors.h"
#include "fileio.h"
#include "fileutils.h"
-#include "lib7z_facade.h"
#include "utils.h"
#include "kdupdaterupdateoperationfactory.h"
@@ -53,9 +52,6 @@
#include <QResource>
#include <QTemporaryFile>
-using namespace QInstaller;
-using namespace QInstallerCreator;
-
/*!
Search through 1MB, if smaller through the whole file. Note: QFile::map() does
not change QFile::pos(). Fallback to read the file content in case we can't map it.
@@ -102,28 +98,30 @@ qint64 QInstaller::findMagicCookie(QFile *in, quint64 magicCookie)
return -1; // never reached
}
+
+namespace QInstallerCreator {
+
+/*!
+ \class Archive
+ \brief The Archive class provides an interface for reading from an underlying device.
+
+ Archive is an interface for reading inside a binary file, but is not supposed to write to
+ the binary it wraps. The Archive class is created by either passing a path to an already
+ zipped archive or by passing an identifier and segment inside an already existing binary,
+ passed as device.
+
+ The archive name can be set at any time using setName(). The segment passed inside the
+ constructor represents the offset and size of the archive inside the binary file.
+*/
+
/*!
Creates an archive providing the data in \a path.
- \a path can be a path to a file or to a directory. If it's a file, it's considered to be
- pre-zipped and gets delivered as it is. If it's a directory, it gets zipped by Archive.
*/
Archive::Archive(const QString &path)
: m_device(0)
- , m_isTempFile(false)
- , m_path(path)
, m_name(QFileInfo(path).fileName().toUtf8())
{
-}
-
-Archive::Archive(const QByteArray &identifier, const QByteArray &data)
- : m_device(0)
- , m_isTempFile(true)
- , m_path(generateTemporaryFileName())
- , m_name(identifier)
-{
- QFile file(m_path);
- file.open(QIODevice::WriteOnly);
- file.write(data);
+ m_inputFile.setFileName(path);
}
/*!
@@ -132,7 +130,6 @@ Archive::Archive(const QByteArray &identifier, const QByteArray &data)
Archive::Archive(const QByteArray &identifier, const QSharedPointer<QFile> &device, const Range<qint64> &segment)
: m_device(device)
, m_segment(segment)
- , m_isTempFile(false)
, m_name(identifier)
{
}
@@ -141,8 +138,6 @@ Archive::~Archive()
{
if (isOpen())
close();
- if (m_isTempFile)
- QFile::remove(m_path);
}
/*!
@@ -171,8 +166,6 @@ void Archive::setName(const QByteArray &name)
void Archive::close()
{
m_inputFile.close();
- if (QFileInfo(m_path).isDir())
- m_inputFile.remove();
QIODevice::close();
}
@@ -194,57 +187,11 @@ bool Archive::open(OpenMode mode)
if (m_device != 0)
return QIODevice::open(mode);
- const QFileInfo fi(m_path);
- if (fi.isFile()) {
- m_inputFile.setFileName(m_path);
- if (!m_inputFile.open(mode)) {
- setErrorString(tr("Could not open archive file %1 for reading.").arg(m_path));
- return false;
- }
- setOpenMode(mode);
- return true;
- }
-
- if (fi.isDir()) {
- if (m_inputFile.fileName().isEmpty() || !m_inputFile.exists()) {
- if (!createZippedFile())
- return false;
- }
- Q_ASSERT(!m_inputFile.fileName().isEmpty());
- if (!m_inputFile.open(mode))
- return false;
- setOpenMode(mode);
- return true;
- }
-
- setErrorString(tr("Could not create archive from %1: Not a file.").arg(m_path));
- return false;
-}
-
-bool Archive::createZippedFile()
-{
- QTemporaryFile file;
- file.setAutoRemove(false);
- if (!file.open())
- return false;
-
- m_inputFile.setFileName(file.fileName());
- file.close();
- m_inputFile.open(QIODevice::ReadWrite);
- try {
- Lib7z::createArchive(&m_inputFile, QStringList() << m_path);
- } catch(Lib7z::SevenZipException &e) {
- m_inputFile.close();
- setErrorString(e.message());
- return false;
- }
-
- if (!Lib7z::isSupportedArchive(&m_inputFile)) {
- m_inputFile.close();
- setErrorString(tr("Error while packing directory at %1").arg(m_path));
+ if (!m_inputFile.open(mode)) {
+ setErrorString(m_inputFile.errorString());
return false;
}
- m_inputFile.close();
+ setOpenMode(mode);
return true;
}
@@ -253,26 +200,9 @@ bool Archive::createZippedFile()
*/
qint64 Archive::size() const
{
- // if we got a device, we just pass the length of the segment
if (m_device != 0)
return m_segment.length();
-
- const QFileInfo fi(m_path);
- // if we got a regular file, we pass the size of the file
- if (fi.isFile())
- return fi.size();
-
- if (fi.isDir()) {
- if (m_inputFile.fileName().isEmpty() || !m_inputFile.exists()) {
- if (!const_cast< Archive* >(this)->createZippedFile()) {
- throw Error(QCoreApplication::translate("Archive",
- "Cannot create zipped file for path %1: %2").arg(m_path, errorString()));
- }
- }
- Q_ASSERT(!m_inputFile.fileName().isEmpty());
- return m_inputFile.size();
- }
- return 0;
+ return m_inputFile.size();
}
/*!
@@ -301,24 +231,37 @@ qint64 Archive::writeData(const char* data, qint64 maxSize)
return -1;
}
-QByteArray Component::name() const
+void Archive::copyData(Archive *archive, QFileDevice *out)
{
- return m_name;
+ qint64 left = archive->size();
+ char data[4096];
+ while (left > 0) {
+ const qint64 len = qMin<qint64>(left, 4096);
+ const qint64 bytesRead = archive->read(data, len);
+ if (bytesRead != len) {
+ throw QInstaller::Error(tr("Read failed after % 1 bytes: % 2")
+ .arg(QString::number(archive->size() - left), archive->errorString()));
+ }
+ const qint64 bytesWritten = out->write(data, len);
+ if (bytesWritten != len) {
+ throw QInstaller::Error(tr("Write failed after % 1 bytes: % 2")
+ .arg(QString::number(archive->size() - left), out->errorString()));
+ }
+ left -= len;
+ }
}
-void Component::setName(const QByteArray &ba)
-{
- m_name = ba;
-}
-Range<qint64> Component::binarySegment() const
+// -- Component
+
+QByteArray Component::name() const
{
- return m_binarySegment;
+ return m_name;
}
-void Component::setBinarySegment(const Range<qint64> &r)
+void Component::setName(const QByteArray &ba)
{
- m_binarySegment = r;
+ m_name = ba;
}
Component Component::readFromIndexEntry(const QSharedPointer<QFile> &in, qint64 offset)
@@ -336,8 +279,8 @@ void Component::writeIndexEntry(QFileDevice *out, qint64 positionOffset) const
{
QInstaller::appendByteArray(out, m_name);
m_binarySegment.moved(positionOffset);
- QInstaller::appendInt64(out, binarySegment().start());
- QInstaller::appendInt64(out, binarySegment().length());
+ QInstaller::appendInt64(out, m_binarySegment.start());
+ QInstaller::appendInt64(out, m_binarySegment.length());
}
void Component::writeData(QFileDevice *out, qint64 offset) const
@@ -364,8 +307,8 @@ void Component::writeData(QFileDevice *out, qint64 offset) const
foreach (const QSharedPointer<Archive> &archive, m_archives) {
if (!archive->open(QIODevice::ReadOnly)) {
- throw Error(tr("Could not open archive %1: %2").arg(QLatin1String(archive->name()),
- archive->errorString()));
+ throw QInstaller::Error(tr("Could not open archive %1: %2")
+ .arg(QString::fromUtf8(archive->name()), archive->errorString()));
}
const qint64 expectedStart = starts.takeFirst();
@@ -373,7 +316,7 @@ void Component::writeData(QFileDevice *out, qint64 offset) const
Q_UNUSED(expectedStart);
Q_UNUSED(actualStart);
Q_ASSERT(expectedStart == actualStart);
- QInstaller::blockingCopy(archive.data(), out, archive->size());
+ archive->copyData(out);
}
m_binarySegment = Range<qint64>::fromStartAndEnd(dataBegin, out->pos() + offset);
@@ -399,16 +342,6 @@ void Component::readData(const QSharedPointer<QFile> &in, qint64 offset)
in->seek(pos);
}
-QString Component::dataDirectory() const
-{
- return m_dataDirectory;
-}
-
-void Component::setDataDirectory(const QString &path)
-{
- m_dataDirectory = path;
-}
-
bool Component::operator<(const Component& other) const
{
if (m_name != other.name())
@@ -422,13 +355,6 @@ bool Component::operator==(const Component& other) const
}
/*!
- Destroys this component.
- */
-Component::~Component()
-{
-}
-
-/*!
Appends \a archive to this component. The component takes ownership of \a archive.
*/
void Component::appendArchive(const QSharedPointer<Archive>& archive)
@@ -515,6 +441,9 @@ int ComponentIndex::componentCount() const
return m_components.size();
}
+} // namespace QInstallerCreator
+
+namespace QInstaller {
/*!
\internal
@@ -832,14 +761,12 @@ void BinaryContent::readBinaryData(BinaryContent &content, const QSharedPointer<
const QVector<QInstallerCreator::Component> components = content.d->m_componentIndex.components();
qDebug() << "Number of components loaded:" << components.count();
foreach (const QInstallerCreator::Component &component, components) {
- const QVector<QSharedPointer<Archive> > archives = component.archives();
+ const QVector<QSharedPointer<QInstallerCreator::Archive> > archives = component.archives();
qDebug() << component.name().data() << "loaded...";
QStringList archivesWithSize;
- foreach (const QSharedPointer<Archive> &archive, archives) {
- QString archiveWithSize(QLatin1String("%1 - %2"));
- archiveWithSize = archiveWithSize.arg(QString::fromLocal8Bit(archive->name()),
- humanReadableSize(archive->size()));
- archivesWithSize.append(archiveWithSize);
+ foreach (const QSharedPointer<QInstallerCreator::Archive> &archive, archives) {
+ archivesWithSize.append(QString::fromLatin1("%1 - %2")
+ .arg(QString::fromUtf8(archive->name()), humanReadableSize(archive->size())));
}
if (!archivesWithSize.isEmpty()) {
qDebug() << " - " << archives.count() << "archives: "
@@ -946,3 +873,5 @@ QInstallerCreator::ComponentIndex BinaryContent::componentIndex() const
{
return d->m_componentIndex;
}
+
+} // namespace QInstaller