summaryrefslogtreecommitdiffstats
path: root/src/corelib/io
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-05-08 13:22:49 +0200
committerAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-05-08 13:26:44 +0200
commit1fce111809f216383e60f56b57a9f68ccd2b766f (patch)
tree521d560ae28354ec495ee49e2abf4cfd28303c3f /src/corelib/io
parente6ffb36b5517d1d4025718b56423db9bdf0a63f8 (diff)
parent3545ef41217e3ce4d1bea2b07793fa7a8c1058a6 (diff)
Merge remote-tracking branch 'origin/5.4' into merge5.5
Conflicts: src/corelib/global/qglobal.h src/corelib/io/qnoncontiguousbytedevice_p.h src/gui/image/qjpeghandler.cpp src/network/access/qhttpthreaddelegate_p.h tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp tests/auto/widgets/widgets/qmenubar/BLACKLIST Change-Id: I01de8c1c28efcedfd7953d05025f54802dc08ab3
Diffstat (limited to 'src/corelib/io')
-rw-r--r--src/corelib/io/qlockfile_unix.cpp10
-rw-r--r--src/corelib/io/qlockfile_win.cpp22
-rw-r--r--src/corelib/io/qnoncontiguousbytedevice.cpp18
-rw-r--r--src/corelib/io/qnoncontiguousbytedevice_p.h4
-rw-r--r--src/corelib/io/qwindowspipereader.cpp102
-rw-r--r--src/corelib/io/qwindowspipereader_p.h4
6 files changed, 89 insertions, 71 deletions
diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp
index b817a24c74..d1804f2cb6 100644
--- a/src/corelib/io/qlockfile_unix.cpp
+++ b/src/corelib/io/qlockfile_unix.cpp
@@ -185,11 +185,11 @@ bool QLockFilePrivate::isApparentlyStale() const
{
qint64 pid;
QString hostname, appname;
- if (!getLockInfo(&pid, &hostname, &appname))
- return false;
- if (hostname.isEmpty() || hostname == QString::fromLocal8Bit(localHostName())) {
- if (::kill(pid, 0) == -1 && errno == ESRCH)
- return true; // PID doesn't exist anymore
+ if (getLockInfo(&pid, &hostname, &appname)) {
+ if (hostname.isEmpty() || hostname == QString::fromLocal8Bit(localHostName())) {
+ if (::kill(pid, 0) == -1 && errno == ESRCH)
+ return true; // PID doesn't exist anymore
+ }
}
const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTime());
return staleLockTime > 0 && age > staleLockTime;
diff --git a/src/corelib/io/qlockfile_win.cpp b/src/corelib/io/qlockfile_win.cpp
index 9fe86e1ad8..27a63e126a 100644
--- a/src/corelib/io/qlockfile_win.cpp
+++ b/src/corelib/io/qlockfile_win.cpp
@@ -126,21 +126,21 @@ bool QLockFilePrivate::isApparentlyStale() const
{
qint64 pid;
QString hostname, appname;
- if (!getLockInfo(&pid, &hostname, &appname))
- return false;
// On WinRT there seems to be no way of obtaining information about other
// processes due to sandboxing
#ifndef Q_OS_WINRT
- if (hostname == QString::fromLocal8Bit(localHostName())) {
- HANDLE procHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
- if (!procHandle)
- return true;
- // We got a handle but check if process is still alive
- DWORD dwR = ::WaitForSingleObject(procHandle, 0);
- ::CloseHandle(procHandle);
- if (dwR == WAIT_TIMEOUT)
- return true;
+ if (getLockInfo(&pid, &hostname, &appname)) {
+ if (hostname == QString::fromLocal8Bit(localHostName())) {
+ HANDLE procHandle = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+ if (!procHandle)
+ return true;
+ // We got a handle but check if process is still alive
+ DWORD dwR = ::WaitForSingleObject(procHandle, 0);
+ ::CloseHandle(procHandle);
+ if (dwR == WAIT_TIMEOUT)
+ return true;
+ }
}
#endif // !Q_OS_WINRT
const qint64 age = QFileInfo(fileName).lastModified().msecsTo(QDateTime::currentDateTime());
diff --git a/src/corelib/io/qnoncontiguousbytedevice.cpp b/src/corelib/io/qnoncontiguousbytedevice.cpp
index c17b42ef81..af1bb56fe9 100644
--- a/src/corelib/io/qnoncontiguousbytedevice.cpp
+++ b/src/corelib/io/qnoncontiguousbytedevice.cpp
@@ -216,6 +216,11 @@ qint64 QNonContiguousByteDeviceByteArrayImpl::size()
return byteArray->size();
}
+qint64 QNonContiguousByteDeviceByteArrayImpl::pos()
+{
+ return currentPosition;
+}
+
QNonContiguousByteDeviceRingBufferImpl::QNonContiguousByteDeviceRingBufferImpl(QSharedPointer<QRingBuffer> rb)
: QNonContiguousByteDevice(), currentPosition(0)
{
@@ -253,6 +258,11 @@ bool QNonContiguousByteDeviceRingBufferImpl::atEnd()
return currentPosition >= size();
}
+qint64 QNonContiguousByteDeviceRingBufferImpl::pos()
+{
+ return currentPosition;
+}
+
bool QNonContiguousByteDeviceRingBufferImpl::reset()
{
currentPosition = 0;
@@ -381,6 +391,14 @@ qint64 QNonContiguousByteDeviceIoDeviceImpl::size()
return device->size() - initialPosition;
}
+qint64 QNonContiguousByteDeviceIoDeviceImpl::pos()
+{
+ if (device->isSequential())
+ return -1;
+
+ return device->pos();
+}
+
QByteDeviceWrappingIoDevice::QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd) : QIODevice((QObject*)0)
{
byteDevice = bd;
diff --git a/src/corelib/io/qnoncontiguousbytedevice_p.h b/src/corelib/io/qnoncontiguousbytedevice_p.h
index 8b5bf3080a..38089dedd7 100644
--- a/src/corelib/io/qnoncontiguousbytedevice_p.h
+++ b/src/corelib/io/qnoncontiguousbytedevice_p.h
@@ -61,6 +61,7 @@ public:
virtual const char* readPointer(qint64 maximumLength, qint64 &len) = 0;
virtual bool advanceReadPointer(qint64 amount) = 0;
virtual bool atEnd() = 0;
+ virtual qint64 pos() { return -1; }
virtual bool reset() = 0;
virtual qint64 size() = 0;
@@ -103,6 +104,7 @@ public:
bool atEnd() Q_DECL_OVERRIDE;
bool reset() Q_DECL_OVERRIDE;
qint64 size() Q_DECL_OVERRIDE;
+ qint64 pos() Q_DECL_OVERRIDE;
protected:
QByteArray* byteArray;
qint64 currentPosition;
@@ -118,6 +120,7 @@ public:
bool atEnd() Q_DECL_OVERRIDE;
bool reset() Q_DECL_OVERRIDE;
qint64 size() Q_DECL_OVERRIDE;
+ qint64 pos() Q_DECL_OVERRIDE;
protected:
QSharedPointer<QRingBuffer> ringBuffer;
qint64 currentPosition;
@@ -135,6 +138,7 @@ public:
bool atEnd() Q_DECL_OVERRIDE;
bool reset() Q_DECL_OVERRIDE;
qint64 size() Q_DECL_OVERRIDE;
+ qint64 pos() Q_DECL_OVERRIDE;
protected:
QIODevice* device;
QByteArray* currentReadBuffer;
diff --git a/src/corelib/io/qwindowspipereader.cpp b/src/corelib/io/qwindowspipereader.cpp
index ab2025ee5c..14aad0e193 100644
--- a/src/corelib/io/qwindowspipereader.cpp
+++ b/src/corelib/io/qwindowspipereader.cpp
@@ -36,7 +36,6 @@
#include <qdebug.h>
#include <qelapsedtimer.h>
#include <qeventloop.h>
-#include <qtimer.h>
QT_BEGIN_NAMESPACE
@@ -45,13 +44,11 @@ QWindowsPipeReader::QWindowsPipeReader(QObject *parent)
handle(INVALID_HANDLE_VALUE),
readBufferMaxSize(0),
actualReadBufferSize(0),
+ stopped(true),
readSequenceStarted(false),
- emitReadyReadTimer(new QTimer(this)),
pipeBroken(false),
readyReadEmitted(false)
{
- emitReadyReadTimer->setSingleShot(true);
- connect(emitReadyReadTimer, SIGNAL(timeout()), SIGNAL(readyRead()));
dataReadNotifier = new QWinOverlappedIoNotifier(this);
connect(dataReadNotifier, &QWinOverlappedIoNotifier::notified, this, &QWindowsPipeReader::notified);
}
@@ -73,12 +70,7 @@ static bool qt_cancelIo(HANDLE handle, OVERLAPPED *overlapped)
QWindowsPipeReader::~QWindowsPipeReader()
{
- if (readSequenceStarted) {
- if (qt_cancelIo(handle, &overlapped))
- dataReadNotifier->waitForNotified(-1, &overlapped);
- else
- qErrnoWarning("QWindowsPipeReader: qt_cancelIo on handle %x failed.", handle);
- }
+ stop();
}
/*!
@@ -89,9 +81,9 @@ void QWindowsPipeReader::setHandle(HANDLE hPipeReadEnd)
readBuffer.clear();
actualReadBufferSize = 0;
handle = hPipeReadEnd;
- ZeroMemory(&overlapped, sizeof(overlapped));
pipeBroken = false;
readyReadEmitted = false;
+ stopped = false;
if (hPipeReadEnd != INVALID_HANDLE_VALUE) {
dataReadNotifier->setHandle(hPipeReadEnd);
dataReadNotifier->setEnabled(true);
@@ -100,13 +92,24 @@ void QWindowsPipeReader::setHandle(HANDLE hPipeReadEnd)
/*!
Stops the asynchronous read sequence.
- This function assumes that the file already has been closed.
- It does not cancel any I/O operation.
+ If the read sequence is running then the I/O operation is canceled.
*/
void QWindowsPipeReader::stop()
{
- dataReadNotifier->setEnabled(false);
+ stopped = true;
+ if (readSequenceStarted) {
+ if (qt_cancelIo(handle, &overlapped)) {
+ dataReadNotifier->waitForNotified(-1, &overlapped);
+ } else {
+ const DWORD dwError = GetLastError();
+ if (dwError != ERROR_NOT_FOUND) {
+ qErrnoWarning(dwError, "QWindowsPipeReader: qt_cancelIo on handle %x failed.",
+ handle);
+ }
+ }
+ }
readSequenceStarted = false;
+ dataReadNotifier->setEnabled(false);
handle = INVALID_HANDLE_VALUE;
}
@@ -119,7 +122,7 @@ qint64 QWindowsPipeReader::bytesAvailable() const
}
/*!
- Stops the asynchronous read sequence.
+ Copies at most \c{maxlen} bytes from the internal read buffer to \c{data}.
*/
qint64 QWindowsPipeReader::read(char *data, qint64 maxlen)
{
@@ -147,9 +150,7 @@ qint64 QWindowsPipeReader::read(char *data, qint64 maxlen)
}
if (!pipeBroken) {
- if (!actualReadBufferSize)
- emitReadyReadTimer->stop();
- if (!readSequenceStarted)
+ if (!readSequenceStarted && !stopped)
startAsyncRead();
if (readSoFar == 0)
return -2; // signal EWOULDBLOCK
@@ -172,13 +173,41 @@ void QWindowsPipeReader::notified(quint32 numberOfBytesRead, quint32 errorCode,
{
if (&overlapped != notifiedOverlapped)
return;
- if (!completeAsyncRead(numberOfBytesRead, errorCode)) {
+
+ switch (errorCode) {
+ case ERROR_SUCCESS:
+ break;
+ case ERROR_MORE_DATA:
+ // This is not an error. We're connected to a message mode
+ // pipe and the message didn't fit into the pipe's system
+ // buffer. We will read the remaining data in the next call.
+ break;
+ case ERROR_BROKEN_PIPE:
+ case ERROR_PIPE_NOT_CONNECTED:
pipeBroken = true;
+ break;
+ default:
+ emit winError(errorCode, QLatin1String("QWindowsPipeReader::completeAsyncRead"));
+ pipeBroken = true;
+ break;
+ }
+
+ readSequenceStarted = false;
+
+ // After the reader was stopped, the only reason why this function can be called is the
+ // completion of a cancellation. No signals should be emitted, and no new read sequence should
+ // be started in this case.
+ if (stopped)
+ return;
+
+ if (pipeBroken) {
emit pipeClosed();
return;
}
+
+ actualReadBufferSize += numberOfBytesRead;
+ readBuffer.truncate(actualReadBufferSize);
startAsyncRead();
- emitReadyReadTimer->stop();
readyReadEmitted = true;
emit readyRead();
}
@@ -206,6 +235,7 @@ void QWindowsPipeReader::startAsyncRead()
char *ptr = readBuffer.reserve(bytesToRead);
readSequenceStarted = true;
+ ZeroMemory(&overlapped, sizeof(overlapped));
if (ReadFile(handle, ptr, bytesToRead, NULL, &overlapped)) {
// We get notified by the QWinOverlappedIoNotifier - even in the synchronous case.
return;
@@ -241,38 +271,6 @@ void QWindowsPipeReader::startAsyncRead()
/*!
\internal
- Sets the correct size of the read buffer after a read operation.
- Returns \c false, if an error occurred or the connection dropped.
- */
-bool QWindowsPipeReader::completeAsyncRead(DWORD bytesRead, DWORD errorCode)
-{
- readSequenceStarted = false;
-
- switch (errorCode) {
- case ERROR_SUCCESS:
- break;
- case ERROR_MORE_DATA:
- // This is not an error. We're connected to a message mode
- // pipe and the message didn't fit into the pipe's system
- // buffer. We will read the remaining data in the next call.
- break;
- case ERROR_BROKEN_PIPE:
- case ERROR_PIPE_NOT_CONNECTED:
- return false;
- default:
- emit winError(errorCode, QLatin1String("QWindowsPipeReader::completeAsyncRead"));
- return false;
- }
-
- actualReadBufferSize += bytesRead;
- readBuffer.truncate(actualReadBufferSize);
- if (!emitReadyReadTimer->isActive())
- emitReadyReadTimer->start();
- return true;
-}
-
-/*!
- \internal
Returns the number of available bytes in the pipe.
Sets QWindowsPipeReader::pipeBroken to true if the connection is broken.
*/
diff --git a/src/corelib/io/qwindowspipereader_p.h b/src/corelib/io/qwindowspipereader_p.h
index 7904f116cb..2c32eeb9ce 100644
--- a/src/corelib/io/qwindowspipereader_p.h
+++ b/src/corelib/io/qwindowspipereader_p.h
@@ -47,7 +47,6 @@
#include <qbytearray.h>
#include <qobject.h>
-#include <qtimer.h>
#include <private/qringbuffer_p.h>
#include <qt_windows.h>
@@ -89,7 +88,6 @@ private Q_SLOTS:
void notified(quint32 numberOfBytesRead, quint32 errorCode, OVERLAPPED *notifiedOverlapped);
private:
- bool completeAsyncRead(DWORD bytesRead, DWORD errorCode);
DWORD checkPipeState();
private:
@@ -99,8 +97,8 @@ private:
qint64 readBufferMaxSize;
QRingBuffer readBuffer;
int actualReadBufferSize;
+ bool stopped;
bool readSequenceStarted;
- QTimer *emitReadyReadTimer;
bool pipeBroken;
bool readyReadEmitted;
};