diff options
Diffstat (limited to 'src/corelib/io')
-rw-r--r-- | src/corelib/io/qdatastream.h | 75 | ||||
-rw-r--r-- | src/corelib/io/qdir.cpp | 2 | ||||
-rw-r--r-- | src/corelib/io/qfileselector.cpp | 2 | ||||
-rw-r--r-- | src/corelib/io/qlockfile_unix.cpp | 6 | ||||
-rw-r--r-- | src/corelib/io/qsettings.cpp | 109 | ||||
-rw-r--r-- | src/corelib/io/qsettings_winrt.cpp | 2 | ||||
-rw-r--r-- | src/corelib/io/qstorageinfo_unix.cpp | 44 | ||||
-rw-r--r-- | src/corelib/io/qurl.cpp | 14 | ||||
-rw-r--r-- | src/corelib/io/qurlidna.cpp | 2 |
9 files changed, 143 insertions, 113 deletions
diff --git a/src/corelib/io/qdatastream.h b/src/corelib/io/qdatastream.h index c7f8840a82..5956f9ac40 100644 --- a/src/corelib/io/qdatastream.h +++ b/src/corelib/io/qdatastream.h @@ -195,6 +195,29 @@ private: int readBlock(char *data, int len); }; +namespace QtPrivate { + +class StreamStateSaver +{ +public: + inline StreamStateSaver(QDataStream *s) : stream(s), oldStatus(s->status()) + { + stream->resetStatus(); + } + inline ~StreamStateSaver() + { + if (oldStatus != QDataStream::Ok) { + stream->resetStatus(); + stream->setStatus(oldStatus); + } + } + +private: + QDataStream *stream; + QDataStream::Status oldStatus; +}; + +} // QtPrivate namespace /***************************************************************************** QDataStream inline functions @@ -239,6 +262,8 @@ inline QDataStream &QDataStream::operator<<(quint64 i) template <typename T> QDataStream& operator>>(QDataStream& s, QList<T>& l) { + QtPrivate::StreamStateSaver stateSaver(&s); + l.clear(); quint32 c; s >> c; @@ -247,10 +272,13 @@ QDataStream& operator>>(QDataStream& s, QList<T>& l) { T t; s >> t; - l.append(t); - if (s.atEnd()) + if (s.status() != QDataStream::Ok) { + l.clear(); break; + } + l.append(t); } + return s; } @@ -266,6 +294,8 @@ QDataStream& operator<<(QDataStream& s, const QList<T>& l) template <typename T> QDataStream& operator>>(QDataStream& s, QLinkedList<T>& l) { + QtPrivate::StreamStateSaver stateSaver(&s); + l.clear(); quint32 c; s >> c; @@ -273,10 +303,13 @@ QDataStream& operator>>(QDataStream& s, QLinkedList<T>& l) { T t; s >> t; - l.append(t); - if (s.atEnd()) + if (s.status() != QDataStream::Ok) { + l.clear(); break; + } + l.append(t); } + return s; } @@ -293,6 +326,8 @@ QDataStream& operator<<(QDataStream& s, const QLinkedList<T>& l) template<typename T> QDataStream& operator>>(QDataStream& s, QVector<T>& v) { + QtPrivate::StreamStateSaver stateSaver(&s); + v.clear(); quint32 c; s >> c; @@ -300,8 +335,13 @@ QDataStream& operator>>(QDataStream& s, QVector<T>& v) for(quint32 i = 0; i < c; ++i) { T t; s >> t; + if (s.status() != QDataStream::Ok) { + v.clear(); + break; + } v[i] = t; } + return s; } @@ -317,16 +357,21 @@ QDataStream& operator<<(QDataStream& s, const QVector<T>& v) template <typename T> QDataStream &operator>>(QDataStream &in, QSet<T> &set) { + QtPrivate::StreamStateSaver stateSaver(&in); + set.clear(); quint32 c; in >> c; for (quint32 i = 0; i < c; ++i) { T t; in >> t; - set << t; - if (in.atEnd()) + if (in.status() != QDataStream::Ok) { + set.clear(); break; + } + set << t; } + return in; } @@ -345,10 +390,9 @@ QDataStream& operator<<(QDataStream &out, const QSet<T> &set) template <class Key, class T> Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash) { - QDataStream::Status oldStatus = in.status(); - in.resetStatus(); - hash.clear(); + QtPrivate::StreamStateSaver stateSaver(&in); + hash.clear(); quint32 n; in >> n; @@ -364,10 +408,6 @@ Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QHash<Key, T> &has if (in.status() != QDataStream::Ok) hash.clear(); - if (oldStatus != QDataStream::Ok) { - in.resetStatus(); - in.setStatus(oldStatus); - } return in; } @@ -391,10 +431,9 @@ template <class aKey, class aT> Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &map) #endif { - QDataStream::Status oldStatus = in.status(); - in.resetStatus(); - map.clear(); + QtPrivate::StreamStateSaver stateSaver(&in); + map.clear(); quint32 n; in >> n; @@ -410,10 +449,6 @@ Q_OUTOFLINE_TEMPLATE QDataStream &operator>>(QDataStream &in, QMap<aKey, aT> &ma } if (in.status() != QDataStream::Ok) map.clear(); - if (oldStatus != QDataStream::Ok) { - in.resetStatus(); - in.setStatus(oldStatus); - } return in; } diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 8197ea63e7..91953ebf26 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -694,7 +694,7 @@ QString QDir::filePath(const QString &fileName) const { const QDirPrivate* d = d_ptr.constData(); if (isAbsolutePath(fileName)) - return QString(fileName); + return fileName; QString ret = d->dirEntry.filePath(); if (!fileName.isEmpty()) { diff --git a/src/corelib/io/qfileselector.cpp b/src/corelib/io/qfileselector.cpp index 63a15c9fac..920281cef7 100644 --- a/src/corelib/io/qfileselector.cpp +++ b/src/corelib/io/qfileselector.cpp @@ -388,7 +388,7 @@ QStringList QFileSelectorPrivate::platformSelectors() # endif QString productName = QSysInfo::productType(); # ifdef Q_OS_MACOS - if (productName != QStringLiteral("osx")) + if (productName != QLatin1String("osx")) ret << QStringLiteral("osx"); // compatibility # endif if (productName != QLatin1String("unknown")) diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp index 79141d1e8f..924d2e7214 100644 --- a/src/corelib/io/qlockfile_unix.cpp +++ b/src/corelib/io/qlockfile_unix.cpp @@ -71,12 +71,12 @@ #elif defined(Q_OS_HAIKU) # include <kernel/OS.h> #elif defined(Q_OS_BSD4) && !defined(QT_PLATFORM_UIKIT) -# if !defined(Q_OS_NETBSD) -# include <sys/user.h> -# endif # include <sys/cdefs.h> # include <sys/param.h> # include <sys/sysctl.h> +# if !defined(Q_OS_NETBSD) +# include <sys/user.h> +# endif #endif QT_BEGIN_NAMESPACE diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index ed3273eb2e..856108e417 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -248,8 +248,7 @@ QString QSettingsPrivate::actualKey(const QString &key) const { QString n = normalizedKey(key); Q_ASSERT_X(!n.isEmpty(), "QSettings", "empty key"); - n.prepend(groupPrefix); - return n; + return groupPrefix + n; } /* @@ -324,10 +323,9 @@ void QSettingsPrivate::processChild(QStringRef key, ChildSpec spec, QStringList void QSettingsPrivate::beginGroupOrArray(const QSettingsGroup &group) { groupStack.push(group); - if (!group.name().isEmpty()) { - groupPrefix += group.name(); - groupPrefix += QLatin1Char('/'); - } + const QString name = group.name(); + if (!name.isEmpty()) + groupPrefix += name + QLatin1Char('/'); } /* @@ -403,9 +401,9 @@ QString QSettingsPrivate::variantToString(const QVariant &v) case QVariant::ByteArray: { QByteArray a = v.toByteArray(); - result = QLatin1String("@ByteArray("); - result += QString::fromLatin1(a.constData(), a.size()); - result += QLatin1Char(')'); + result = QLatin1String("@ByteArray(") + + QLatin1String(a.constData(), a.size()) + + QLatin1Char(')'); break; } @@ -425,33 +423,17 @@ QString QSettingsPrivate::variantToString(const QVariant &v) #ifndef QT_NO_GEOM_VARIANT case QVariant::Rect: { QRect r = qvariant_cast<QRect>(v); - result += QLatin1String("@Rect("); - result += QString::number(r.x()); - result += QLatin1Char(' '); - result += QString::number(r.y()); - result += QLatin1Char(' '); - result += QString::number(r.width()); - result += QLatin1Char(' '); - result += QString::number(r.height()); - result += QLatin1Char(')'); + result = QString::asprintf("@Rect(%d %d %d %d)", r.x(), r.y(), r.width(), r.height()); break; } case QVariant::Size: { QSize s = qvariant_cast<QSize>(v); - result += QLatin1String("@Size("); - result += QString::number(s.width()); - result += QLatin1Char(' '); - result += QString::number(s.height()); - result += QLatin1Char(')'); + result = QString::asprintf("@Size(%d %d)", s.width(), s.height()); break; } case QVariant::Point: { QPoint p = qvariant_cast<QPoint>(v); - result += QLatin1String("@Point("); - result += QString::number(p.x()); - result += QLatin1Char(' '); - result += QString::number(p.y()); - result += QLatin1Char(')'); + result = QString::asprintf("@Point(%d %d)", p.x(), p.y()); break; } #endif // !QT_NO_GEOM_VARIANT @@ -474,9 +456,9 @@ QString QSettingsPrivate::variantToString(const QVariant &v) s << v; } - result = QLatin1String(typeSpec); - result += QString::fromLatin1(a.constData(), a.size()); - result += QLatin1Char(')'); + result = QLatin1String(typeSpec) + + QLatin1String(a.constData(), a.size()) + + QLatin1Char(')'); #else Q_ASSERT(!"QSettings: Cannot save custom types without QDataStream support"); #endif @@ -647,8 +629,7 @@ void QSettingsPrivate::iniEscapedString(const QString &str, QByteArray &result, && ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) { - result += "\\x"; - result += QByteArray::number(ch, 16); + result += "\\x" + QByteArray::number(ch, 16); continue; } @@ -687,8 +668,7 @@ void QSettingsPrivate::iniEscapedString(const QString &str, QByteArray &result, break; default: if (ch <= 0x1F || (ch >= 0x7F && !useCodec)) { - result += "\\x"; - result += QByteArray::number(ch, 16); + result += "\\x" + QByteArray::number(ch, 16); escapeNextIfDigit = true; #ifndef QT_NO_TEXTCODEC } else if (useCodec) { @@ -1038,10 +1018,33 @@ static inline int pathHashKey(QSettings::Format format, QSettings::Scope scope) return int((uint(format) << 1) | uint(scope == QSettings::SystemScope)); } +#ifndef Q_OS_WIN +static QString make_user_path() +{ + static Q_CONSTEXPR QChar sep = QLatin1Char('/'); +#ifndef QSETTINGS_USE_QSTANDARDPATHS + // Non XDG platforms (OS X, iOS, Android...) have used this code path erroneously + // for some time now. Moving away from that would require migrating existing settings. + QByteArray env = qgetenv("XDG_CONFIG_HOME"); + if (env.isEmpty()) { + return QDir::homePath() + QLatin1String("/.config/"); + } else if (env.startsWith('/')) { + return QFile::decodeName(env) + sep; + } else { + return QDir::homePath() + sep + QFile::decodeName(env) + sep; + } +#else + // When using a proper XDG platform, use QStandardPaths rather than the above hand-written code; + // it makes the use of test mode from unit tests possible. + // Ideally all platforms should use this, but see above for the migration issue. + return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + sep; +#endif +} +#endif // !Q_OS_WIN + static void initDefaultPaths(QMutexLocker *locker) { PathHash *pathHash = pathHashFunc(); - QString systemPath; locker->unlock(); @@ -1050,8 +1053,7 @@ static void initDefaultPaths(QMutexLocker *locker) avoid a dead-lock, we can't hold the global mutex while calling it. */ - systemPath = QLibraryInfo::location(QLibraryInfo::SettingsPath); - systemPath += QLatin1Char('/'); + QString systemPath = QLibraryInfo::location(QLibraryInfo::SettingsPath) + QLatin1Char('/'); locker->relock(); if (pathHash->isEmpty()) { @@ -1067,38 +1069,14 @@ static void initDefaultPaths(QMutexLocker *locker) pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), windowsConfigPath(CSIDL_COMMON_APPDATA) + QDir::separator()); #else - -#ifndef QSETTINGS_USE_QSTANDARDPATHS - // Non XDG platforms (OS X, iOS, Android...) have used this code path erroneously - // for some time now. Moving away from that would require migrating existing settings. - QString userPath; - QByteArray env = qgetenv("XDG_CONFIG_HOME"); - if (env.isEmpty()) { - userPath = QDir::homePath(); - userPath += QLatin1Char('/'); - userPath += QLatin1String(".config"); - } else if (env.startsWith('/')) { - userPath = QFile::decodeName(env); - } else { - userPath = QDir::homePath(); - userPath += QLatin1Char('/'); - userPath += QFile::decodeName(env); - } -#else - // When using a proper XDG platform, use QStandardPaths rather than the above hand-written code; - // it makes the use of test mode from unit tests possible. - // Ideally all platforms should use this, but see above for the migration issue. - QString userPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); -#endif - userPath += QLatin1Char('/'); - + const QString userPath = make_user_path(); pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::UserScope), userPath); pathHash->insert(pathHashKey(QSettings::IniFormat, QSettings::SystemScope), systemPath); #ifndef Q_OS_MAC pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::UserScope), userPath); pathHash->insert(pathHashKey(QSettings::NativeFormat, QSettings::SystemScope), systemPath); #endif -#endif +#endif // Q_OS_WIN } } @@ -3486,8 +3464,7 @@ QSettings::Format QSettings::registerFormat(const QString &extension, ReadFunc r return QSettings::InvalidFormat; QConfFileCustomFormat info; - info.extension = QLatin1Char('.'); - info.extension += extension; + info.extension = QLatin1Char('.') + extension; info.readFunc = readFunc; info.writeFunc = writeFunc; info.caseSensitivity = caseSensitivity; diff --git a/src/corelib/io/qsettings_winrt.cpp b/src/corelib/io/qsettings_winrt.cpp index 0a87e7ce66..708287ce5e 100644 --- a/src/corelib/io/qsettings_winrt.cpp +++ b/src/corelib/io/qsettings_winrt.cpp @@ -92,7 +92,7 @@ static IApplicationDataContainer *subContainer(IApplicationDataContainer *parent if (FAILED(hr)) return 0; - while (SUCCEEDED(S_OK) && current) { + while (SUCCEEDED(hr) && current) { ComPtr<ContainerItem> item; hr = iterator->get_Current(&item); if (FAILED(hr)) diff --git a/src/corelib/io/qstorageinfo_unix.cpp b/src/corelib/io/qstorageinfo_unix.cpp index 0daf041954..ae5c42ffd1 100644 --- a/src/corelib/io/qstorageinfo_unix.cpp +++ b/src/corelib/io/qstorageinfo_unix.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com> +** Copyright (C) 2016 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -153,29 +154,41 @@ static bool isParentOf(const String &parent, const QString &dirName) parent.size() == 1); } -static bool isPseudoFs(const QStorageIterator &it) -{ +static bool shouldIncludeFs(const QStorageIterator &it) +{ + /* + * This function implements a heuristic algorithm to determine whether a + * given mount should be reported to the user. Our objective is to list + * only entries that the end-user would find useful. + * + * We therefore ignore: + * - mounted in /dev, /proc, /sys: special mounts + * (this will catch /sys/fs/cgroup, /proc/sys/fs/binfmt_misc, /dev/pts, + * some of which are tmpfs on Linux) + * - mounted in /var/run or /var/lock: most likely pseudofs + * (on earlier systemd versions, /var/run was a bind-mount of /run, so + * everything would be unnecessarily duplicated) + * - filesystem type is "rootfs": artifact of the root-pivot on some Linux + * initrd + * - if the filesystem total size is zero, it's a pseudo-fs (not checked here). + */ + QString mountDir = it.rootPath(); if (isParentOf(QLatin1String("/dev"), mountDir) || isParentOf(QLatin1String("/proc"), mountDir) || isParentOf(QLatin1String("/sys"), mountDir) || isParentOf(QLatin1String("/var/run"), mountDir) || isParentOf(QLatin1String("/var/lock"), mountDir)) { - return true; + return false; } - QByteArray type = it.fileSystemType(); - if (type == "tmpfs") +#ifdef Q_OS_LINUX + if (it.fileSystemType() == "rootfs") return false; -#if defined(Q_OS_LINUX) - if (type == "rootfs" || type == "rpc_pipefs") - return true; #endif - if (!it.device().startsWith('/')) - return true; - - return false; + // size checking in mountedVolumes() + return true; } #if defined(Q_OS_BSD4) @@ -557,11 +570,14 @@ QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes() QList<QStorageInfo> volumes; while (it.next()) { - if (isPseudoFs(it)) + if (!shouldIncludeFs(it)) continue; const QString mountDir = it.rootPath(); - volumes.append(QStorageInfo(mountDir)); + QStorageInfo info(mountDir); + if (info.bytesTotal() == 0) + continue; + volumes.append(info); } return volumes; diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index e20a98fbf1..71a0228eeb 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -816,7 +816,7 @@ static const ushort * const fragmentInUrl = userNameInUrl + 6; static inline void parseDecodedComponent(QString &data) { - data.replace(QLatin1Char('%'), QStringLiteral("%25")); + data.replace(QLatin1Char('%'), QLatin1String("%25")); } static inline QString @@ -981,10 +981,12 @@ inline bool QUrlPrivate::setScheme(const QString &value, int len, bool doSetErro needsLowercasing = i; continue; } - if (p[i] >= '0' && p[i] <= '9' && i > 0) - continue; - if (p[i] == '+' || p[i] == '-' || p[i] == '.') - continue; + if (i) { + if (p[i] >= '0' && p[i] <= '9') + continue; + if (p[i] == '+' || p[i] == '-' || p[i] == '.') + continue; + } // found something else // don't call setError needlessly: @@ -1472,7 +1474,7 @@ QString QUrlPrivate::toLocalFile(QUrl::FormattingOptions options) const // magic for shared drive on windows if (!host.isEmpty()) { - tmp = QStringLiteral("//") + host; + tmp = QLatin1String("//") + host; #ifdef Q_OS_WIN // QTBUG-42346, WebDAV is visible as local file on Windows only. if (scheme == webDavScheme()) tmp += webDavSslTag(); diff --git a/src/corelib/io/qurlidna.cpp b/src/corelib/io/qurlidna.cpp index 11b2280cd1..16bb924b17 100644 --- a/src/corelib/io/qurlidna.cpp +++ b/src/corelib/io/qurlidna.cpp @@ -2269,7 +2269,7 @@ Q_AUTOTEST_EXPORT void qt_punycodeEncoder(const QChar *s, int ucLength, QString } // prepend ACE prefix - output->insert(outLen, QStringLiteral("xn--")); + output->insert(outLen, QLatin1String("xn--")); return; } |