summaryrefslogtreecommitdiffstats
path: root/src/corelib/io
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/io')
-rw-r--r--src/corelib/io/qfiledevice.cpp2
-rw-r--r--src/corelib/io/qfileselector.cpp11
-rw-r--r--src/corelib/io/qfsfileengine.cpp46
-rw-r--r--src/corelib/io/qiodevice_p.h16
-rw-r--r--src/corelib/io/qlockfile_p.h2
-rw-r--r--src/corelib/io/qlockfile_unix.cpp42
-rw-r--r--src/corelib/io/qsettings.cpp8
-rw-r--r--src/corelib/io/qwinoverlappedionotifier.cpp2
8 files changed, 82 insertions, 47 deletions
diff --git a/src/corelib/io/qfiledevice.cpp b/src/corelib/io/qfiledevice.cpp
index 4c5ed0aef6..8531d32523 100644
--- a/src/corelib/io/qfiledevice.cpp
+++ b/src/corelib/io/qfiledevice.cpp
@@ -560,7 +560,7 @@ qint64 QFileDevice::writeData(const char *data, qint64 len)
char *writePointer = d->writeBuffer.reserve(len);
if (len == 1)
*writePointer = *data;
- else
+ else if (len)
::memcpy(writePointer, data, len);
return len;
}
diff --git a/src/corelib/io/qfileselector.cpp b/src/corelib/io/qfileselector.cpp
index f033392df8..52ac414645 100644
--- a/src/corelib/io/qfileselector.cpp
+++ b/src/corelib/io/qfileselector.cpp
@@ -248,9 +248,16 @@ QUrl QFileSelector::select(const QUrl &filePath) const
return filePath;
QUrl ret(filePath);
if (isLocalScheme(filePath.scheme())) {
- QString equivalentPath = QLatin1Char(':') + filePath.path();
+ QLatin1String scheme(":");
+#ifdef Q_OS_ANDROID
+ // use other scheme because ":" means "qrc" here
+ if (filePath.scheme() == QLatin1String("assets"))
+ scheme = QLatin1String("assets:");
+#endif
+
+ QString equivalentPath = scheme + filePath.path();
QString selectedPath = d->select(equivalentPath);
- ret.setPath(selectedPath.remove(0, 1));
+ ret.setPath(selectedPath.remove(0, scheme.size()));
} else {
ret = QUrl::fromLocalFile(d->select(ret.toLocalFile()));
}
diff --git a/src/corelib/io/qfsfileengine.cpp b/src/corelib/io/qfsfileengine.cpp
index 429c40da1a..10e116a23a 100644
--- a/src/corelib/io/qfsfileengine.cpp
+++ b/src/corelib/io/qfsfileengine.cpp
@@ -724,29 +724,33 @@ qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len)
qint64 writtenBytes = 0;
- if (fh) {
- // Buffered stdlib mode.
-
- size_t result;
- do {
- result = fwrite(data + writtenBytes, 1, size_t(len - writtenBytes), fh);
- writtenBytes += result;
- } while (result == 0 ? errno == EINTR : writtenBytes < len);
+ if (len) { // avoid passing nullptr to fwrite() or QT_WRITE() (UB)
- } else if (fd != -1) {
- // Unbuffered stdio mode.
+ if (fh) {
+ // Buffered stdlib mode.
+
+ size_t result;
+ do {
+ result = fwrite(data + writtenBytes, 1, size_t(len - writtenBytes), fh);
+ writtenBytes += result;
+ } while (result == 0 ? errno == EINTR : writtenBytes < len);
+
+ } else if (fd != -1) {
+ // Unbuffered stdio mode.
+
+ SignedIOType result;
+ do {
+ // calculate the chunk size
+ // on Windows or 32-bit no-largefile Unix, we'll need to read in chunks
+ // we limit to the size of the signed type, otherwise we could get a negative number as a result
+ quint64 wantedBytes = quint64(len) - quint64(writtenBytes);
+ UnsignedIOType chunkSize = std::numeric_limits<SignedIOType>::max();
+ if (chunkSize > wantedBytes)
+ chunkSize = wantedBytes;
+ result = QT_WRITE(fd, data + writtenBytes, chunkSize);
+ } while (result > 0 && (writtenBytes += result) < len);
+ }
- SignedIOType result;
- do {
- // calculate the chunk size
- // on Windows or 32-bit no-largefile Unix, we'll need to read in chunks
- // we limit to the size of the signed type, otherwise we could get a negative number as a result
- quint64 wantedBytes = quint64(len) - quint64(writtenBytes);
- UnsignedIOType chunkSize = std::numeric_limits<SignedIOType>::max();
- if (chunkSize > wantedBytes)
- chunkSize = wantedBytes;
- result = QT_WRITE(fd, data + writtenBytes, chunkSize);
- } while (result > 0 && (writtenBytes += result) < len);
}
if (len && writtenBytes == 0) {
diff --git a/src/corelib/io/qiodevice_p.h b/src/corelib/io/qiodevice_p.h
index 56a89ab680..6ab1de27e0 100644
--- a/src/corelib/io/qiodevice_p.h
+++ b/src/corelib/io/qiodevice_p.h
@@ -102,14 +102,17 @@ public:
}
qint64 read(char* target, qint64 size) {
qint64 r = qMin(size, len);
- memcpy(target, first, r);
- len -= r;
- first += r;
+ if (r) {
+ memcpy(target, first, r);
+ len -= r;
+ first += r;
+ }
return r;
}
qint64 peek(char* target, qint64 size) {
qint64 r = qMin(size, len);
- memcpy(target, first, r);
+ if (r)
+ memcpy(target, first, r);
return r;
}
char* reserve(qint64 size) {
@@ -141,7 +144,7 @@ public:
return r;
}
bool canReadLine() const {
- return memchr(first, '\n', len);
+ return first && memchr(first, '\n', len);
}
void ungetChar(char c) {
if (first == buf) {
@@ -172,7 +175,8 @@ private:
if (newCapacity > capacity) {
// allocate more space
char* newBuf = new char[newCapacity];
- memmove(newBuf + moveOffset, first, len);
+ if (first)
+ memmove(newBuf + moveOffset, first, len);
delete [] buf;
buf = newBuf;
capacity = newCapacity;
diff --git a/src/corelib/io/qlockfile_p.h b/src/corelib/io/qlockfile_p.h
index 168062f467..48b642abd0 100644
--- a/src/corelib/io/qlockfile_p.h
+++ b/src/corelib/io/qlockfile_p.h
@@ -78,7 +78,7 @@ public:
static QString processNameByPid(qint64 pid);
#ifdef Q_OS_UNIX
- static int checkFcntlWorksAfterFlock();
+ static int checkFcntlWorksAfterFlock(const QString &fn);
#endif
QString fileName;
diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp
index 365f3e07ab..623968b869 100644
--- a/src/corelib/io/qlockfile_unix.cpp
+++ b/src/corelib/io/qlockfile_unix.cpp
@@ -39,6 +39,10 @@
#include "QtCore/qfileinfo.h"
#include "QtCore/qdebug.h"
#include "QtCore/qdatetime.h"
+#include "QtCore/qfileinfo.h"
+#include "QtCore/qcache.h"
+#include "QtCore/qglobalstatic.h"
+#include "QtCore/qmutex.h"
#include "private/qcore_unix_p.h" // qt_safe_open
#include "private/qabstractfileengine_p.h"
@@ -89,10 +93,10 @@ static qint64 qt_write_loop(int fd, const char *data, qint64 len)
return pos;
}
-int QLockFilePrivate::checkFcntlWorksAfterFlock()
+int QLockFilePrivate::checkFcntlWorksAfterFlock(const QString &fn)
{
#ifndef QT_NO_TEMPORARYFILE
- QTemporaryFile file;
+ QTemporaryFile file(fn);
if (!file.open())
return 0;
const int fd = file.d_func()->engine()->handle();
@@ -114,24 +118,34 @@ int QLockFilePrivate::checkFcntlWorksAfterFlock()
#endif
}
-static QBasicAtomicInt fcntlOK = Q_BASIC_ATOMIC_INITIALIZER(-1);
+// Cache the result of checkFcntlWorksAfterFlock for each directory a lock
+// file is created in because in some filesystems, like NFS, both locks
+// are the same. This does not take into account a filesystem changing.
+// QCache is set to hold a maximum of 10 entries, this is to avoid unbounded
+// growth, this is caching directories of files and it is assumed a low number
+// will be sufficient.
+typedef QCache<QString, bool> CacheType;
+Q_GLOBAL_STATIC_WITH_ARGS(CacheType, fcntlOK, (10));
+static QBasicMutex fcntlLock;
/*!
\internal
Checks that the OS isn't using POSIX locks to emulate flock().
OS X is one of those.
*/
-static bool fcntlWorksAfterFlock()
+static bool fcntlWorksAfterFlock(const QString &fn)
{
- int value = fcntlOK.load();
- if (Q_UNLIKELY(value == -1)) {
- value = QLockFilePrivate::checkFcntlWorksAfterFlock();
- fcntlOK.store(value);
+ QMutexLocker lock(&fcntlLock);
+ bool *worksPtr = fcntlOK->object(fn);
+ if (!worksPtr) {
+ worksPtr = new bool(QLockFilePrivate::checkFcntlWorksAfterFlock(fn));
+ fcntlOK->insert(fn, worksPtr);
}
- return value == 1;
+
+ return *worksPtr;
}
-static bool setNativeLocks(int fd)
+static bool setNativeLocks(const QString &fileName, int fd)
{
#if defined(LOCK_EX) && defined(LOCK_NB)
if (flock(fd, LOCK_EX | LOCK_NB) == -1) // other threads, and other processes on a local fs
@@ -143,8 +157,10 @@ static bool setNativeLocks(int fd)
flockData.l_start = 0;
flockData.l_len = 0; // 0 = entire file
flockData.l_pid = getpid();
- if (fcntlWorksAfterFlock() && fcntl(fd, F_SETLK, &flockData) == -1) // for networked filesystems
+ if (fcntlWorksAfterFlock(QDir::cleanPath(QFileInfo(fileName).absolutePath()) + QString('/'))
+ && fcntl(fd, F_SETLK, &flockData) == -1) { // for networked filesystems
return false;
+ }
return true;
}
@@ -171,7 +187,7 @@ QLockFile::LockError QLockFilePrivate::tryLock_sys()
}
}
// Ensure nobody else can delete the file while we have it
- if (!setNativeLocks(fd)) {
+ if (!setNativeLocks(fileName, fd)) {
const int errnoSaved = errno;
qWarning() << "setNativeLocks failed:" << qt_error_string(errnoSaved);
}
@@ -202,7 +218,7 @@ bool QLockFilePrivate::removeStaleLock()
const int fd = qt_safe_open(lockFileName.constData(), O_WRONLY, 0644);
if (fd < 0) // gone already?
return false;
- bool success = setNativeLocks(fd) && (::unlink(lockFileName) == 0);
+ bool success = setNativeLocks(fileName, fd) && (::unlink(lockFileName) == 0);
close(fd);
return success;
}
diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp
index 0c44582af8..1c7ceed3c1 100644
--- a/src/corelib/io/qsettings.cpp
+++ b/src/corelib/io/qsettings.cpp
@@ -1410,13 +1410,17 @@ void QConfFileSettingsPrivate::syncConfFile(int confFileNo)
Concurrent read and write are not a problem because the writing operation is atomic.
*/
QLockFile lockFile(confFile->name + QLatin1String(".lock"));
+#endif
if (!readOnly) {
- if (!confFile->isWritable() || !lockFile.lock() ) {
+ if (!confFile->isWritable()
+#ifndef QT_BOOTSTRAPPED
+ || !lockFile.lock()
+#endif
+ ) {
setStatus(QSettings::AccessError);
return;
}
}
-#endif
/*
We hold the lock. Let's reread the file if it has changed
diff --git a/src/corelib/io/qwinoverlappedionotifier.cpp b/src/corelib/io/qwinoverlappedionotifier.cpp
index c9de6671f7..0cefa374fa 100644
--- a/src/corelib/io/qwinoverlappedionotifier.cpp
+++ b/src/corelib/io/qwinoverlappedionotifier.cpp
@@ -364,7 +364,7 @@ bool QWinOverlappedIoNotifier::waitForNotified(int msecs, OVERLAPPED *overlapped
return false;
if (triggeredOverlapped == overlapped)
return true;
- msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
+ t = qt_subtract_from_timeout(msecs, stopWatch.elapsed());
if (t == 0)
return false;
}