+/**
+ * Wrapper around a QProcess which enforces IO to block .
+ *
+ * Several QIODevice users (notably QJSON) expect blocking semantics, e.g.,
+ * they expect that QIODevice::read() will blocked until the requested ammount
+ * of bytes is read or end of file is reached. But by default QProcess, does
+ * not block. And passing QIODevice::Unbuffered mitigates but does not fully
+ * address the problem either.
+ *
+ * This class wraps around QProcess, providing QIODevice interface, while
+ * ensuring that all reads block.
+ *
+ * This class also works around a bug in QProcess::atEnd() implementation.
+ *
+ * See also:
+ * - http://qt-project.org/wiki/Simple_Crypt_IO_Device
+ * - http://qt-project.org/wiki/Custom_IO_Device
+ */
+class BlockingIODevice : public QIODevice
+{
+ /* We don't use the Q_OBJECT in this class given we don't declare any
+ * signals and slots or use any other services provided by Qt's meta-object
+ * system. */
+public:
+ BlockingIODevice(QProcess * io);
+ bool isSequential() const;
+ bool atEnd() const;
+ bool waitForReadyRead(int msecs = -1);
+
+protected:
+ qint64 readData(char * data, qint64 maxSize);
+ qint64 writeData(const char * data, qint64 maxSize);
+
+private:
+ QProcess *m_device;
+};
+
+BlockingIODevice::BlockingIODevice(QProcess * io) :
+ m_device(io)
+{
+ /*
+ * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do
+ * its own buffering on top of the overridden readData() method.
+ *
+ * The only buffering used will be to satisfy QIODevice::peek() and
+ * QIODevice::ungetChar().
+ */
+ setOpenMode(ReadOnly | Unbuffered);
+}
+
+bool BlockingIODevice::isSequential() const
+{
+ return true;
+}
+
+bool BlockingIODevice::atEnd() const
+{
+ /*
+ * XXX: QProcess::atEnd() documentation is wrong -- it will return true
+ * even when the process is running --, so we try to workaround that here.
+ */
+ if (m_device->atEnd()) {
+ if (m_device->state() == QProcess::Running) {
+ if (!m_device->waitForReadyRead(-1)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+bool BlockingIODevice::waitForReadyRead(int msecs)
+{
+ Q_UNUSED(msecs);
+ return true;
+}
+
+qint64 BlockingIODevice::readData(char * data, qint64 maxSize)
+{
+ qint64 bytesToRead = maxSize;
+ qint64 readSoFar = 0;
+ do {
+ qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead);
+ if (chunkSize < 0) {
+ if (readSoFar) {
+ return readSoFar;
+ } else {
+ return chunkSize;
+ }
+ }
+ Q_ASSERT(chunkSize <= bytesToRead);
+ bytesToRead -= chunkSize;
+ readSoFar += chunkSize;
+ if (bytesToRead) {
+ if (!m_device->waitForReadyRead(-1)) {
+ qDebug() << "waitForReadyRead failed\n";
+ break;
+ }
+ }
+ } while(bytesToRead);
+
+ return readSoFar;
+}
+
+qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize)
+{
+ Q_ASSERT(false);
+ return -1;
+}
+
+Q_DECLARE_METATYPE(QList<ApiTraceError>);
+