diff options
-rw-r--r-- | src/corelib/io/qprocess.cpp | 27 | ||||
-rw-r--r-- | src/corelib/io/qprocess.h | 2 | ||||
-rw-r--r-- | tests/auto/corelib/io/qprocess/tst_qprocess.cpp | 25 |
3 files changed, 54 insertions, 0 deletions
diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 96cec568df..d473281acc 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -1317,6 +1317,10 @@ void QProcess::closeWriteChannel() object will be in read-only mode (calling write() will result in error). + To make the process read EOF right away, pass nullDevice() here. + This is cleaner than using closeWriteChannel() before writing any + data, because it can be set up prior to starting the process. + If the file \a fileName does not exist at the moment start() is called or is not readable, starting the process will fail. @@ -1340,6 +1344,10 @@ void QProcess::setStandardInputFile(const QString &fileName) read channel is closed: reading from it using read() will always fail, as will readAllStandardOutput(). + To discard all standard output from the process, pass nullDevice() + here. This is more efficient than simply never reading the standard + output, as no QProcess buffers are filled. + If the file \a fileName doesn't exist at the moment start() is called, it will be created. If it cannot be created, the starting will fail. @@ -2440,6 +2448,25 @@ QStringList QProcess::systemEnvironment() */ /*! + \since 5.2 + + \brief The null device of the operating system. + + The returned file path uses native directory separators. + + \sa QProcess::setStandardInputFile(), QProcess::setStandardOutputFile(), + QProcess::setStandardErrorFile() +*/ +QString QProcess::nullDevice() +{ +#ifdef Q_OS_WIN + return QStringLiteral("\\\\.\\NUL"); +#else + return QStringLiteral("/dev/null"); +#endif +} + +/*! \typedef Q_PID \relates QProcess diff --git a/src/corelib/io/qprocess.h b/src/corelib/io/qprocess.h index 9da3e63f38..2919788e34 100644 --- a/src/corelib/io/qprocess.h +++ b/src/corelib/io/qprocess.h @@ -209,6 +209,8 @@ public: static QStringList systemEnvironment(); + static QString nullDevice(); + public Q_SLOTS: void terminate(); void kill(); diff --git a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp index 7a3f6837f8..8965c0787e 100644 --- a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp +++ b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp @@ -122,6 +122,7 @@ private slots: void setStandardInputFile(); void setStandardOutputFile_data(); void setStandardOutputFile(); + void setStandardOutputFile2(); void setStandardOutputProcess_data(); void setStandardOutputProcess(); void removeFileWhileProcessIsRunning(); @@ -1868,6 +1869,13 @@ void tst_QProcess::setStandardInputFile() QByteArray all = process.readAll(); QCOMPARE(all.size(), int(sizeof data) - 1); // testProcessEcho drops the ending \0 QVERIFY(all == data); + + QProcess process2; + process2.setStandardInputFile(QProcess::nullDevice()); + process2.start("testProcessEcho/testProcessEcho"); + QPROCESS_VERIFY(process2, waitForFinished()); + all = process2.readAll(); + QCOMPARE(all.size(), 0); } #endif @@ -1902,6 +1910,23 @@ void tst_QProcess::setStandardOutputFile_data() << true; } +//----------------------------------------------------------------------------- +#ifndef Q_OS_WINCE +void tst_QProcess::setStandardOutputFile2() +{ + static const char testdata[] = "Test data."; + + QProcess process; + process.setStandardOutputFile(QProcess::nullDevice()); + process.start("testProcessEcho2/testProcessEcho2"); + process.write(testdata, sizeof testdata); + QPROCESS_VERIFY(process,waitForFinished()); + QVERIFY(!process.bytesAvailable()); + + QVERIFY(!QFileInfo(QProcess::nullDevice()).isFile()); +} +#endif + void tst_QProcess::setStandardOutputFile() { static const char data[] = "Original data. "; |