File indexing completed on 2024-06-23 05:14:19

0001 /* utils/windowsprocessdevice.h
0002     This file is part of Kleopatra, the KDE keymanager
0003     SPDX-FileCopyrightText: 2019 g 10code GmbH
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #if defined(WIN32) || defined(Q_MOC_RUN)
0009 #ifndef WINDOWSPROCESSDEVICE_H
0010 #define WINDOWSPROCESSDEVICE_H
0011 
0012 #include <memory>
0013 
0014 #include <QIODevice>
0015 
0016 class QString;
0017 #include <QStringList>
0018 
0019 namespace Kleo
0020 {
0021 /* Simplistic anonymous pipe io device
0022  *
0023  * Create an IODevice using Windows Create Process and pipes
0024  * this class serves as an alternative to use QProcess on
0025  * Windows which event driven nature does not play well
0026  * in our threading and IPC model.
0027  *
0028  * This class was written with gpgtar in mind and mostly
0029  * a reaction to multiple issues we had with QProcess
0030  * and gpgtar in GPGME on Windows. It was so hard to debug them
0031  * that we decided for a simple approach that gives us
0032  * full control.
0033  *
0034  * As there are use cases streaming terrabytes through this
0035  * even the control of the buffer size is an advantage.
0036  *
0037  **/
0038 class WindowsProcessDevice : public QIODevice
0039 {
0040     Q_OBJECT
0041 public:
0042     WindowsProcessDevice(const QString &path, const QStringList &args, const QString &wd);
0043 
0044     /* Starts the process. Only supports
0045        QIODevice::ReadOnly
0046        QIODevice::WriteOnly
0047        QIODevice::ReadWrite */
0048     bool open(OpenMode mode) override;
0049 
0050     /* Terminates the process */
0051     void close() override;
0052 
0053     bool isSequential() const override;
0054 
0055     /* Closes the write channel */
0056     void closeWriteChannel();
0057 
0058     /* Get the an error string either stderr or a windows error */
0059     QString errorString();
0060 
0061 protected:
0062     /* Blocking read */
0063     qint64 readData(char *data, qint64 maxSize) override;
0064     /* Blocking write */
0065     qint64 writeData(const char *data, qint64 size) override;
0066 
0067 private:
0068     Q_DISABLE_COPY(WindowsProcessDevice)
0069     class Private;
0070     std::shared_ptr<Private> d;
0071 };
0072 
0073 } // namespace Kleo
0074 
0075 #endif // WINDOWSPROCESSDEVICE_H
0076 #endif // WIN32