1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 2001, 2002, 2007 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "klimitediodevice_p.h"
8#include "loggingcategory.h"
9
10#ifdef TEST_MODE
11#define WARNING qWarning()
12#else
13#define WARNING qCWarning(KArchiveLog)
14#endif
15
16KLimitedIODevice::KLimitedIODevice(QIODevice *dev, qint64 start, qint64 length)
17 : m_dev(dev)
18 , m_start(start)
19 , m_length(length)
20{
21 // qCDebug(KArchiveLog) << "start=" << start << "length=" << length;
22
23 const bool res = open(m: QIODevice::ReadOnly); // krazy:exclude=syscalls
24
25 // KLimitedIODevice always returns true
26 Q_ASSERT(res);
27
28 if(!res) {
29 WARNING << "failed to open LimitedIO device for reading.";
30 }
31}
32
33bool KLimitedIODevice::open(QIODevice::OpenMode m)
34{
35 // qCDebug(KArchiveLog) << "m=" << m;
36 if (m & QIODevice::ReadOnly) {
37 /*bool ok = false;
38 if ( m_dev->isOpen() )
39 ok = ( m_dev->mode() == QIODevice::ReadOnly );
40 else
41 ok = m_dev->open( m );
42 if ( ok )*/
43 m_dev->seek(pos: m_start); // No concurrent access !
44 } else {
45 WARNING << "KLimitedIODevice::open only supports QIODevice::ReadOnly!";
46 }
47 setOpenMode(QIODevice::ReadOnly);
48 return true;
49}
50
51void KLimitedIODevice::close()
52{
53}
54
55qint64 KLimitedIODevice::size() const
56{
57 return m_length;
58}
59
60qint64 KLimitedIODevice::readData(char *data, qint64 maxlen)
61{
62 maxlen = qMin(a: maxlen, b: m_length - pos()); // Apply upper limit
63 return m_dev->read(data, maxlen);
64}
65
66bool KLimitedIODevice::seek(qint64 pos)
67{
68 Q_ASSERT(pos <= m_length);
69 pos = qMin(a: pos, b: m_length); // Apply upper limit
70 bool ret = m_dev->seek(pos: m_start + pos);
71 if (ret) {
72 QIODevice::seek(pos);
73 }
74 return ret;
75}
76
77qint64 KLimitedIODevice::bytesAvailable() const
78{
79 return QIODevice::bytesAvailable();
80}
81
82bool KLimitedIODevice::isSequential() const
83{
84 return m_dev->isSequential();
85}
86
87#include "moc_klimitediodevice_p.cpp"
88

source code of karchive/src/klimitediodevice.cpp